JDK-4515126 : Playing same clip repeatedly generates different sounds
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.sound
  • Affected Version: 1.3.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-10-16
  • Updated: 2003-01-06
  • Resolved: 2002-12-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.2 mantisFixed
Description

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) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mantis FIXED IN: mantis INTEGRATED IN: mantis
2004-06-14

WORK AROUND Name: gm110360 Date: 10/16/2001 Run with AudioFormat in little-endian mode. ======================================================================
2004-06-11

PUBLIC COMMENTS Playing same clip repeatedly generates different sounds
2004-06-10

EVALUATION ###@###.### 2001-10-17 Reproducable on Windows 2000 and solaris (with little endian). The bug may be related to the FlushBug contained in bug #4326534, where the same effect can be heard when using a SourceDataLine. Changing the sample rate to 44100 (the mixer's sample rate) does not improve anything. The buffer size is aligned. ###@###.### 2002-12-06 Fixed along with other fixes for 1.4.2b08.
2002-12-06