summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2015-12-03 02:40:01 -0500
committerAndrew Cady <d@jerkface.net>2015-12-03 02:40:01 -0500
commit6b1f45968645c12a31a750e7c1e428ca44ab4172 (patch)
tree7e860cebdbfd34b690044b90b597217a947289be
parent5d6537d8e548b3c1a19b3af25194cb4a0f9121c1 (diff)
separate out a midi event hook in midi-dump.hs
-rw-r--r--AlsaSeq.hs6
-rw-r--r--midi-dump.hs16
2 files changed, 14 insertions, 8 deletions
diff --git a/AlsaSeq.hs b/AlsaSeq.hs
index 22208bd..1c53b18 100644
--- a/AlsaSeq.hs
+++ b/AlsaSeq.hs
@@ -1,7 +1,7 @@
1{-# LANGUAGE NondecreasingIndentation #-} 1{-# LANGUAGE NondecreasingIndentation #-}
2module AlsaSeq (withAlsaInit, parseAlsaEvents, forwardNoteEvent, 2module AlsaSeq (withAlsaInit, parseAlsaEvents, forwardNoteEvent,
3cmdlineAlsaConnect, printChordLn, showChord, pitchList, showPitch, 3cmdlineAlsaConnect, printChordLn, showChord, pitchList, showPitch,
4unPitch, unChannel) where 4unPitch, unChannel, MidiHook) where
5import qualified Sound.ALSA.Exception as AlsaExc 5import qualified Sound.ALSA.Exception as AlsaExc
6import qualified Sound.ALSA.Sequencer.Address as Addr 6import qualified Sound.ALSA.Sequencer.Address as Addr
7import qualified Sound.ALSA.Sequencer as SndSeq 7import 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
165type MidiHook = Event.T -> IO ()
166
167forwardNoteEvent :: SndSeq.AllowOutput mode => SndSeq.T mode -> Queue.T -> Addr.T -> MidiHook
165forwardNoteEvent h q publicAddr ev = do 168forwardNoteEvent 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 #-}
2import AlsaSeq 1import AlsaSeq
3import Control.Concurrent (threadDelay) 2import Control.Concurrent (threadDelay)
4import qualified Sound.ALSA.Exception as AlsaExc 3import qualified Sound.ALSA.Exception as AlsaExc
5import qualified Data.Set as Set 4import qualified Data.Set as Set
6 5
7main = (do 6main = 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 10main' = 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 26hook :: MidiHook
27hook ev = return ()