1

我有一小段 Clojure 代码使用该javax.sound.midi库来播放 MIDI 文件。问题是,在播放时,序列经常暂停播放约 20-30 秒,然后恢复。这是代码片段:

;; 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"))

为了确保,我javax.sound.midi.Synthesizer在 REPL 检查了对象的延迟:

user> (* 0.001 (.getLatency (javax.sound.midi.MidiSystem/getSynthesizer)))
11.61

有没有人有使用这些库进行 MIDI 播放的经验,如果有,是什么原因导致播放间歇性暂停?以下是更多系统信息:

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")
4

0 回答 0