# HG changeset patch # User Robert McIntyre # Date 1335190927 18000 # Node ID 476f7da175a4ad6adb2dc9bcd0561092e9e87c1f # Parent fbccf46cf34d14cd76dd6f02a25e1eb5b27da1b4 correctly handle silent spaces in midi files. diff -r fbccf46cf34d -r 476f7da175a4 clojure/com/aurellem/run/music.clj --- a/clojure/com/aurellem/run/music.clj Mon Apr 23 08:26:23 2012 -0500 +++ b/clojure/com/aurellem/run/music.clj Mon Apr 23 09:22:07 2012 -0500 @@ -333,7 +333,10 @@ ;; want: seconds - +(defn silence [length] + {:frequency 1 + :duration length + :volume 0}) (defn midi->mini-midi [#^File midi-file] (let [midi-events (parse-midi midi-file) @@ -356,25 +359,46 @@ tempo (:args (first (filter #(= :Tempo (:command %)) midi-events))) division (:division (:args (first (filter #(= :Header (:command %)) midi-events)))) + + notes + (map + (fn [note-on note-off] + {:frequency (midi-code->frequency (:note (:args note-on))) + :duration + (/ (* (/ tempo division) + (- (:time note-off) (:time note-on))) + 1e6) ;; convert clock-pulses into seconds + :volume (int (/ (:velocity (:args note-on)) 10)) + :time-stamp (/ (* (/ tempo division) + (:time note-on)) 1e6)}) + channel-1-on channel-1-off) + + silences + (map (fn [note-1 note-2] + (let [note-1-space (- (:time-stamp note-2) + (:time-stamp note-1)) + note-1-length (:duration note-1)] + (silence (- note-1-space note-1-length)))) + ;; to handle silence at the beginning. + (concat [(assoc (silence 0) + :time-stamp 0)] notes) + notes) + + notes-with-silence + (filter (comp not zero? :duration) (interleave silences notes)) ] - + (map (fn [note-event] (note-codes (:frequency note-event) (:volume note-event) (int (* (:duration note-event) 0x100)))) + notes-with-silence))) + + - (map - (fn [note-on note-off] - {:frequency (midi-code->frequency (:note (:args note-on))) - :duration - (/ (* (/ tempo division) - (- (:time note-off) (:time note-on))) - 1e6) ;; convert clock-pulses into seconds - :volume (int (/ (:velocity (:args note-on)) 10)) - :time-stamp (/ (* (/ tempo division) - (:time note-on)) 1e6)}) - channel-1-on channel-1-off)))) + +