javax.sound.sampled.LineUnavailableException Java Examples

The following examples show how to use javax.sound.sampled.LineUnavailableException. 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: PortMixer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void open() throws LineUnavailableException {
    synchronized (mixer) {
        // if the line is not currently open, try to open it with this format and buffer size
        if (!isOpen()) {
            if (Printer.trace) Printer.trace("> PortMixerPort: open");
            // reserve mixer resources for this line
            mixer.open(this);
            try {
                // open the line.  may throw LineUnavailableException.
                implOpen();

                // if we succeeded, set the open state to true and send events
                setOpen(true);
            } catch (LineUnavailableException e) {
                // release mixer resources for this line and then throw the exception
                mixer.close(this);
                throw e;
            }
            if (Printer.trace) Printer.trace("< PortMixerPort: open succeeded");
        }
    }
}
 
Example #2
Source File: PortMixer.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
void implOpen() throws LineUnavailableException {
    long newID = ((PortMixer) mixer).getID();
    if ((id == 0) || (newID != id) || (controls.length == 0)) {
        id = newID;
        Vector<Control> vector = new Vector<>();
        synchronized (vector) {
            nGetControls(id, portIndex, vector);
            controls = new Control[vector.size()];
            for (int i = 0; i < controls.length; i++) {
                controls[i] = vector.elementAt(i);
            }
        }
    } else {
        enableControls(controls, true);
    }
}
 
Example #3
Source File: DataLine_ArrayIndexOutOfBounds.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    log("" + infos.length + " mixers detected");
    for (int i=0; i<infos.length; i++) {
        Mixer mixer = AudioSystem.getMixer(infos[i]);
        log("Mixer " + (i+1) + ": " + infos[i]);
        try {
            mixer.open();
            for (Scenario scenario: scenarios) {
                testSDL(mixer, scenario);
                testTDL(mixer, scenario);
            }
            mixer.close();
        } catch (LineUnavailableException ex) {
            log("LineUnavailableException: " + ex);
        }
    }
    if (failed == 0) {
        log("PASSED (" + total + " tests)");
    } else {
        log("FAILED (" + failed + " of " + total + " tests)");
        throw new Exception("Test FAILED");
    }
}
 
Example #4
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
/**
 * WAVE�t�@�C�������[�h
 * @param url WAVE�t�@�C����URL
 */
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    // �I�[�f�B�I�X�g���[�����J��
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    // WAVE�t�@�C���̃t�H�[�}�b�g���擾
    AudioFormat format = ais.getFormat();
    // ���C�����擾
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);

    // WAVE�f�[�^���擾
    DataClip clip = new DataClip(ais);
    
    // WAVE�f�[�^��o�^
    clips[counter] = clip;
    lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
    
    // ���C�����J��
    lines[counter].open(format);

    counter++;
}
 
Example #5
Source File: JoggStreamer.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
private void openOutput() throws IOException {

		AudioFormat audioFormat = new AudioFormat((float) vorbisInfo.rate, 16, vorbisInfo.channels, true, false);

		DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);

		if (!AudioSystem.isLineSupported(info))

		{
			throw new IOException("line format " + info + "not supported");
		}

		try

		{
			out = (SourceDataLine) AudioSystem.getLine(info);
			out.open(audioFormat);
		}

		catch (LineUnavailableException e) {
			throw new IOException("audio unavailable: " + e.toString());
		}

		out.start();
		updateVolume(volume);
	}
 
Example #6
Source File: DataLine_ArrayIndexOutOfBounds.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    log("" + infos.length + " mixers detected");
    for (int i=0; i<infos.length; i++) {
        Mixer mixer = AudioSystem.getMixer(infos[i]);
        log("Mixer " + (i+1) + ": " + infos[i]);
        try {
            mixer.open();
            for (Scenario scenario: scenarios) {
                testSDL(mixer, scenario);
                testTDL(mixer, scenario);
            }
            mixer.close();
        } catch (LineUnavailableException ex) {
            log("LineUnavailableException: " + ex);
        }
    }
    if (failed == 0) {
        log("PASSED (" + total + " tests)");
    } else {
        log("FAILED (" + failed + " of " + total + " tests)");
        throw new Exception("Test FAILED");
    }
}
 
