Search in sources :

Example 11 with AudioFileFormat

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

the class WaveFileReader 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 = getFMT(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 12 with AudioFileFormat

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

the class WaveFileReader 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 = getFMT(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 13 with AudioFileFormat

use of javax.sound.sampled.AudioFileFormat in project JMRI by JMRI.

the class SoundUtil method bufferFromFile.

public static byte[] bufferFromFile(String filename, float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian) throws java.io.IOException, javax.sound.sampled.UnsupportedAudioFileException {
    File sourceFile = new File(filename);
    // Get the type of the source file. We need this information 
    // later to write the audio data to a file of the same type. 
    AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(sourceFile);
    //AudioFileFormat.Type targetFileType = fileFormat.getType(); 
    AudioFormat audioFormat = fileFormat.getFormat();
    // get desired output format
    AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    // get a conversion stream
    // (Errors not checked yet)
    AudioInputStream stream = AudioSystem.getAudioInputStream(sourceFile);
    AudioInputStream inputAIS = AudioSystem.getAudioInputStream(format, stream);
    // Read the audio data into a memory buffer. 
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int nBufferSize = BUFFER_LENGTH * audioFormat.getFrameSize();
    byte[] abBuffer = new byte[nBufferSize];
    while (true) {
        if (log.isDebugEnabled()) {
            log.debug("trying to read (bytes): " + abBuffer.length);
        }
        int nBytesRead = inputAIS.read(abBuffer);
        if (log.isDebugEnabled()) {
            log.debug("read (bytes): " + nBytesRead);
        }
        if (nBytesRead == -1) {
            break;
        }
        baos.write(abBuffer, 0, nBytesRead);
    }
    // Create byte array
    byte[] abAudioData = baos.toByteArray();
    return abAudioData;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AudioFormat(javax.sound.sampled.AudioFormat) File(java.io.File) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 14 with AudioFileFormat

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

the class WaveFloatFileReader method getAudioInputStream.

public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException {
    AudioFileFormat format = getAudioFileFormat(stream);
    RIFFReader riffiterator = new RIFFReader(stream);
    if (!riffiterator.getFormat().equals("RIFF"))
        throw new UnsupportedAudioFileException();
    if (!riffiterator.getType().equals("WAVE"))
        throw new UnsupportedAudioFileException();
    while (riffiterator.hasNextChunk()) {
        RIFFReader chunk = riffiterator.nextChunk();
        if (chunk.getFormat().equals("data")) {
            return new AudioInputStream(chunk, format.getFormat(), chunk.getSize());
        }
    }
    throw new UnsupportedAudioFileException();
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 15 with AudioFileFormat

use of javax.sound.sampled.AudioFileFormat in project tika by apache.

the class AudioParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    // AudioSystem expects the stream to support the mark feature
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }
    try {
        AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(stream);
        Type type = fileFormat.getType();
        if (type == Type.AIFC || type == Type.AIFF) {
            metadata.set(Metadata.CONTENT_TYPE, "audio/x-aiff");
        } else if (type == Type.AU || type == Type.SND) {
            metadata.set(Metadata.CONTENT_TYPE, "audio/basic");
        } else if (type == Type.WAVE) {
            metadata.set(Metadata.CONTENT_TYPE, "audio/x-wav");
        }
        AudioFormat audioFormat = fileFormat.getFormat();
        int channels = audioFormat.getChannels();
        if (channels != AudioSystem.NOT_SPECIFIED) {
            metadata.set("channels", String.valueOf(channels));
        // TODO: Use XMPDM.TRACKS? (see also frame rate in AudioFormat)
        }
        float rate = audioFormat.getSampleRate();
        if (rate != AudioSystem.NOT_SPECIFIED) {
            metadata.set("samplerate", String.valueOf(rate));
            metadata.set(XMPDM.AUDIO_SAMPLE_RATE, Integer.toString((int) rate));
        }
        int bits = audioFormat.getSampleSizeInBits();
        if (bits != AudioSystem.NOT_SPECIFIED) {
            metadata.set("bits", String.valueOf(bits));
            if (bits == 8) {
                metadata.set(XMPDM.AUDIO_SAMPLE_TYPE, "8Int");
            } else if (bits == 16) {
                metadata.set(XMPDM.AUDIO_SAMPLE_TYPE, "16Int");
            } else if (bits == 32) {
                metadata.set(XMPDM.AUDIO_SAMPLE_TYPE, "32Int");
            }
        }
        metadata.set("encoding", audioFormat.getEncoding().toString());
        // Javadoc suggests that some of the following properties might
        // be available, but I had no success in finding any:
        // "duration" Long playback duration of the file in microseconds
        // "author" String name of the author of this file
        // "title" String title of this file
        // "copyright" String copyright message
        // "date" Date date of the recording or release
        // "comment" String an arbitrary text
        addMetadata(metadata, fileFormat.properties());
        addMetadata(metadata, audioFormat.properties());
    } catch (UnsupportedAudioFileException e) {
    // There is no way to know whether this exception was
    // caused by the document being corrupted or by the format
    // just being unsupported. So we do nothing.
    }
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    xhtml.endDocument();
}
Also used : UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) MediaType(org.apache.tika.mime.MediaType) Type(javax.sound.sampled.AudioFileFormat.Type) BufferedInputStream(java.io.BufferedInputStream) AudioFormat(javax.sound.sampled.AudioFormat) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) 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