13

I'm developing a music programming language, and using the JVM (via Clojure) to play musical scores written in this language. So far, we are just using the javax.sound.midi MidiSynthesizer to play the scores.

Because Clojure has a slow startup time and we want to be able to play a score from the command-line and hear it immediately, we've chosen to structure the score interpreter as a background server process, and communicate with it using a more lightweight command-line client written in Java.

All of this is working great for the most part, however, there is a strange issue that we're seeing where if you start the server, then close your laptop* and let it hibernate, then open it again and have the server play a score, the audio doesn't happen immediately but is delayed for several seconds. Running the server with debug logging, I can actually see that the MIDI note on/off events are happening immediately (and timed correctly), but the audio is delayed.

*This may or may not be platform-specific. I'm seeing the issue on my 2014 Macbook Pro running OS X 10.9.5 Mavericks.

To help narrow it down, I put together this simple example (using Java, not Clojure) that demonstrates the problem:

https://github.com/daveyarwood/java-midi-delayed-audio-example

I've been scratching my head over this for a while now. Why is the audio delayed, and is there anything we can do about it?

2 Answers 2

4

This looks like a bug in Sun's implementation of Synthesizer.

I did not investigate this deeply, but I've found that the problem is apparently in Jitter Corrector that wraps AudioInputStream. Jitter Corrector thread relies on System.nanoTime(). However nanoTime may jump when a computer wakes up from standby or hibernate mode.

The work-around is to disable Jitter Corrector. You can do so by opening Synthesizer this way:

    synth = MidiSystem.getSynthesizer();

    if (synth instanceof com.sun.media.sound.SoftSynthesizer) {
        Map<String, Object> params = Collections.singletonMap("jitter correction", false);
        ((com.sun.media.sound.SoftSynthesizer) synth).open(null, params);
    } else {
       synth.open();
    }
4
  • I tried this and can verify that it works as a workaround! Awesome! Commented Aug 17, 2016 at 0:21
  • Unfortunately, using this work-around does have the nasty side effect of making the timing of events a little less accurate. It's not terrible, but it is obvious in the case of the application I'm working on. I'm noticing a clear difference between jitter correction being on vs. off. Commented Aug 22, 2016 at 1:44
  • 1
    Since this is the only answer I've gotten, and it does at least explain the problem and one way to solve it, I'll consider it the accepted answer. I did file a bug with Java, so fingers crossed that it might be fixed at some point. Commented Aug 29, 2016 at 13:02
  • Any idea how to disable jitter correction on Java 11?
    – Cromax
    Commented Dec 2, 2019 at 0:21
0

In addition to @apangin's solution, I have found two other workarounds:

  • Before each playback, close and re-open the same Synthesizer instance.

  • Use a new Synthesizer instance for each playback.

Neither of these are ideal because it takes a couple seconds to open a synthesizer instance (even if it's an existing one that was previously open), but these workarounds may be sufficient for some use cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.