Java Code Examples for javax.sound.sampled.FloatControl#setValue()

The following examples show how to use javax.sound.sampled.FloatControl#setValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetronomePlugin.java    From plugins with GNU General Public License v3.0 8 votes vote down vote up
private Clip GetAudioClip(String path)
{
	File audioFile = new File(path);
	if (!audioFile.exists())
	{
		return null;
	}

	try (AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile))
	{
		Clip audioClip = AudioSystem.getClip();
		audioClip.open(audioStream);
		FloatControl gainControl = (FloatControl) audioClip.getControl(FloatControl.Type.MASTER_GAIN);
		float gainValue = (((float) config.volume()) * 40f / 100f) - 35f;
		gainControl.setValue(gainValue);

		return audioClip;
	}
	catch (IOException | LineUnavailableException | UnsupportedAudioFileException e)
	{
		log.warn("Error opening audiostream from " + audioFile, e);
		return null;
	}
}
 
Example 2
Source File: VoicePlay.java    From oim-fx with MIT License 6 votes vote down vote up
public void playVoice(byte[] bytes, float value) {
	if (null != playLine) {
		FloatControl control = (FloatControl) playLine.getControl(FloatControl.Type.MASTER_GAIN);
		float size = 0;

		float minimum = control.getMinimum();
		float maximum = control.getMaximum();
		float all = maximum - minimum;
		if (value <= 0) {
			size = minimum;
		} else if (value >= 100) {
			size = maximum;
		} else {
			float i = all / 100F;
			size = (i * value) + minimum;
		}
		control.setValue(size);// newVal - the value of volume slider
		playLine.write(bytes, 0, bytes.length);
	}
}
 
Example 3
Source File: SoundTools.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public void setGain(float ctrl) {
    try {
        Mixer.Info[] infos = AudioSystem.getMixerInfo();
        for (Mixer.Info info : infos) {
            Mixer mixer = AudioSystem.getMixer(info);
            if (mixer.isLineSupported(Port.Info.SPEAKER)) {
                try ( Port port = (Port) mixer.getLine(Port.Info.SPEAKER)) {
                    port.open();
                    if (port.isControlSupported(FloatControl.Type.VOLUME)) {
                        FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
                        volume.setValue(ctrl);
                    }
                }
            }
        }
    } catch (Exception e) {

    }
}
 
Example 4
Source File: MultiClip.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Plays the clip with the specified volume.
 * @param volume the volume the play at
 * @param listener the line listener
 * @throws LineUnavailableException if a clip object is not available or
 *         if the line cannot be opened due to resource restrictions
 */
public void start(float volume, LineListener listener) throws LineUnavailableException {
	Clip clip = getClip();
	if (clip == null)
		return;

	// PulseAudio does not support Master Gain
	if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
		// 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(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(Utils.clamp(amplitude, volumeControl.getMinimum(), volumeControl.getMaximum()));
	}

	if (listener != null)
		clip.addLineListener(listener);
	clip.setFramePosition(0);
	clip.start();
}
 
Example 5
Source File: MultiClip.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Plays the clip with the specified volume.
 * @param volume the volume the play at
 * @param listener the line listener
 * @throws LineUnavailableException if a clip object is not available or
 *         if the line cannot be opened due to resource restrictions
 */
public void start(float volume, LineListener listener) throws LineUnavailableException {
	Clip clip = getClip();
	if (clip == null)
		return;

	// PulseAudio does not support Master Gain
	if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
		// 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);
	}

	if (listener != null)
		clip.addLineListener(listener);
	clip.setFramePosition(0);
	clip.start();
}
 
