Name: gm110360 Date: 10/16/2001
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
The following short program attempts to play the same clip
many times in a row. The output alternates between correct
and incorrect and suggests that half the times, the sample
is incorrectly considered to be little-endian instead of
big endian. I did not find a bug that covers this.
// A very simple JavaSound program that actually plays a sine wave
// and shows a bug in JDK 1.3.1 running on Win2K :-(
// Author: Pierre Lewis ###@###.###
import java.io.*;
import javax.sound.sampled.*;
public class Klang
{
// when true, shows a bug on Win2K with JDK 1.3.1; when false, works fine
final static boolean bigendian = true;
public static void main(String args[])
{
try
{
AudioFormat pcm = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
8000, 16, 1, 2, 8000, bigendian);
DataLine.Info info = new DataLine.Info(
Clip.class,
pcm,
(int) 8000);
Clip clip = (Clip) AudioSystem.getLine(info);
// prepare data for clip
byte sampleMilli[] = new byte[8000];
for (int k = 0; k < 4000; ++k)
{
// 1004 Hz (the famous milliwatt tone)
double volt1 = 0.707 * Math.sin(k * 1004. * Math.PI / 8000.);
short val1 = (short)(volt1 * 32767.);
// 880 Hz
double volt2 = 0.707 * Math.sin(k * 880. * Math.PI / 8000.);
short val2 = (short)(volt2 * 32767.);
if (bigendian)
{
sampleMilli[2*k] = (byte)(val1 >> 8);
// overriding least-sig byte to show flipflop in tone
// by setting it to most-sig byte of another sine wave
sampleMilli[2*k+1] = (byte)(val2 >> 8);
// (with line below, sounds really ugly)
// sampleMilli[2*k+1] = (byte)(val1 & 0xff);
}
else // little endian (native) works OK on Win2k
{
sampleMilli[2*k] = (byte)(val1 & 0xff);
sampleMilli[2*k+1] = (byte)(val1 >> 8);
}
}
// play clip a few times (I know, I could also loop)
for (int k = 0; k < 9; ++k) // alternates between good & bad ?!
{
System.out.println("Clip open " + k + " " + pcm.toString());
clip.open(pcm, sampleMilli, 0, sampleMilli.length);
clip.start();
while (clip.isActive())
Thread.sleep(100);
Thread.sleep(100);
clip.stop();
clip.close();
}
System.out.println("Done!");
System.exit(0);
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Exception: " + ex);
System.exit(1);
}
}
}
(Review ID: 133693)
======================================================================