开发者

Why java midi synth on mac stop playing notes

开发者 https://www.devze.com 2023-04-13 03:53 出处:网络
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 mid

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!


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号