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

The following examples show how to use javax.sound.sampled.Mixer#isLineSupported() . 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: 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 2
Source File: DefaultMixers.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean testMixers(Mixer.Info[] infos,
                                  String providerClassName) {
    boolean allOk = true;

    for (int i = 0; i < infos.length; i++) {
        Mixer mixer = null;
        try {
            mixer = AudioSystem.getMixer(infos[i]);
        } catch (NullPointerException e) {
            out("Exception thrown; Test NOT failed.");
            e.printStackTrace();
        }
        for (int j = 0; j < lineClasses.length; j++) {
            if (mixer.isLineSupported(new Line.Info(lineClasses[j]))) {
                allOk &= testMixer(mixer, lineClasses[j],
                                   providerClassName);
            }
        }
    }
    return allOk;
}
 
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 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 4
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 5
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 6
Source File: AudioSystem.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
/** Checks if a Mixer is appropriate.
    A Mixer is considered appropriate if it support the given line type.
    If isMixingRequired is true and the line type is an output one
    (SourceDataLine, Clip), the mixer is appropriate if it supports
    at least 2 (concurrent) lines of the given type.

    @return true if the mixer is considered appropriate according to the
    rules given above, false otherwise.
 */
private static boolean isAppropriateMixer(Mixer mixer,
                                          Line.Info lineInfo,
                                          boolean isMixingRequired) {
    if (! mixer.isLineSupported(lineInfo)) {
        return false;
    }
    Class lineClass = lineInfo.getLineClass();
    if (isMixingRequired
        && (SourceDataLine.class.isAssignableFrom(lineClass) ||
            Clip.class.isAssignableFrom(lineClass))) {
        int maxLines = mixer.getMaxLines(lineInfo);
        return ((maxLines == NOT_SPECIFIED) || (maxLines > 1));
    }
    return true;
}
 
Example 7
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 8
Source File: ClickInPlay.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    println("This is an interactive test.");
    println("If you can hear clicks during playback in");
    println("any mixer, the test is failed.");
    println("");
    println("Make sure that you have speakers connected");
    println("and that the system mixer is not muted.");
    println("");
    println("Press a key to start the test.");
    key();
    Mixer.Info[] mixers=null;

        println("   ...using self-generated sine wave for playback");
        audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);
        for (int i=0; i<audioData.length; i++) {
            audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
        }
    info = new DataLine.Info(Clip.class, audioFormat);

    mixers = AudioSystem.getMixerInfo();
    int succMixers = 0;
    for (int i=0; i<mixers.length; i++) {
        try {
            Mixer mixer = AudioSystem.getMixer(mixers[i]);
            if (mixer.isLineSupported(info)) {
                succMixers++;
                println("  ...using mixer "+mixer.getMixerInfo());
                play(mixer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (succMixers == 0) {
            println("No mixers available! ");
            println("Cannot run test.");
    }
}
 
Example 9
Source File: BasicPlayer.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public List<String> getMixers() {
	final ArrayList<String> mixers = new ArrayList<>();
	final Mixer.Info[] mInfos = AudioSystem.getMixerInfo();
	if (mInfos != null) {
		for (final Info mInfo : mInfos) {
			final Line.Info lineInfo = new Line.Info(SourceDataLine.class);
			final Mixer mixer = AudioSystem.getMixer(mInfo);
			if (mixer.isLineSupported(lineInfo)) {
				mixers.add(mInfo.getName());
			}
		}
	}
	return mixers;
}
 
Example 10
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.");
}
 
Example 11
Source File: DirectSoundUnderrunSilence.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 {
    println("This is an interactive test for DirectAudio.");
    println("If it's impossible to play data after an underun, the test fails.");
    println("");
    println("Make sure that you have speakers connected");
    println("and that the system mixer is not muted.");
    println("Also stop all other programs playing sounds:");
    println("It has been seen that it alters the results.");
    println("");
    println("Press a key to start the test.");
    key();
    Mixer.Info[] mixers=null;

    println("   ...using self-generated sine wave for playback");
    audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);
    for (int i=0; i<audioData.length; i++) {
        audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
    }
    info = new DataLine.Info(SourceDataLine.class, audioFormat);

    mixers = AudioSystem.getMixerInfo();
    int succMixers = 0;
    for (int i=0; i<mixers.length; i++) {
        println(""+mixers[i]+":");
        if ((mixers[i].getName()+mixers[i].getDescription()+mixers[i].getVendor()).indexOf("Direct") < 0) {
            println("  ->not a DirectAudio Mixer!");
        } else {
            try {
                Mixer mixer = AudioSystem.getMixer(mixers[i]);
                if (!mixer.isLineSupported(info)) {
                    println("  ->doesn't support SourceDataLine!");
                } else {
                    succMixers++;
                    println("  -> is getting tested.");
                    play(mixer);
                }
            } catch (Exception e) {
                println("  -> Exception occured: "+e);
                e.printStackTrace();
            }
        }
    }
    if (succMixers == 0) {
        println("No DirectAudio mixers available! ");
        println("Cannot run test.");
    }
}
 
Example 12
Source File: DirectSoundRepeatingBuffer.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 {
    println("This is an interactive test for DirectAudio.");
    println("If the tone repeats, the test is failed.");
    println("");
    println("Make sure that you have speakers connected");
    println("and that the system mixer is not muted.");
    println("");
    println("Press a key to start the test.");
    key();
    Mixer.Info[] mixers=null;

        println("   ...using self-generated sine wave for playback");
        audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);
        for (int i=0; i<audioData.length; i++) {
            audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
        }
    info = new DataLine.Info(SourceDataLine.class, audioFormat);

    mixers = AudioSystem.getMixerInfo();
    int succMixers = 0;
    for (int i=0; i<mixers.length; i++) {
        println(""+mixers[i]+":");
        if ((mixers[i].getName()+mixers[i].getDescription()+mixers[i].getVendor()).indexOf("Direct") < 0) {
            println("  ->not a DirectAudio Mixer!");
        } else {
        try {
            Mixer mixer = AudioSystem.getMixer(mixers[i]);
            if (!mixer.isLineSupported(info)) {
                    println("  ->doesn't support SourceDataLine!");
            } else {
                succMixers++;
                println("  -> is getting tested.");
                play(mixer);
            }
        } catch (Exception e) {
            println("  -> Exception occured: "+e);
            e.printStackTrace();
        }
      }
    }
    if (succMixers == 0) {
            println("No DirectAudio mixers available! ");
            println("Cannot run test.");
    }
}