Example 6
Source File: SoundNotifier.java    From MercuryTrade with MIT License 6 votes vote down vote up
private void play(String wavPath, float db) {
    if (!dnd && db > -40) {
        ClassLoader classLoader = getClass().getClassLoader();
        try (AudioInputStream stream = AudioSystem.getAudioInputStream(classLoader.getResource(wavPath))) {
            Clip clip = AudioSystem.getClip();
            clip.open(stream);
            if (db != 0.0) {
                FloatControl gainControl =
                        (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
                gainControl.setValue(db);
            }
            clip.start();
        } catch (Exception e) {
            logger.error("Cannot start playing wav file: ", e);
        }
    }
}
 
Example 7
Source File: WaveEngine.java    From nullpomino with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Set the volume
 * @param vol New configuration volume (1.0The default )
 */
public void setVolume(double vol) {
	volume = vol;

	Set<String> set = clipMap.keySet();
	for(String name: set) {
		try {
			Clip clip = clipMap.get(name);
			FloatControl ctrl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
			ctrl.setValue((float)Math.log10(volume) * 20);
		} catch (Exception e) {}
	}
}
 
Example 8
Source File: JoggStreamer.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public void updateVolume(int percent) {
	if (out != null && out.isOpen()) {
		try {
			FloatControl c = (FloatControl) out.getControl(FloatControl.Type.MASTER_GAIN);
			float min = c.getMinimum();
			float v = percent * (c.getMaximum() - min) / 100f + min;
			c.setValue(v);
		} catch (IllegalArgumentException e) {
		}
	}
	volume = percent;
}
 
Example 9
Source File: LWaveSound.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
private void rawplay(AudioFormat trgFormat, AudioInputStream ain, float volume)
		throws IOException, LineUnavailableException {
	byte[] data = new byte[8192];
	try {
		clip = getLine(ain, trgFormat);
		if (clip == null) {
			return;
		}
		Control.Type vol1 = FloatControl.Type.VOLUME, vol2 = FloatControl.Type.MASTER_GAIN;
		FloatControl c = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
		float min = c.getMinimum();
		float v = volume * (c.getMaximum() - min) / 100f + min;
		if (this.clip.isControlSupported(vol1)) {
			FloatControl volumeControl = (FloatControl) this.clip.getControl(vol1);
			volumeControl.setValue(v);
		} else if (this.clip.isControlSupported(vol2)) {
			FloatControl gainControl = (FloatControl) this.clip.getControl(vol2);
			gainControl.setValue(v);
		}
		clip.start();
		int nBytesRead = 0;
		while (isRunning && (nBytesRead != -1)) {
			nBytesRead = ain.read(data, 0, data.length);
			if (nBytesRead != -1) {
				clip.write(data, 0, nBytesRead);
			}
		}
	} finally {
		clip.drain();
		clip.stop();
		clip.close();
		ain.close();
	}
}
 
Example 10
Source File: ClipSFXModule.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
public void setPanning(int chan,int sep){
Clip c=channels[chan];

if (c.isControlSupported(Type.PAN)){
	FloatControl bc=(FloatControl) c.getControl(Type.PAN);
	// Q: how does Doom's sep map to stereo panning?
	// A: Apparently it's 0-255 L-R.
	float pan= bc.getMinimum()+(bc.getMaximum()-bc.getMinimum())*(float)sep/ISoundDriver.PANNING_STEPS;
	bc.setValue(pan);
	}
}
 
Example 11
Source File: Beeper.java    From zxpoly with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setMasterGain(final float valueInDb) {
  final FloatControl gainControl = this.gainControl.get();
  if (gainControl != null && this.working) {
    gainControl.setValue(
        Math.max(gainControl.getMinimum(), Math.min(gainControl.getMaximum(), valueInDb)));
  }
}
 
Example 12
Source File: AudioThread.java    From open-ig with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Set the linear volume.
 * @param volume the volume 0..100, volume 0 mutes the sound
 */
public void setVolume(int volume) {
	if (volume == 0) {
		setMute(true);
	} else {
		setMute(false);
		FloatControl fc = (FloatControl)sdl.getControl(FloatControl.Type.MASTER_GAIN);
		if (fc != null) {
			fc.setValue(computeGain(fc, volume));
		}
	}
}
 
Example 13
Source File: JavaSoundAudioDevice.java    From epic-inventor with GNU General Public License v2.0 5 votes vote down vote up
public void setLineGain(float gain) {
    if (source != null) {
        if (source.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
            FloatControl volControl = (FloatControl) source.getControl(FloatControl.Type.MASTER_GAIN);
            float newGain = Math.min(Math.max(gain, volControl.getMinimum()), volControl.getMaximum());

            volControl.setValue(newGain);
        }
    }
}
 
Example 14
Source File: OggClip.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Attempt to set the balance between the two speakers. -1.0 is full left
 * speak, 1.0 if full right speaker. Anywhere in between moves between the
 * two speakers. If the control is not supported this method has no effect
 *
 * @param balance The balance value
 */
private void setBalance(float balance) {
    this.balance = balance;

    if (outputLine == null) {
        return;
    }

    try {
        FloatControl control = (FloatControl) outputLine.getControl(FloatControl.Type.BALANCE);
        control.setValue(balance);
    } catch (IllegalArgumentException e) {
        // balance not supported
    }
}
 
Example 15
Source File: OggClip.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Attempt to set the global gain (volume ish) for the play back. If the
 * control is not supported this method has no effect. 1.0 will set maximum
 * gain, 0.0 minimum gain
 *
 * @param gain The gain value
 */
private void setGain(float gain) {
    if (gain != -1) {
        if ((gain < 0) || (gain > 1)) {
            throw new IllegalArgumentException("Volume must be between 0.0 and 1.0");
        }
    }

    this.gain = gain;

    if (outputLine == null) {
        return;
    }

    try {
        FloatControl control = (FloatControl) outputLine.getControl(FloatControl.Type.MASTER_GAIN);
        if (gain == -1) {
            control.setValue(0);
        } else {
            float max = control.getMaximum();
            float min = control.getMinimum(); // negative values all seem to be zero?
            float range = max - min;

            control.setValue(min + (range * gain));
        }
    } catch (IllegalArgumentException e) {
        // gain not supported
        e.printStackTrace();
    }
}
 
Example 16
Source File: Audio.java    From salty-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the volume of the clip, 1f is the normal volume, 0f is completely quiet and 2f is twice as loud.
 *
 * @param volume the new volume of this audio
 */
public void setVolume(float volume) {

    volume = GeneralUtil.clamp(volume, 0f, 2f);
    final FloatControl gainControl = (FloatControl) getClip().getControl(FloatControl.Type.MASTER_GAIN);
    gainControl.setValue(20f * (float) Math.log10(volume));
    this.volume = volume;
}
 
Example 17
Source File: MetronomePlugin.java    From plugins with GNU General Public License v3.0 5 votes vote down vote up
@Subscribe
private void onConfigChanged(ConfigChanged event)
{
	if (!event.getGroup().equals("metronome"))
	{
		return;
	}

	if (event.getKey().equals("volume"))
	{
		float gainValue = (((float) config.volume()) * 40f / 100f) - 35f;
		FloatControl gainControlTick = (FloatControl) tickClip.getControl(FloatControl.Type.MASTER_GAIN);
		gainControlTick.setValue(gainValue);

		FloatControl gainControlTock = (FloatControl) tockClip.getControl(FloatControl.Type.MASTER_GAIN);
		gainControlTock.setValue(gainValue);
	}
	if (event.getKey().equals("tickSoundFilePath"))
	{
		if (tickClip != null)
		{
			tickClip.close();
		}
		tickClip = GetAudioClip(config.tickPath());
	}
	if (event.getKey().equals("tockSoundFilePath"))
	{
		if (tockClip != null)
		{
			tockClip.close();
		}
		tockClip = GetAudioClip(config.tockPath());
	}
}
 
Example 18
Source File: AudioThread.java    From open-ig with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Set the master gain on the source data line.
 * @param gain the master gain in decibels, typically -80 to 0.
 */
public void setMasterGain(float gain) {
	FloatControl fc = (FloatControl)sdl.getControl(FloatControl.Type.MASTER_GAIN);
	fc.setValue(gain);
}
 
Example 19
Source File: Beeper.java    From zxpoly with GNU General Public License v3.0 4 votes vote down vote up
private void initMasterGain() {
  final FloatControl gainControl = this.gainControl.get();
  if (gainControl != null) {
    gainControl.setValue(-20.0f); // 50%
  }
}
 
Example 20
Source File: PlayerThread.java    From logbook with MIT License 2 votes vote down vote up
/**
 * 音量を調節する
 *
 * @param control
 * @param linearScalar
 */
private static void controlByLinearScalar(FloatControl control, double linearScalar) {
    control.setValue((float) Math.log10(linearScalar) * 20);
}