下面列出了javax.sound.sampled.FloatControl#setValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
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);
}
}
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) {
}
}
/**
* 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();
}
/**
* 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();
}
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);
}
}
}
@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());
}
}
/**
* 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;
}
/**
* 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();
}
}
/**
* 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
}
}
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);
}
}
}
/**
* 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) {}
}
}
/**
* 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));
}
}
}
@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)));
}
}
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);
}
}
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();
}
}
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;
}
/**
* 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);
}
private void initMasterGain() {
final FloatControl gainControl = this.gainControl.get();
if (gainControl != null) {
gainControl.setValue(-20.0f); // 50%
}
}
/**
* 音量を調節する
*
* @param control
* @param linearScalar
*/
private static void controlByLinearScalar(FloatControl control, double linearScalar) {
control.setValue((float) Math.log10(linearScalar) * 20);
}