diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-06-09 22:32:15 +0200 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-06-09 22:32:15 +0200 |
| commit | 60cc6cf218d8f1a12360d0188450f83e04d92c1a (patch) | |
| tree | b4c642c837277dd37bb02a6e437fae7d19772052 | |
| parent | efe79077888f5f22dae9aeb9e1e82745748b2f15 (diff) | |
| download | trivmix-60cc6cf218d8f1a12360d0188450f83e04d92c1a.tar trivmix-60cc6cf218d8f1a12360d0188450f83e04d92c1a.tar.gz trivmix-60cc6cf218d8f1a12360d0188450f83e04d92c1a.tar.bz2 trivmix-60cc6cf218d8f1a12360d0188450f83e04d92c1a.tar.xz trivmix-60cc6cf218d8f1a12360d0188450f83e04d92c1a.zip | |
decibel levels && multiple level files
| -rw-r--r-- | src/Trivmix.hs | 133 | ||||
| -rw-r--r-- | trivmix.cabal | 3 |
2 files changed, 98 insertions, 38 deletions
diff --git a/src/Trivmix.hs b/src/Trivmix.hs index c1fbe8a..abb7c32 100644 --- a/src/Trivmix.hs +++ b/src/Trivmix.hs | |||
| @@ -17,6 +17,7 @@ import System.Process | |||
| 17 | 17 | ||
| 18 | import Control.Concurrent | 18 | import Control.Concurrent |
| 19 | import Control.Concurrent.MVar | 19 | import Control.Concurrent.MVar |
| 20 | import Control.Concurrent.Chan | ||
| 20 | 21 | ||
| 21 | import qualified Control.Monad.Trans.Class as Trans | 22 | import qualified Control.Monad.Trans.Class as Trans |
| 22 | import qualified Control.Monad.Exception.Synchronous as Sync | 23 | import qualified Control.Monad.Exception.Synchronous as Sync |
| @@ -27,16 +28,40 @@ import System.IO.Error | |||
| 27 | import System.INotify | 28 | import System.INotify |
| 28 | 29 | ||
| 29 | import Data.Char | 30 | import Data.Char |
| 31 | import Data.Function | ||
| 32 | |||
| 33 | import Control.Monad | ||
| 34 | |||
| 35 | import Data.CaseInsensitive ( CI ) | ||
| 36 | import qualified Data.CaseInsensitive as CI | ||
| 30 | 37 | ||
| 31 | data Options = Options | 38 | data Options = Options |
| 32 | { input :: String | 39 | { input :: String |
| 33 | , output :: String | 40 | , output :: String |
| 34 | , client :: String | 41 | , client :: String |
| 35 | , run :: Maybe String | 42 | , run :: Maybe String |
| 36 | , initialLevel :: Float | 43 | , levelFiles :: [FilePath] |
| 37 | , stateDir :: FilePath | ||
| 38 | } | 44 | } |
| 39 | 45 | ||
| 46 | data Level = Lin Float | DB Float | ||
| 47 | |||
| 48 | instance Show Level where | ||
| 49 | show (Lin x) = show x | ||
| 50 | show (DB x) = (show x) ++ "dB" | ||
| 51 | |||
| 52 | instance Read Level where | ||
| 53 | readsPrec i = map toL . readsPrec i | ||
| 54 | where | ||
| 55 | toL :: (Float, String) -> (Level, String) | ||
| 56 | toL (f, str) | ||
| 57 | | ((==) `on` CI.mk) prec unit = (DB f, rest) | ||
| 58 | | otherwise = (Lin f, str) | ||
| 59 | where | ||
| 60 | prec = take lU str | ||
| 61 | rest = drop lU str | ||
| 62 | unit = "dB" | ||
| 63 | lU = length unit | ||
| 64 | |||
| 40 | optionParser :: Parser Options | 65 | optionParser :: Parser Options |
| 41 | optionParser = Options <$> | 66 | optionParser = Options <$> |
| 42 | (fromMaybe "in" <$> optional (strOption ( long "input" | 67 | (fromMaybe "in" <$> optional (strOption ( long "input" |
| @@ -56,14 +81,24 @@ optionParser = Options <$> | |||
| 56 | <> metavar "FILE" | 81 | <> metavar "FILE" |
| 57 | ) | 82 | ) |
| 58 | ) | 83 | ) |
| 59 | <*> (fromMaybe 0 <$> optional (option auto ( long "level" | 84 | <*> some (strOption ( long "level" |
| 60 | <> metavar "FLOAT" | 85 | <> metavar "FILE" |
| 61 | ) | 86 | ) |
| 62 | ) | 87 | ) |
| 63 | ) | 88 | |
| 64 | <*> strOption ( long "dir" | 89 | initialLevel :: Level |
| 65 | <> metavar "DIRECTORY" | 90 | initialLevel = Lin 0 |
| 66 | ) | 91 | |
| 92 | watchedAttrs :: [EventVariety] | ||
| 93 | watchedAttrs = [ Modify | ||
| 94 | , Move | ||
| 95 | , MoveIn | ||
| 96 | , MoveOut | ||
| 97 | , MoveSelf | ||
| 98 | , Create | ||
| 99 | , Delete | ||
| 100 | , DeleteSelf | ||
| 101 | ] | ||
| 67 | 102 | ||
| 68 | main :: IO () | 103 | main :: IO () |
| 69 | main = execParser opts >>= trivmix | 104 | main = execParser opts >>= trivmix |
| @@ -75,11 +110,10 @@ main = execParser opts >>= trivmix | |||
| 75 | ) | 110 | ) |
| 76 | 111 | ||
| 77 | trivmix :: Options -> IO () | 112 | trivmix :: Options -> IO () |
| 78 | trivmix Options{..} = onDirectory stateDir $ do | 113 | trivmix Options{..} = do |
| 79 | level <- newMVar initialLevel | 114 | level <- newMVar initialLevel |
| 80 | let levelFile = stateDir </> "level" | 115 | withINotify $ \inotify -> do |
| 81 | onLevelFile levelFile initialLevel $ withINotify $ \n -> do | 116 | handleFiles inotify level levelFiles |
| 82 | addWatch n [Modify] levelFile (const $ handleLevel level levelFile) | ||
| 83 | Jack.handleExceptions $ | 117 | Jack.handleExceptions $ |
| 84 | Jack.withClientDefault client $ \client' -> | 118 | Jack.withClientDefault client $ \client' -> |
| 85 | Jack.withPort client' input $ \input' -> | 119 | Jack.withPort client' input $ \input' -> |
| @@ -93,42 +127,67 @@ trivmix Options{..} = onDirectory stateDir $ do | |||
| 93 | Audio.withProcessMono client' input' (mix level) output' $ | 127 | Audio.withProcessMono client' input' (mix level) output' $ |
| 94 | Jack.withActivation client' $ Trans.lift Jack.waitForBreak | 128 | Jack.withActivation client' $ Trans.lift Jack.waitForBreak |
| 95 | 129 | ||
| 96 | onDirectory :: FilePath -> IO () -> IO () | 130 | mix :: MVar Level -> CFloat -> IO CFloat |
| 97 | onDirectory stateDir io = do | ||
| 98 | exists <- doesDirectoryExist stateDir | ||
| 99 | createDirectoryIfMissing True stateDir | ||
| 100 | finally io $ if exists then removeDirectory stateDir else return () | ||
| 101 | |||
| 102 | mix :: MVar Float -> CFloat -> IO CFloat | ||
| 103 | mix level input = do | 131 | mix level input = do |
| 104 | level' <- readMVar level | 132 | level' <- readMVar level |
| 105 | return $ (CFloat level') * input | 133 | return $ (CFloat $ toFloat level') * input |
| 106 | 134 | where | |
| 107 | onLevelFile :: FilePath -> Float -> IO a -> IO a | 135 | toFloat (Lin x) = x |
| 108 | onLevelFile file initial action = do | 136 | toFloat (DB x) = 10 ** (0.05 * x) |
| 137 | |||
| 138 | handleFiles :: INotify -> MVar Level -> [FilePath] -> IO () | ||
| 139 | handleFiles inotify level files = do | ||
| 140 | initLevel <- readMVar level | ||
| 141 | levelChanges <- (newChan :: IO (Chan Level)) | ||
| 142 | let | ||
| 143 | handleFiles' = mapM handleFile files | ||
| 144 | handleFile file = do | ||
| 145 | levelChanges' <- dupChan levelChanges | ||
| 146 | forkIO $ forever $ do -- Broadcast level changes and update all files | ||
| 147 | readChan levelChanges' >>= writeLevel file | ||
| 148 | addWatch inotify watchedAttrs file (const $ readLevel levelChanges level file) | ||
| 149 | foldl (.) id [onStateFile f (show initLevel ++ "\n") | f <- files] $ handleFiles' | ||
| 150 | forkIO $ forever $ do | ||
| 151 | readChan levelChanges >>= swapMVar level | ||
| 152 | return () | ||
| 153 | return () | ||
| 154 | |||
| 155 | onStateFile :: FilePath -> String -> IO a -> IO a | ||
| 156 | onStateFile file initial action = do | ||
| 157 | let directory = takeDirectory file | ||
| 158 | dirExists <- doesDirectoryExist directory | ||
| 109 | exists <- doesFileExist file | 159 | exists <- doesFileExist file |
| 110 | let acquire = case exists of | 160 | createDirectoryIfMissing True directory |
| 161 | let acquireFile = case exists of | ||
| 111 | True -> return () | 162 | True -> return () |
| 112 | False -> createFile file mode >>= closeFd >> writeFile file (show initial ++ "\n") | 163 | False -> createFile file fileMode >>= closeFd >> writeFile file initial |
| 113 | mode = foldl unionFileModes nullFileMode [ ownerReadMode | 164 | fileMode = foldl unionFileModes nullFileMode [ ownerReadMode |
| 114 | , ownerWriteMode | 165 | , ownerWriteMode |
| 115 | , groupReadMode | 166 | , groupReadMode |
| 116 | , groupWriteMode | 167 | , groupWriteMode |
| 117 | ] | 168 | ] |
| 118 | release = case exists of | 169 | releaseFile = case exists of |
| 119 | True -> return () | 170 | True -> return () |
| 120 | False -> removeFile file | 171 | False -> removeFile file |
| 172 | releaseDir = case dirExists of | ||
| 173 | True -> return () | ||
| 174 | False -> removeFile directory | ||
| 175 | acquire = acquireFile | ||
| 176 | release = releaseFile >> releaseDir | ||
| 121 | bracket_ acquire release action | 177 | bracket_ acquire release action |
| 122 | 178 | ||
| 123 | handleLevel :: MVar Float -> FilePath -> IO () | 179 | readLevel :: Chan Level -> MVar Level -> FilePath -> IO () |
| 124 | handleLevel level file = catch action handler | 180 | readLevel levelChan current file = catch action handler |
| 125 | where | 181 | where |
| 126 | action = readFile file >>= readIO . stripSpace >>= swapMVar level >>= const (return ()) | 182 | action = readFile file >>= readIO . stripSpace >>= writeChan levelChan |
| 127 | handler e = if isUserError e | 183 | handler e = if isUserError e |
| 128 | then readMVar level >>= \l -> writeFile file (show l ++ "\n") | 184 | then readMVar current >>= writeLevel file |
| 129 | else throw e | 185 | else throw e |
| 130 | stripSpace = reverse . stripSpace' . reverse . stripSpace' | 186 | stripSpace = reverse . stripSpace' . reverse . stripSpace' |
| 131 | stripSpace' [] = [] | 187 | stripSpace' [] = [] |
| 132 | stripSpace' l@(x:xs) = if isSpace x | 188 | stripSpace' l@(x:xs) = if isSpace x |
| 133 | then stripSpace' xs | 189 | then stripSpace' xs |
| 134 | else l | 190 | else l |
| 191 | |||
| 192 | writeLevel :: FilePath -> Level -> IO () | ||
| 193 | writeLevel file level = writeFile file (show level ++ "\n") | ||
diff --git a/trivmix.cabal b/trivmix.cabal index 5da022b..ec5f806 100644 --- a/trivmix.cabal +++ b/trivmix.cabal | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | -- documentation, see http://haskell.org/cabal/users-guide/ | 2 | -- documentation, see http://haskell.org/cabal/users-guide/ |
| 3 | 3 | ||
| 4 | name: trivmix | 4 | name: trivmix |
| 5 | version: 1.2.0 | 5 | version: 2.0.0 |
| 6 | -- synopsis: | 6 | -- synopsis: |
| 7 | -- description: | 7 | -- description: |
| 8 | license: PublicDomain | 8 | license: PublicDomain |
| @@ -29,6 +29,7 @@ executable trivmix | |||
| 29 | , transformers >=0.3 && <1 | 29 | , transformers >=0.3 && <1 |
| 30 | , explicit-exception >=0.1 && <1 | 30 | , explicit-exception >=0.1 && <1 |
| 31 | , process >=1.2 && <2 | 31 | , process >=1.2 && <2 |
| 32 | , case-insensitive >=1.2 && <2 | ||
| 32 | hs-source-dirs: src | 33 | hs-source-dirs: src |
| 33 | default-language: Haskell2010 | 34 | default-language: Haskell2010 |
| 34 | ghc-options: -threaded | 35 | ghc-options: -threaded |
