4

I am trying to make a simple app that read from a midi port (hardware) and forward events to the software synth. It mostly work except that the soft synth stop playing from time to time. I can see midi messages being forwarded in the logs, I can trace in debug and see that the event reach the native code in the synth receiver but for some reason, the synth does not play the note. If you wait then the sound play again, then stop, then play again...

Here is a demo app that shows the problem. If you hold the enter button in the console, you will hear a note repeatedly. After some time (likely less than a minute), the sound will stop (event if you keep the button pressed), and then it will come back.

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.sound.midi.MidiSystem;
import javax.sound.midi.Synthesizer;

public class TestMidi2 {

    public static void main( String[] args ) throws Exception {
        Synthesizer synth = MidiSystem.getSynthesizer();
        synth.open();

        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        boolean on = true;
        while ( in.readLine() != null ) {
            if ( on ) {
                synth.getChannels()[0].noteOn( 45, 127 );
            } else {
                synth.getChannels()[0].noteOff( 45 );
            }
            on = !on;
        }
    }

}

I am on MacOS X lion if this make a difference (which I guess it does).

Any idea? Workaround? I would like to try other software synth but could not find any. I am also willing to try hardware midi synth as long as they can play basic piano, flute and guitar (I don't need anything pro, just decent sound).

Thank you!

Manuel Darveau
  • 4,585
  • 5
  • 26
  • 36

2 Answers2

2

It's a Lion Problem. I'm developing a tool which sends MIDI to different ports and tested it on many platforms. Java Sound Synthesiser works well for all OS X - Versions except Lion. There it seems like the buffer of the Synthesiser overflows. After a few notes it stops playing, if I send a punch of notes it starts working again and stops again,..

However, sadly the Java Sound Synthesiser is an old thing where it seems nobody supports it any more.

Does anyone know other possibilities to play MIDI Sounds via Java except sending it to an third party sequencer? Would be nice if there would be something like just another General MIDI Library.

Thanks and greez!

pianissimo
  • 265
  • 3
  • 8
  • Thanks for the reply. What I did is setup a virtual midi connection using the "Audio midi setup" utility and used logic express as a synthesizer. The drawback is that you need an external software synthesizer ($$ unless you use garage band which is limited since it read all midi input devices) but you get much better sounds than the build in general midi sound bank. – Manuel Darveau Nov 12 '11 at 03:21
  • Damn here I thought it was just me! I filed a bug report the other day about this issue. – Ben Nov 19 '11 at 03:17
  • Since Java 6, there is a substitute available: Gervill. It works fine, can load soundbanks and thus, provides a lot of flexibility. In Java < 7 it can be activated by the following VM-argument: -Dsun.sound.useNewAudioEngine=true Java 7 doesn't need this argument any more. – pianissimo Dec 06 '12 at 18:29
0

Your problem isn't with the midi, it's with this:

while ( in.readLine() != null )

You're holding down the enter key, which allows the loop to execute very quickly many times. Your code will flip the note on and off as fast as the input buffer can take in data, and it will sound continuous, but I'd bet that if you were to read the midi signals you would have hundreds of them in the span of a few seconds. Your sound probably stops because the synth can't deal with the immense amount of midi data you are feeding it.

Try just pressing enter once and see if that keeps a continuous note going.

I would look into a better way to read the keys into your program. See this SO question for a possible method for doing that.

Community
  • 1
  • 1
Nate
  • 2,462
  • 1
  • 20
  • 28
  • This is just a demo code. The actual software reads midi from a hardware device (drum pad) and even if I play slowly, I get the same result. The while is there only to get a quick and dirty input method. Anyway, thanks for the answer... – Manuel Darveau Oct 13 '11 at 04:09