Example usage for javax.sound.sampled Mixer getLine

List of usage examples for javax.sound.sampled Mixer getLine

Introduction

In this page you can find the example usage for javax.sound.sampled Mixer getLine.

Prototype

Line getLine(Line.Info info) throws LineUnavailableException;

Source Link

Document

Obtains a line that is available for use and that matches the description in the specified Line.Info object.

Usage

From source file:com.lfv.lanzius.application.SoundClip.java

public SoundClip(Mixer outputMixer, String filenameAlternativeA, String filenameAlternativeB, int periodMillis,
        float volumeAdjustment) {
    log = LogFactory.getLog(getClass());

    if (soundTimer == null)
        soundTimer = new Timer("Ssoundclip", true);

    boolean altA = true;
    this.periodMillis = periodMillis;
    this.volumeAdjustment = volumeAdjustment;

    // Try to open the first alternative clip
    try {/*from ww  w.j  av  a 2  s.co m*/
        stream = AudioSystem.getAudioInputStream(new File(filenameAlternativeA));
        DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, stream.getFormat());
        clip = (Clip) outputMixer.getLine(dataLineInfo);
        clip.open(stream);
    } catch (Exception ex) {
        // The first alternative clip could not be opened, try with second alternative
        try {
            if (stream != null)
                stream.close();
            if (filenameAlternativeB == null)
                throw ex;
            stream = AudioSystem.getAudioInputStream(new File(filenameAlternativeB));
            DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, stream.getFormat());
            clip = (Clip) outputMixer.getLine(dataLineInfo);
            clip.open(stream);
            altA = false;
        } catch (Exception ex2) {
            log.error("Unable to get stream for file " + filenameAlternativeA);
            log.error("Unable to get stream for file " + filenameAlternativeB);
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException ex3) {
                    log.error("Error closing stream ", ex3);
                }
            }
            stream = null;
            return;
        }
    }

    int clipLength = (int) (clip.getMicrosecondLength() / 1000L);
    log.debug("Loading sound clip " + (altA ? filenameAlternativeA : filenameAlternativeB) + " (" + clipLength
            + "ms)");
    // Check length
    if (periodMillis < clipLength)
        throw new IllegalArgumentException("The periodMillis value must be larger than length of the clip");
}

From source file:BasicPlayer.java

/**
 * Inits a DateLine.<br>/*from   w  ww. j a  va 2s .c o m*/
 *
 * We check if the line supports Gain and Pan controls.
 *
 * From the AudioInputStream, i.e. from the sound file, we
 * fetch information about the format of the audio data. These
 * information include the sampling frequency, the number of
 * channels and the size of the samples. There information
 * are needed to ask JavaSound for a suitable output line
 * for this audio file.
 * Furthermore, we have to give JavaSound a hint about how
 * big the internal buffer for the line should be. Here,
 * we say AudioSystem.NOT_SPECIFIED, signaling that we don't
 * care about the exact size. JavaSound will use some default
 * value for the buffer size.
 */
protected void createLine() throws LineUnavailableException {
    log.info("Create Line");
    if (m_line == null) {
        AudioFormat sourceFormat = m_audioInputStream.getFormat();
        log.info("Create Line : Source format : " + sourceFormat.toString());
        int nSampleSizeInBits = sourceFormat.getSampleSizeInBits();
        if (nSampleSizeInBits <= 0)
            nSampleSizeInBits = 16;
        if ((sourceFormat.getEncoding() == AudioFormat.Encoding.ULAW)
                || (sourceFormat.getEncoding() == AudioFormat.Encoding.ALAW))
            nSampleSizeInBits = 16;
        if (nSampleSizeInBits != 8)
            nSampleSizeInBits = 16;
        AudioFormat targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                sourceFormat.getSampleRate(), nSampleSizeInBits, sourceFormat.getChannels(),
                sourceFormat.getChannels() * (nSampleSizeInBits / 8), sourceFormat.getSampleRate(), false);
        log.info("Create Line : Target format: " + targetFormat);
        // Keep a reference on encoded stream to progress notification.
        m_encodedaudioInputStream = m_audioInputStream;
        try {
            // Get total length in bytes of the encoded stream.
            encodedLength = m_encodedaudioInputStream.available();
        } catch (IOException e) {
            log.error("Cannot get m_encodedaudioInputStream.available()", e);
        }
        // Create decoded stream.
        m_audioInputStream = AudioSystem.getAudioInputStream(targetFormat, m_audioInputStream);
        AudioFormat audioFormat = m_audioInputStream.getFormat();
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);
        Mixer mixer = getMixer(m_mixerName);
        if (mixer != null) {
            log.info("Mixer : " + mixer.getMixerInfo().toString());
            m_line = (SourceDataLine) mixer.getLine(info);
        } else {
            m_line = (SourceDataLine) AudioSystem.getLine(info);
            m_mixerName = null;
        }
        log.info("Line : " + m_line.toString());
        log.debug("Line Info : " + m_line.getLineInfo().toString());
        log.debug("Line AudioFormat: " + m_line.getFormat().toString());
    }
}

From source file:org.eclipse.smarthome.io.javasound.internal.JavaSoundAudioSink.java

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;/*w w w.  ja  va2  s .  co m*/
            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);
            }
        }
    }
}

From source file:org.openhab.io.javasound.internal.JavaSoundAudioSink.java

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;/* w w  w. j  a v  a  2  s.c  om*/
            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);
            }
        }
    }
}

From source file:org.openhab.io.multimedia.actions.Audio.java

private static 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;/*from   w w  w. ja v a  2 s.co m*/
            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);
            }
        }
    }
}