From 1010c8a31e2b441683f5d8c3905a87d4369e7586 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 15 May 2018 16:04:27 +0200 Subject: Smooth out discontinuity from start --- trivmix.cabal | 2 +- trivmix.nix | 2 +- trivmix/Trivmix.hs | 28 ++++++++++++++-------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/trivmix.cabal b/trivmix.cabal index e12ea00..aa3ea99 100644 --- a/trivmix.cabal +++ b/trivmix.cabal @@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: trivmix -version: 3.1.2 +version: 3.1.3 -- synopsis: -- description: license: PublicDomain diff --git a/trivmix.nix b/trivmix.nix index 416c55d..22961b0 100644 --- a/trivmix.nix +++ b/trivmix.nix @@ -5,7 +5,7 @@ }: mkDerivation { pname = "trivmix"; - version = "3.1.2"; + version = "3.1.3"; src = ./.; isLibrary = true; isExecutable = true; diff --git a/trivmix/Trivmix.hs b/trivmix/Trivmix.hs index 2e05b13..825e3fa 100644 --- a/trivmix/Trivmix.hs +++ b/trivmix/Trivmix.hs @@ -149,22 +149,11 @@ main = execParser opts >>= trivmix trivmix :: Options -> IO () trivmix Options{..} = do - level <- newMVar initialLevel + level <- newMVar 0 balance <- newMVar initialBalance level' <- newMVar initialLevel - forkIO $ forever $ do -- Smooth out discontinuity - let - fps = 200 - interval = 0.2 - frames = interval * fps - delay = round $ recip fps * 1e6 - linInt x a b = a * (1 - x) + b * x - linInt' x a b = either error id $ asFloat (linInt x) a b - mulBalance (bToFloat -> b) x = either error id $ asFloat (*) (Lin . either error id $ refine b) x - newLevel <- mulBalance <$> readMVar balance <*> readMVar level - currentLevel <- readMVar level' - mapM_ (\x -> swapMVar level' (linInt' x currentLevel newLevel) >> threadDelay delay) ([0,recip frames..1] :: [Float]) let withFiles = foldl (.) id $ map (\f -> onStateFile f (show initialLevel ++ "\n")) levelFiles + ++ map (\f -> onStateFile b (show initialBalance ++ "\n")) balanceFiles withFiles $ withINotify $ \inotify -> do handleFiles inotify level levelFiles handleFiles inotify balance levelFiles @@ -177,7 +166,18 @@ trivmix Options{..} = do forM_ run $ \script -> (callProcess script [client ++ ":" ++ input, client ++ ":" ++ output]) `catch` (\code -> hPutStrLn stderr $ script ++ " failed: " ++ show (code :: ExitCode)) notifyReady - forever $ threadDelay 1000000 + forever $ do -- Smooth out discontinuity + let + fps = 200 + interval = 0.2 + frames = interval * fps + delay = round $ recip fps * 1e6 + linInt x a b = a * (1 - x) + b * x + linInt' x a b = either error id $ asFloat (linInt x) a b + mulBalance (bToFloat -> b) x = either error id $ asFloat (*) (Lin . either error id $ refine b) x + newLevel <- mulBalance <$> readMVar balance <*> readMVar level + currentLevel <- readMVar level' + mapM_ (\x -> swapMVar level' (linInt' x currentLevel newLevel) >> threadDelay delay) ([0,recip frames..1] :: [Float]) mix :: MVar Level -> CFloat -> IO CFloat mix level input = do -- cgit v1.2.3