jdk/src/share/classes/com/sun/media/sound/WaveFileFormat.java
author amenkov
Mon, 28 Feb 2011 18:36:33 +0300
changeset 8525 08f98f5a11df
parent 5506 202f599c92aa
child 18215 b2afd66ce6db
permissions -rw-r--r--
7013521: AudioSystem.write for AIFF files closes source audio stream Reviewed-by: dav
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2007, 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 java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.IllegalArgumentException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.SequenceInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.sound.sampled.AudioFileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.sound.sampled.AudioInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.sound.sampled.AudioFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * WAVE file format class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
class WaveFileFormat extends AudioFileFormat {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Wave format type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int waveType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    //$$fb 2001-07-13: added management of header size in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int STANDARD_HEADER_SIZE = 28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * fmt_ chunk size in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final int STANDARD_FMT_CHUNK_SIZE = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // magic numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static  final int RIFF_MAGIC         = 1380533830;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static  final int WAVE_MAGIC         = 1463899717;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static  final int FMT_MAGIC                  = 0x666d7420; // "fmt "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static  final int DATA_MAGIC                 = 0x64617461; // "data"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // encodings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static final int WAVE_FORMAT_UNKNOWN   = 0x0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static final int WAVE_FORMAT_PCM       = 0x0001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static final int WAVE_FORMAT_ADPCM     = 0x0002;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final int WAVE_FORMAT_ALAW      = 0x0006;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final int WAVE_FORMAT_MULAW     = 0x0007;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static final int WAVE_FORMAT_OKI_ADPCM = 0x0010;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static final int WAVE_FORMAT_DIGISTD   = 0x0015;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static final int WAVE_FORMAT_DIGIFIX   = 0x0016;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static final int WAVE_IBM_FORMAT_MULAW = 0x0101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static final int WAVE_IBM_FORMAT_ALAW  = 0x0102;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static final int WAVE_IBM_FORMAT_ADPCM = 0x0103;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static final int WAVE_FORMAT_DVI_ADPCM = 0x0011;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final int WAVE_FORMAT_SX7383    = 0x1C07;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    WaveFileFormat( AudioFileFormat aff ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this( aff.getType(), aff.getByteLength(), aff.getFormat(), aff.getFrameLength() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    WaveFileFormat(AudioFileFormat.Type type, int lengthInBytes, AudioFormat format, int lengthInFrames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        super(type,lengthInBytes,format,lengthInFrames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        AudioFormat.Encoding encoding = format.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if( encoding.equals(AudioFormat.Encoding.ALAW) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            waveType = WAVE_FORMAT_ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else if( encoding.equals(AudioFormat.Encoding.ULAW) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            waveType = WAVE_FORMAT_MULAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } else if( encoding.equals(AudioFormat.Encoding.PCM_SIGNED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                   encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            waveType = WAVE_FORMAT_PCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            waveType = WAVE_FORMAT_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    int getWaveType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return waveType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    int getHeaderSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return getHeaderSize(getWaveType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static int getHeaderSize(int waveType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // use dynamic format chunk size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return STANDARD_HEADER_SIZE + getFmtChunkSize(waveType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static int getFmtChunkSize(int waveType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // add 2 bytes for "codec specific data length" for non-PCM codecs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int result = STANDARD_FMT_CHUNK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (waveType != WAVE_FORMAT_PCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            result += 2; // WORD for "codec specific data length"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}