summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2015-12-11 09:48:48 -0500
committerAndrew Cady <d@jerkface.net>2015-12-11 10:02:30 -0500
commitfb2de6dfe362f62f3dde0027833f09caf3e71529 (patch)
tree162345c86967c6baa63acd90a077ca54b7c4fd5c
parent8feda549202e197a8e3e83ff986ca64bc158efdb (diff)
Command "save" now saves latest recording to disk
The filename is generated automatically from the date of the earliest event in the recording.
-rw-r--r--midi-dump.hs33
1 files changed, 27 insertions, 6 deletions
diff --git a/midi-dump.hs b/midi-dump.hs
index 5285849..56a1d50 100644
--- a/midi-dump.hs
+++ b/midi-dump.hs
@@ -34,6 +34,10 @@ import qualified Codec.Midi
34import Codec.Midi (Midi(..), FileType(..), TimeDiv(..)) 34import Codec.Midi (Midi(..), FileType(..), TimeDiv(..))
35 35
36import qualified Control.Concurrent.Thread as Thread 36import qualified Control.Concurrent.Thread as Thread
37import Data.Time.Format
38import Data.Time.LocalTime (utcToLocalZonedTime)
39import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
40import Data.Time.Clock (picosecondsToDiffTime, UTCTime)
37 41
38verbose :: Bool 42verbose :: Bool
39verbose = False 43verbose = False
@@ -113,8 +117,8 @@ sqlInsert = fromString . concat $
113 , "VALUES (?,?,?,?, ?,?,?,?, ?)" 117 , "VALUES (?,?,?,?, ?,?,?,?, ?)"
114 ] 118 ]
115 119
116sqlSelectRECENT :: MidiController [CompleteRecording] 120_sqlSelectRECENT :: MidiController [CompleteRecording]
117sqlSelectRECENT = do 121_sqlSelectRECENT = do
118 conn <- asks _sqlite 122 conn <- asks _sqlite
119 fmap reverse $ liftIO $ query_ conn $ fromString . concat $ 123 fmap reverse $ liftIO $ query_ conn $ fromString . concat $
120 [ "SELECT " 124 [ "SELECT "
@@ -132,8 +136,8 @@ sqlSelectRECENT = do
132 , " LIMIT 10" 136 , " LIMIT 10"
133 ] 137 ]
134 138
135_sqlSelectEVERYTHING :: MidiController [CompleteRecording] 139sqlSelectEVERYTHING :: MidiController [CompleteRecording]
136_sqlSelectEVERYTHING = do 140sqlSelectEVERYTHING = do
137 conn <- asks _sqlite 141 conn <- asks _sqlite
138 liftIO $ query_ conn $ fromString . concat $ 142 liftIO $ query_ conn $ fromString . concat $
139 [ "SELECT " 143 [ "SELECT "
@@ -219,12 +223,25 @@ whenFlag flag f = asks flag >>= flip when f
219mkNote :: Word8 -> Event.Note 223mkNote :: Word8 -> Event.Note
220mkNote pitch = Event.simpleNote (Event.Channel 0) (Event.Pitch pitch) (Event.Velocity 127) 224mkNote pitch = Event.simpleNote (Event.Channel 0) (Event.Pitch pitch) (Event.Velocity 127)
221 225
226chooseFileName :: Recording -> MidiController FilePath
227chooseFileName r = do
228 let startTime = earliestEvent r
229 zonedTime <- liftIO $ utcToLocalZonedTime $ timeSpecAsUTCTime startTime
230 return $ formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S%Q %Z%z.mid" zonedTime
231
232timeSpecAsUTCTime :: TimeSpec -> UTCTime
233timeSpecAsUTCTime = posixSecondsToUTCTime . fromRational . toRational . picosecondsToDiffTime . (* 1000) . timeSpecAsNanoSecs
234
222processCommand :: String -> MidiController () 235processCommand :: String -> MidiController ()
223processCommand "exit" = modify $ \s -> s { _wantExit = True } 236processCommand "exit" = modify $ \s -> s { _wantExit = True }
224-- processCommand "" = return () 237-- processCommand "" = return ()
225processCommand "" = gets _replay >>= playRecording 238processCommand "" = gets _replay >>= playRecording
226processCommand "dump" = sqlSelectRECENT >>= playRecording . mconcat 239processCommand "save" = do
227processCommand "save" = sqlSelectRECENT >>= saveRecording "saved.mid" . mconcat 240 recording <- gets _replay
241 filename <- chooseFileName recording
242 saveRecording filename recording
243 liftIO $ putStrLn $ "Saved to " ++ filename
244processCommand "dump" = sqlSelectEVERYTHING >>= saveRecording "dumped.mid" . mconcat
228processCommand "C" = do 245processCommand "C" = do
229 let notes = (Event.NoteEv Event.NoteOn . mkNote) <$> [60, 64, 67] 246 let notes = (Event.NoteEv Event.NoteOn . mkNote) <$> [60, 64, 67]
230 forM_ notes (delayNoteEv (TimeSpec 0 0)) 247 forM_ notes (delayNoteEv (TimeSpec 0 0))
@@ -382,6 +399,10 @@ detectTriads pitches = concatMap f (Map.keys pitches)
382sumM :: (Monad m, Num a, Foldable t) => t (m a) -> m a 399sumM :: (Monad m, Num a, Foldable t) => t (m a) -> m a
383sumM = foldM (fmap . (+)) 0 400sumM = foldM (fmap . (+)) 0
384 401
402earliestEvent :: Recording -> TimeSpec
403earliestEvent (StartRecording x) = x
404earliestEvent (RecordingInProgress _ x _) = x
405
385latestEvent :: Recording -> TimeSpec 406latestEvent :: Recording -> TimeSpec
386latestEvent (StartRecording x) = x 407latestEvent (StartRecording x) = x
387latestEvent (RecordingInProgress _ x []) = x 408latestEvent (RecordingInProgress _ x []) = x