jdk/src/share/classes/com/sun/media/sound/WaveFileWriter.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8525 08f98f5a11df
child 18215 b2afd66ce6db
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8525
diff changeset
     2
 * Copyright (c) 1999, 2011, 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.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.RandomAccessFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.SequenceInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.sound.sampled.AudioFileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.sound.sampled.AudioInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.sound.sampled.AudioFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
//$$fb this class is buggy. Should be replaced in future.
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 writer.
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
public class WaveFileWriter extends SunFileWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // magic numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static  final int RIFF_MAGIC = 1380533830;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static  final int WAVE_MAGIC = 1463899717;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static  final int FMT_MAGIC  = 0x666d7420; // "fmt "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static  final int DATA_MAGIC = 0x64617461; // "data"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // encodings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final int WAVE_FORMAT_UNKNOWN   = 0x0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final int WAVE_FORMAT_PCM       = 0x0001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final int WAVE_FORMAT_ADPCM     = 0x0002;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final int WAVE_FORMAT_ALAW      = 0x0006;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final int WAVE_FORMAT_MULAW     = 0x0007;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final int WAVE_FORMAT_OKI_ADPCM = 0x0010;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static final int WAVE_FORMAT_DIGISTD   = 0x0015;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final int WAVE_FORMAT_DIGIFIX   = 0x0016;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static final int WAVE_IBM_FORMAT_MULAW = 0x0101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static final int WAVE_IBM_FORMAT_ALAW  = 0x0102;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static final int WAVE_IBM_FORMAT_ADPCM = 0x0103;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static final int WAVE_FORMAT_DVI_ADPCM = 0x0011;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final int WAVE_FORMAT_SX7383    = 0x1C07;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * WAVE type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final AudioFileFormat.Type waveTypes[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        AudioFileFormat.Type.WAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Constructs a new WaveFileWriter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public WaveFileWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super(waveTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // METHODS TO IMPLEMENT AudioFileWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.arraycopy(types, 0, filetypes, 0, types.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // make sure we can write this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        AudioFormat format = stream.getFormat();
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( AudioFormat.Encoding.ALAW.equals(encoding) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            AudioFormat.Encoding.ULAW.equals(encoding) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            AudioFormat.Encoding.PCM_SIGNED.equals(encoding) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return filetypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return new AudioFileFormat.Type[0];
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
    public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        //$$fb the following check must come first ! Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // the next frame length check may throw an IOException and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // interrupt iterating File Writers. (see bug 4351296)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        //$$fb when we got this far, we are committed to write this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // we must know the total data length to calculate the file length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IOException("stream length not specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int bytesWritten = writeWaveFile(stream, waveFileFormat, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // first write the file without worrying about length fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int bytesWritten = writeWaveFile(stream, waveFileFormat, bos );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        bos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // now, if length fields were not specified, calculate them,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // open as a random access file, write the appropriate fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // close again....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            int dataLength=bytesWritten-waveFileFormat.getHeaderSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            int riffLength=dataLength + waveFileFormat.getHeaderSize() - 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            RandomAccessFile raf=new RandomAccessFile(out, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            // skip RIFF magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            raf.skipBytes(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            raf.writeInt(big2little( riffLength ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // skip WAVE magic, fmt_ magic, fmt_ length, fmt_ chunk, data magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            raf.skipBytes(4+4+4+WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType())+4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            raf.writeInt(big2little( dataLength ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // that's all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Throws IllegalArgumentException if not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        AudioFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        WaveFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        AudioFormat streamFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        float sampleRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int sampleSizeInBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        float frameRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int fileSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (!types[0].equals(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new IllegalArgumentException("File type " + type + " not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int waveType = WaveFileFormat.WAVE_FORMAT_PCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if( AudioFormat.Encoding.ALAW.equals(streamEncoding) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            AudioFormat.Encoding.ULAW.equals(streamEncoding) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            encoding = streamEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            sampleSizeInBits = streamFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (streamEncoding.equals(AudioFormat.Encoding.ALAW)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                waveType = WAVE_FORMAT_ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                waveType = WAVE_FORMAT_MULAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } else if ( streamFormat.getSampleSizeInBits()==8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            encoding = AudioFormat.Encoding.PCM_UNSIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            sampleSizeInBits=8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            sampleSizeInBits=streamFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        format = new AudioFormat( encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                  streamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                  sampleSizeInBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                  streamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                  streamFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                  streamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                  false);       // WAVE is little endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if( stream.getFrameLength()!=AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                + WaveFileFormat.getHeaderSize(waveType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            fileSize = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        fileFormat = new WaveFileFormat( AudioFileFormat.Type.WAVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                         fileSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                         format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                         (int)stream.getFrameLength() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return fileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private int writeWaveFile(InputStream in, WaveFileFormat waveFileFormat, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        InputStream fileStream = getFileStream(waveFileFormat, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        byte buffer[] = new byte[bisBufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        int maxLength = waveFileFormat.getByteLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (maxLength>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if( bytesRead < maxLength ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    out.write( buffer, 0, (int)bytesRead );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    maxLength -= bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    out.write( buffer, 0, (int)maxLength );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    bytesWritten += maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    maxLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                out.write( buffer, 0, (int)bytesRead );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private InputStream getFileStream(WaveFileFormat waveFileFormat, InputStream audioStream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // private method ... assumes audioFileFormat is a supported file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // WAVE header fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        AudioFormat audioFormat = waveFileFormat.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int headerLength       = waveFileFormat.getHeaderSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int riffMagic          = WaveFileFormat.RIFF_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int waveMagic          = WaveFileFormat.WAVE_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int fmtMagic           = WaveFileFormat.FMT_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int fmtLength          = WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        short wav_type         = (short) waveFileFormat.getWaveType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        short channels         = (short) audioFormat.getChannels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        short sampleSizeInBits = (short) audioFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        int sampleRate         = (int) audioFormat.getSampleRate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int frameSizeInBytes   = (int) audioFormat.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int frameRate              = (int) audioFormat.getFrameRate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        int avgBytesPerSec     = channels * sampleSizeInBits * sampleRate / 8;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        short blockAlign       = (short) ((sampleSizeInBits / 8) * channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int dataMagic              = WaveFileFormat.DATA_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        int dataLength             = waveFileFormat.getFrameLength() * frameSizeInBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int length                         = waveFileFormat.getByteLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        int riffLength = dataLength + headerLength - 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        byte header[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        ByteArrayInputStream headerStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        ByteArrayOutputStream baos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        DataOutputStream dos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        SequenceInputStream waveStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        AudioFormat audioStreamFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        AudioFormat.Encoding encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        InputStream codedAudioStream = audioStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // if audioStream is an AudioInputStream and we need to convert, do it here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if(audioStream instanceof AudioInputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            audioStreamFormat = ((AudioInputStream)audioStream).getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            encoding = audioStreamFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if(AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                if( sampleSizeInBits==8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    // plug in the transcoder to convert from PCM_SIGNED to PCM_UNSIGNED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                                                                        AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                                                                        audioStreamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                                                                        audioStreamFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                                                                        audioStreamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                                                                        audioStreamFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                                                                        audioStreamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                                                                        false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                                                        (AudioInputStream)audioStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if( (AudioFormat.Encoding.PCM_SIGNED.equals(encoding) && audioStreamFormat.isBigEndian()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) && !audioStreamFormat.isBigEndian()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) && audioStreamFormat.isBigEndian()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if( sampleSizeInBits!=8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    // plug in the transcoder to convert to PCM_SIGNED_LITTLE_ENDIAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                                                                        AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                                                                        audioStreamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                                                                        audioStreamFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                                                                        audioStreamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                                                                        audioStreamFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                                                                        audioStreamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                                                                        false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                                                        (AudioInputStream)audioStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // Now push the header into a stream, concat, and return the new SequenceInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        dos = new DataOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // we write in littleendian...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        dos.writeInt(riffMagic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        dos.writeInt(big2little( riffLength ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        dos.writeInt(waveMagic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        dos.writeInt(fmtMagic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        dos.writeInt(big2little(fmtLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        dos.writeShort(big2littleShort(wav_type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        dos.writeShort(big2littleShort(channels));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        dos.writeInt(big2little(sampleRate));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        dos.writeInt(big2little(avgBytesPerSec));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        dos.writeShort(big2littleShort(blockAlign));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        dos.writeShort(big2littleShort(sampleSizeInBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (wav_type != WaveFileFormat.WAVE_FORMAT_PCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            // add length 0 for "codec specific data length"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            dos.writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        dos.writeInt(dataMagic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        dos.writeInt(big2little(dataLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        header = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        headerStream = new ByteArrayInputStream( header );
8525
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   372
        waveStream = new SequenceInputStream(headerStream,
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   373
                            new NoCloseInputStream(codedAudioStream));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return (InputStream)waveStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
}