From c51b5c819872b32c354ac7e48261f84ff6a07161 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 7 Jun 2015 22:22:15 +0200 Subject: =?UTF-8?q?removed=20connection=20options=20that=20weren=C2=B4t=20?= =?UTF-8?q?working=20and=20added=20support=20for=20forking=20a=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Trivmix.hs | 48 +++++++++++++++++++++++++----------------------- trivmix.cabal | 3 ++- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/Trivmix.hs b/src/Trivmix.hs index a139a15..2d6a50a 100644 --- a/src/Trivmix.hs +++ b/src/Trivmix.hs @@ -13,6 +13,7 @@ import System.FilePath import System.Posix.Files import System.Posix.IO import System.Environment +import System.Process import Control.Concurrent import Control.Concurrent.MVar @@ -29,8 +30,7 @@ data Options = Options { input :: String , output :: String , client :: String - , connFrom :: Maybe String - , connTo :: Maybe String + , run :: Maybe String , initialLevel :: Float , stateDir :: FilePath } @@ -46,12 +46,10 @@ optionParser = Options <$> <*> strOption ( long "client" <> metavar "STRING" ) - <*> optional (strOption ( long "input-from" - <> metavar "STRING" - )) - <*> optional (strOption ( long "output-to" - <> metavar "STRING" - )) + <*> optional ( strOption ( long "run" + <> metavar "FILE" + ) + ) <*> (fromMaybe 0 <$> optional (option auto ( long "level" <> metavar "FLOAT" ) @@ -71,25 +69,29 @@ main = execParser opts >>= trivmix ) trivmix :: Options -> IO () -trivmix Options{..} = do - createDirectoryIfMissing True stateDir +trivmix Options{..} = onDirectory stateDir $ do level <- newMVar initialLevel let levelFile = stateDir "level" onLevelFile levelFile initialLevel $ withINotify $ \n -> do addWatch n [Modify] levelFile (const $ handleLevel level levelFile) Jack.handleExceptions $ - Jack.withClientDefault client $ \client -> - Jack.withPort client input $ \input' -> - Jack.withPort client output $ \output' -> do - case connFrom of - Nothing -> return () - Just f -> Jack.connect client input f - case connTo of - Nothing -> return () - Just t -> Jack.connect client t output - Audio.withProcessMono client input' (mix level) output' $ - Jack.withActivation client $ Trans.lift $ do - Jack.waitForBreak + Jack.withClientDefault client $ \client' -> + Jack.withPort client' input $ \input' -> + Jack.withPort client' output $ \output' -> do + Trans.lift $ do + case run of + Nothing -> return () + Just run' -> do + (_, _, _, ph) <- createProcess $ (proc run' [client ++ ":" ++ input, client ++ ":" ++ output]) { delegate_ctlc = True } + return () + Audio.withProcessMono client' input' (mix level) output' $ + Jack.withActivation client' $ Trans.lift Jack.waitForBreak + +onDirectory :: FilePath -> IO () -> IO () +onDirectory stateDir io = do + exists <- doesDirectoryExist stateDir + createDirectoryIfMissing True stateDir + finally io $ if exists then removeDirectory stateDir else return () mix :: MVar Float -> CFloat -> IO CFloat mix level input = do @@ -101,7 +103,7 @@ onLevelFile file initial action = do exists <- doesFileExist file let acquire = case exists of True -> return () - False -> createFile file mode >>= closeFd + False -> createFile file mode >>= closeFd >> writeFile file (show initial) mode = foldl unionFileModes nullFileMode [ ownerReadMode , ownerWriteMode , groupReadMode diff --git a/trivmix.cabal b/trivmix.cabal index 5d37032..c90aae4 100644 --- a/trivmix.cabal +++ b/trivmix.cabal @@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: trivmix -version: 0.2.0 +version: 1.0.0 -- synopsis: -- description: license: PublicDomain @@ -28,6 +28,7 @@ executable trivmix , hinotify >=0.3 && <1 , transformers >=0.3 && <1 , explicit-exception >=0.1 && <1 + , process >=1.2 && <2 hs-source-dirs: src default-language: Haskell2010 ghc-options: -threaded -- cgit v1.2.3