jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java
author serb
Wed, 16 Mar 2016 15:55:27 +0300
changeset 36888 0905e6c2d1af
parent 35686 1c21a27682a5
permissions -rw-r--r--
8146239: Support of PCM_FLOAT for the AU file format Reviewed-by: amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35656
diff changeset
     2
 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    28
import java.io.BufferedOutputStream;
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    29
import java.io.ByteArrayInputStream;
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    30
import java.io.ByteArrayOutputStream;
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    31
import java.io.DataOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    33
import java.io.FileOutputStream;
e5582272e65b 8135088: Typo in AuFileReader
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: 32865
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;
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    42
import javax.sound.sampled.AudioFileFormat.Type;
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    43
import javax.sound.sampled.AudioFormat;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.sound.sampled.AudioInputStream;
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
 * AU file writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 9035
diff changeset
    52
public final class AuFileWriter extends SunFileWriter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    54
    /**
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    55
     * Value for length field if length is not known.
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    56
     */
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    57
    private static final int UNKNOWN_SIZE = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Constructs a new AuFileWriter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public AuFileWriter() {
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    63
        super(new Type[]{Type.AU});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    66
    @Override
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    67
    public Type[] getAudioFileTypes(AudioInputStream stream) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    69
        Type[] filetypes = new Type[types.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.arraycopy(types, 0, filetypes, 0, types.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // make sure we can write this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        AudioFormat format = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        AudioFormat.Encoding encoding = format.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    76
        if (AudioFormat.Encoding.ALAW.equals(encoding)
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    77
                || AudioFormat.Encoding.ULAW.equals(encoding)
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    78
                || AudioFormat.Encoding.PCM_SIGNED.equals(encoding)
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    79
                || AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    80
                || AudioFormat.Encoding.PCM_FLOAT.equals(encoding)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return filetypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    84
        return new Type[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
    87
    @Override
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
    88
    public int write(AudioInputStream stream, Type fileType, OutputStream out) throws IOException {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32865
diff changeset
    89
        Objects.requireNonNull(stream);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32865
diff changeset
    90
        Objects.requireNonNull(fileType);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32865
diff changeset
    91
        Objects.requireNonNull(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // we must know the total data length to calculate the file length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        //$$fb 2001-07-13: fix for bug 4351296: do not throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        //if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        //      throw new IOException("stream length not specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   101
        return writeAuFile(stream, auFileFormat, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   104
    @Override
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   105
    public int write(AudioInputStream stream, Type fileType, File out) throws IOException {
34412
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32865
diff changeset
   106
        Objects.requireNonNull(stream);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32865
diff changeset
   107
        Objects.requireNonNull(fileType);
bed825be8cd8 8135100: Behavior of null arguments not specified in javax.sound.sampled.spi
serb
parents: 32865
diff changeset
   108
        Objects.requireNonNull(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // first write the file without worrying about length fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int bytesWritten = writeAuFile(stream, auFileFormat, bos );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        bos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // now, if length fields were not specified, calculate them,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // open as a random access file, write the appropriate fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // close again....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // $$kk: 10.22.99: jan: please either implement this or throw an exception!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // $$fb: 2001-07-13: done. Fixes Bug 4479981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            RandomAccessFile raf=new RandomAccessFile(out, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (raf.length()<=0x7FFFFFFFl) {
8525
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   128
                // skip AU magic and data offset field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                raf.skipBytes(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                raf.writeInt(bytesWritten-AuFileFormat.AU_HEADERSIZE);
8525
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   131
                // that's all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // -------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Throws IllegalArgumentException if not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   145
    private AudioFileFormat getAudioFileFormat(Type type, AudioInputStream stream) {
35686
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35656
diff changeset
   146
        if (!isFileTypeSupported(type, stream)) {
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35656
diff changeset
   147
            throw new IllegalArgumentException("File type " + type + " not supported.");
1c21a27682a5 8064800: AudioSystem/WaveFileWriter can't write PCM_FLOAT, but writes it anyway
serb
parents: 35656
diff changeset
   148
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        AudioFormat streamFormat = stream.getFormat();
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   151
        AudioFormat.Encoding encoding = streamFormat.getEncoding();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   153
        if (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   157
        // We always write big endian au files, this is by far the standard
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   158
        AudioFormat format = new AudioFormat(encoding,
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   159
                                             streamFormat.getSampleRate(),
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   160
                                             streamFormat.getSampleSizeInBits(),
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   161
                                             streamFormat.getChannels(),
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   162
                                             streamFormat.getFrameSize(),
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   163
                                             streamFormat.getFrameRate(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   165
        int fileSize;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   166
        if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AuFileFormat.AU_HEADERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            fileSize = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   172
        return new AuFileFormat(Type.AU, fileSize, format,
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   173
                                (int) stream.getFrameLength());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   176
    private InputStream getFileStream(AuFileFormat auFileFormat, AudioInputStream audioStream) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // private method ... assumes auFileFormat is a supported file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        AudioFormat format            = auFileFormat.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int headerSize     = AuFileFormat.AU_HEADERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        long dataSize       = auFileFormat.getFrameLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        //$$fb fix for Bug 4351296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        //int dataSizeInBytes = dataSize * format.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        long dataSizeInBytes = (dataSize==AudioSystem.NOT_SPECIFIED)?UNKNOWN_SIZE:dataSize * format.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (dataSizeInBytes>0x7FFFFFFFl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            dataSizeInBytes=UNKNOWN_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   190
        int auType = auFileFormat.getAuType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        int sampleRate     = (int)format.getSampleRate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int channels       = format.getChannels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        byte header[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        ByteArrayInputStream headerStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        ByteArrayOutputStream baos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        DataOutputStream dos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        SequenceInputStream auStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   200
        // if we need to do any format conversion, we do it here.
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   201
        //$$ fb 2001-07-13: Bug 4391108
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   202
        audioStream = AudioSystem.getAudioInputStream(format, audioStream);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        dos = new DataOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   207
        dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   208
        dos.writeInt(headerSize);
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   209
        dos.writeInt((int)dataSizeInBytes);
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   210
        dos.writeInt(auType);
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   211
        dos.writeInt(sampleRate);
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   212
        dos.writeInt(channels);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // Now create a new InputStream from headerStream and the InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // in audioStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        header = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        headerStream = new ByteArrayInputStream( header );
8525
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   220
        auStream = new SequenceInputStream(headerStream,
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35686
diff changeset
   221
                        new NoCloseInputStream(audioStream));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return auStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 34412
diff changeset
   226
    private int writeAuFile(AudioInputStream in, AuFileFormat auFileFormat, OutputStream out) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        InputStream fileStream = getFileStream(auFileFormat, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        byte buffer[] = new byte[bisBufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int maxLength = auFileFormat.getByteLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (maxLength>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if( bytesRead < maxLength ) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   237
                    out.write( buffer, 0, bytesRead );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    maxLength -= bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                } else {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   241
                    out.write( buffer, 0, maxLength );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    bytesWritten += maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    maxLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            } else {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18215
diff changeset
   247
                out.write( buffer, 0, bytesRead );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
}