summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2015-12-04 00:38:15 -0500
committerAndrew Cady <d@jerkface.net>2015-12-04 00:38:15 -0500
commit7af27c322b1aeae772030f8c821e9167f95f9889 (patch)
treeaed71d5e4183220bf284fda0d81d278111ee37f4
parentd4427c26c730edaa07fb2258288ec133b5169744 (diff)
hlint
-rw-r--r--midi-dump.hs38
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
4import AlsaSeq 4import AlsaSeq
5import Control.Concurrent (threadDelay)
6import qualified Sound.ALSA.Exception as AlsaExc 5import qualified Sound.ALSA.Exception as AlsaExc
7import qualified Data.Set as Set 6import qualified Data.Set as Set
8import qualified Haskore.Basic.Pitch as Pitch
9import qualified Sound.ALSA.Sequencer.Event as Event 7import qualified Sound.ALSA.Sequencer.Event as Event
10import Control.Monad.RWS.Strict 8import Control.Monad.RWS.Strict
11import Data.Maybe 9import Data.Maybe
@@ -15,7 +13,7 @@ import System.Clock
15import Control.Applicative 13import Control.Applicative
16import qualified Data.ByteString as BS 14import qualified Data.ByteString as BS
17import Database.SQLite.Simple 15import Database.SQLite.Simple
18import Database.SQLite.Simple.FromRow 16import Database.SQLite.Simple.FromRow ()
19import Data.Int 17import Data.Int
20import Data.ByteString.Char8 (pack) 18import Data.ByteString.Char8 (pack)
21 19
@@ -33,7 +31,7 @@ verbose = False
33 31
34main = main' `AlsaExc.catch` handler 32main = 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
38data EVENT = MidiEvent TimeSpec Event.T | Silence TimeSpec 36data 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
68getAbsTime = do 66getAbsTime = 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
135maybeReadLine = asks _lineReader >>= liftIO . tryTakeMVar 133maybeReadLine = asks _lineReader >>= liftIO . tryTakeMVar
136startLineReader = do 134startLineReader = 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
153startSaver sqlite = do 151startSaver 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
170tsDeltas :: [TimeSpec] -> [Integer] 168tsDeltas :: [TimeSpec] -> [Integer]
171tsDeltas [] = [] 169tsDeltas [] = []
172tsDeltas ls@(x:xs) = map (\(a,b) -> a - b) $ zip nsecs (0:nsecs) 170tsDeltas 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