Search in sources :

Example 6 with AudioFileFormat

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

the class AiffFileReader 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 {
    // throws IOException
    InputStream urlStream = url.openStream();
    AudioFileFormat fileFormat = null;
    try {
        fileFormat = getCOMM(urlStream, false);
    } finally {
        if (fileFormat == null) {
            urlStream.close();
        }
    }
    return new AudioInputStream(urlStream, fileFormat.getFormat(), fileFormat.getFrameLength());
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) DataInputStream(java.io.DataInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 7 with AudioFileFormat

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

the class AiffFileReader method getAudioFileFormat.

/**
     * Obtains the audio file format of the URL provided.  The URL must
     * point to valid audio file data.
     * @param url the URL from which file format information should be
     * extracted
     * @return an <code>AudioFileFormat</code> object describing the audio file format
     * @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 AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException {
    AudioFileFormat fileFormat = null;
    // throws IOException
    InputStream urlStream = url.openStream();
    try {
        fileFormat = getCOMM(urlStream, false);
    } finally {
        urlStream.close();
    }
    return fileFormat;
}
Also used : DataInputStream(java.io.DataInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 8 with AudioFileFormat

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

the class AiffFileReader method getAudioInputStream.

/**
     * Obtains an audio stream from the File provided.  The File must
     * point to valid audio file data.
     * @param file the File 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 File
     * @throws UnsupportedAudioFileException if the File does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     */
public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
    // throws IOException
    FileInputStream fis = new FileInputStream(file);
    AudioFileFormat fileFormat = null;
    // part of fix for 4325421
    try {
        fileFormat = getCOMM(fis, false);
    } finally {
        if (fileFormat == null) {
            fis.close();
        }
    }
    return new AudioInputStream(fis, fileFormat.getFormat(), fileFormat.getFrameLength());
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) FileInputStream(java.io.FileInputStream) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 9 with AudioFileFormat

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

the class AiffFileReader method getCOMM.

