Skip to content

Commit

Permalink
Clamp volume levels for hit sounds. (fixes #306)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
  • Loading branch information
itdelatrisu committed Jul 2, 2017
1 parent ab39b3e commit 212a395
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/itdelatrisu/opsu/audio/MultiClip.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package itdelatrisu.opsu.audio;

import itdelatrisu.opsu.ErrorHandler;
import itdelatrisu.opsu.Utils;

import java.io.IOException;
import java.util.Iterator;
Expand Down Expand Up @@ -140,13 +141,13 @@ public void start(float volume, LineListener listener) throws LineUnavailableExc
// set volume
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
gainControl.setValue(Utils.clamp(dB, gainControl.getMinimum(), gainControl.getMaximum()));
} else if (clip.isControlSupported(FloatControl.Type.VOLUME)) {
// The docs don't mention what unit "volume" is supposed to be,
// but for PulseAudio it seems to be amplitude
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.VOLUME);
float amplitude = (float) Math.sqrt(volume) * volumeControl.getMaximum();
volumeControl.setValue(amplitude);
volumeControl.setValue(Utils.clamp(amplitude, volumeControl.getMinimum(), volumeControl.getMaximum()));
}

if (listener != null)
Expand Down

0 comments on commit 212a395

Please # to comment.