jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileFormat.java
author serb
Thu, 18 Feb 2016 22:11:29 +0300
changeset 36454 d2853d1fc614
parent 35687 78e8e8fea09b
child 38399 bd91ce346b5b
permissions -rw-r--r--
8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV Reviewed-by: prr, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 25859
diff changeset
     2
 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
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: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.sound.sampled.AudioFileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.sound.sampled.AudioFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * WAVE file format class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    36
final class WaveFileFormat extends AudioFileFormat {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * Wave format type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    41
    private final int waveType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    //$$fb 2001-07-13: added management of header size in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final int STANDARD_HEADER_SIZE = 28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * fmt_ chunk size in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int STANDARD_FMT_CHUNK_SIZE = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // magic numbers
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 25859
diff changeset
    54
    static final int RIFF_MAGIC = 1380533830;
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 25859
diff changeset
    55
    static final int WAVE_MAGIC = 1463899717;
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 25859
diff changeset
    56
    static final int FMT_MAGIC  = 0x666d7420; // "fmt "
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 25859
diff changeset
    57
    static final int DATA_MAGIC = 0x64617461; // "data"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // encodings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final int WAVE_FORMAT_UNKNOWN   = 0x0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final int WAVE_FORMAT_PCM       = 0x0001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final int WAVE_FORMAT_ADPCM     = 0x0002;
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 25859
diff changeset
    63
    static final int WAVE_FORMAT_IEEE_FLOAT= 0x0003;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final int WAVE_FORMAT_ALAW      = 0x0006;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final int WAVE_FORMAT_MULAW     = 0x0007;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final int WAVE_FORMAT_OKI_ADPCM = 0x0010;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final int WAVE_FORMAT_DIGISTD   = 0x0015;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static final int WAVE_FORMAT_DIGIFIX   = 0x0016;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final int WAVE_IBM_FORMAT_MULAW = 0x0101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static final int WAVE_IBM_FORMAT_ALAW  = 0x0102;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static final int WAVE_IBM_FORMAT_ADPCM = 0x0103;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static final int WAVE_FORMAT_DVI_ADPCM = 0x0011;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static final int WAVE_FORMAT_SX7383    = 0x1C07;
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 25859
diff changeset
    74
    static final int WAVE_FORMAT_EXTENSIBLE= 0xFFFE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    WaveFileFormat(AudioFileFormat.Type type, int lengthInBytes, AudioFormat format, int lengthInFrames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        super(type,lengthInBytes,format,lengthInFrames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        AudioFormat.Encoding encoding = format.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if( encoding.equals(AudioFormat.Encoding.ALAW) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            waveType = WAVE_FORMAT_ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } else if( encoding.equals(AudioFormat.Encoding.ULAW) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            waveType = WAVE_FORMAT_MULAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } else if( encoding.equals(AudioFormat.Encoding.PCM_SIGNED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                   encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            waveType = WAVE_FORMAT_PCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            waveType = WAVE_FORMAT_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    int getWaveType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return waveType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    int getHeaderSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return getHeaderSize(getWaveType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static int getHeaderSize(int waveType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // use dynamic format chunk size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return STANDARD_HEADER_SIZE + getFmtChunkSize(waveType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static int getFmtChunkSize(int waveType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // add 2 bytes for "codec specific data length" for non-PCM codecs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int result = STANDARD_FMT_CHUNK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (waveType != WAVE_FORMAT_PCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            result += 2; // WORD for "codec specific data length"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}