Java Code Examples for javax.sound.sampled.Mixer#getLine()

The following examples show how to use javax.sound.sampled.Mixer#getLine() . 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: LineDefFormat.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void doMixerClip(Mixer mixer, AudioFormat format) {
    if (mixer==null) return;
    try {
        System.out.println("Clip from mixer "+mixer+":");
        System.out.println("   "+mixer.getMixerInfo());
            DataLine.Info info = new DataLine.Info(
                                      Clip.class,
                                      format);

        if (mixer.isLineSupported(info)) {
            Clip clip = (Clip) mixer.getLine(info);
            doLine1(clip, format);
        } else {
            System.out.println("  - Line not supported");
        }
    } catch (Throwable t) {
        System.out.println("  - Caught exception. Not failed.");
        System.out.println("  - "+t.toString());
    }
}
 
Example 2
Source File: LineDefFormat.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void doMixerSDL(Mixer mixer, AudioFormat format) {
    if (mixer==null) return;
    try {
        System.out.println("SDL from mixer "+mixer+":");
            DataLine.Info info = new DataLine.Info(
                                      SourceDataLine.class,
                                      format);

        if (mixer.isLineSupported(info)) {
            SourceDataLine sdl = (SourceDataLine) mixer.getLine(info);
            doLine1(sdl, format);
            doLine2(sdl, format);
        } else {
            System.out.println("  - Line not supported");
        }
    } catch (Throwable t) {
        System.out.println("  - Caught exception. Not failed.");
        System.out.println("  - "+t.toString());
    }
}
 
Example 3
Source File: LineDefFormat.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void doMixerTDL(Mixer mixer, AudioFormat format) {
    if (mixer==null) return;
    try {
        System.out.println("TDL from mixer "+mixer+":");
            DataLine.Info info = new DataLine.Info(
                                      TargetDataLine.class,
                                      format);
        if (mixer.isLineSupported(info)) {
            TargetDataLine tdl = (TargetDataLine) mixer.getLine(info);
            doLine1(tdl, format);
            doLine2(tdl, format);
        } else {
            System.out.println("  - Line not supported");
        }
    } catch (Throwable t) {
        System.out.println("  - Caught exception. Not failed.");
        System.out.println("  - "+t.toString());
    }
}
 
Example 4
Source File: ClickInPlay.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void play(Mixer mixer) {
    int res = 0;
    try {
        println("Getting clip from mixer...");
        source = (Clip) mixer.getLine(info);
        println("Opening clip...");
        source.open(audioFormat, audioData, 0, audioData.length);
        println("Starting clip...");
        source.loop(Clip.LOOP_CONTINUOUSLY);
        println("Now open your ears:");
        println("- if you hear a sine wave playing,");
        println("  listen carefully if you can hear clicks.");
        println("  If no, the bug is fixed.");
        println("- if you don't hear anything, it's possible");
        println("  that this mixer is not connected to an ");
        println("  amplifier, or that its volume is set to 0");
        key();
    } catch (IllegalArgumentException iae) {
        println("IllegalArgumentException: "+iae.getMessage());
        println("Sound device cannot handle this audio format.");
        println("ERROR: Test environment not correctly set up.");
        if (source!=null) {
            source.close();
            source = null;
        }
        return;
    } catch (LineUnavailableException lue) {
        println("LineUnavailableException: "+lue.getMessage());
        println("This is normal for some mixers.");
    } catch (Exception e) {
        println("Unexpected Exception: "+e.toString());
    }
    if (source != null) {
        println("Stopping...");
        source.stop();
        println("Closing...");
        source.close();
        println("Closed.");
        source = null;
    }
}
 
