Search in sources :

Example 51 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class AuFileReader method getAudioInputStream.

/**
     * Obtains an audio stream from the URL provided.  The URL must
     * point to valid audio file data.
     * @param url the URL for which the <code>AudioInputStream</code> should be
     * constructed
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
     * to by the URL
     * @throws UnsupportedAudioFileException if the URL does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     */
public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException {
    InputStream urlStream = null;
    BufferedInputStream bis = null;
    AudioFileFormat fileFormat = null;
    // throws IOException
    urlStream = url.openStream();
    AudioInputStream result = null;
    try {
        bis = new BufferedInputStream(urlStream, bisBufferSize);
        result = getAudioInputStream((InputStream) bis);
    } finally {
        if (result == null) {
            urlStream.close();
        }
    }
    return result;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 52 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class AudioFileSoundbankReader method getSoundbank.

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;
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException)

Example 53 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class AudioFloatInputStream method getInputStream.

public static AudioFloatInputStream getInputStream(AudioFormat format, byte[] buffer, int offset, int len) {
    AudioFloatConverter converter = AudioFloatConverter.getConverter(format);
    if (converter != null)
        return new BytaArrayAudioFloatInputStream(converter, buffer, offset, len);
    InputStream stream = new ByteArrayInputStream(buffer, offset, len);
    long aLen = format.getFrameSize() == AudioSystem.NOT_SPECIFIED ? AudioSystem.NOT_SPECIFIED : len / format.getFrameSize();
    AudioInputStream astream = new AudioInputStream(stream, format, aLen);
    return getInputStream(astream);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream)

Example 54 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class DLSSample method getData.

public Object getData() {
    AudioFormat format = getFormat();
    InputStream is = data.getInputStream();
    if (is == null)
        return null;
    return new AudioInputStream(is, format, data.capacity());
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) AudioFormat(javax.sound.sampled.AudioFormat)

Example 55 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project jdk8u_jdk by JetBrains.

the class DLSSoundbank method writeSample.

private void writeSample(RIFFWriter writer, DLSSample sample) throws IOException {
    AudioFormat audioformat = sample.getFormat();
    Encoding encoding = audioformat.getEncoding();
    float sampleRate = audioformat.getSampleRate();
    int sampleSizeInBits = audioformat.getSampleSizeInBits();
    int channels = audioformat.getChannels();
    int frameSize = audioformat.getFrameSize();
    float frameRate = audioformat.getFrameRate();
    boolean bigEndian = audioformat.isBigEndian();
    boolean convert_needed = false;
    if (audioformat.getSampleSizeInBits() == 8) {
        if (!encoding.equals(Encoding.PCM_UNSIGNED)) {
            encoding = Encoding.PCM_UNSIGNED;
            convert_needed = true;
        }
    } else {
        if (!encoding.equals(Encoding.PCM_SIGNED)) {
            encoding = Encoding.PCM_SIGNED;
            convert_needed = true;
        }
        if (bigEndian) {
            bigEndian = false;
            convert_needed = true;
        }
    }
    if (convert_needed) {
        audioformat = new AudioFormat(encoding, sampleRate, sampleSizeInBits, channels, frameSize, frameRate, bigEndian);
    }
    // fmt
    RIFFWriter fmt_chunk = writer.writeChunk("fmt ");
    int sampleformat = 0;
    if (audioformat.getEncoding().equals(Encoding.PCM_UNSIGNED))
        sampleformat = 1;
    else if (audioformat.getEncoding().equals(Encoding.PCM_SIGNED))
        sampleformat = 1;
    else if (audioformat.getEncoding().equals(Encoding.PCM_FLOAT))
        sampleformat = 3;
    fmt_chunk.writeUnsignedShort(sampleformat);
    fmt_chunk.writeUnsignedShort(audioformat.getChannels());
    fmt_chunk.writeUnsignedInt((long) audioformat.getSampleRate());
    long srate = ((long) audioformat.getFrameRate()) * audioformat.getFrameSize();
    fmt_chunk.writeUnsignedInt(srate);
    fmt_chunk.writeUnsignedShort(audioformat.getFrameSize());
    fmt_chunk.writeUnsignedShort(audioformat.getSampleSizeInBits());
    fmt_chunk.write(0);
    fmt_chunk.write(0);
    writeSampleOptions(writer.writeChunk("wsmp"), sample.sampleoptions);
    if (convert_needed) {
        RIFFWriter data_chunk = writer.writeChunk("data");
        AudioInputStream stream = AudioSystem.getAudioInputStream(audioformat, (AudioInputStream) sample.getData());
        byte[] buff = new byte[1024];
        int ret;
        while ((ret = stream.read(buff)) != -1) {
            data_chunk.write(buff, 0, ret);
        }
    } else {
        RIFFWriter data_chunk = writer.writeChunk("data");
        ModelByteBuffer databuff = sample.getDataBuffer();
        databuff.writeTo(data_chunk);
    /*
            data_chunk.write(databuff.array(),
            databuff.arrayOffset(),
            databuff.capacity());
             */
    }
    writeInfo(writer.writeList("INFO"), sample.info);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Encoding(javax.sound.sampled.AudioFormat.Encoding) AudioFormat(javax.sound.sampled.AudioFormat)

Aggregations

AudioInputStream (javax.sound.sampled.AudioInputStream)92 IOException (java.io.IOException)44 AudioFormat (javax.sound.sampled.AudioFormat)41 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)27 InputStream (java.io.InputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)19 LineUnavailableException (javax.sound.sampled.LineUnavailableException)19 File (java.io.File)18 SourceDataLine (javax.sound.sampled.SourceDataLine)14 Clip (javax.sound.sampled.Clip)13 AudioFileFormat (javax.sound.sampled.AudioFileFormat)12 DataLine (javax.sound.sampled.DataLine)12 FileInputStream (java.io.FileInputStream)11 BufferedInputStream (java.io.BufferedInputStream)10 URL (java.net.URL)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataInputStream (java.io.DataInputStream)5 SequenceInputStream (java.io.SequenceInputStream)5 LineEvent (javax.sound.sampled.LineEvent)5 AudioSynthesizer (com.sun.media.sound.AudioSynthesizer)4