diff options
author | Andrew Cady <d@jerkface.net> | 2015-12-04 00:38:15 -0500 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2015-12-04 00:38:15 -0500 |
commit | 7af27c322b1aeae772030f8c821e9167f95f9889 (patch) | |
tree | aed71d5e4183220bf284fda0d81d278111ee37f4 | |
parent | d4427c26c730edaa07fb2258288ec133b5169744 (diff) |
hlint
-rw-r--r-- | midi-dump.hs | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/midi-dump.hs b/midi-dump.hs index 3d3f393..864bf96 100644 --- a/midi-dump.hs +++ b/midi-dump.hs | |||
@@ -2,10 +2,8 @@ | |||
2 | {-# LANGUAGE FlexibleContexts #-} | 2 | {-# LANGUAGE FlexibleContexts #-} |
3 | 3 | ||
4 | import AlsaSeq | 4 | import AlsaSeq |
5 | import Control.Concurrent (threadDelay) | ||
6 | import qualified Sound.ALSA.Exception as AlsaExc | 5 | import qualified Sound.ALSA.Exception as AlsaExc |
7 | import qualified Data.Set as Set | 6 | import qualified Data.Set as Set |
8 | import qualified Haskore.Basic.Pitch as Pitch | ||
9 | import qualified Sound.ALSA.Sequencer.Event as Event | 7 | import qualified Sound.ALSA.Sequencer.Event as Event |
10 | import Control.Monad.RWS.Strict | 8 | import Control.Monad.RWS.Strict |
11 | import Data.Maybe | 9 | import Data.Maybe |
@@ -15,7 +13,7 @@ import System.Clock | |||
15 | import Control.Applicative | 13 | import Control.Applicative |
16 | import qualified Data.ByteString as BS | 14 | import qualified Data.ByteString as BS |
17 | import Database.SQLite.Simple | 15 | import Database.SQLite.Simple |
18 | import Database.SQLite.Simple.FromRow | 16 | import Database.SQLite.Simple.FromRow () |
19 | import Data.Int | 17 | import Data.Int |
20 | import Data.ByteString.Char8 (pack) | 18 | import Data.ByteString.Char8 (pack) |
21 | 19 | ||
@@ -33,7 +31,7 @@ verbose = False | |||
33 | 31 | ||
34 | main = main' `AlsaExc.catch` handler | 32 | main = main' `AlsaExc.catch` handler |
35 | where | 33 | where |
36 | handler e = when (verbose) $ putStrLn $ "alsa_exception: " ++ AlsaExc.show e | 34 | handler e = when verbose $ putStrLn $ "alsa_exception: " ++ AlsaExc.show e |
37 | 35 | ||
38 | data EVENT = MidiEvent TimeSpec Event.T | Silence TimeSpec | 36 | data EVENT = MidiEvent TimeSpec Event.T | Silence TimeSpec |
39 | deriving Show | 37 | deriving Show |
@@ -62,11 +60,10 @@ data LoopEnv = LoopEnv { | |||
62 | _publicAddr :: Sound.ALSA.Sequencer.Address.T, | 60 | _publicAddr :: Sound.ALSA.Sequencer.Address.T, |
63 | _privateAddr :: Sound.ALSA.Sequencer.Address.T, | 61 | _privateAddr :: Sound.ALSA.Sequencer.Address.T, |
64 | _doSave :: Bool, | 62 | _doSave :: Bool, |
65 | _lineReader :: MVar (String) | 63 | _lineReader :: MVar String |
66 | } | 64 | } |
67 | 65 | ||
68 | getAbsTime = do | 66 | getAbsTime = do |
69 | sqlite <- asks _sqlite | ||
70 | startTime <- asks _startTime | 67 | startTime <- asks _startTime |
71 | startTimeReal <- asks _startTimeReal | 68 | startTimeReal <- asks _startTimeReal |
72 | now <- liftIO $ getTime Monotonic | 69 | now <- liftIO $ getTime Monotonic |
@@ -106,7 +103,8 @@ loop = do | |||
106 | let forwardNOW = forwardNoteEvent h q publicAddr | 103 | let forwardNOW = forwardNoteEvent h q publicAddr |
107 | (events, newKeys) <- liftIO $ parseAlsaEvents' h oldKeys forwardNOW | 104 | (events, newKeys) <- liftIO $ parseAlsaEvents' h oldKeys forwardNOW |
108 | 105 | ||
109 | if (oldKeys == newKeys) then | 106 | |
107 | if oldKeys == newKeys then | ||
110 | liftIO $ threadDelay 15000 -- 15ms. Seems like a lot, but it sounds OK. Cuts CPU down to 2%. | 108 | liftIO $ threadDelay 15000 -- 15ms. Seems like a lot, but it sounds OK. Cuts CPU down to 2%. |
111 | else do | 109 | else do |
112 | now <- getAbsTime | 110 | now <- getAbsTime |
@@ -116,15 +114,15 @@ loop = do | |||
116 | modify $ \s -> s { keysDown = newKeys, inputHistory = newEvents ++ inputHistory s, lastTick = now } | 114 | modify $ \s -> s { keysDown = newKeys, inputHistory = newEvents ++ inputHistory s, lastTick = now } |
117 | 115 | ||
118 | when (Set.null newKeys) $ do | 116 | when (Set.null newKeys) $ do |
119 | {- | ||
120 | chunk <- gets $ takeWhile (not . isSilence) . inputHistory | ||
121 | saveMidi chunk | ||
122 | 117 | ||
123 | hist <- gets $ filter (not . isSilence) . inputHistory | 118 | when False $ do |
124 | liftIO $ print $ map (`div` 1000000) $ tsDeltas $ map getTS $ reverse hist | 119 | chunk <- gets $ takeWhile (not . isSilence) . inputHistory |
125 | liftIO $ print $ mapMaybe getMidiDesc $ reverse hist | 120 | saveMidi chunk |
126 | modify $ \s -> s { inputHistory = Silence now:inputHistory s } | 121 | |
127 | -} | 122 | hist <- gets $ filter (not . isSilence) . inputHistory |
123 | liftIO $ print $ map (`div` 1000000) $ tsDeltas $ map getTS $ reverse hist | ||
124 | liftIO $ print $ mapMaybe getMidiDesc $ reverse hist | ||
125 | modify $ \s -> s { inputHistory = Silence now:inputHistory s } | ||
128 | 126 | ||
129 | doSave <- asks _doSave | 127 | doSave <- asks _doSave |
130 | when doSave $ gets inputHistory >>= saveMidi >> return () | 128 | when doSave $ gets inputHistory >>= saveMidi >> return () |
@@ -134,7 +132,7 @@ loop = do | |||
134 | 132 | ||
135 | maybeReadLine = asks _lineReader >>= liftIO . tryTakeMVar | 133 | maybeReadLine = asks _lineReader >>= liftIO . tryTakeMVar |
136 | startLineReader = do | 134 | startLineReader = do |
137 | mv <- liftIO $ newEmptyMVar | 135 | mv <- liftIO newEmptyMVar |
138 | thread <- liftIO $ forkIO (forever $ getLine >>= putMVar mv) | 136 | thread <- liftIO $ forkIO (forever $ getLine >>= putMVar mv) |
139 | return mv | 137 | return mv |
140 | 138 | ||
@@ -151,11 +149,11 @@ saveMidi chunk = do | |||
151 | liftIO $ writeChan saver (s, ns, chunk) | 149 | liftIO $ writeChan saver (s, ns, chunk) |
152 | 150 | ||
153 | startSaver sqlite = do | 151 | startSaver sqlite = do |
154 | chan <- liftIO $ newChan | 152 | chan <- liftIO newChan |
155 | thread <- liftIO $ forkIO (saver sqlite chan) | 153 | thread <- liftIO $ forkIO (saver chan) |
156 | return chan | 154 | return chan |
157 | where | 155 | where |
158 | saver sqlite chan = forever $ do | 156 | saver chan = forever $ do |
159 | (s, ns, chunk) <- readChan chan | 157 | (s, ns, chunk) <- readChan chan |
160 | let bytes = pack $ show chunk | 158 | let bytes = pack $ show chunk |
161 | liftIO $ execute sqlite "INSERT INTO chunks (sec, nsec, midi) VALUES (?,?,?)" (Chunk s ns bytes) | 159 | liftIO $ execute sqlite "INSERT INTO chunks (sec, nsec, midi) VALUES (?,?,?)" (Chunk s ns bytes) |
@@ -169,7 +167,7 @@ getMidiDesc _ = Nothing | |||
169 | 167 | ||
170 | tsDeltas :: [TimeSpec] -> [Integer] | 168 | tsDeltas :: [TimeSpec] -> [Integer] |
171 | tsDeltas [] = [] | 169 | tsDeltas [] = [] |
172 | tsDeltas ls@(x:xs) = map (\(a,b) -> a - b) $ zip nsecs (0:nsecs) | 170 | tsDeltas ls@(x:_) = zipWith (-) nsecs (0:nsecs) |
173 | where | 171 | where |
174 | nsecs = map timeSpecAsNanoSecs rel | 172 | nsecs = map timeSpecAsNanoSecs rel |
175 | rel = map (\y -> y - x) ls | 173 | rel = map (\y -> y - x) ls |