Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Midi in Mac OSX Broken?

Tags:

java

macos

midi

I'm trying to play midi within a browser, and have been using a Java Applet that works just fine on PCs. Its extremely unreliable on OSX, so I wrote a simple test case that exhibits the same problem:

import javax.sound.midi.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class MidiPlayer {

  public static void main(String[] args) {
      try {
          Sequencer sequencer = MidiSystem.getSequencer();
          if (sequencer == null)
              throw new MidiUnavailableException();
          sequencer.open();
          FileInputStream is = new FileInputStream("sample.mid");
          Sequence mySeq = MidiSystem.getSequence(is);
          sequencer.setSequence(mySeq);
          sequencer.start();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}

It sounds like the occasional message is getting dropped.. Like a noteoff won't fire, and a random note will hang on forever. Is this a known problem in OSX? Seems like Java just isn't getting enough love from Apple these days.

If anyone has a better solution to playing Midi in a browser, I'm all ears!

like image 563
Ben Avatar asked Nov 16 '11 08:11

Ben


2 Answers

This appears to be a two part problem. I too could not send midi sysex's using a mid-2011 OSX 10.7.5 equipped iMac. I did find a workaround - first, I had to use the mmj jar and jnilib's and secondly I had to tell my code to use timestamps of -1 and NOT to use system.currentTimeMillis(). In my case I'm sending realtime sysex messages, thus a timestamp of -1 works for me. I don't know what timestamp to use if you're dealing with midi note on/off's etc. Perhaps the timestamp is milliseconds into the future? I don't know. But I do know that I had to use both mmj and take better control of my timestamps. After that, things work as expected.

like image 101
kurt6string Avatar answered Sep 20 '22 11:09

kurt6string


From mmj - Midi for java on Mac OS X:

Apple's java Midi implementation appears a bit half-hearted. It does not consider MIDI data with status bytes >= 0xF0 to be valid (i.e. does not work with sysex, MIDI clock, timecode etc.), ignores timestamps on Midi events, device names will default to only the port's name (without hints on the device this port belongs to) and there may be other things missing alike.

The situation on OS X seems dire re MIDI, though that API is offered as a replacement.

like image 24
Andrew Thompson Avatar answered Sep 20 '22 11:09

Andrew Thompson