JDK-5078180 : REGRESSION: javax.sound wrongly sets frameSize wrong and throws exception
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.sound
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-07-25
  • Updated: 2004-07-26
  • Resolved: 2004-07-26
Related Reports
Duplicate :  
Description

Name: rmT116609			Date: 07/25/2004


FULL PRODUCT VERSION :
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

EXTRA RELEVANT SYSTEM CONFIGURATION :
Using on-board VIA soundchip.

A DESCRIPTION OF THE PROBLEM :
When I set the AudioFormat to a mono, 16 bit, 2 bytes per frame format, and I start streaming data to a SourceDataLine, I get the error:

Exception in thread "main" java.lang.IllegalArgumentException: illegal request t
o write non-integral number of frames (1026 bytes, frameSize = 4 bytes)
        at com.sun.media.sound.DirectAudioDevice$DirectDL.write(DirectAudioDevic
e.java:687)

I explicitly write 1026 bytes to show the problem (it's not a multiple of 4). The frameSize should be 2 bytes, not 4.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the provided source of 'SoundBug'.
Run the program on JRE 1.4.2, it produces a noise sound.
Run the program again on JRE 1.5.0-beta2-b51, the program crashes.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When the program runs on JRE 1.5.0-beta2, the result should be the same as when running on 1.4.2: It should produce a short noise sound.
ACTUAL -
When the program is started on 1.5.0, the program crashes with the Exception stated below.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.IllegalArgumentException: illegal request t
o write non-integral number of frames (1026 bytes, frameSize = 4 bytes)
        at com.sun.media.sound.DirectAudioDevice$DirectDL.write(DirectAudioDevice.java:687)
        at SoundBug.run(SoundBug.java:35)
        at SoundBug.main(SoundBug.java:50)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class SoundBug {

	private static final int BUFFER_SIZE = 1026;

	private AudioFormat format;
	private SourceDataLine line;

	private byte[] buffer;

	private void run() {
		buffer = new byte[BUFFER_SIZE];

		format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 1, 2, 44100, false);
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
		try {
			line = (SourceDataLine) AudioSystem.getLine(info);
			line.open();
			line.start();
			System.out.println("Sound initialized successfully.");
		} catch (LineUnavailableException lue) {
			System.err.println("Unavailable data line");
		}

		for (int i = 0; i < 100; i++) {
			// fill with noise
			for (int ii = 0; ii < buffer.length; ii++) {
				buffer[ii] = (byte) ((int) (Math.random() * 256) & 0xff);
			}
			line.write(buffer, 0, buffer.length);
		}

		line.drain();
		line.stop();
		line.close();

		// dunno why this is necessary, but the javasound seems to not let the
		// program end...
		System.exit(0);

	}

	public static void main(String[] args) {
		SoundBug bug = new SoundBug();
		bug.run();
	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Since I don't know why the frameSize is set to 4 (maybe javasound wrongly decided to use a stereo format, maybe not. No way to be sure), I wouldn't know for sure how to fill the buffers and I didn't find a workaround yet. In my own application I tried to force the buffer size to be a multiple of 4, but then the whole application froze...

Release Regression From : 1.4.2_04
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Incident Review ID: 286975) 
======================================================================

Comments
EVALUATION The sympton is due to bug 5053380. See also 5067526: Using line.open() without a format parameter will open it with the default format. Use one of the other SourceDataLine.open methods to open it with a specific format. ###@###.### 2004-07-26
26-07-2004

PUBLIC COMMENTS User error -- Line.open() with parameters should not be used for SourceDataLines, since it will open it with a default format. ###@###.### 2004-07-26
26-07-2004