From 990bae4f495b80e38a2ca1c3f2fbe6482c2e6cc8 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Mon, 7 Dec 2015 01:31:21 -0500 Subject: 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. --- Midi.hs | 5 ++++ midi-dump.hs | 74 ++++++++++++++++++++++++++++++++++++------------------------ 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 where (CompleteRecording (TimeSpec s ns) (TimeSpec s' ns') (TimeSpec s'' ns'') (TimeSpec s''' ns''') midi) = reco +instance Monoid CompleteRecording where + mempty = CompleteRecording 0 0 0 0 [] + (CompleteRecording s _e f _l evts) `mappend` (CompleteRecording _s' e' _f' l' evts') = + CompleteRecording s e' f l' (evts' ++ evts) + maybesnd :: forall t t1. (t, Maybe t1) -> Maybe (t, t1) maybesnd (_, Nothing) = Nothing maybesnd (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 return $ now - startTime + startTimeReal createTable :: Query -createTable = fromString $ concat - ["CREATE TABLE IF NOT EXISTS axis_input", - " (id INTEGER PRIMARY KEY,", - - " start_sec INTEGER,", - " start_nsec INTEGER,", - " end_sec INTEGER,", - " end_nsec INTEGER,", - - " first_sec INTEGER,", - " first_nsec INTEGER,", - " last_sec INTEGER,", - " last_nsec INTEGER,", - - " midi BLOB)"] +createTable = fromString . concat $ + [ "CREATE TABLE IF NOT EXISTS axis_input" + , " (id INTEGER PRIMARY KEY," + , " start_sec INTEGER," + , " start_nsec INTEGER," + , " end_sec INTEGER," + , " end_nsec INTEGER," + , " first_sec INTEGER," + , " first_nsec INTEGER," + , " last_sec INTEGER," + , " last_nsec INTEGER," + , " midi BLOB)" + ] sqlInsert :: Query -sqlInsert = fromString $ concat - ["INSERT INTO axis_input", - - "(start_sec,", - " start_nsec,", - " end_sec,", - " end_nsec,", - - " first_sec,", - " first_nsec,", - " last_sec,", - " last_nsec,", - - " midi)", - "VALUES (?,?,?,?, ?,?,?,?, ?)"] +sqlInsert = fromString . concat $ + [ "INSERT INTO axis_input " + , "(start_sec," + , " start_nsec," + , " end_sec," + , " end_nsec," + , " first_sec," + , " first_nsec," + , " last_sec," + , " last_nsec," + , " midi)" + , "VALUES (?,?,?,?, ?,?,?,?, ?)" + ] + +sqlSelectEVERYTHING :: MidiController [CompleteRecording] +sqlSelectEVERYTHING = do + conn <- asks _sqlite + liftIO $ query_ conn $ fromString . concat $ + [ "SELECT " + , "start_sec," + , "start_nsec," + , "end_sec," + , "end_nsec," + , "first_sec," + , "first_nsec," + , "last_sec," + , "last_nsec," + , "midi" + , " FROM axis_input ORDER BY start_sec, start_nsec;" + ] main' :: IO () main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do @@ -176,6 +189,7 @@ processCommand :: String -> MidiController () processCommand "exit" = modify $ \s -> s { _wantExit = True } -- processCommand "" = return () processCommand "" = gets _replay >>= playRecording +processCommand "dump" = sqlSelectEVERYTHING >>= playRecording . mconcat processCommand "C" = do let notes = (Event.NoteEv Event.NoteOn . mkNote) <$> [60, 64, 67] forM_ notes (delayNoteEv (TimeSpec 0 0)) -- cgit v1.2.3