I have a short snippet of Clojure code using the javax.sound.midi
library to play a MIDI file. The problem is that, upon playback, the Sequence frequently pauses playback for about 20-30 seconds, then resumes. Here is the snippet of code:
;; given a string representing path to midi file, evaluate to a Sequence object
(defn read-midi-file [s]
(if (nil? (re-matches #"[^\r\n]+.(mid|midi)" s))
(throw (InvalidMidiDataException. "Invalid MIDI file."))
(try
(MidiSystem/getSequence (java.io.File. s))
(catch Exception e
(throw (IOException. "Could not read MIDI file."))))))
;; play the midi sequence back
(defn play-midi-file [mseq]
(let [sequencer (MidiSystem/getSequencer)]
(.open sequencer)
(.setSequence sequencer mseq)
(.start sequencer))) ; TODO: add MetaEventListener to determine when to stop
(play-midi-file (read-midi-file "/path/to/file/test.mid"))
Just to make sure, I checked the latency of the javax.sound.midi.Synthesizer
object at the REPL:
user> (* 0.001 (.getLatency (javax.sound.midi.MidiSystem/getSynthesizer)))
11.61
Does anyone have any experience with MIDI playback using these libraries, and if so, what might account for the playback to pause intermittently? Here is some more System information:
user> (-> (javax.sound.midi.MidiSystem/getSynthesizer) .getDeviceInfo bean)
{:version "Version 1.0", :vendor "Sun Microsystems", :name "Java Sound Synthesizer", :description \
"Software wavetable synthesizer and receiver", :class com.sun.media.sound.MixerSynth$MixerSynthInf\
o}
user> (map #(System/getProperty %) ["java.version" "java.vm.version" "os.name"])
("1.6.0_51" "20.51-b01-457" "Mac OS X")