Example 5
Source File: ChangingBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean doMixerClip(Mixer mixer, AudioFormat format) {
    if (mixer==null) return false;
    try {
        System.out.println("Trying mixer "+mixer+":");
            DataLine.Info info = new DataLine.Info(
                                      Clip.class,
                                      format,
                                      (int) samplerate);

            Clip clip = (Clip) mixer.getLine(info);
        System.out.println("  - got clip: "+clip);
        System.out.println("  - open with format "+format);
        clip.open(format, buffer, 0, buffer.length);
        System.out.println("  - playing...");
        clip.start();
        System.out.println("  - waiting while it's active...");
        while (clip.isActive())
                Thread.sleep(100);
        System.out.println("  - waiting 100millis");
        Thread.sleep(100);
        System.out.println("  - drain1");
        clip.drain();
        System.out.println("  - drain2");
        clip.drain();
        System.out.println("  - stop");
        clip.stop();
        System.out.println("  - close");
        clip.close();
        System.out.println("  - closed");
    } catch (Throwable t) {
        System.out.println("  - Caught exception. Not failed.");
        System.out.println("  - "+t.toString());
        return false;
    }
    return true;
}
 
Example 6
Source File: JavaSoundAudioSink.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private void runVolumeCommand(Closure closure) {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    for (Mixer.Info info : infos) {
        Mixer mixer = AudioSystem.getMixer(info);
        if (mixer.isLineSupported(Port.Info.SPEAKER)) {
            Port port;
            try {
                port = (Port) mixer.getLine(Port.Info.SPEAKER);
                port.open();
                if (port.isControlSupported(FloatControl.Type.VOLUME)) {
                    FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
                    closure.execute(volume);
                }
                port.close();
            } catch (LineUnavailableException e) {
                logger.error("Cannot access master volume control", e);
            }
        }
    }
}
 
Example 7
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 8
Source File: UnexpectedIAE.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String argv[]) throws Exception {
    boolean success = true;

    Mixer.Info [] infos = AudioSystem.getMixerInfo();

    for (int i=0; i<infos.length; i++) {
        Mixer mixer = AudioSystem.getMixer(infos[i]);
        System.out.println("Mixer is: " + mixer);
        Line.Info [] target_line_infos = mixer.getTargetLineInfo();
        for (int j = 0; j < target_line_infos.length; j++) {
            try {
                System.out.println("Trying to get:" + target_line_infos[j]);
                mixer.getLine(target_line_infos[j]);
            } catch (IllegalArgumentException iae) {
                System.out.println("Unexpected IllegalArgumentException raised:");
                iae.printStackTrace();
                success = false;
            } catch (LineUnavailableException lue) {
                System.out.println("Unexpected LineUnavailableException raised:");
                lue.printStackTrace();
                success = false;
            }
        }
    }
    if (success) {
        System.out.println("Test passed");
    } else {
        throw new Exception("Test FAILED");
    }
}
 
Example 9
Source File: JavaSoundAudioDevice.java    From jsyn with Apache License 2.0 5 votes vote down vote up
Line getDataLine(DataLine.Info info) throws LineUnavailableException {
    Line dataLine;
    if (deviceID >= 0) {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        Mixer mixer = AudioSystem.getMixer(mixers[deviceID]);
        dataLine = mixer.getLine(info);
    } else {
        dataLine = AudioSystem.getLine(info);
    }
    return dataLine;
}
 
Example 10
Source File: JavaSoundAudioDevice.java    From jsyn with Apache License 2.0 5 votes vote down vote up
Line getDataLine(DataLine.Info info) throws LineUnavailableException {
    Line dataLine;
    if (deviceID >= 0) {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        Mixer mixer = AudioSystem.getMixer(mixers[deviceID]);
        dataLine = mixer.getLine(info);
    } else {
        dataLine = AudioSystem.getLine(info);
    }
    return dataLine;
}
 
Example 11
Source File: BackgroundMusicUtils.java    From WorldGrower with GNU General Public License v3.0 5 votes vote down vote up
public static Clip readMusicFile(InputStream audioFilePath, SoundOutput soundOutput) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
	AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFilePath);
	AudioFormat format = audioStream.getFormat();
	DataLine.Info info = new DataLine.Info(Clip.class, format);
	
	Mixer mixer = soundOutput.getMixer();
	Clip audioClip = (Clip) mixer.getLine(info);
	audioClip.open(audioStream);
	return audioClip;
}
 
