src/java.desktop/share/classes/com/sun/media/sound/WaveFileWriter.java
author serb
Sun, 10 Dec 2017 00:08:42 -0800
changeset 48282 4483880d8811
parent 47216 71c04702a3d5
child 52248 2e330da7cbf4
permissions -rw-r--r--
8178401: Various audio files writers do not close file streams properly Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48282
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2017, 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
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
    28
import java.io.BufferedOutputStream;
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
    29
import java.io.ByteArrayInputStream;
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
    30
import java.io.ByteArrayOutputStream;
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
    31
import java.io.DataOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
    33
import java.io.FileOutputStream;
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
    34
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.RandomAccessFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.SequenceInputStream;
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
    39
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.sound.sampled.AudioFileFormat;
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
    42
import javax.sound.sampled.AudioFormat;
2
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.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
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    53
public final class WaveFileWriter extends SunFileWriter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Constructs a new WaveFileWriter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public WaveFileWriter() {
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    59
        super(new AudioFileFormat.Type[]{AudioFileFormat.Type.WAVE});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
    62
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        System.arraycopy(types, 0, filetypes, 0, types.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // make sure we can write this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        AudioFormat format = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        AudioFormat.Encoding encoding = format.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if( AudioFormat.Encoding.ALAW.equals(encoding) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            AudioFormat.Encoding.ULAW.equals(encoding) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            AudioFormat.Encoding.PCM_SIGNED.equals(encoding) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return filetypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return new AudioFileFormat.Type[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
    83
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
    85
        Objects.requireNonNull(stream);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
    86
        Objects.requireNonNull(fileType);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
    87
        Objects.requireNonNull(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        //$$fb the following check must come first ! Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // the next frame length check may throw an IOException and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // interrupt iterating File Writers. (see bug 4351296)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        //$$fb when we got this far, we are committed to write this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // we must know the total data length to calculate the file length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new IOException("stream length not specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 35686
diff changeset
   103
        return writeWaveFile(stream, waveFileFormat, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
   106
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
   108
        Objects.requireNonNull(stream);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
   109
        Objects.requireNonNull(fileType);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 25859
diff changeset
   110
        Objects.requireNonNull(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // first write the file without worrying about length fields
48282
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   116
        final int bytesWritten;
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   117
        try (final FileOutputStream fos = new FileOutputStream(out);
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   118
             final BufferedOutputStream bos = new BufferedOutputStream(fos)) {
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   119
            bytesWritten = writeWaveFile(stream, waveFileFormat, bos);
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   120
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // now, if length fields were not specified, calculate them,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // open as a random access file, write the appropriate fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // close again....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            int dataLength=bytesWritten-waveFileFormat.getHeaderSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            int riffLength=dataLength + waveFileFormat.getHeaderSize() - 8;
48282
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   129
            try (final RandomAccessFile raf = new RandomAccessFile(out, "rw")) {
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   130
                // skip RIFF magic
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   131
                raf.skipBytes(4);
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   132
                raf.writeInt(big2little(riffLength));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   133
                // skip WAVE magic, fmt_ magic, fmt_ length, fmt_ chunk, data magic
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   134
                raf.skipBytes(4 + 4 + 4 + WaveFileFormat.getFmtChunkSize(
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   135
                        waveFileFormat.getWaveType()) + 4);
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   136
                raf.writeInt(big2little(dataLength));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   137
                // that's all
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   138
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Throws IllegalArgumentException if not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) {
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
   151
        if (!isFileTypeSupported(type, stream)) {
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
   152
            throw new IllegalArgumentException("File type " + type + " not supported.");
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 34412
diff changeset
   153
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        AudioFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        WaveFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        AudioFormat streamFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        float sampleRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int sampleSizeInBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        float frameRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int fileSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int waveType = WaveFileFormat.WAVE_FORMAT_PCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if( AudioFormat.Encoding.ALAW.equals(streamEncoding) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            AudioFormat.Encoding.ULAW.equals(streamEncoding) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            encoding = streamEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            sampleSizeInBits = streamFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (streamEncoding.equals(AudioFormat.Encoding.ALAW)) {
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 35686
diff changeset
   176
                waveType = WaveFileFormat.WAVE_FORMAT_ALAW;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } else {
35687
78e8e8fea09b 8147407: Provide support of WaveExtensible sound format
serb
parents: 35686
diff changeset
   178
                waveType = WaveFileFormat.WAVE_FORMAT_MULAW;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } else if ( streamFormat.getSampleSizeInBits()==8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            encoding = AudioFormat.Encoding.PCM_UNSIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            sampleSizeInBits=8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            sampleSizeInBits=streamFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        format = new AudioFormat( encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                  streamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                  sampleSizeInBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                  streamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                  streamFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                  streamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                  false);       // WAVE is little endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if( stream.getFrameLength()!=AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                + WaveFileFormat.getHeaderSize(waveType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            fileSize = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        fileFormat = new WaveFileFormat( AudioFileFormat.Type.WAVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                         fileSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                         format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                         (int)stream.getFrameLength() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return fileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private int writeWaveFile(InputStream in, WaveFileFormat waveFileFormat, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        InputStream fileStream = getFileStream(waveFileFormat, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        byte buffer[] = new byte[bisBufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int maxLength = waveFileFormat.getByteLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (maxLength>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if( bytesRead < maxLength ) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   225
                    out.write( buffer, 0, bytesRead );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    maxLength -= bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                } else {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   229
                    out.write( buffer, 0, maxLength );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    bytesWritten += maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    maxLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            } else {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   235
                out.write( buffer, 0, bytesRead );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private InputStream getFileStream(WaveFileFormat waveFileFormat, InputStream audioStream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // private method ... assumes audioFileFormat is a supported file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // WAVE header fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        AudioFormat audioFormat = waveFileFormat.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int headerLength       = waveFileFormat.getHeaderSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        int riffMagic          = WaveFileFormat.RIFF_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        int waveMagic          = WaveFileFormat.WAVE_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int fmtMagic           = WaveFileFormat.FMT_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int fmtLength          = WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        short wav_type         = (short) waveFileFormat.getWaveType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        short channels         = (short) audioFormat.getChannels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        short sampleSizeInBits = (short) audioFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int sampleRate         = (int) audioFormat.getSampleRate();
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   257
        int frameSizeInBytes   = audioFormat.getFrameSize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int frameRate              = (int) audioFormat.getFrameRate();
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 35687
diff changeset
   259
        int avgBytesPerSec     = channels * sampleSizeInBits * sampleRate / 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        short blockAlign       = (short) ((sampleSizeInBits / 8) * channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int dataMagic              = WaveFileFormat.DATA_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int dataLength             = waveFileFormat.getFrameLength() * frameSizeInBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        int length                         = waveFileFormat.getByteLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int riffLength = dataLength + headerLength - 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        AudioFormat audioStreamFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        AudioFormat.Encoding encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        InputStream codedAudioStream = audioStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // if audioStream is an AudioInputStream and we need to convert, do it here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if(audioStream instanceof AudioInputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            audioStreamFormat = ((AudioInputStream)audioStream).getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            encoding = audioStreamFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if(AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                if( sampleSizeInBits==8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    // plug in the transcoder to convert from PCM_SIGNED to PCM_UNSIGNED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                                                                        AudioFormat.Encoding.PCM_UNSIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                                                                        audioStreamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                                                                        audioStreamFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                                                                        audioStreamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                                                                        audioStreamFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                                                                        audioStreamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                                                                        false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                                                        (AudioInputStream)audioStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if( (AudioFormat.Encoding.PCM_SIGNED.equals(encoding) && audioStreamFormat.isBigEndian()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) && !audioStreamFormat.isBigEndian()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) && audioStreamFormat.isBigEndian()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                if( sampleSizeInBits!=8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    // plug in the transcoder to convert to PCM_SIGNED_LITTLE_ENDIAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                                                                        AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                                                                        audioStreamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                                                                        audioStreamFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                                                                        audioStreamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                                                                        audioStreamFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                                                                        audioStreamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                                                                        false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                                                        (AudioInputStream)audioStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // Now push the header into a stream, concat, and return the new SequenceInputStream
48282
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   312
        final byte[] header;
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   313
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   314
             final DataOutputStream dos = new DataOutputStream(baos)) {
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   315
            // we write in littleendian...
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   316
            dos.writeInt(riffMagic);
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   317
            dos.writeInt(big2little(riffLength));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   318
            dos.writeInt(waveMagic);
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   319
            dos.writeInt(fmtMagic);
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   320
            dos.writeInt(big2little(fmtLength));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   321
            dos.writeShort(big2littleShort(wav_type));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   322
            dos.writeShort(big2littleShort(channels));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   323
            dos.writeInt(big2little(sampleRate));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   324
            dos.writeInt(big2little(avgBytesPerSec));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   325
            dos.writeShort(big2littleShort(blockAlign));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   326
            dos.writeShort(big2littleShort(sampleSizeInBits));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   327
            //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   328
            if (wav_type != WaveFileFormat.WAVE_FORMAT_PCM) {
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   329
                // add length 0 for "codec specific data length"
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   330
                dos.writeShort(0);
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   331
            }
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   332
            dos.writeInt(dataMagic);
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   333
            dos.writeInt(big2little(dataLength));
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   334
            header = baos.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
48282
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   336
        return new SequenceInputStream(new ByteArrayInputStream(header),
4483880d8811 8178401: Various audio files writers do not close file streams properly
serb
parents: 47216
diff changeset
   337
                                       new NoCloseInputStream(codedAudioStream));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
}