Java Code Examples for javax.sound.sampled.AudioInputStream#close()

The following examples show how to use javax.sound.sampled.AudioInputStream#close() . 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: JavaSoundAudioClip.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void readStream(AudioInputStream as, long byteLen) throws IOException {
    // arrays "only" max. 2GB
    int intLen;
    if (byteLen > 2147483647) {
        intLen = 2147483647;
    } else {
        intLen = (int) byteLen;
    }
    loadedAudio = new byte[intLen];
    loadedAudioByteLength = 0;

    // this loop may throw an IOException
    while (true) {
        int bytesRead = as.read(loadedAudio, loadedAudioByteLength, intLen - loadedAudioByteLength);
        if (bytesRead <= 0) {
            as.close();
            break;
        }
        loadedAudioByteLength += bytesRead;
    }
}
 
Example 2
Source File: JavaSoundAudioClip.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void readStream(AudioInputStream as, long byteLen) throws IOException {
    // arrays "only" max. 2GB
    int intLen;
    if (byteLen > 2147483647) {
        intLen = 2147483647;
    } else {
        intLen = (int) byteLen;
    }
    loadedAudio = new byte[intLen];
    loadedAudioByteLength = 0;

    // this loop may throw an IOException
    while (true) {
        int bytesRead = as.read(loadedAudio, loadedAudioByteLength, intLen - loadedAudioByteLength);
        if (bytesRead <= 0) {
            as.close();
            break;
        }
        loadedAudioByteLength += bytesRead;
    }
}
 
Example 3
Source File: JavaSoundAudioClip.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void readStream(AudioInputStream as, long byteLen) throws IOException {
    // arrays "only" max. 2GB
    int intLen;
    if (byteLen > 2147483647) {
        intLen = 2147483647;
    } else {
        intLen = (int) byteLen;
    }
    loadedAudio = new byte[intLen];
    loadedAudioByteLength = 0;

    // this loop may throw an IOException
    while (true) {
        int bytesRead = as.read(loadedAudio, loadedAudioByteLength, intLen - loadedAudioByteLength);
        if (bytesRead <= 0) {
            as.close();
            break;
        }
        loadedAudioByteLength += bytesRead;
    }
}
 
Example 4
Source File: JavaSoundAudioClip.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void readStream(AudioInputStream as, long byteLen) throws IOException {
    // arrays "only" max. 2GB
    int intLen;
    if (byteLen > 2147483647) {
        intLen = 2147483647;
    } else {
        intLen = (int) byteLen;
    }
    loadedAudio = new byte[intLen];
    loadedAudioByteLength = 0;

    // this loop may throw an IOException
    while (true) {
        int bytesRead = as.read(loadedAudio, loadedAudioByteLength, intLen - loadedAudioByteLength);
        if (bytesRead <= 0) {
            as.close();
            break;
        }
        loadedAudioByteLength += bytesRead;
    }
}
 
Example 5
Source File: JavaSoundAudioClip.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private void readStream(AudioInputStream as) throws IOException {

        DirectBAOS baos = new DirectBAOS();
        byte[] buffer = new byte[16384];
        int bytesRead = 0;
        int totalBytesRead = 0;

        // this loop may throw an IOException
        while( true ) {
            bytesRead = as.read(buffer, 0, buffer.length);
            if (bytesRead <= 0) {
                as.close();
                break;
            }
            totalBytesRead += bytesRead;
            baos.write(buffer, 0, bytesRead);
        }
        loadedAudio = baos.getInternalBuffer();
        loadedAudioByteLength = totalBytesRead;
    }
 
Example 6
Source File: AudioFileSoundbankReader.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public Soundbank getSoundbank(File file)
        throws InvalidMidiDataException, IOException {
    try {
        AudioInputStream ais = AudioSystem.getAudioInputStream(file);
        ais.close();
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
                new ModelByteBuffer(file, 0, file.length()), -4800);
        ModelPerformer performer = new ModelPerformer();
        performer.getOscillators().add(osc);
        SimpleSoundbank sbk = new SimpleSoundbank();
        SimpleInstrument ins = new SimpleInstrument();
        ins.add(performer);
        sbk.addInstrument(ins);
        return sbk;
    } catch (UnsupportedAudioFileException e1) {
        return null;
    } catch (IOException e) {
        return null;
    }
}
 