Example 12
Source File: SinkAudio.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public void setDevice(int position) throws LineUnavailableException, IllegalArgumentException {
	if (position == 0 || position == -1) {
		initializeOutput();
	} else {
		Mixer appMixer = mixerList[position];

		Info sdlLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
		
		sourceDataLine = (SourceDataLine) appMixer.getLine(sdlLineInfo);
		sourceDataLine.open(audioFormat);
		sourceDataLine.start();
	}
		

}
 
Example 13
Source File: ChangingBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean doMixerSDL(Mixer mixer, AudioFormat format) {
    if (mixer==null) return false;
    try {
        System.out.println("Trying mixer "+mixer+":");
            DataLine.Info info = new DataLine.Info(
                                      SourceDataLine.class,
                                      format,
                                      (int) samplerate);

            SourceDataLine sdl = (SourceDataLine) mixer.getLine(info);
        System.out.println("  - got sdl: "+sdl);
        System.out.println("  - open with format "+format);
        sdl.open(format);
        System.out.println("  - start...");
        sdl.start();
        System.out.println("  - write...");
        sdl.write(buffer, 0, buffer.length);
        Thread.sleep(200);
        System.out.println("  - drain...");
        sdl.drain();
        System.out.println("  - stop...");
        sdl.stop();
        System.out.println("  - close...");
        sdl.close();
        System.out.println("  - closed");
    } catch (Throwable t) {
        System.out.println("  - Caught exception. Not failed.");
        System.out.println("  - "+t.toString());
        return false;
    }
    return true;
}
 
Example 14
Source File: ClipDrain.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void doMixerClip(Mixer mixer) throws Exception {
    boolean waitedEnough=false;
    try {
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        Clip clip = (Clip) mixer.getLine(info);
        clip.open(format, soundData, 0, soundData.length);

        // sanity
        if (clip.getMicrosecondLength()/1000 < 9900) {
            throw new Exception("clip's microsecond length should be at least 9900000, but it is "+clip.getMicrosecondLength());
        }
        long start = System.currentTimeMillis();

        System.out.println(" ---------- start --------");
        clip.start();
        // give time to actually start it. ALSA implementation needs that...
        Thread.sleep(300);
        System.out.println("drain ... ");
        clip.drain();
        long elapsedTime = System.currentTimeMillis() - start;
        System.out.println("close ... ");
        clip.close();
        System.out.println("... done");
        System.out.println("Playback duration: "+elapsedTime+" milliseconds.");
        waitedEnough = elapsedTime >= ((clip.getMicrosecondLength() / 1000) - TOLERANCE_MS);
    } catch (Throwable t) {
            System.out.println("  - Caught exception. Not failed.");
            System.out.println("  - "+t.toString());
            return;
    }
    if (!waitedEnough) {
        throw new Exception("Drain did not wait long enough to play entire clip.");
    }
    successfulTests++;
}
 
Example 15
Source File: DirectSoundRepeatingBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void play(Mixer mixer) {
    int res = 0;
    try {
        println("Getting SDL from mixer...");
        source = (SourceDataLine) mixer.getLine(info);
        println("Opening SDL...");
        source.open(audioFormat);
        println("Writing data to SDL...");
        source.write(audioData, 0, audioData.length);
        println("Starting SDL...");
        source.start();
        println("Now open your ears:");
        println("- you should have heard a short tone,");
        println("  followed by silence.");
        println("- if after a while you hear repeated tones,");
        println("  the bug is NOT fixed.");
        println("- if the program remains silent after the ");
        println("  initial tone, the bug is fixed.");
        key();
    } catch (IllegalArgumentException iae) {
        println("IllegalArgumentException: "+iae.getMessage());
        println("Sound device cannot handle this audio format.");
        println("ERROR: Test environment not correctly set up.");
        if (source!=null) {
            source.close();
            source = null;
        }
        return;
    } catch (LineUnavailableException lue) {
        println("LineUnavailableException: "+lue.getMessage());
        println("This is normal for some mixers.");
    } catch (Exception e) {
        println("Unexpected Exception: "+e.toString());
    }
    if (source != null) {
        println("Stopping...");
        source.stop();
        println("Closing...");
        source.close();
        println("Closed.");
        source = null;
    }
}
 
