jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFloatFileReader.java
author serb
Thu, 14 Jan 2016 23:14:01 +0300
changeset 35687 78e8e8fea09b
parent 32121 93ad23865c9e
child 36454 d2853d1fc614
permissions -rw-r--r--
8147407: Provide support of WaveExtensible sound format Reviewed-by: prr, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     1
/*
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 32121
diff changeset
     2
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     4
 *
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1846
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1846
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    10
 *
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    15
 * accompanied this code).
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    16
 *
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1846
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1846
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1846
diff changeset
    23
 * questions.
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    24
 */
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    25
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    26
package com.sun.media.sound;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    27
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    28
import java.io.IOException;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    29
import java.io.InputStream;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    30
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    31
import javax.sound.sampled.AudioFileFormat;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    32
import javax.sound.sampled.AudioFormat;
6505
b1faae5e44a3 6944033: RFE: add PCM_FLOAT support
amenkov
parents: 5506
diff changeset
    33
import javax.sound.sampled.AudioFormat.Encoding;
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    34
import javax.sound.sampled.AudioInputStream;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    35
import javax.sound.sampled.AudioSystem;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    36
import javax.sound.sampled.UnsupportedAudioFileException;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    37
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    38
/**
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    39
 * Floating-point encoded (format 3) WAVE file loader.
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    40
 *
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    41
 * @author Karl Helgason
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    42
 */
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    43
public final class WaveFloatFileReader extends SunFileReader {
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    44
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    45
    @Override
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    46
    AudioFileFormat getAudioFileFormatImpl(final InputStream stream)
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    47
            throws UnsupportedAudioFileException, IOException {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    48
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    49
        RIFFReader riffiterator = new RIFFReader(stream);
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    50
        if (!riffiterator.getFormat().equals("RIFF"))
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    51
            throw new UnsupportedAudioFileException();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    52
        if (!riffiterator.getType().equals("WAVE"))
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    53
            throw new UnsupportedAudioFileException();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    54
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    55
        boolean fmt_found = false;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    56
        boolean data_found = false;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    57
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    58
        int channels = 1;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    59
        long samplerate = 1;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    60
        int framesize = 1;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    61
        int bits = 1;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    62
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    63
        while (riffiterator.hasNextChunk()) {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    64
            RIFFReader chunk = riffiterator.nextChunk();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    65
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    66
            if (chunk.getFormat().equals("fmt ")) {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    67
                fmt_found = true;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    68
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    69
                int format = chunk.readUnsignedShort();
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 32121
diff changeset
    70
                if (format != WaveFileFormat.WAVE_FORMAT_IEEE_FLOAT) {
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    71
                    throw new UnsupportedAudioFileException();
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 32121
diff changeset
    72
                }
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    73
                channels = chunk.readUnsignedShort();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    74
                samplerate = chunk.readUnsignedInt();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    75
                /* framerate = */chunk.readUnsignedInt();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    76
                framesize = chunk.readUnsignedShort();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    77
                bits = chunk.readUnsignedShort();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    78
            }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    79
            if (chunk.getFormat().equals("data")) {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    80
                data_found = true;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    81
                break;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    82
            }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    83
        }
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    84
        if (!fmt_found || !data_found) {
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    85
            throw new UnsupportedAudioFileException();
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    86
        }
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    87
        AudioFormat audioformat = new AudioFormat(
6505
b1faae5e44a3 6944033: RFE: add PCM_FLOAT support
amenkov
parents: 5506
diff changeset
    88
                Encoding.PCM_FLOAT, samplerate, bits, channels,
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    89
                framesize, samplerate, false);
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    90
        return new AudioFileFormat(AudioFileFormat.Type.WAVE, audioformat,
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    91
                                   AudioSystem.NOT_SPECIFIED);
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    92
    }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    93
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    94
    @Override
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    95
    public AudioInputStream getAudioInputStream(final InputStream stream)
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    96
            throws UnsupportedAudioFileException, IOException {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    97
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    98
        AudioFileFormat format = getAudioFileFormat(stream);
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    99
        // we've got everything, the stream is supported and it is at the
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   100
        // beginning of the header, so find the data chunk again and return an
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   101
        // AudioInputStream
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   102
        RIFFReader riffiterator = new RIFFReader(stream);
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   103
        while (riffiterator.hasNextChunk()) {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   104
            RIFFReader chunk = riffiterator.nextChunk();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   105
            if (chunk.getFormat().equals("data")) {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   106
                return new AudioInputStream(chunk, format.getFormat(),
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   107
                        chunk.getSize());
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   108
            }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   109
        }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   110
        throw new UnsupportedAudioFileException();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   111
    }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
   112
}