下面列出了javax.sound.sampled.FloatControl#getMinimum ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
/**
* Sets the volume.
*
* @param vol the volume
*/
private void setVolume(final int vol) {
int volume = vol;
if (volume < 0) {
volume = MAX_VOLUME;
}
float gainDb = 0.0f;
final FloatControl gain = (FloatControl)
clip.getControl(FloatControl.Type.MASTER_GAIN);
if (volume == 0) {
gainDb = gain.getMinimum();
} else if (volume < MAX_VOLUME) {
// The volume algorithm is subtractive: The implementation assumes that
// the sound is already at maximum volume, so we avoid distortion by
// making the amplitude values
// The scaling factor for the volume would be 20 normally, but
// it seems that 13 is better
gainDb = (float) (Math.log10(MAX_VOLUME - volume) * 13.0);
}
gain.setValue(-gainDb);
}
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;
}
/**
* 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();
}
}
public FloatControlBoundedRangeModel(FloatControl control) {
this.control = control;
float range = 100;
float steps = range / 100;
factor = range / steps;
int min = (int) (control.getMinimum() * factor);
int max = (int) (control.getMaximum() * factor);
int value = (int) (control.getValue() * factor);
setRangeProperties(value, 0, min, max, false);
}