Example 16
Source File: SourceSoundCardAudio.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Pass the combo box position and select this device
 * @param position
 * @throws LineUnavailableException
 * @throws IllegalArgumentException
 */
private void setDevice(int position) throws LineUnavailableException, IllegalArgumentException {
	//Mixer.Info[] mixers = AudioSystem.getMixerInfo();
	//AudioFormat audioFmt = getAudioFormat();

	Mixer appMixer = mixerList[position];

	Info sdlLineInfo = new DataLine.Info(TargetDataLine.class, makeAudioFormat(sampleRate));
	
	stop();

	targetDataLine = (TargetDataLine) appMixer.getLine(sdlLineInfo);

	init();

}
 
Example 17
Source File: DirectSoundUnderrunSilence.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void play(Mixer mixer) {
    int res = 0;
    try {
        println("Getting SDL from mixer...");
        source = (SourceDataLine) mixer.getLine(info);
        println("Opening SDL...");
        source.open(audioFormat);
        println("Writing data to SDL...");
        source.write(audioData, 0, audioData.length);
        println("Starting SDL...");
        source.start();
        println("Now open your ears:");
        println("You should have heard a short tone,");
        println("followed by silence (no repeating tones).");
        key();
        source.write(audioData, 0, audioData.length);
        println("Now you should have heard another short tone.");
        println("If you did not hear a second tone, or more than 2 tones,");
        println("the test is FAILED.");
        println("otherwise, if you heard a total of 2 tones, the bug is fixed.");
        key();
    } catch (IllegalArgumentException iae) {
        println("IllegalArgumentException: "+iae.getMessage());
        println("Sound device cannot handle this audio format.");
        println("ERROR: Test environment not correctly set up.");
        if (source!=null) {
            source.close();
            source = null;
        }
        return;
    } catch (LineUnavailableException lue) {
        println("LineUnavailableException: "+lue.getMessage());
        println("This is normal for some mixers.");
    } catch (Exception e) {
        println("Unexpected Exception: "+e.toString());
    }
    if (source != null) {
        println("Stopping...");
        source.stop();
        println("Closing...");
        source.close();
        println("Closed.");
        source = null;
    }
}
 
Example 18
Source File: ClipOpenException.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    int succMixers = 0;
    println("Using formats: ");
    for (int i = 0 ; i<formats.length; i++) {
            println(""+(i+1)+". "+formats[i]);
    }
    for (int i=0; i<mixers.length; i++) {
        boolean succ = false;
        try {
            Mixer mixer = AudioSystem.getMixer(mixers[i]);
            println("Mixer "+mixer.getMixerInfo()+":");
            if (mixer.isLineSupported(clipInfo)) {
                println("Getting clip from mixer...");
                Clip clip = (Clip) mixer.getLine(clipInfo);
                succ = true;
                test(clip);
            }
            if (mixer.isLineSupported(sdlInfo)) {
                println("Getting source data line from mixer...");
                SourceDataLine sdl = (SourceDataLine) mixer.getLine(sdlInfo);
                succ = true;
                test(sdl);
            }
            if (mixer.isLineSupported(tdlInfo)) {
                println("Getting target data line from mixer...");
                TargetDataLine tdl = (TargetDataLine) mixer.getLine(tdlInfo);
                succ = true;
                test(tdl);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (succ) {
            succMixers++;
        }
    }
    if (succMixers == 0) {
        println("No mixers available! ");
        println("Cannot run test. NOT FAILED");
    }
    else if (failed) {
        throw new Exception("Test FAILED");
    }
    println("Test passed.");
}