Example #7
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
/**
 * WAVE�t�@�C�������[�h
 * @param url WAVE�t�@�C����URL
 */
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    // �I�[�f�B�I�X�g���[�����J��
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    // WAVE�t�@�C���̃t�H�[�}�b�g���擾
    AudioFormat format = ais.getFormat();
    // ���C�����擾
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);

    // WAVE�f�[�^���擾
    DataClip clip = new DataClip(ais);
    
    // WAVE�f�[�^��o�^
    clips[counter] = clip;
    lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
    
    // ���C�����J��
    lines[counter].open(format);

    counter++;
}
 
Example #8
Source File: StdAudio.java    From algs4 with GNU General Public License v3.0 6 votes vote down vote up
private static void init() {
    try {
        // 44,100 Hz, 16-bit audio, mono, signed PCM, little endian
        AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, MONO, SIGNED, LITTLE_ENDIAN);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
        
        // the internal buffer is a fraction of the actual buffer size, this choice is arbitrary
        // it gets divided because we can't expect the buffered data to line up exactly with when
        // the sound card decides to push out its samples.
        buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE/3];
    }
    catch (LineUnavailableException e) {
        System.out.println(e.getMessage());
    }

    // no sound gets made before this call
    line.start();
}
 
Example #9
Source File: DataLine_ArrayIndexOutOfBounds.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    log("" + infos.length + " mixers detected");
    for (int i=0; i<infos.length; i++) {
        Mixer mixer = AudioSystem.getMixer(infos[i]);
        log("Mixer " + (i+1) + ": " + infos[i]);
        try {
            mixer.open();
            for (Scenario scenario: scenarios) {
                testSDL(mixer, scenario);
                testTDL(mixer, scenario);
            }
            mixer.close();
        } catch (LineUnavailableException ex) {
            log("LineUnavailableException: " + ex);
        }
    }
    if (failed == 0) {
        log("PASSED (" + total + " tests)");
    } else {
        log("FAILED (" + failed + " of " + total + " tests)");
        throw new Exception("Test FAILED");
    }
}
 
Example #10
Source File: MultiClip.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
* 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();
	}
}
 
Example #11
Source File: MultiClip.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #12
Source File: DataLine_ArrayIndexOutOfBounds.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    log("" + infos.length + " mixers detected");
    for (int i=0; i<infos.length; i++) {
        Mixer mixer = AudioSystem.getMixer(infos[i]);
        log("Mixer " + (i+1) + ": " + infos[i]);
        try {
            mixer.open();
            for (Scenario scenario: scenarios) {
                testSDL(mixer, scenario);
                testTDL(mixer, scenario);
            }
            mixer.close();
        } catch (LineUnavailableException ex) {
            log("LineUnavailableException: " + ex);
        }
    }
    if (failed == 0) {
        log("PASSED (" + total + " tests)");
    } else {
        log("FAILED (" + failed + " of " + total + " tests)");
        throw new Exception("Test FAILED");
    }
}
 
Example #13
Source File: Sample.java    From mpcmaid with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static Sample open(final AudioInputStream audioStream) throws LineUnavailableException, IOException {
	final int frameLength = (int) audioStream.getFrameLength();
	if (frameLength > 44100 * 8 * 2) {
		throw new IllegalArgumentException("The audio file is too long (must be shorter than 4 bars at 50BPM)");
	}
	final AudioFormat format = audioStream.getFormat();
	final int frameSize = (int) format.getFrameSize();
	final byte[] bytes = new byte[frameLength * frameSize];
	final int result = audioStream.read(bytes);
	if (result < 0) {
		return null;
	}

	audioStream.close();

	return new Sample(bytes, format, frameLength);
}
 
Example #14
Source File: SoundTools.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static void rawplay(AudioFormat targetFormat, AudioInputStream din)
        throws IOException, LineUnavailableException {
    byte[] data = new byte[CommonValues.IOBufferLength];
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, targetFormat);
    SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
    line.open(targetFormat);

    if (line != null) {
        // Start
        FloatControl vol = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
        logger.debug(vol.getValue() + vol.getUnits());
        line.start();
        int nBytesRead = 0, nBytesWritten = 0;
        while (nBytesRead != -1) {
            nBytesRead = din.read(data, 0, data.length);
            if (nBytesRead != -1) {
                nBytesWritten = line.write(data, 0, nBytesRead);
            }
        }
        // Stop
        line.drain();
        line.stop();
        line.close();
        din.close();
    }
}
 
