5

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!

3
  • Can you reproduce the problem using a MIDI that can be hot-linked to? E.G. there are 2 below 12Kb available at my media page. Nov 16, 2011 at 9:42
  • here's a sample file I left my mac at work; get back to you tomorrow after I test your files. The problem seems to occur at random though! I haven't been able to reproduce it reliably. It happens at least once every 30 seconds or so.
    – Ben
    Nov 16, 2011 at 12:00
  • Andrew, I have been able to recreate the problem very reliably with the EverLove and AftrMdnt test files you linked to. I can't play more than 20 seconds of either without the synthesizer hanging (notes are left hanging, no new noteon commands sent either). The problem seems to occur at random.
    – Ben
    Nov 17, 2011 at 8:53

2 Answers 2

4

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.

3

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.

4
  • Thanks for the answer. It seems like I'm not the only one experiencing this problem after all! I'm looking into MMJ right now but it seems like the problem is in the synthesizer more than anything.
    – Ben
    Nov 19, 2011 at 3:15
  • (For some reason I was not notified of your comment, but happened to notice it anyway) Doesn't MMJ also supply a synthesizer? I didn't look that deeply into it. Nov 19, 2011 at 3:37
  • I went through the source quite comprehensively and couldn't find a synthesizer.. It looks more like a library for communicating with hardware midi devices. Then again I know nuts about JNI and CoreMIDI! I couldn't find any examples on Google either. I got the test in the archive to compile, but all it supplied was a midi keyboard. There was no option for audio synthesis with mmj; I only managed to get audio output with the native option. Really discouraging stuff.
    – Ben
    Nov 19, 2011 at 9:25
  • I did hear that 2 new JREs were about to become available for the Mac, but when it comes to applets, it is most often 'use the existing JRE or lose the content (and visitor, most likely)'. :( There does not seem much help from Flash or HTML (e.g. object/embed) either. It seems the overriding message is "don't put sound in web pages". Nov 19, 2011 at 9:41

Your Answer

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

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