Search in sources :

Example 1 with FloatControl

use of javax.sound.sampled.FloatControl in project playn by threerings.

the class JavaSound method setVolumeImpl.

@Override
protected void setVolumeImpl(float volume) {
    if (impl.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
        FloatControl volctrl = (FloatControl) impl.getControl(FloatControl.Type.MASTER_GAIN);
        volctrl.setValue(toGain(volume, volctrl.getMinimum(), volctrl.getMaximum()));
    }
}
Also used : FloatControl(javax.sound.sampled.FloatControl)

Example 2 with FloatControl

use of javax.sound.sampled.FloatControl in project lionengine by b3dgs.

the class WavImpl method updateVolume.

/**
 * Update the sound volume.
 *
 * @param dataLine Audio source data.
 * @param volume The audio playback volume value.
 */
private static void updateVolume(DataLine dataLine, int volume) {
    if (dataLine.isControlSupported(Type.MASTER_GAIN)) {
        final FloatControl gainControl = (FloatControl) dataLine.getControl(Type.MASTER_GAIN);
        final double gain = UtilMath.clamp(volume / 100.0, 0.0, 100.0);
        final double dB = Math.log(gain) / Math.log(10.0) * 20.0;
        gainControl.setValue((float) dB);
    }
}
Also used : FloatControl(javax.sound.sampled.FloatControl)

Example 3 with FloatControl

use of javax.sound.sampled.FloatControl in project smarthome by eclipse.

the class JavaSoundAudioSink method getVolume.

@Override
public PercentType getVolume() throws IOException {
    if (!isMac) {
        final Float[] volumes = new Float[1];
        runVolumeCommand(new Closure() {

            @Override
            public void execute(Object input) {
                FloatControl volumeControl = (FloatControl) input;
                volumes[0] = volumeControl.getValue();
            }
        });
        if (volumes[0] != null) {
            return new PercentType(new BigDecimal(volumes[0] * 100f));
        } else {
            throw new IOException("Cannot determine master volume level");
        }
    } else {
        // we use a cache of the value as the script execution is pretty slow
        if (macVolumeValue == null) {
            Process p = Runtime.getRuntime().exec(new String[] { "osascript", "-e", "output volume of (get volume settings)" });
            String value = IOUtils.toString(p.getInputStream()).trim();
            macVolumeValue = new PercentType(value);
        }
        return macVolumeValue;
    }
}
Also used : Closure(org.apache.commons.collections.Closure) PercentType(org.eclipse.smarthome.core.library.types.PercentType) IOException(java.io.IOException) FloatControl(javax.sound.sampled.FloatControl) BigDecimal(java.math.BigDecimal)

Example 4 with FloatControl

use of javax.sound.sampled.FloatControl in project smarthome by eclipse.

the class JavaSoundAudioSink method runVolumeCommand.

private void runVolumeCommand(Closure closure) {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    for (Mixer.Info info : infos) {
        Mixer mixer = AudioSystem.getMixer(info);
        if (mixer.isLineSupported(Port.Info.SPEAKER)) {
            Port port;
            try {
                port = (Port) mixer.getLine(Port.Info.SPEAKER);
                port.open();
                if (port.isControlSupported(FloatControl.Type.VOLUME)) {
                    FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
                    closure.execute(volume);
                }
                port.close();
            } catch (LineUnavailableException e) {
                logger.error("Cannot access master volume control", e);
            }
        }
    }
}
Also used : Mixer(javax.sound.sampled.Mixer) Port(javax.sound.sampled.Port) LineUnavailableException(javax.sound.sampled.LineUnavailableException) FloatControl(javax.sound.sampled.FloatControl)

Example 5 with FloatControl

use of javax.sound.sampled.FloatControl in project smarthome by eclipse.

the class JavaSoundAudioSink method setVolume.

@Override
public void setVolume(final PercentType volume) throws IOException {
    if (volume.intValue() < 0 || volume.intValue() > 100) {
        throw new IllegalArgumentException("Volume value must be in the range [0,100]!");
    }
    if (!isMac) {
        runVolumeCommand(new Closure() {

            @Override
            public void execute(Object input) {
                FloatControl volumeControl = (FloatControl) input;
                volumeControl.setValue(volume.floatValue() / 100f);
            }
        });
    } else {
        Runtime.getRuntime().exec(new String[] { "osascript", "-e", "set volume output volume " + volume.intValue() });
        macVolumeValue = volume;
    }
}
Also used : Closure(org.apache.commons.collections.Closure) FloatControl(javax.sound.sampled.FloatControl)

Aggregations

FloatControl (javax.sound.sampled.FloatControl)9 Closure (org.apache.commons.collections.Closure)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 BooleanControl (javax.sound.sampled.BooleanControl)1 Control (javax.sound.sampled.Control)1 LineUnavailableException (javax.sound.sampled.LineUnavailableException)1 Mixer (javax.sound.sampled.Mixer)1 Port (javax.sound.sampled.Port)1 PercentType (org.eclipse.smarthome.core.library.types.PercentType)1