Example 7
Source File: JavaSoundAudioClip.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void readStream(AudioInputStream as) throws IOException {

        DirectBAOS baos = new DirectBAOS();
        byte buffer[] = new byte[16384];
        int bytesRead = 0;
        int totalBytesRead = 0;

        // this loop may throw an IOException
        while( true ) {
            bytesRead = as.read(buffer, 0, buffer.length);
            if (bytesRead <= 0) {
                as.close();
                break;
            }
            totalBytesRead += bytesRead;
            baos.write(buffer, 0, bytesRead);
        }
        loadedAudio = baos.getInternalBuffer();
        loadedAudioByteLength = totalBytesRead;
    }
 
Example 8
Source File: JavaSoundAudioClip.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void readStream(AudioInputStream as) throws IOException {

        DirectBAOS baos = new DirectBAOS();
        byte buffer[] = new byte[16384];
        int bytesRead = 0;
        int totalBytesRead = 0;

        // this loop may throw an IOException
        while( true ) {
            bytesRead = as.read(buffer, 0, buffer.length);
            if (bytesRead <= 0) {
                as.close();
                break;
            }
            totalBytesRead += bytesRead;
            baos.write(buffer, 0, bytesRead);
        }
        loadedAudio = baos.getInternalBuffer();
        loadedAudioByteLength = totalBytesRead;
    }
 
Example 9
Source File: MidiToAudioWriter.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void write(OutputStream out, List<MidiEvent> events, MidiToAudioSettings settings) throws Throwable {
	MidiToAudioSynth.instance().openSynth();
	MidiToAudioSynth.instance().loadSoundbank(getPatchs(events), settings.getSoundbankPath());
	
	int usqTempo = 60000000 / 120;
	long previousTick = 0;
	long timePosition = 0;
	MidiToAudioWriter.sort(events);
	Receiver receiver = MidiToAudioSynth.instance().getReceiver();
	AudioInputStream stream = MidiToAudioSynth.instance().getStream();
	
	Iterator<MidiEvent> it = events.iterator();
	while(it.hasNext()){
		MidiEvent event = (MidiEvent)it.next();
		MidiMessage msg = event.getMessage();
		
		timePosition += ( (event.getTick() - previousTick) * usqTempo) / TGDuration.QUARTER_TIME;
		
		if (msg instanceof MetaMessage) {
			if (((MetaMessage) msg).getType() == 0x51) {
				byte[] data = ((MetaMessage) msg).getData();
				usqTempo = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
			}
		} else {
			receiver.send(msg, timePosition);
		}
		previousTick = event.getTick();
	}
	
	long duration = (long) (stream.getFormat().getFrameRate() * ( (timePosition / 1000000.0) ));
	
	AudioInputStream srcStream = new AudioInputStream(stream, stream.getFormat(), duration );
	AudioInputStream dstStream = AudioSystem.getAudioInputStream(settings.getFormat(), srcStream );
	AudioSystem.write(new AudioInputStream(dstStream, dstStream.getFormat(), duration ), settings.getType(), out);
	
	dstStream.close();
	srcStream.close();
	
	MidiToAudioSynth.instance().closeSynth();
}
 
Example 10
Source File: AudioFileSoundbankReader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(AudioInputStream ais)
        throws InvalidMidiDataException, IOException {
    try {
        byte[] buffer;
        if (ais.getFrameLength() == -1) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buff = new byte[1024
                    - (1024 % ais.getFormat().getFrameSize())];
            int ret;
            while ((ret = ais.read(buff)) != -1) {
                baos.write(buff, 0, ret);
            }
            ais.close();
            buffer = baos.toByteArray();
        } else {
            buffer = new byte[(int) (ais.getFrameLength()
                                * ais.getFormat().getFrameSize())];
            new DataInputStream(ais).readFully(buffer);
        }
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
                new ModelByteBuffer(buffer), ais.getFormat(), -4800);
        ModelPerformer performer = new ModelPerformer();
        performer.getOscillators().add(osc);

        SimpleSoundbank sbk = new SimpleSoundbank();
        SimpleInstrument ins = new SimpleInstrument();
        ins.add(performer);
        sbk.addInstrument(ins);
        return sbk;
    } catch (Exception e) {
        return null;
    }
}
 
