Java Gervill Software Synth not working

235 Views Asked by At

In JDK8, I can't seem to get any useful data out of the AudioInputStream created by a SoftSynthesizer.

    AudioSynthesizer s = new SoftSynthesizer();

    AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
    AudioInputStream stream = s.openStream(format, null);

    s.getChannels()[0].noteOn(60, 100);

    while (stream.available() >= 0) {
        byte[] b = new byte[2];
        stream.read(b);
        System.out.println(b[0] << 8 | b[1]);
    }

    s.getChannels()[0].noteOff(60);

However, println() only shows 0, there is no audio data coming out from the AudioInputStream. Am I doing something wrong? Or is Gervill deprecated as of JDK 8?

1

There are 1 best solutions below

0
On BEST ANSWER

Seems that I have found the answer. you have to read exactly an integer multiple of frames of data from the AudioInputStream or it will always return zero. In my case, reading 4 bytes solved the problem as the AudioFormat is 16 bits (2 bytes), 2 channels, so it is 4 bytes/frame.

change

byte[] b = new byte[2]

to

byte[] b = new byte[4]

In fact, any integer multiple of 4 will work. 4, 8, 12, 16, 20, etc.