diff options
author | Andrew Cady <d@jerkface.net> | 2015-12-03 02:40:01 -0500 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2015-12-03 02:40:01 -0500 |
commit | 6b1f45968645c12a31a750e7c1e428ca44ab4172 (patch) | |
tree | 7e860cebdbfd34b690044b90b597217a947289be | |
parent | 5d6537d8e548b3c1a19b3af25194cb4a0f9121c1 (diff) |
separate out a midi event hook in midi-dump.hs
-rw-r--r-- | AlsaSeq.hs | 6 | ||||
-rw-r--r-- | midi-dump.hs | 16 |
2 files changed, 14 insertions, 8 deletions
@@ -1,7 +1,7 @@ | |||
1 | {-# LANGUAGE NondecreasingIndentation #-} | 1 | {-# LANGUAGE NondecreasingIndentation #-} |
2 | module AlsaSeq (withAlsaInit, parseAlsaEvents, forwardNoteEvent, | 2 | module AlsaSeq (withAlsaInit, parseAlsaEvents, forwardNoteEvent, |
3 | cmdlineAlsaConnect, printChordLn, showChord, pitchList, showPitch, | 3 | cmdlineAlsaConnect, printChordLn, showChord, pitchList, showPitch, |
4 | unPitch, unChannel) where | 4 | unPitch, unChannel, MidiHook) where |
5 | import qualified Sound.ALSA.Exception as AlsaExc | 5 | import qualified Sound.ALSA.Exception as AlsaExc |
6 | import qualified Sound.ALSA.Sequencer.Address as Addr | 6 | import qualified Sound.ALSA.Sequencer.Address as Addr |
7 | import qualified Sound.ALSA.Sequencer as SndSeq | 7 | import qualified Sound.ALSA.Sequencer as SndSeq |
@@ -162,6 +162,9 @@ parseAlsaEvents h keysDown immediate = loop keysDown | |||
162 | loop (Set.delete (Event.noteChannel n, Event.noteNote n) keysDown) | 162 | loop (Set.delete (Event.noteChannel n, Event.noteNote n) keysDown) |
163 | _ -> loop keysDown | 163 | _ -> loop keysDown |
164 | 164 | ||
165 | type MidiHook = Event.T -> IO () | ||
166 | |||
167 | forwardNoteEvent :: SndSeq.AllowOutput mode => SndSeq.T mode -> Queue.T -> Addr.T -> MidiHook | ||
165 | forwardNoteEvent h q publicAddr ev = do | 168 | forwardNoteEvent h q publicAddr ev = do |
166 | --let immediate = \a b c -> do { Event.output h $ mkEv $ a b c; _ <- Event.drainOutput h; return (); } | 169 | --let immediate = \a b c -> do { Event.output h $ mkEv $ a b c; _ <- Event.drainOutput h; return (); } |
167 | 170 | ||
@@ -169,3 +172,4 @@ forwardNoteEvent h q publicAddr ev = do | |||
169 | 172 | ||
170 | let (Event.Cons highPriority tag _ time _ _ body) = ev in Event.output h (Event.Cons highPriority tag q time publicAddr Addr.subscribers body) | 173 | let (Event.Cons highPriority tag _ time _ _ body) = ev in Event.output h (Event.Cons highPriority tag q time publicAddr Addr.subscribers body) |
171 | Event.drainOutput h | 174 | Event.drainOutput h |
175 | return () | ||
diff --git a/midi-dump.hs b/midi-dump.hs index aa19def..0f714ed 100644 --- a/midi-dump.hs +++ b/midi-dump.hs | |||
@@ -1,18 +1,19 @@ | |||
1 | {-# LANGUAGE NondecreasingIndentation #-} | ||
2 | import AlsaSeq | 1 | import AlsaSeq |
3 | import Control.Concurrent (threadDelay) | 2 | import Control.Concurrent (threadDelay) |
4 | import qualified Sound.ALSA.Exception as AlsaExc | 3 | import qualified Sound.ALSA.Exception as AlsaExc |
5 | import qualified Data.Set as Set | 4 | import qualified Data.Set as Set |
6 | 5 | ||
7 | main = (do | 6 | main = main' `AlsaExc.catch` handler |
7 | where | ||
8 | handler e = putStrLn $ "alsa_exception: " ++ AlsaExc.show e | ||
8 | 9 | ||
9 | withAlsaInit $ \h public private q publicAddr privateAddr -> do | 10 | main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do |
10 | cmdlineAlsaConnect h public | 11 | cmdlineAlsaConnect h public |
11 | 12 | ||
12 | let | 13 | let |
13 | forwardNOW = forwardNoteEvent h q publicAddr | 14 | forwardNOW = forwardNoteEvent h q publicAddr |
14 | go keysDown = do | 15 | go keysDown = do |
15 | keysDown' <- parseAlsaEvents h keysDown forwardNOW | 16 | keysDown' <- parseAlsaEvents h keysDown (\ev -> do forwardNOW ev; hook ev) |
16 | if (keysDown == keysDown') then | 17 | if (keysDown == keysDown') then |
17 | threadDelay 15000 -- 15ms. Seems like a lot, but it sounds OK. Cuts CPU down to 2%. | 18 | threadDelay 15000 -- 15ms. Seems like a lot, but it sounds OK. Cuts CPU down to 2%. |
18 | else do | 19 | else do |
@@ -20,6 +21,7 @@ main = (do | |||
20 | go keysDown' | 21 | go keysDown' |
21 | 22 | ||
22 | putStrLn "Rock on!" | 23 | putStrLn "Rock on!" |
23 | go Set.empty) | 24 | go Set.empty |
24 | `AlsaExc.catch` \e -> | 25 | |
25 | putStrLn $ "alsa_exception: " ++ AlsaExc.show e | 26 | hook :: MidiHook |
27 | hook ev = return () | ||