1 Reply Latest reply on Apr 11, 2012 8:31 AM by Phil Freihofner

    Proper Way to Wait for MIDI

      Hello everyone,

      I'm using a keyboard connecting with a MIDI cable to my computer, and writing a program that should react to the keys I press on the keyboard.
      I've so far successfully managed to play notes exactly when I play (for example, each time I play C, E is also played).

      Now, however, I want to also produce sounds with a slight delay- for example, once I play a certain note, the system will pause for x milliseconds, then play a note, perhaps multiple times.

      I've so far used Thread.sleep() for the delay. This doesn't work very well when I press multiple keys, and also when the CPU lags.

      What would be the proper way to generate the delay between the notes played?

      I realize the solution might not necessarily be from the midi library and perhaps it's just a matter of concurrency. I've written it here just in case there is a better solution for that in the library.

      Thanks in advance for any help.
        • 1. Re: Proper Way to Wait for MIDI
          Phil Freihofner
          I don't know much about sequencers. But maybe a concept from the "raw audio" world would help. Riven's comment in this thread is what is most pertinent:

          How are MIDI sounds launched? Are you doing them singly? Or can they be placed in the context of a running sequence, running timeline? If the latter can be done, then you have a better chance of accuracy.

          There's also a link, in that thread, to a good article about timing issues for audio, in my first post there.

          It might be helpful to enqueue the MIDI events as they arrive, with a timestamp, and use the timestamps when calculating when to feed them to the Sequencer. But as I said, I could be way off, as I haven't worked with MIDI in Java yet, and don't know if that even fits the model of how things are done.

          But it's not like you've gotten a lot of help here from anyone else, yet...

          You might try StackOverflow forums.