Example #15
Source File: PortMixer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void implOpen() throws LineUnavailableException {
    if (Printer.trace) Printer.trace(">> PortMixerPort: implOpen().");
    long newID = ((PortMixer) mixer).getID();
    if ((id == 0) || (newID != id) || (controls.length == 0)) {
        id = newID;
        Vector<Control> vector = new Vector<>();
        synchronized (vector) {
            nGetControls(id, portIndex, vector);
            controls = new Control[vector.size()];
            for (int i = 0; i < controls.length; i++) {
                controls[i] = vector.elementAt(i);
            }
        }
    } else {
        enableControls(controls, true);
    }
    if (Printer.trace) Printer.trace("<< PortMixerPort: implOpen() succeeded");
}
 
Example #16
Source File: PortMixer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void open() throws LineUnavailableException {
    synchronized (mixer) {
        // if the line is not currently open, try to open it with this format and buffer size
        if (!isOpen()) {
            if (Printer.trace) Printer.trace("> PortMixerPort: open");
            // reserve mixer resources for this line
            mixer.open(this);
            try {
                // open the line.  may throw LineUnavailableException.
                implOpen();

                // if we succeeded, set the open state to true and send events
                setOpen(true);
            } catch (LineUnavailableException e) {
                // release mixer resources for this line and then throw the exception
                mixer.close(this);
                throw e;
            }
            if (Printer.trace) Printer.trace("< PortMixerPort: open succeeded");
        }
    }
}
 
Example #17
Source File: PlayerTest.java    From DTMF-Decoder with MIT License 6 votes vote down vote up
private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException
{
	byte[] data = new byte[4096];
	SourceDataLine line = getLine(targetFormat);		
	if (line != null)
	{
	  // Start
	  line.start();
	  int nBytesRead = 0, nBytesWritten = 0;
	  while (nBytesRead != -1)
	  {
		nBytesRead = din.read(data, 0, data.length);
		if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
	  }
	  // Stop
	  line.drain();
	  line.stop();
	  line.close();
	  din.close();
	}		
}
 
Example #18
Source File: DesktopTelephonySupport.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void play(final SynthesizedOutput output,
        final CallControlProperties props)
        throws IOException, NoresourceError {
    if (!active) {
        throw new NoresourceError("desktop telephony is no longer active");
    }
    if (output instanceof AudioSource) {
        final AudioSource source = (AudioSource) output;
        try {
            play(source);
        } catch (LineUnavailableException e) {
            throw new NoresourceError(e.getMessage(), e);
        }
    }
    busy = true;
    synchronized (listener) {
        final Collection<TelephonyListener> copy =
                new java.util.ArrayList<TelephonyListener>();
        copy.addAll(listener);
        final TelephonyEvent event = new TelephonyEvent(this,
                TelephonyEvent.PLAY_STARTED);
        for (TelephonyListener current : copy) {
            current.telephonyMediaEvent(event);
        }
    }
}
 
Example #19
Source File: PortMixer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public Line getLine(Line.Info info) throws LineUnavailableException {
    Line.Info fullInfo = getLineInfo(info);

    if ((fullInfo != null) && (fullInfo instanceof Port.Info)) {
        for (int i = 0; i < portInfos.length; i++) {
            if (fullInfo.equals(portInfos[i])) {
                return getPort(i);
            }
        }
    }
    throw new IllegalArgumentException("Line unsupported: " + info);
}
 
Example #20
Source File: SoftMixingClip.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void open() throws LineUnavailableException {
    if (data == null) {
        throw new IllegalArgumentException(
                "Illegal call to open() in interface Clip");
    }
    open(format, data, offset, bufferSize);
}
 
Example #21
Source File: PortMixer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public Line getLine(Line.Info info) throws LineUnavailableException {
    Line.Info fullInfo = getLineInfo(info);

    if ((fullInfo != null) && (fullInfo instanceof Port.Info)) {
        for (int i = 0; i < portInfos.length; i++) {
            if (fullInfo.equals(portInfos[i])) {
                return getPort(i);
            }
        }
    }
    throw new IllegalArgumentException("Line unsupported: " + info);
}
 
Example #22
Source File: AbstractMixer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation tries to open the mixer with its current format and buffer size settings.
 */
