summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2015-12-03 22:49:47 -0500
committerAndrew Cady <d@jerkface.net>2015-12-03 22:49:47 -0500
commitbb07239262278670136554693ab0dde56c184384 (patch)
tree669b6785b465b4cd9ac02d58192480498299c6c2
parent359baa32103902c5d9e734a8dc7bac8c82705f24 (diff)
reader has its own data type, LoopEnv, instead of a giant tuple
-rw-r--r--midi-dump.hs36
1 files changed, 31 insertions, 5 deletions
diff --git a/midi-dump.hs b/midi-dump.hs
index c4e319a..0ed7ecb 100644
--- a/midi-dump.hs
+++ b/midi-dump.hs
@@ -44,12 +44,26 @@ data LoopState = LoopState {
44 lastTick :: TimeSpec 44 lastTick :: TimeSpec
45} 45}
46 46
47data LoopEnv = LoopEnv {
48 saver :: _,
49 sqlite :: _,
50 startTime :: TimeSpec,
51 startTimeReal :: TimeSpec,
52 h :: _,
53 public :: _,
54 private :: _,
55 q :: _,
56 publicAddr :: _,
57 privateAddr :: _
58}
59
47getAbsTime = do 60getAbsTime = do
48 (_, sqlite, startTime, startTimeReal, _, _, _, _, _, _) <- ask 61 sqlite <- asks sqlite
62 startTime <- asks startTime
63 startTimeReal <- asks startTimeReal
49 now <- liftIO $ getTime Monotonic 64 now <- liftIO $ getTime Monotonic
50 return $ now - startTime + startTimeReal 65 return $ now - startTime + startTimeReal
51 66
52
53emptyLoopState = LoopState Set.empty [] 67emptyLoopState = LoopState Set.empty []
54 68
55main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do 69main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do
@@ -63,11 +77,17 @@ main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do
63 execute_ sqlite "CREATE TABLE IF NOT EXISTS chunks (id INTEGER PRIMARY KEY, sec INTEGER, nsec INTEGER, midi BLOB)" 77 execute_ sqlite "CREATE TABLE IF NOT EXISTS chunks (id INTEGER PRIMARY KEY, sec INTEGER, nsec INTEGER, midi BLOB)"
64 saver <- startSaver sqlite 78 saver <- startSaver sqlite
65 79
66 (_, ()) <- execRWST loop (saver, sqlite, startTime, startTimeReal, h, public, private, q, publicAddr, privateAddr) (emptyLoopState startTime) 80 let env = LoopEnv saver sqlite startTime startTimeReal h public private q publicAddr privateAddr
81
82 (_, ()) <- execRWST loop env (emptyLoopState startTime)
67 return () 83 return ()
68 84
69loop = do 85loop = do
70 (_, _, startTime, startTimeReal, h, _, _, q, publicAddr, _) <- ask 86 startTime <- asks startTime
87 startTimeReal <- asks startTimeReal
88 h <- asks h
89 q <- asks q
90 publicAddr <- asks publicAddr
71 91
72 oldKeys <- gets keysDown 92 oldKeys <- gets keysDown
73 let forwardNOW = forwardNoteEvent h q publicAddr 93 let forwardNOW = forwardNoteEvent h q publicAddr
@@ -83,12 +103,18 @@ loop = do
83 modify $ \s -> s { keysDown = newKeys, inputHistory = newEvents ++ inputHistory s, lastTick = now } 103 modify $ \s -> s { keysDown = newKeys, inputHistory = newEvents ++ inputHistory s, lastTick = now }
84 104
85 when (Set.null newKeys) $ do 105 when (Set.null newKeys) $ do
106 {-
86 chunk <- gets $ takeWhile (not . isSilence) . inputHistory 107 chunk <- gets $ takeWhile (not . isSilence) . inputHistory
87 saveMidi chunk 108 saveMidi chunk
109
88 hist <- gets $ filter (not . isSilence) . inputHistory 110 hist <- gets $ filter (not . isSilence) . inputHistory
89 liftIO $ print $ map (`div` 1000000) $ tsDeltas $ map getTS $ reverse hist 111 liftIO $ print $ map (`div` 1000000) $ tsDeltas $ map getTS $ reverse hist
90 liftIO $ print $ mapMaybe getMidiDesc $ reverse hist 112 liftIO $ print $ mapMaybe getMidiDesc $ reverse hist
91 modify $ \s -> s { inputHistory = Silence now:inputHistory s } 113 modify $ \s -> s { inputHistory = Silence now:inputHistory s }
114 -}
115
116 gets inputHistory >>= saveMidi >> return ()
117 modify $ \s -> s { inputHistory = [] }
92 118
93 loop 119 loop
94 120
@@ -99,7 +125,7 @@ instance ToRow Chunk where
99 toRow (Chunk s ns b) = toRow (s, ns, b) 125 toRow (Chunk s ns b) = toRow (s, ns, b)
100 126
101saveMidi chunk = do 127saveMidi chunk = do
102 (saver, _, _, _, _, _, _, _, _, _) <- ask 128 saver <- asks saver
103 (TimeSpec s ns) <- gets lastTick 129 (TimeSpec s ns) <- gets lastTick
104 liftIO $ writeChan saver (s, ns, chunk) 130 liftIO $ writeChan saver (s, ns, chunk)
105 131