javax.sound.sampled.Clip#getControl ( )源码实例Demo

下面列出了javax.sound.sampled.Clip#getControl ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: plugins   文件: MetronomePlugin.java
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;
	}
}
 
源代码2 项目: openjdk-jdk9   文件: ClipOpenBug.java
public static void main(String args[]) throws Exception {
    boolean res = true;
    try {
        AudioInputStream ais = new AudioInputStream(
                new ByteArrayInputStream(new byte[2000]),
                new AudioFormat(8000.0f, 8, 1, false, false), 2000); //
        AudioFormat format = ais.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format,
                                               ((int) ais.getFrameLength()
                                                        * format
                                                       .getFrameSize()));
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open();
        FloatControl rateControl = (FloatControl) clip.getControl(
                FloatControl.Type.SAMPLE_RATE);
        int c = 0;
        while (c++ < 10) {
            clip.stop();
            clip.setFramePosition(0);
            clip.start();
            for (float frq = 22000; frq < 44100; frq = frq + 100) {
                try {
                    Thread.currentThread().sleep(20);
                } catch (Exception e) {
                    break;
                }
                rateControl.setValue(frq);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        res = ex.getMessage().indexOf(
                "This method should not have been invoked!") < 0;
    }
    if (res) {
        System.out.println("Test passed");
    } else {
        System.out.println("Test failed");
        throw new Exception("Test failed");
    }
}
 
源代码3 项目: opsu-dance   文件: MultiClip.java
/**
 * 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();
}
 
源代码4 项目: MercuryTrade   文件: SoundNotifier.java
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);
        }
    }
}
 
源代码5 项目: opsu   文件: MultiClip.java
/**
 * 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();
}
 
源代码6 项目: MyBox   文件: SoundTools.java
public static synchronized Clip playback(AudioInputStream in, float addVolume) {
    try {
        AudioFormat baseFormat = in.getFormat();
        AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(),
                16,
                baseFormat.getChannels(),
                baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(),
                false);
        AudioInputStream ain = AudioSystem.getAudioInputStream(decodedFormat, in);
        DataLine.Info info = new DataLine.Info(Clip.class, decodedFormat);

        // https://stackoverflow.com/questions/25564980/java-use-a-clip-and-a-try-with-resources-block-which-results-with-no-sound
        final Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(ain);
        // Input stream must be closed, or else some thread is still running when application is exited.
        in.close();
        ain.close();
        FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
        gainControl.setValue(addVolume); // Add volume by decibels.
        return clip;

    } catch (Exception e) {
        logger.debug(e.toString());
        return null;
    }
}
 
源代码7 项目: nullpomino   文件: WaveEngine.java
/**
 * 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) {}
	}
}
 
源代码8 项目: mochadoom   文件: ClipSFXModule.java
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);
	}
}