final synchronized void open(boolean manual) throws LineUnavailableException {
    if (Printer.trace) Printer.trace(">> AbstractMixer: open()");
    if (!isOpen()) {
        implOpen();
        // if the mixer is not currently open, set open to true and send event
        setOpen(true);
        if (manual) {
            manuallyOpened = true;
        }
    }

    if (Printer.trace) Printer.trace("<< AbstractMixer: open() succeeded");
}
 
Example #23
Source File: DirectAudioDevice.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static native long nOpen(int mixerIndex, int deviceID, boolean isSource,
int encoding,
float sampleRate,
int sampleSizeInBits,
int frameSize,
int channels,
boolean signed,
boolean bigEndian,
int bufferSize) throws LineUnavailableException;
 
Example #24
Source File: SoftMixingClip.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void open() throws LineUnavailableException {
    if (data == null) {
        throw new IllegalArgumentException(
                "Illegal call to open() in interface Clip");
    }
    open(format, data, offset, bufferSize);
}
 
Example #25
Source File: DummySourceDataLine.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void open(AudioFormat format, int bufferSize)
        throws LineUnavailableException {
    this.format = format;
    this.bufferSize = bufferSize;
    this.framesize = format.getFrameSize();
    opened = true;
}
 
Example #26
Source File: SoundController.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Plays a sound clip.
 * @param clip the Clip to play
 * @param volume the volume [0, 1]
 * @param listener the line listener
 */
private static void playClip(MultiClip clip, float volume, LineListener listener) {
	if (clip == null)  // clip failed to load properly
		return;

       currentSoundComponent = clip;

	if (volume > 0f && !isMuted) {
		try {
			clip.start(volume, listener);
		} catch (LineUnavailableException e) {
			softErr(e, "Could not start clip %s", clip.getName());
		}
	}
}
 
Example #27
Source File: AbstractMixer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation tries to open the mixer with its current format and buffer size settings.
 */
final synchronized void open(boolean manual) throws LineUnavailableException {
    if (Printer.trace) Printer.trace(">> AbstractMixer: open()");
    if (!isOpen()) {
        implOpen();
        // if the mixer is not currently open, set open to true and send event
        setOpen(true);
        if (manual) {
            manuallyOpened = true;
        }
    }

    if (Printer.trace) Printer.trace("<< AbstractMixer: open() succeeded");
}
 
Example #28
Source File: SessionClient.java    From message_interface with MIT License 5 votes vote down vote up
/**
 * 发送语音消息
 * @param fromUid 消息来源访客的ID
 * @param path 语音文件的路径
 * @param duration 语音消息长度,如果没有提供,这里也会尝试去计算,不过效率会稍差
 * @return 发送的结果
 * @throws IOException
 */
public CommonResult sendAudioMessage(String fromUid, String path, long duration) throws IOException, LineUnavailableException, UnsupportedAudioFileException, NoSuchAlgorithmException {
    String md5 = FileUtil.getMd5(path).toLowerCase();
    long size = FileUtil.getSize(path);
    String url = uploadFile(path, md5);

    if (duration <= 0) {
        duration = MediaUtil.queryAudioDuration(path);
    }

    return sendAudioMessage(fromUid, url, md5, size, duration);
}
 
Example #29
Source File: AbstractDataLine.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Try to open the line with the current format and buffer size values.
 * If the line is not open, these will be the defaults.  If the
 * line is open, this should return quietly because the values
 * requested will match the current ones.
 */
public final void open() throws LineUnavailableException {

    if (Printer.trace) Printer.trace("> "+getClass().getName()+".open() - AbstractDataLine");

    // this may throw a LineUnavailableException.
    open(format, bufferSize);
    if (Printer.trace) Printer.trace("< "+getClass().getName()+".open() - AbstractDataLine");
}
 
Example #30
Source File: PortMixer.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public Line getLine(Line.Info info) throws LineUnavailableException {
    Line.Info fullInfo = getLineInfo(info);

    if ((fullInfo != null) && (fullInfo instanceof Port.Info)) {
        for (int i = 0; i < portInfos.length; i++) {
            if (fullInfo.equals(portInfos[i])) {
                return getPort(i);
            }
        }
    }
    throw new IllegalArgumentException("Line unsupported: " + info);
}