summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2015-12-07 01:31:21 -0500
committerAndrew Cady <d@jerkface.net>2015-12-07 01:31:21 -0500
commit990bae4f495b80e38a2ca1c3f2fbe6482c2e6cc8 (patch)
tree2ba344f3f1265dcfe30e4cdf08c43da85039c9d1
parent9adf1aff92e1921203feeedf361deee1984fe2c6 (diff)
Implement playing MIDI from the database
Currently, the "dump" command plays the entire database. Only the SQL SELECT statement needs to be changed in order to play a specific time-range.
-rw-r--r--Midi.hs5
-rw-r--r--midi-dump.hs74
2 files changed, 49 insertions, 30 deletions
diff --git a/Midi.hs b/Midi.hs
index 402f683..cc14eb6 100644
--- a/Midi.hs
+++ b/Midi.hs
@@ -59,6 +59,11 @@ instance ToRow CompleteRecording where
59 where 59 where
60 (CompleteRecording (TimeSpec s ns) (TimeSpec s' ns') (TimeSpec s'' ns'') (TimeSpec s''' ns''') midi) = reco 60 (CompleteRecording (TimeSpec s ns) (TimeSpec s' ns') (TimeSpec s'' ns'') (TimeSpec s''' ns''') midi) = reco
61 61
62instance Monoid CompleteRecording where
63 mempty = CompleteRecording 0 0 0 0 []
64 (CompleteRecording s _e f _l evts) `mappend` (CompleteRecording _s' e' _f' l' evts') =
65 CompleteRecording s e' f l' (evts' ++ evts)
66
62maybesnd :: forall t t1. (t, Maybe t1) -> Maybe (t, t1) 67maybesnd :: forall t t1. (t, Maybe t1) -> Maybe (t, t1)
63maybesnd (_, Nothing) = Nothing 68maybesnd (_, Nothing) = Nothing
64maybesnd (x, Just y) = Just (x, y) 69maybesnd (x, Just y) = Just (x, y)
diff --git a/midi-dump.hs b/midi-dump.hs
index 064ce88..4c74792 100644
--- a/midi-dump.hs
+++ b/midi-dump.hs
@@ -75,38 +75,51 @@ getAbsTime = do
75 return $ now - startTime + startTimeReal 75 return $ now - startTime + startTimeReal
76 76
77createTable :: Query 77createTable :: Query
78createTable = fromString $ concat 78createTable = fromString . concat $
79 ["CREATE TABLE IF NOT EXISTS axis_input", 79 [ "CREATE TABLE IF NOT EXISTS axis_input"
80 " (id INTEGER PRIMARY KEY,", 80 , " (id INTEGER PRIMARY KEY,"
81 81 , " start_sec INTEGER,"
82 " start_sec INTEGER,", 82 , " start_nsec INTEGER,"
83 " start_nsec INTEGER,", 83 , " end_sec INTEGER,"
84 " end_sec INTEGER,", 84 , " end_nsec INTEGER,"
85 " end_nsec INTEGER,", 85 , " first_sec INTEGER,"
86 86 , " first_nsec INTEGER,"
87 " first_sec INTEGER,", 87 , " last_sec INTEGER,"
88 " first_nsec INTEGER,", 88 , " last_nsec INTEGER,"
89 " last_sec INTEGER,", 89 , " midi BLOB)"
90 " last_nsec INTEGER,", 90 ]
91
92 " midi BLOB)"]
93 91
94sqlInsert :: Query 92sqlInsert :: Query
95sqlInsert = fromString $ concat 93sqlInsert = fromString . concat $
96 ["INSERT INTO axis_input", 94 [ "INSERT INTO axis_input "
97 95 , "(start_sec,"
98 "(start_sec,", 96 , " start_nsec,"
99 " start_nsec,", 97 , " end_sec,"
100 " end_sec,", 98 , " end_nsec,"
101 " end_nsec,", 99 , " first_sec,"
102 100 , " first_nsec,"
103 " first_sec,", 101 , " last_sec,"
104 " first_nsec,", 102 , " last_nsec,"
105 " last_sec,", 103 , " midi)"
106 " last_nsec,", 104 , "VALUES (?,?,?,?, ?,?,?,?, ?)"
107 105 ]
108 " midi)", 106
109 "VALUES (?,?,?,?, ?,?,?,?, ?)"] 107sqlSelectEVERYTHING :: MidiController [CompleteRecording]
108sqlSelectEVERYTHING = do
109 conn <- asks _sqlite
110 liftIO $ query_ conn $ fromString . concat $
111 [ "SELECT "
112 , "start_sec,"
113 , "start_nsec,"
114 , "end_sec,"
115 , "end_nsec,"
116 , "first_sec,"
117 , "first_nsec,"
118 , "last_sec,"
119 , "last_nsec,"
120 , "midi"
121 , " FROM axis_input ORDER BY start_sec, start_nsec;"
122 ]
110 123
111main' :: IO () 124main' :: IO ()
112main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do 125main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do
@@ -176,6 +189,7 @@ processCommand :: String -> MidiController ()
176processCommand "exit" = modify $ \s -> s { _wantExit = True } 189processCommand "exit" = modify $ \s -> s { _wantExit = True }
177-- processCommand "" = return () 190-- processCommand "" = return ()
178processCommand "" = gets _replay >>= playRecording 191processCommand "" = gets _replay >>= playRecording
192processCommand "dump" = sqlSelectEVERYTHING >>= playRecording . mconcat
179processCommand "C" = do 193processCommand "C" = do
180 let notes = (Event.NoteEv Event.NoteOn . mkNote) <$> [60, 64, 67] 194 let notes = (Event.NoteEv Event.NoteOn . mkNote) <$> [60, 64, 67]
181 forM_ notes (delayNoteEv (TimeSpec 0 0)) 195 forM_ notes (delayNoteEv (TimeSpec 0 0))