summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2015-06-07 22:22:15 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2015-06-07 22:22:15 +0200
commitc51b5c819872b32c354ac7e48261f84ff6a07161 (patch)
treedab249ef1eb6c4dbae262da21a493281d16d7481 /src
parent9b6d75be910cf0062269dc5d19b1c8bbb7d6e45e (diff)
downloadtrivmix-c51b5c819872b32c354ac7e48261f84ff6a07161.tar
trivmix-c51b5c819872b32c354ac7e48261f84ff6a07161.tar.gz
trivmix-c51b5c819872b32c354ac7e48261f84ff6a07161.tar.bz2
trivmix-c51b5c819872b32c354ac7e48261f84ff6a07161.tar.xz
trivmix-c51b5c819872b32c354ac7e48261f84ff6a07161.zip
removed connection options that weren´t working and added support for
forking a script
Diffstat (limited to 'src')
-rw-r--r--src/Trivmix.hs48
1 files changed, 25 insertions, 23 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
13import System.Posix.Files 13import System.Posix.Files
14import System.Posix.IO 14import System.Posix.IO
15import System.Environment 15import System.Environment
16import System.Process
16 17
17import Control.Concurrent 18import Control.Concurrent
18import Control.Concurrent.MVar 19import Control.Concurrent.MVar
@@ -29,8 +30,7 @@ data Options = Options
29 { input :: String 30 { input :: String
30 , output :: String 31 , output :: String
31 , client :: String 32 , client :: String
32 , connFrom :: Maybe String 33 , run :: Maybe String
33 , connTo :: Maybe String
34 , initialLevel :: Float 34 , initialLevel :: Float
35 , stateDir :: FilePath 35 , stateDir :: FilePath
36 } 36 }
@@ -46,12 +46,10 @@ optionParser = Options <$>
46 <*> strOption ( long "client" 46 <*> strOption ( long "client"
47 <> metavar "STRING" 47 <> metavar "STRING"
48 ) 48 )
49 <*> optional (strOption ( long "input-from" 49 <*> optional ( strOption ( long "run"
50 <> metavar "STRING" 50 <> metavar "FILE"
51 )) 51 )
52 <*> optional (strOption ( long "output-to" 52 )
53 <> metavar "STRING"
54 ))
55 <*> (fromMaybe 0 <$> optional (option auto ( long "level" 53 <*> (fromMaybe 0 <$> optional (option auto ( long "level"
56 <> metavar "FLOAT" 54 <> metavar "FLOAT"
57 ) 55 )
@@ -71,25 +69,29 @@ main = execParser opts >>= trivmix
71 ) 69 )
72 70
73trivmix :: Options -> IO () 71trivmix :: Options -> IO ()
74trivmix Options{..} = do 72trivmix Options{..} = onDirectory stateDir $ do
75 createDirectoryIfMissing True stateDir
76 level <- newMVar initialLevel 73 level <- newMVar initialLevel
77 let levelFile = stateDir </> "level" 74 let levelFile = stateDir </> "level"
78 onLevelFile levelFile initialLevel $ withINotify $ \n -> do 75 onLevelFile levelFile initialLevel $ withINotify $ \n -> do
79 addWatch n [Modify] levelFile (const $ handleLevel level levelFile) 76 addWatch n [Modify] levelFile (const $ handleLevel level levelFile)
80 Jack.handleExceptions $ 77 Jack.handleExceptions $
81 Jack.withClientDefault client $ \client -> 78 Jack.withClientDefault client $ \client' ->
82 Jack.withPort client input $ \input' -> 79 Jack.withPort client' input $ \input' ->
83 Jack.withPort client output $ \output' -> do 80 Jack.withPort client' output $ \output' -> do
84 case connFrom of 81 Trans.lift $ do
85 Nothing -> return () 82 case run of
86 Just f -> Jack.connect client input f 83 Nothing -> return ()
87 case connTo of 84 Just run' -> do
88 Nothing -> return () 85 (_, _, _, ph) <- createProcess $ (proc run' [client ++ ":" ++ input, client ++ ":" ++ output]) { delegate_ctlc = True }
89 Just t -> Jack.connect client t output 86 return ()
90 Audio.withProcessMono client input' (mix level) output' $ 87 Audio.withProcessMono client' input' (mix level) output' $
91 Jack.withActivation client $ Trans.lift $ do 88 Jack.withActivation client' $ Trans.lift Jack.waitForBreak
92 Jack.waitForBreak 89
90onDirectory :: FilePath -> IO () -> IO ()
91onDirectory stateDir io = do
92 exists <- doesDirectoryExist stateDir
93 createDirectoryIfMissing True stateDir
94 finally io $ if exists then removeDirectory stateDir else return ()
93 95
94mix :: MVar Float -> CFloat -> IO CFloat 96mix :: MVar Float -> CFloat -> IO CFloat
95mix level input = do 97mix level input = do
@@ -101,7 +103,7 @@ onLevelFile file initial action = do
101 exists <- doesFileExist file 103 exists <- doesFileExist file
102 let acquire = case exists of 104 let acquire = case exists of
103 True -> return () 105 True -> return ()
104 False -> createFile file mode >>= closeFd 106 False -> createFile file mode >>= closeFd >> writeFile file (show initial)
105 mode = foldl unionFileModes nullFileMode [ ownerReadMode 107 mode = foldl unionFileModes nullFileMode [ ownerReadMode
106 , ownerWriteMode 108 , ownerWriteMode
107 , groupReadMode 109 , groupReadMode