Example 11
Source File: AudioFileSoundbankReader.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(AudioInputStream ais)
        throws InvalidMidiDataException, IOException {
    try {
        byte[] buffer;
        if (ais.getFrameLength() == -1) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buff = new byte[1024
                    - (1024 % ais.getFormat().getFrameSize())];
            int ret;
            while ((ret = ais.read(buff)) != -1) {
                baos.write(buff, 0, ret);
            }
            ais.close();
            buffer = baos.toByteArray();
        } else {
            buffer = new byte[(int) (ais.getFrameLength()
                                * ais.getFormat().getFrameSize())];
            new DataInputStream(ais).readFully(buffer);
        }
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
                new ModelByteBuffer(buffer), ais.getFormat(), -4800);
        ModelPerformer performer = new ModelPerformer();
        performer.getOscillators().add(osc);

        SimpleSoundbank sbk = new SimpleSoundbank();
        SimpleInstrument ins = new SimpleInstrument();
        ins.add(performer);
        sbk.addInstrument(ins);
        return sbk;
    } catch (Exception e) {
        return null;
    }
}
 
Example 12
Source File: AudioFileSoundbankReader.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(AudioInputStream ais)
        throws InvalidMidiDataException, IOException {
    try {
        byte[] buffer;
        if (ais.getFrameLength() == -1) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buff = new byte[1024
                    - (1024 % ais.getFormat().getFrameSize())];
            int ret;
            while ((ret = ais.read(buff)) != -1) {
                baos.write(buff, 0, ret);
            }
            ais.close();
            buffer = baos.toByteArray();
        } else {
            buffer = new byte[(int) (ais.getFrameLength()
                                * ais.getFormat().getFrameSize())];
            new DataInputStream(ais).readFully(buffer);
        }
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
                new ModelByteBuffer(buffer), ais.getFormat(), -4800);
        ModelPerformer performer = new ModelPerformer();
        performer.getOscillators().add(osc);

        SimpleSoundbank sbk = new SimpleSoundbank();
        SimpleInstrument ins = new SimpleInstrument();
        ins.add(performer);
        sbk.addInstrument(ins);
        return sbk;
    } catch (Exception e) {
        return null;
    }
}
 
Example 13
Source File: AudioFileSoundbankReader.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(AudioInputStream ais)
        throws InvalidMidiDataException, IOException {
    try {
        byte[] buffer;
        if (ais.getFrameLength() == -1) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buff = new byte[1024
                    - (1024 % ais.getFormat().getFrameSize())];
            int ret;
            while ((ret = ais.read(buff)) != -1) {
                baos.write(buff, 0, ret);
            }
            ais.close();
            buffer = baos.toByteArray();
        } else {
            buffer = new byte[(int) (ais.getFrameLength()
                                * ais.getFormat().getFrameSize())];
            new DataInputStream(ais).readFully(buffer);
        }
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
                new ModelByteBuffer(buffer), ais.getFormat(), -4800);
        ModelPerformer performer = new ModelPerformer();
        performer.getOscillators().add(osc);

        SimpleSoundbank sbk = new SimpleSoundbank();
        SimpleInstrument ins = new SimpleInstrument();
        ins.add(performer);
        sbk.addInstrument(ins);
        return sbk;
    } catch (Exception e) {
        return null;
    }
}
 
