diff options
Diffstat (limited to 'trivmix/Trivmix.hs')
-rw-r--r-- | trivmix/Trivmix.hs | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/trivmix/Trivmix.hs b/trivmix/Trivmix.hs index b2d87ec..5cddf6f 100644 --- a/trivmix/Trivmix.hs +++ b/trivmix/Trivmix.hs | |||
@@ -43,8 +43,9 @@ import Control.Monad | |||
43 | 43 | ||
44 | import Text.Heredoc (str) | 44 | import Text.Heredoc (str) |
45 | 45 | ||
46 | import Refined (refine) | 46 | import Refined |
47 | 47 | ||
48 | import Data.Scientific | ||
48 | import Trivmix.Types | 49 | import Trivmix.Types |
49 | 50 | ||
50 | data Options = Options | 51 | data Options = Options |
@@ -53,7 +54,7 @@ data Options = Options | |||
53 | , client :: String | 54 | , client :: String |
54 | , initialLevel :: Level | 55 | , initialLevel :: Level |
55 | , initialBalance :: Balance | 56 | , initialBalance :: Balance |
56 | , fps, interval :: Float | 57 | , fps, interval, watchdogInterval :: Scientific |
57 | , run :: [FilePath] | 58 | , run :: [FilePath] |
58 | , balanceFiles :: [FilePath] | 59 | , balanceFiles :: [FilePath] |
59 | , levelFiles :: [FilePath] | 60 | , levelFiles :: [FilePath] |
@@ -101,6 +102,12 @@ optionParser = Options | |||
101 | <> value 0.2 | 102 | <> value 0.2 |
102 | <> showDefault | 103 | <> showDefault |
103 | ) | 104 | ) |
105 | <*> option auto ( long "watchdog" | ||
106 | <> metavar "NUMBER" | ||
107 | <> help "Signal watchdog every ’NUMBER’ seconds" | ||
108 | <> value 1 | ||
109 | <> showDefault | ||
110 | ) | ||
104 | <*> many ( strOption ( long "run" | 111 | <*> many ( strOption ( long "run" |
105 | <> metavar "FILE" | 112 | <> metavar "FILE" |
106 | <> help [str|Execute a file once setup of jacks is done (use this to autoconnect) | 113 | <> help [str|Execute a file once setup of jacks is done (use this to autoconnect) |
@@ -183,18 +190,16 @@ trivmix Options{..} = do | |||
183 | frames = interval * fps | 190 | frames = interval * fps |
184 | delay = round $ recip fps * 1e6 | 191 | delay = round $ recip fps * 1e6 |
185 | linInt x a b = a * (1 - x) + b * x | 192 | linInt x a b = a * (1 - x) + b * x |
186 | linInt' x a b = either error id $ asFloat (linInt x) a b | 193 | linInt' x a b = either error id $ asScientific (linInt x) a b |
187 | mulBalance (bToFloat -> b) x = either error id $ asFloat (*) (Lin . either error id $ refine b) x | 194 | mulBalance (bToScientific -> b) x = either error id $ asScientific (*) (Lin . either error id $ refine b) x |
188 | newLevel <- mulBalance <$> readMVar balance <*> readMVar level | 195 | newLevel <- mulBalance <$> readMVar balance <*> readMVar level |
189 | currentLevel <- readMVar level' | 196 | currentLevel <- (\(CFloat f) -> Lin . either error id . refine $ realToFrac f) <$> readMVar level' |
190 | mapM_ (\x -> swapMVar level' (linInt' x currentLevel newLevel) >> threadDelay delay) [0,recip frames..1] | 197 | mapM_ (\x -> (swapMVar level' $! toRealFloat . unrefine . toLin $ linInt' x currentLevel newLevel) >> threadDelay delay) (takeWhile (<= 1) $ iterate (+ recip frames) 0) |
191 | notifyReady | 198 | notifyReady |
192 | forever $ threadDelay 1000000 >> notifyWatchdog | 199 | forever $ threadDelay (round $ watchdogInterval * 1e6) >> notifyWatchdog |
193 | 200 | ||
194 | mix :: MVar Level -> CFloat -> IO CFloat | 201 | mix :: MVar CFloat -> CFloat -> IO CFloat |
195 | mix level input = do | 202 | mix level input = (input *) <$> readMVar level |
196 | level' <- readMVar level | ||
197 | return $ (CFloat $ toFloat level') * input | ||
198 | 203 | ||
199 | handleFiles :: (Read l, Show l, Eq l) => INotify -> MVar l -> [FilePath] -> IO () | 204 | handleFiles :: (Read l, Show l, Eq l) => INotify -> MVar l -> [FilePath] -> IO () |
200 | handleFiles inotify level files = do | 205 | handleFiles inotify level files = do |