summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2015-12-03 06:14:55 -0500
committerAndrew Cady <d@jerkface.net>2015-12-03 06:14:55 -0500
commitc74a2049a1677293c67b49b8a23e0c53d8f47636 (patch)
treeb470becf874d23d62883e2de1f41cc7050270dad
parent24df7f3c166ba9b7528d204490a833209703ed67 (diff)
mididump will use RSWT for main loop
-rw-r--r--midi-dump.hs39
1 files changed, 25 insertions, 14 deletions
diff --git a/midi-dump.hs b/midi-dump.hs
index a5a3d9c..1a331e8 100644
--- a/midi-dump.hs
+++ b/midi-dump.hs
@@ -4,30 +4,41 @@ import qualified Sound.ALSA.Exception as AlsaExc
4import qualified Data.Set as Set 4import qualified Data.Set as Set
5import qualified Haskore.Basic.Pitch as Pitch 5import qualified Haskore.Basic.Pitch as Pitch
6import qualified Sound.ALSA.Sequencer.Event as Event 6import qualified Sound.ALSA.Sequencer.Event as Event
7import Control.Monad.RWS.Strict
7 8
8main = main' `AlsaExc.catch` handler 9main = main' `AlsaExc.catch` handler
9 where 10 where
10 handler e = putStrLn $ "alsa_exception: " ++ AlsaExc.show e 11 handler e = putStrLn $ "alsa_exception: " ++ AlsaExc.show e
11 12
12data State = State { 13data LoopState = LoopState {
13 keysDown :: MidiPitchSet, 14 keysDown :: MidiPitchSet,
14 inputHistory :: [Maybe Event.T] 15 inputHistory :: [Maybe Event.T]
15} 16}
16 17
18emptyLoopState = LoopState Set.empty []
19
17main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do 20main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do
18 cmdlineAlsaConnect h public 21 cmdlineAlsaConnect h public
19 22
20 let
21 forwardNOW = forwardNoteEvent h q publicAddr
22 go state = do
23 (events, keysDown') <- parseAlsaEvents' h (keysDown state) forwardNOW
24 if ((keysDown state) == keysDown') then
25 threadDelay 15000 -- 15ms. Seems like a lot, but it sounds OK. Cuts CPU down to 2%.
26 else do
27 let newHistory = if (Set.null $ keysDown state) then Nothing:newEvents else newEvents
28 newEvents = map Just events
29 printChordLn keysDown'
30 go (state { keysDown = keysDown', inputHistory = newHistory ++ inputHistory state })
31
32 putStrLn "Rock on!" 23 putStrLn "Rock on!"
33 go (State Set.empty []) 24
25 (s, ()) <- execRWST loop (h, public, private, q, publicAddr, privateAddr) emptyLoopState
26 return ()
27
28loop = do
29 (h, public, private, q, publicAddr, privateAddr) <- ask
30 loopState <- get
31 oldKeys <- gets keysDown
32 let forwardNOW = forwardNoteEvent h q publicAddr
33
34 (events, newKeys) <- liftIO $ parseAlsaEvents' h oldKeys forwardNOW
35
36 if (oldKeys == newKeys) then
37 liftIO $ threadDelay 15000 -- 15ms. Seems like a lot, but it sounds OK. Cuts CPU down to 2%.
38 else do
39 let newHistory = if Set.null oldKeys then Nothing:newEvents else newEvents
40 newEvents = map Just events
41 liftIO $ printChordLn newKeys
42 put loopState { keysDown = newKeys, inputHistory = newHistory ++ inputHistory loopState }
43
44 loop