Example 14
Source File: SoftSynthesizer.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void close() {

        if (!isOpen())
            return;

        SoftAudioPusher pusher_to_be_closed = null;
        AudioInputStream pusher_stream_to_be_closed = null;
        synchronized (control_mutex) {
            if (pusher != null) {
                pusher_to_be_closed = pusher;
                pusher_stream_to_be_closed = pusher_stream;
                pusher = null;
                pusher_stream = null;
            }
        }

        if (pusher_to_be_closed != null) {
            // Pusher must not be closed synchronized against control_mutex,
            // this may result in synchronized conflict between pusher
            // and current thread.
            pusher_to_be_closed.stop();

            try {
                pusher_stream_to_be_closed.close();
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }

        synchronized (control_mutex) {

            if (mainmixer != null)
                mainmixer.close();
            open = false;
            implicitOpen = false;
            mainmixer = null;
            voices = null;
            channels = null;

            if (external_channels != null)
                for (int i = 0; i < external_channels.length; i++)
                    external_channels[i].setChannel(null);

            if (sourceDataLine != null) {
                sourceDataLine.close();
                sourceDataLine = null;
            }

            inslist.clear();
            loadedlist.clear();
            tunings.clear();

            while (recvslist.size() != 0)
                recvslist.get(recvslist.size() - 1).close();

        }
    }
 
Example 15
Source File: SoftSynthesizer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void close() throws IOException
{
    AudioInputStream astream  = weak_stream_link.get();
    if(astream != null)
        astream.close();
}
 
Example 16
Source File: SoftMixingMixer.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void close() {
    if (!isOpen())
        return;

    sendEvent(new LineEvent(this, LineEvent.Type.CLOSE,
            AudioSystem.NOT_SPECIFIED));

    SoftAudioPusher pusher_to_be_closed = null;
    AudioInputStream pusher_stream_to_be_closed = null;
    synchronized (control_mutex) {
        if (pusher != null) {
            pusher_to_be_closed = pusher;
            pusher_stream_to_be_closed = pusher_stream;
            pusher = null;
            pusher_stream = null;
        }
    }

    if (pusher_to_be_closed != null) {
        // Pusher must not be closed synchronized against control_mutex
        // this may result in synchronized conflict between pusher and
        // current thread.
        pusher_to_be_closed.stop();

        try {
            pusher_stream_to_be_closed.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    synchronized (control_mutex) {

        if (mainmixer != null)
            mainmixer.close();
        open = false;

        if (sourceDataLine != null) {
            sourceDataLine.drain();
            sourceDataLine.close();
            sourceDataLine = null;
        }

    }

}
 
Example 17
Source File: SoftMixingMixer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void close() {
    if (!isOpen())
        return;

    sendEvent(new LineEvent(this, LineEvent.Type.CLOSE,
            AudioSystem.NOT_SPECIFIED));

    SoftAudioPusher pusher_to_be_closed = null;
    AudioInputStream pusher_stream_to_be_closed = null;
    synchronized (control_mutex) {
        if (pusher != null) {
            pusher_to_be_closed = pusher;
            pusher_stream_to_be_closed = pusher_stream;
            pusher = null;
            pusher_stream = null;
        }
    }

    if (pusher_to_be_closed != null) {
        // Pusher must not be closed synchronized against control_mutex
        // this may result in synchronized conflict between pusher and
        // current thread.
        pusher_to_be_closed.stop();

        try {
            pusher_stream_to_be_closed.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    synchronized (control_mutex) {

        if (mainmixer != null)
            mainmixer.close();
        open = false;

        if (sourceDataLine != null) {
            sourceDataLine.drain();
            sourceDataLine.close();
            sourceDataLine = null;
        }

    }

}
 
Example 18
Source File: SoftSynthesizer.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void close() {

        if (!isOpen())
            return;

        SoftAudioPusher pusher_to_be_closed = null;
        AudioInputStream pusher_stream_to_be_closed = null;
        synchronized (control_mutex) {
            if (pusher != null) {
                pusher_to_be_closed = pusher;
                pusher_stream_to_be_closed = pusher_stream;
                pusher = null;
                pusher_stream = null;
            }
        }

        if (pusher_to_be_closed != null) {
            // Pusher must not be closed synchronized against control_mutex,
            // this may result in synchronized conflict between pusher
            // and current thread.
            pusher_to_be_closed.stop();

            try {
                pusher_stream_to_be_closed.close();
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }

        synchronized (control_mutex) {

            if (mainmixer != null)
                mainmixer.close();
            open = false;
            implicitOpen = false;
            mainmixer = null;
            voices = null;
            channels = null;

            if (external_channels != null)
                for (int i = 0; i < external_channels.length; i++)
                    external_channels[i].setChannel(null);

            if (sourceDataLine != null) {
                sourceDataLine.close();
                sourceDataLine = null;
            }

            inslist.clear();
            loadedlist.clear();
            tunings.clear();

            while (recvslist.size() != 0)
                recvslist.get(recvslist.size() - 1).close();

        }
    }
 
Example 19
Source File: SoftSynthesizer.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void close() throws IOException
{
    AudioInputStream astream  = weak_stream_link.get();
    if(astream != null)
        astream.close();
}
 
Example 20
Source File: SoftMixingMixer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void close() {
    if (!isOpen())
        return;

    sendEvent(new LineEvent(this, LineEvent.Type.CLOSE,
            AudioSystem.NOT_SPECIFIED));

    SoftAudioPusher pusher_to_be_closed = null;
    AudioInputStream pusher_stream_to_be_closed = null;
    synchronized (control_mutex) {
        if (pusher != null) {
            pusher_to_be_closed = pusher;
            pusher_stream_to_be_closed = pusher_stream;
            pusher = null;
            pusher_stream = null;
        }
    }

    if (pusher_to_be_closed != null) {
        // Pusher must not be closed synchronized against control_mutex
        // this may result in synchronized conflict between pusher and
        // current thread.
        pusher_to_be_closed.stop();

        try {
            pusher_stream_to_be_closed.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    synchronized (control_mutex) {

        if (mainmixer != null)
            mainmixer.close();
        open = false;

        if (sourceDataLine != null) {
            sourceDataLine.drain();
            sourceDataLine.close();
            sourceDataLine = null;
        }

    }

}