jdk/src/share/classes/com/sun/media/sound/AuFileWriter.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
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class AuFileWriter extends SunFileWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    //$$fb value for length field if length is not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public final static int UNKNOWN_SIZE=-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * AU type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final AudioFileFormat.Type auTypes[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        AudioFileFormat.Type.AU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Constructs a new AuFileWriter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public AuFileWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        super(auTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.arraycopy(types, 0, filetypes, 0, types.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // make sure we can write this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        AudioFormat format = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        AudioFormat.Encoding encoding = format.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if( (AudioFormat.Encoding.ALAW.equals(encoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            (AudioFormat.Encoding.ULAW.equals(encoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return filetypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return new AudioFileFormat.Type[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
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 int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // we must know the total data length to calculate the file length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        //$$fb 2001-07-13: fix for bug 4351296: do not throw an exception
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int bytesWritten = writeAuFile(stream, auFileFormat, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // first write the file without worrying about length fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int bytesWritten = writeAuFile(stream, auFileFormat, bos );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        bos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // now, if length fields were not specified, calculate them,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // open as a random access file, write the appropriate fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // close again....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // $$kk: 10.22.99: jan: please either implement this or throw an exception!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // $$fb: 2001-07-13: done. Fixes Bug 4479981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            RandomAccessFile raf=new RandomAccessFile(out, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (raf.length()<=0x7FFFFFFFl) {
8525
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   132
                // skip AU magic and data offset field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                raf.skipBytes(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                raf.writeInt(bytesWritten-AuFileFormat.AU_HEADERSIZE);
8525
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   135
                // that's all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        AudioFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        AuFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        AudioFormat streamFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        float sampleRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int sampleSizeInBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        float frameRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int fileSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if( !types[0].equals(type) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new IllegalArgumentException("File type " + type + " not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if( (AudioFormat.Encoding.ALAW.equals(streamEncoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            (AudioFormat.Encoding.ULAW.equals(streamEncoding)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            encoding = streamEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            sampleSizeInBits = streamFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else if ( streamFormat.getSampleSizeInBits()==8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            sampleSizeInBits=8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
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
                                  true);        // AU is always big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if( stream.getFrameLength()!=AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AuFileFormat.AU_HEADERSIZE;
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 AuFileFormat( AudioFileFormat.Type.AU,
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 InputStream getFileStream(AuFileFormat auFileFormat, InputStream audioStream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // private method ... assumes auFileFormat is a supported file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        AudioFormat format            = auFileFormat.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int magic          = AuFileFormat.AU_SUN_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int headerSize     = AuFileFormat.AU_HEADERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        long dataSize       = auFileFormat.getFrameLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        //$$fb fix for Bug 4351296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        //int dataSizeInBytes = dataSize * format.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        long dataSizeInBytes = (dataSize==AudioSystem.NOT_SPECIFIED)?UNKNOWN_SIZE:dataSize * format.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (dataSizeInBytes>0x7FFFFFFFl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            dataSizeInBytes=UNKNOWN_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int encoding_local = auFileFormat.getAuType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int sampleRate     = (int)format.getSampleRate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int channels       = format.getChannels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        //$$fb below is the fix for 4297100.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        //boolean bigendian      = format.isBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        boolean bigendian      = true;                  // force bigendian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        byte header[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        ByteArrayInputStream headerStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        ByteArrayOutputStream baos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        DataOutputStream dos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        SequenceInputStream auStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        AudioFormat audioStreamFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        AudioFormat.Encoding encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        InputStream codedAudioStream = audioStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // if we need to do any format conversion, do it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        codedAudioStream = audioStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if( audioStream instanceof AudioInputStream ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            audioStreamFormat = ((AudioInputStream)audioStream).getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            encoding = audioStreamFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            //$$ fb 2001-07-13: Bug 4391108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if( (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                 && bigendian != audioStreamFormat.isBigEndian()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                // plug in the transcoder to convert to PCM_SIGNED, bigendian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                // NOTE: little endian AU is not common, so we're always converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                //       to big endian unless the passed in audioFileFormat is little.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                // $$fb this NOTE is superseded. We always write big endian au files, this is by far the standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                                                                     AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                                                     audioStreamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                                                                     audioStreamFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                                                                     audioStreamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                                                                     audioStreamFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                                                                     audioStreamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                                                                     bigendian),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                                                    (AudioInputStream)audioStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        dos = new DataOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (bigendian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            dos.writeInt(headerSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            dos.writeInt((int)dataSizeInBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            dos.writeInt(encoding_local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            dos.writeInt(sampleRate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            dos.writeInt(channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            dos.writeInt(AuFileFormat.AU_SUN_INV_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            dos.writeInt(big2little(headerSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            dos.writeInt(big2little((int)dataSizeInBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            dos.writeInt(big2little(encoding_local));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            dos.writeInt(big2little(sampleRate));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            dos.writeInt(big2little(channels));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // Now create a new InputStream from headerStream and the InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // in audioStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        header = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        headerStream = new ByteArrayInputStream( header );
8525
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   304
        auStream = new SequenceInputStream(headerStream,
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   305
                        new NoCloseInputStream(codedAudioStream));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return auStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private int writeAuFile(InputStream in, AuFileFormat auFileFormat, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        InputStream fileStream = getFileStream(auFileFormat, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        byte buffer[] = new byte[bisBufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        int maxLength = auFileFormat.getByteLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (maxLength>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if( bytesRead < maxLength ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    out.write( buffer, 0, (int)bytesRead );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    maxLength -= bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    out.write( buffer, 0, (int)maxLength );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    bytesWritten += maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    maxLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                out.write( buffer, 0, (int)bytesRead );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}