From 9a15e58ff786aaef0fbb673d14de562f37bbc596 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Sat, 5 Dec 2015 06:34:47 -0500 Subject: recording stores both end time and time of last event --- midi-dump.hs | 56 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/midi-dump.hs b/midi-dump.hs index 8484272..f053e28 100644 --- a/midi-dump.hs +++ b/midi-dump.hs @@ -46,6 +46,7 @@ data Recording = StartRecording TimeSpec | data CompleteRecording = CompleteRecording { _recStart :: TimeSpec, + _recEnd :: TimeSpec, _recFirst :: TimeSpec, _recLast :: TimeSpec, _recEvents :: [RecordedEvent] @@ -57,9 +58,9 @@ recordEvents i@(StartRecording _) [] = i recordEvents (StartRecording x) new@(_:_) = RecordingInProgress x y new where y = fst $ last new -stopRecording :: Recording -> Maybe CompleteRecording -stopRecording (RecordingInProgress x y ls@((z,_):_)) = Just $ CompleteRecording x y z ls -stopRecording _ = Nothing +stopRecording :: Recording -> TimeSpec -> Maybe CompleteRecording +stopRecording (RecordingInProgress x y ls@((z,_):_)) end = Just $ CompleteRecording x end y z ls +stopRecording _ _ = Nothing data LoopState = LoopState { @@ -100,13 +101,36 @@ 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,", - " leading_silence 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 (?,?,?,?, ?,?,?,?, ?)"] + main' :: IO () main' = withAlsaInit $ \h public private q publicAddr privateAddr -> do cmdlineAlsaConnect h public @@ -215,23 +239,26 @@ startLineReader = do _thread <- liftIO $ forkIO (forever $ tryIOError getLine >>= either (const $ putMVar mv "exit") (putMVar mv)) return mv -data Chunk = Chunk Int64 Int64 Int64 Int64 Int64 BS.ByteString -instance FromRow Chunk where - fromRow = Chunk <$> field <*> field <*> field <*> field <*> field <*> field -instance ToRow Chunk where - toRow (Chunk s ns s' ns' d m) = toRow (s, ns, s', ns', d, m) +midiToBytes :: [RecordedEvent] -> BS.ByteString +midiToBytes = pack . show +bytesToMidi :: BS.ByteString -> [RecordedEvent] +bytesToMidi = undefined + +instance FromRow CompleteRecording where + fromRow = cons <$> field <*> field <*> field <*> field <*> field <*> field <*> field <*> field <*> field + where + cons a a' b b' c c' d d' z = CompleteRecording (TimeSpec a a') (TimeSpec b b') (TimeSpec c c') (TimeSpec d d') (bytesToMidi z) instance ToRow CompleteRecording where - toRow reco = toRow (s, ns, s', ns', leadingSilence :: Int64, pack $ show midi) + toRow reco = toRow (s, ns, s', ns', s'', ns'', s''', ns''', midiToBytes midi) where - (CompleteRecording start ts@(TimeSpec s ns) (TimeSpec s' ns') midi) = reco - leadingSilence = fromIntegral $ timeSpecAsNanoSecs $ ts - start + (CompleteRecording (TimeSpec s ns) (TimeSpec s' ns') (TimeSpec s'' ns'') (TimeSpec s''' ns''') midi) = reco saveMidi :: forall (m :: * -> *). (MonadIO m, MonadState LoopState m, MonadReader LoopEnv m) => Recording -> m () saveMidi recording = do saver <- asks _saver - end <- gets lastTick -- TODO: record last tick (?) - mapM_ (liftIO . writeChan saver) $ stopRecording recording + now <- gets lastTick + mapM_ (liftIO . writeChan saver) $ stopRecording recording now startSaver :: forall (m :: * -> *). MonadIO m => Connection -> m (Chan CompleteRecording) startSaver sqlite = do @@ -241,7 +268,6 @@ startSaver sqlite = do where saver chan = forever $ do reco <- readChan chan - let sqlInsert = "INSERT INTO axis_input (start_sec, start_nsec, end_sec, end_nsec, leading_silence, midi) VALUES (?,?,?,?,?,?)" liftIO $ execute sqlite sqlInsert reco return () -- cgit v1.2.3