//--------------------------------------------------------------------
private AudioFileFormat getCOMM(InputStream is, boolean doReset) throws UnsupportedAudioFileException, IOException {
    DataInputStream dis = new DataInputStream(is);
    if (doReset) {
        dis.mark(MAX_READ_LENGTH);
    }
    // assumes a stream at the beginning of the file which has already
    // passed the magic number test...
    // leaves the input stream at the beginning of the audio data
    int fileRead = 0;
    int dataLength = 0;
    AudioFormat format = null;
    // Read the magic number
    int magic = dis.readInt();
    // $$fb: fix for 4369044: javax.sound.sampled.AudioSystem.getAudioInputStream() works wrong with Cp037
    if (magic != AiffFileFormat.AIFF_MAGIC) {
        // not AIFF, throw exception
        if (doReset) {
            dis.reset();
        }
        throw new UnsupportedAudioFileException("not an AIFF file");
    }
    int length = dis.readInt();
    int iffType = dis.readInt();
    fileRead += 12;
    int totallength;
    if (length <= 0) {
        length = AudioSystem.NOT_SPECIFIED;
        totallength = AudioSystem.NOT_SPECIFIED;
    } else {
        totallength = length + 8;
    }
    // Is this an AIFC or just plain AIFF file.
    boolean aifc = false;
    // $$fb: fix for 4369044: javax.sound.sampled.AudioSystem.getAudioInputStream() works wrong with Cp037
    if (iffType == AiffFileFormat.AIFC_MAGIC) {
        aifc = true;
    }
    // Loop through the AIFF chunks until
    // we get to the SSND chunk.
    boolean ssndFound = false;
    while (!ssndFound) {
        // Read the chunk name
        int chunkName = dis.readInt();
        int chunkLen = dis.readInt();
        fileRead += 8;
        int chunkRead = 0;
        // Switch on the chunk name.
        switch(chunkName) {
            case AiffFileFormat.FVER_MAGIC:
                // Ignore format version for now.
                break;
            case AiffFileFormat.COMM_MAGIC:
                // $$fb: fix for 4399551: Repost of bug candidate: cannot replay aif file (Review ID: 108108)
                if ((!aifc && chunkLen < 18) || (aifc && chunkLen < 22)) {
                    throw new UnsupportedAudioFileException("Invalid AIFF/COMM chunksize");
                }
                // Read header info.
                int channels = dis.readUnsignedShort();
                if (channels <= 0) {
                    throw new UnsupportedAudioFileException("Invalid number of channels");
                }
                // numSampleFrames
                dis.readInt();
                int sampleSizeInBits = dis.readUnsignedShort();
                if (sampleSizeInBits < 1 || sampleSizeInBits > 32) {
                    throw new UnsupportedAudioFileException("Invalid AIFF/COMM sampleSize");
                }
                float sampleRate = (float) read_ieee_extended(dis);
                chunkRead += (2 + 4 + 2 + 10);
                // If this is not AIFC then we assume it's
                // a linearly encoded file.
                AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
                if (aifc) {
                    int enc = dis.readInt();
                    chunkRead += 4;
                    switch(enc) {
                        case AiffFileFormat.AIFC_PCM:
                            encoding = AudioFormat.Encoding.PCM_SIGNED;
                            break;
                        case AiffFileFormat.AIFC_ULAW:
                            encoding = AudioFormat.Encoding.ULAW;
                            // Java Sound convention
                            sampleSizeInBits = 8;
                            break;
                        default:
                            throw new UnsupportedAudioFileException("Invalid AIFF encoding");
                    }
                }
                int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
                //$fb what's that ??
                //if (sampleSizeInBits == 8) {
                //    encoding = AudioFormat.Encoding.PCM_SIGNED;
                //}
                format = new AudioFormat(encoding, sampleRate, sampleSizeInBits, channels, frameSize, sampleRate, true);
                break;
            case AiffFileFormat.SSND_MAGIC:
                // Data chunk.
                // we are getting *weird* numbers for chunkLen sometimes;
                // this really should be the size of the data chunk....
                int dataOffset = dis.readInt();
                int blocksize = dis.readInt();
                chunkRead += 8;
                if (chunkLen < length) {
                    dataLength = chunkLen - chunkRead;
                } else {
                    // $$kk: 11.03.98: this seems dangerous!
                    dataLength = length - (fileRead + chunkRead);
                }
                ssndFound = true;
                break;
        }
        // switch
        fileRead += chunkRead;
        // skip the remainder of this chunk
        if (!ssndFound) {
            int toSkip = chunkLen - chunkRead;
            if (toSkip > 0) {
                fileRead += dis.skipBytes(toSkip);
            }
        }
    }
    if (format == null) {
        throw new UnsupportedAudioFileException("missing COMM chunk");
    }
    AudioFileFormat.Type type = aifc ? AudioFileFormat.Type.AIFC : AudioFileFormat.Type.AIFF;
    return new AiffFileFormat(type, totallength, format, dataLength / format.getFrameSize());
}
Also used : UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) DataInputStream(java.io.DataInputStream) AudioFormat(javax.sound.sampled.AudioFormat) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 10 with AudioFileFormat

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

the class AuFileReader method getAudioFileFormat.

/**
     * Obtains the audio file format of the URL provided.  The URL must
     * point to valid audio file data.
     * @param url the URL from which file format information should be
     * extracted
     * @return an <code>AudioFileFormat</code> object describing the audio file format
     * @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 AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException {
    InputStream urlStream = null;
    BufferedInputStream bis = null;
    AudioFileFormat fileFormat = null;
    AudioFormat format = null;
    // throws IOException
    urlStream = url.openStream();
    try {
        bis = new BufferedInputStream(urlStream, bisBufferSize);
        // throws UnsupportedAudioFileException
        fileFormat = getAudioFileFormat(bis);
    } finally {
        urlStream.close();
    }
    return fileFormat;
}
Also used : 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) AudioFormat(javax.sound.sampled.AudioFormat) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Aggregations

AudioFileFormat (javax.sound.sampled.AudioFileFormat)32 AudioInputStream (javax.sound.sampled.AudioInputStream)20 FileInputStream (java.io.FileInputStream)18 InputStream (java.io.InputStream)13 BufferedInputStream (java.io.BufferedInputStream)11 AudioFormat (javax.sound.sampled.AudioFormat)10 DataInputStream (java.io.DataInputStream)9 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)8 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 PushbackInputStream (java.io.PushbackInputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Type (javax.sound.sampled.AudioFileFormat.Type)1 LineUnavailableException (javax.sound.sampled.LineUnavailableException)1 Mixer (javax.sound.sampled.Mixer)1