JDK-6405128 : REGRESSION: Maximum gain value causes sound distortion
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.sound
  • Affected Version: 6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2006-03-28
  • Updated: 2011-01-19
  • Resolved: 2006-04-12
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 JDK 6
5.0u8Fixed 6 b80Fixed
Description
Setting gain control to maximum value (returned by gain control's getMaximum method) causes distortion of played sound.
Regression from 1.4.2
In the test below distartion starts (0.5 sec. from begining)
veronica.wav is attached.

import java.io.*;
import javax.sound.sampled.*;

public class PlayClip {
    public static void main(String[] args) throws Exception {
        String fileName = args.length > 0 ? args[0] : "veronika.wav";
        System.out.println("Opening file <" + fileName + ">...");
        
        InputStream streamFile = new FileInputStream(fileName);
        AudioInputStream streamAudio = AudioSystem.getAudioInputStream(streamFile);
        
        System.out.println("Creating clip...");
        //Clip clip = AudioSystem.getClip();
        Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class));
        clip.open(streamAudio);
        System.out.println("Clip class: " + clip.getClass().toString());
        System.out.println("Clip format: " + clip.getFormat());
        System.out.println("Clip length: " + clip.getMicrosecondLength());

        System.out.println("Playing...");
        clip.start();

        Thread.sleep(500);
        
        if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
            FloatControl gainCtrl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
            System.out.println("gain current value: " + gainCtrl.getValue());
            
            float maxValue = gainCtrl.getMaximum();
            System.out.println("Updating gain to maximum value" + "("
                    + maxValue + " " + gainCtrl.getUnits()
                    + ", " + dBToLinear(maxValue) + " linear)...");
            gainCtrl.setValue(maxValue);
        } else {
            System.out.println("!!! gain doesn't supported!");
        }
        
        while (clip.isRunning())
            Thread.sleep(100);
        
        clip.close();
        
        System.out.println("Done.");
    }
}

Comments
EVALUATION The issue affects all applications that use gain contol to set volume of SourceDataLine/Clip. It should be fixed. Cause of the bug: DirectAudioDevice's GainControl defines incorrect maximum value, that causes overflow in sample values.
28-03-2006