Search in sources :

Example 1 with Type

use of javax.sound.sampled.AudioFileFormat.Type 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

BufferedInputStream (java.io.BufferedInputStream)1 AudioFileFormat (javax.sound.sampled.AudioFileFormat)1 Type (javax.sound.sampled.AudioFileFormat.Type)1 AudioFormat (javax.sound.sampled.AudioFormat)1 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)1 MediaType (org.apache.tika.mime.MediaType)1 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)1