@Override public void onVolumeChanged(short volume) { log.info("apply_volume_callback"); log.debug("volume: " + volume); if (audioLine == null) { log.warn("audioLine not ready"); return; } FloatControl volumeControl = (FloatControl) audioLine.getControl(FloatControl.Type.MASTER_GAIN); float maxDb = volumeControl.getMaximum(); log.debug("maxDb : " + maxDb); float minDbOrig = volumeControl.getMinimum(); float minDb = minDbOrig + ((maxDb - minDbOrig) / 3); log.debug("minDb : " + minDb); float newVolume = 0; float volumePercent = (float) (volume / 655.35); if (volumePercent < 0) { volumePercent = 100 + volumePercent; } log.debug("volume percent : " + volumePercent); newVolume = (volumePercent * minDb / 100); newVolume = (newVolume - minDb) * -1; if (volume == 0) { log.debug("volume 0, setting max"); newVolume = minDbOrig; } log.debug("newVolume : " + newVolume); volumeControl.setValue(newVolume); }
public void unMute() { if (audioLine == null) { isMuted = false; } else { isMuted = false; if (!audioLine.isControlSupported(BooleanControl.Type.MUTE)) return; BooleanControl control = (BooleanControl) audioLine.getControl(BooleanControl.Type.MUTE); control.setValue(false); } }
public void doFade(SourceDataLine line, final double to) { final FloatControl gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN); new Thread( new Runnable() { @Override public void run() { fade(gainControl, to); } }) .start(); }
public void mute() { if (audioLine == null) { isMuted = true; } else { // when requesting the same line you sometimes get different features isMuted = true; if (!audioLine.isControlSupported(BooleanControl.Type.MUTE)) return; BooleanControl control = (BooleanControl) audioLine.getControl(BooleanControl.Type.MUTE); control.setValue(true); } }
@Override public void onActive() { if (audioLine != null) audioLine.close(); try { if (player.getMixer() != null) { log.debug("Custom mixer " + player.getMixer().getName()); audioLine = AudioSystem.getSourceDataLine(PCM, player.getMixer()); } else { audioLine = AudioSystem.getSourceDataLine(PCM); } audioLine.open(PCM, 1048576); onVolumeChanged(player.getVolume()); if (isMuted && audioLine.isControlSupported(BooleanControl.Type.MUTE)) ((BooleanControl) audioLine.getControl(BooleanControl.Type.MUTE)).setValue(true); } catch (LineUnavailableException e) { log.error("onActive error", e); } }
private void setVolume(double value) { for (SourceDataLine line : new SourceDataLine[] {mainLine, bufferLine}) { FloatControl gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN); gainControl.setValue(getDb(value)); } }