diff options
author | Andrew Cady <d@jerkface.net> | 2015-12-11 09:48:48 -0500 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2015-12-11 10:02:30 -0500 |
commit | fb2de6dfe362f62f3dde0027833f09caf3e71529 (patch) | |
tree | 162345c86967c6baa63acd90a077ca54b7c4fd5c | |
parent | 8feda549202e197a8e3e83ff986ca64bc158efdb (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.hs | 33 |
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 | |||
34 | import Codec.Midi (Midi(..), FileType(..), TimeDiv(..)) | 34 | import Codec.Midi (Midi(..), FileType(..), TimeDiv(..)) |
35 | 35 | ||
36 | import qualified Control.Concurrent.Thread as Thread | 36 | import qualified Control.Concurrent.Thread as Thread |
37 | import Data.Time.Format | ||
38 | import Data.Time.LocalTime (utcToLocalZonedTime) | ||
39 | import Data.Time.Clock.POSIX (posixSecondsToUTCTime) | ||
40 | import Data.Time.Clock (picosecondsToDiffTime, UTCTime) | ||
37 | 41 | ||
38 | verbose :: Bool | 42 | verbose :: Bool |
39 | verbose = False | 43 | verbose = False |
@@ -113,8 +117,8 @@ sqlInsert = fromString . concat $ | |||
113 | , "VALUES (?,?,?,?, ?,?,?,?, ?)" | 117 | , "VALUES (?,?,?,?, ?,?,?,?, ?)" |
114 | ] | 118 | ] |
115 | 119 | ||
116 | sqlSelectRECENT :: MidiController [CompleteRecording] | 120 | _sqlSelectRECENT :: MidiController [CompleteRecording] |
117 | sqlSelectRECENT = 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] | 139 | sqlSelectEVERYTHING :: MidiController [CompleteRecording] |
136 | _sqlSelectEVERYTHING = do | 140 | sqlSelectEVERYTHING = 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 | |||
219 | mkNote :: Word8 -> Event.Note | 223 | mkNote :: Word8 -> Event.Note |
220 | mkNote pitch = Event.simpleNote (Event.Channel 0) (Event.Pitch pitch) (Event.Velocity 127) | 224 | mkNote pitch = Event.simpleNote (Event.Channel 0) (Event.Pitch pitch) (Event.Velocity 127) |
221 | 225 | ||
226 | chooseFileName :: Recording -> MidiController FilePath | ||
227 | chooseFileName 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 | |||
232 | timeSpecAsUTCTime :: TimeSpec -> UTCTime | ||
233 | timeSpecAsUTCTime = posixSecondsToUTCTime . fromRational . toRational . picosecondsToDiffTime . (* 1000) . timeSpecAsNanoSecs | ||
234 | |||
222 | processCommand :: String -> MidiController () | 235 | processCommand :: String -> MidiController () |
223 | processCommand "exit" = modify $ \s -> s { _wantExit = True } | 236 | processCommand "exit" = modify $ \s -> s { _wantExit = True } |
224 | -- processCommand "" = return () | 237 | -- processCommand "" = return () |
225 | processCommand "" = gets _replay >>= playRecording | 238 | processCommand "" = gets _replay >>= playRecording |
226 | processCommand "dump" = sqlSelectRECENT >>= playRecording . mconcat | 239 | processCommand "save" = do |
227 | processCommand "save" = sqlSelectRECENT >>= saveRecording "saved.mid" . mconcat | 240 | recording <- gets _replay |
241 | filename <- chooseFileName recording | ||
242 | saveRecording filename recording | ||
243 | liftIO $ putStrLn $ "Saved to " ++ filename | ||
244 | processCommand "dump" = sqlSelectEVERYTHING >>= saveRecording "dumped.mid" . mconcat | ||
228 | processCommand "C" = do | 245 | processCommand "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) | |||
382 | sumM :: (Monad m, Num a, Foldable t) => t (m a) -> m a | 399 | sumM :: (Monad m, Num a, Foldable t) => t (m a) -> m a |
383 | sumM = foldM (fmap . (+)) 0 | 400 | sumM = foldM (fmap . (+)) 0 |
384 | 401 | ||
402 | earliestEvent :: Recording -> TimeSpec | ||
403 | earliestEvent (StartRecording x) = x | ||
404 | earliestEvent (RecordingInProgress _ x _) = x | ||
405 | |||
385 | latestEvent :: Recording -> TimeSpec | 406 | latestEvent :: Recording -> TimeSpec |
386 | latestEvent (StartRecording x) = x | 407 | latestEvent (StartRecording x) = x |
387 | latestEvent (RecordingInProgress _ x []) = x | 408 | latestEvent (RecordingInProgress _ x []) = x |