本文整理汇总了Java中javax.sound.sampled.FloatControl类的典型用法代码示例。如果您正苦于以下问题:Java FloatControl类的具体用法?Java FloatControl怎么用?Java FloatControl使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FloatControl类属于javax.sound.sampled包,在下文中一共展示了FloatControl类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
private void init(AudioFormat baseFormat) throws LineUnavailableException {
decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(decodedFormat);
volumeControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
minGainDB = volumeControl.getMinimum();
ampGainDB = ((10.0f / 20.0f) * volumeControl.getMaximum()) - volumeControl.getMinimum();
cste = Math.log(10.0) / 20;
//for stations we want to set the initial volume to 0
volumeControl.setValue((float) minGainDB);
line.start();
//App.logger.d("prefix + WebPlayer SourceDataLine started!", null);
}
示例2: setGain
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
/**
* Sets Gain value. Line should be opened before calling this method. Linear scale 0.0 <--> 1.0 Threshold Coef. : 1/2 to avoid saturation.
*
* @param fGain
*/
public void setGain(float fGain) {
// if (line != null)
// System.out.println(((FloatControl)
// line.getControl(FloatControl.Type.MASTER_GAIN)).getValue())
// Set the value
gain = fGain;
// Better type
if (line != null && line.isControlSupported(FloatControl.Type.MASTER_GAIN))
( (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN) ).setValue((float) ( 20 * Math.log10(fGain <= 0.0 ? 0.0000 : fGain) ));
// OR (Math.log(fGain == 0.0 ? 0.0000 : fGain) / Math.log(10.0))
// if (line != null)
// System.out.println(((FloatControl)
// line.getControl(FloatControl.Type.MASTER_GAIN)).getValue())
}
示例3: loadClip
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
private void loadClip() {
try {
clip = AudioSystem.getClip();
InputStream in = getClass().getClassLoader().getResourceAsStream(filePath);
BufferedInputStream bufferedIn = new BufferedInputStream(in);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(bufferedIn);
clip.open(audioIn);
volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
示例4: start
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
/**
* 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();
}
示例5: mute
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
/**
* Mute the Clip (because destroying it, won't stop it)
*/
public void mute() {
try {
Clip c = getClip();
if (c == null) {
return;
}
float val = (float) (Math.log(Float.MIN_VALUE) / Math.log(10.0) * 20.0);
if (val < -80.0f) {
val = -80.0f;
}
((FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN)).setValue(val);
} catch (IllegalArgumentException ignored) {
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
示例6: play
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
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);
}
}
}
示例7: PreloadedPlayback
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
/**
* Creates a new {@code PreloadedPlayback}. PreloadedPlayback objects will
* always be created by their associated PreloadedAudio object.
* <p>
* IMPLEMENTATION NOTE: Originally, the fetching of a new {@code Line} was
* done in the {@code run} method, however testing revealed that latency is
* decreased if a {@code Line} is acquired ahead of time, here in the
* constructor.
*
* @param audio
* The {@code Audio} that created this {@code PreloadedPlayback}.
* @param audioFormat
* Specifies the particular arrangement of audio data.
* @param audioBytes
* Holds the audio data from which a {@code Clip} will be
* created.
* @param instanceID
* The {@code instanceID} of this {@code PreloadedPlayback}.
*/
protected PreloadedPlayback(Audio audio, AudioFormat audioFormat,
byte[] audioBytes, long instanceID) {
super(audio, instanceID);
DataLine.Info info = new DataLine.Info(Clip.class, audioFormat);
try {
clip = (Clip) AudioSystem.getLine(info);
clip.open(audioFormat, audioBytes, 0, audioBytes.length);
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
volCtrl = (FloatControl) clip
.getControl(FloatControl.Type.MASTER_GAIN);
} else {
logger.warning("Master-Gain control is not supported."
+ " Volume will be fixed at the default level.");
}
} catch (LineUnavailableException ex) {
ex.printStackTrace();
}
clip.addLineListener(this);
}
示例8: StreamingPlayback
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
/**
* Creates a new {@code StreamingPlayback}. StreamingPlayback objects will
* always be created by their associated StreamingAudio object.
*
* @param audio
* The {@code Audio} that created this {@code StreamingPlayback}.
* @param audioInStream
* The {@code AudioInputStream} used by this
* {@code StreamingPlayback}.
* @param instanceID
* The {@code instanceID} of this {@code StreamingPlayback}.
*/
protected StreamingPlayback(Audio audio, AudioInputStream audioInStream,
long instanceID) {
super(audio, instanceID);
this.audioInStream = audioInStream;
AudioFormat audioFormat = audioInStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
try {
line = (SourceDataLine) AudioSystem.getLine(info);
if (line != null) {
line.open(audioFormat);
}
if (line.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
volCtrl = (FloatControl) line
.getControl(FloatControl.Type.MASTER_GAIN);
} else {
logger.warning("Master-Gain control is not supported."
+ " Volume will be fixed at the default level.");
}
} catch (LineUnavailableException ex) {
ex.printStackTrace();
}
}
示例9: playMainMusic
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
public void playMainMusic() {
try {
clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class));
clip.open(AudioSystem.getAudioInputStream(new File("sounds/main.wav")));
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-20.0f); // Muziek moet niet boven soundeffects uitkomen, dus 20dB zachter
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
catch (Exception exc) {
exc.printStackTrace(System.out);
}
}
示例10: SoundClip
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
public SoundClip(String path) {
try {
InputStream audioSrc = getClass().getResourceAsStream(path);
InputStream bufferedIn = new BufferedInputStream(audioSrc);
AudioInputStream ais = AudioSystem.getAudioInputStream(bufferedIn);
AudioFormat baseFormat = ais.getFormat();
AudioFormat decodeFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
false);
AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais);
clip = AudioSystem.getClip();
clip.open(dais);
gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
} catch (Exception e) {
e.printStackTrace();
}
}
示例11: setPlayer
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
public void setPlayer(Wavefile af) throws Exception{
clip = af.getClip();
clip.open();
fullLength = clip.getFrameLength();
endPoint = fullLength;
gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
playing = false;
clip.addLineListener(new LineListener(){
@Override
public void update(LineEvent arg0) {
while(playing){
int tempframe = clip.getFramePosition();
MakamBoxAnalysis.positionSlide.setValue(tempframe);
if (stopbutton!=null&&tempframe == fullLength){
stopbutton.doClick();
} else if(stopbutton!=null && tempframe>=endPoint){
playAgain();
} else if (tempframe == fullLength){
stop();
}
}
}
});
}
示例12: invokeAction
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
@Override
public void invokeAction(final PiAction action) throws RaspberryPiAppException {
final String soundFile = baseSoundDirectory + action.getValue();
LOGGER.debug("Playing sound file: '{}'", soundFile);
try {
final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
new File(soundFile));
final Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
// play at maximum volume
final FloatControl gainControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(gainControl.getMaximum());
clip.start();
} catch (Exception e) {
throw new RaspberryPiAppException(e.getMessage(), e);
}
}
示例13: VNSound
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
/**
* Constructor that starts off the VNSound tool with a sound at a certain volume. Will not loop.
*
* @param file - the file path and name of the audio to play
* @param volume - volume to play the file at
*/
public VNSound(File file, float volume) {
try {
audio = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(audio);
volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
setVolume(volume);
clip.start();
}
catch(UnsupportedAudioFileException uae) {
System.out.println(uae);
}
catch(IOException ioe) {
System.out.println(ioe);
}
catch(LineUnavailableException lua) {
System.out.println(lua);
}
}
示例14: open
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
/**
* Opens a new sound and replaces the old one. If object is set to loop the sound (isLoop = true) then it will loop
*
* @param file - the file path and name of the audio to play
*/
public void open(File file) {
try {
clip.stop();
clip.close();
audio = AudioSystem.getAudioInputStream(file);
clip.open(audio);
volumeControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
clip.start();
if(isLoop)
clip.loop(Clip.LOOP_CONTINUOUSLY);
else
clip.loop(0);
}
catch(UnsupportedAudioFileException uae) {
System.out.println(uae);
}
catch(IOException ioe) {
System.out.println(ioe);
System.out.println(file);
}
catch(LineUnavailableException lua) {
System.out.println(lua);
}
}
示例15: setVolume
import javax.sound.sampled.FloatControl; //导入依赖的package包/类
/**
* 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);
}