1 /*
   2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.sound;
  27 
  28 import java.io.File;
  29 import java.io.InputStream;
  30 import java.io.OutputStream;
  31 import java.io.IOException;
  32 
  33 import java.io.BufferedOutputStream;
  34 import java.io.DataOutputStream;
  35 import java.io.FileOutputStream;
  36 import java.io.ByteArrayInputStream;
  37 import java.io.ByteArrayOutputStream;
  38 import java.io.RandomAccessFile;
  39 import java.io.SequenceInputStream;
  40 
  41 import javax.sound.sampled.AudioFileFormat;
  42 import javax.sound.sampled.AudioInputStream;
  43 import javax.sound.sampled.AudioFormat;
  44 import javax.sound.sampled.AudioSystem;
  45 
  46 //$$fb this class is buggy. Should be replaced in future.
  47 
  48 /**
  49  * WAVE file writer.
  50  *
  51  * @author Jan Borgersen
  52  */
  53 public final class WaveFileWriter extends SunFileWriter {
  54 
  55     // magic numbers
  56     static  final int RIFF_MAGIC = 1380533830;
  57     static  final int WAVE_MAGIC = 1463899717;
  58     static  final int FMT_MAGIC  = 0x666d7420; // "fmt "
  59     static  final int DATA_MAGIC = 0x64617461; // "data"
  60 
  61     // encodings
  62     static final int WAVE_FORMAT_UNKNOWN   = 0x0000;
  63     static final int WAVE_FORMAT_PCM       = 0x0001;
  64     static final int WAVE_FORMAT_ADPCM     = 0x0002;
  65     static final int WAVE_FORMAT_ALAW      = 0x0006;
  66     static final int WAVE_FORMAT_MULAW     = 0x0007;
  67     static final int WAVE_FORMAT_OKI_ADPCM = 0x0010;
  68     static final int WAVE_FORMAT_DIGISTD   = 0x0015;
  69     static final int WAVE_FORMAT_DIGIFIX   = 0x0016;
  70     static final int WAVE_IBM_FORMAT_MULAW = 0x0101;
  71     static final int WAVE_IBM_FORMAT_ALAW  = 0x0102;
  72     static final int WAVE_IBM_FORMAT_ADPCM = 0x0103;
  73     static final int WAVE_FORMAT_DVI_ADPCM = 0x0011;
  74     static final int WAVE_FORMAT_SX7383    = 0x1C07;
  75 
  76     /**
  77      * Constructs a new WaveFileWriter object.
  78      */
  79     public WaveFileWriter() {
  80         super(new AudioFileFormat.Type[]{AudioFileFormat.Type.WAVE});
  81     }
  82 
  83 
  84     // METHODS TO IMPLEMENT AudioFileWriter
  85 
  86 
  87     public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
  88 
  89         AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
  90         System.arraycopy(types, 0, filetypes, 0, types.length);
  91 
  92         // make sure we can write this stream
  93         AudioFormat format = stream.getFormat();
  94         AudioFormat.Encoding encoding = format.getEncoding();
  95 
  96         if( AudioFormat.Encoding.ALAW.equals(encoding) ||
  97             AudioFormat.Encoding.ULAW.equals(encoding) ||
  98             AudioFormat.Encoding.PCM_SIGNED.equals(encoding) ||
  99             AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) ) {
 100 
 101             return filetypes;
 102         }
 103 
 104         return new AudioFileFormat.Type[0];
 105     }
 106 
 107 
 108     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
 109 
 110         //$$fb the following check must come first ! Otherwise
 111         // the next frame length check may throw an IOException and
 112         // interrupt iterating File Writers. (see bug 4351296)
 113 
 114         // throws IllegalArgumentException if not supported
 115         WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
 116 
 117         //$$fb when we got this far, we are committed to write this file
 118 
 119         // we must know the total data length to calculate the file length
 120         if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
 121             throw new IOException("stream length not specified");
 122         }
 123 
 124         int bytesWritten = writeWaveFile(stream, waveFileFormat, out);
 125         return bytesWritten;
 126     }
 127 
 128 
 129     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
 130 
 131         // throws IllegalArgumentException if not supported
 132         WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
 133 
 134         // first write the file without worrying about length fields
 135         FileOutputStream fos = new FileOutputStream( out );     // throws IOException
 136         BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
 137         int bytesWritten = writeWaveFile(stream, waveFileFormat, bos );
 138         bos.close();
 139 
 140         // now, if length fields were not specified, calculate them,
 141         // open as a random access file, write the appropriate fields,
 142         // close again....
 143         if( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
 144 
 145             int dataLength=bytesWritten-waveFileFormat.getHeaderSize();
 146             int riffLength=dataLength + waveFileFormat.getHeaderSize() - 8;
 147 
 148             RandomAccessFile raf=new RandomAccessFile(out, "rw");
 149             // skip RIFF magic
 150             raf.skipBytes(4);
 151             raf.writeInt(big2little( riffLength ));
 152             // skip WAVE magic, fmt_ magic, fmt_ length, fmt_ chunk, data magic
 153             raf.skipBytes(4+4+4+WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType())+4);
 154             raf.writeInt(big2little( dataLength ));
 155             // that's all
 156             raf.close();
 157         }
 158 
 159         return bytesWritten;
 160     }
 161 
 162     //--------------------------------------------------------------------
 163 
 164     /**
 165      * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
 166      * Throws IllegalArgumentException if not supported.
 167      */
 168     private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) {
 169         AudioFormat format = null;
 170         WaveFileFormat fileFormat = null;
 171         AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
 172 
 173         AudioFormat streamFormat = stream.getFormat();
 174         AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
 175 
 176         float sampleRate;
 177         int sampleSizeInBits;
 178         int channels;
 179         int frameSize;
 180         float frameRate;
 181         int fileSize;
 182 
 183         if (!types[0].equals(type)) {
 184             throw new IllegalArgumentException("File type " + type + " not supported.");
 185         }
 186         int waveType = WaveFileFormat.WAVE_FORMAT_PCM;
 187 
 188         if( AudioFormat.Encoding.ALAW.equals(streamEncoding) ||
 189             AudioFormat.Encoding.ULAW.equals(streamEncoding) ) {
 190 
 191             encoding = streamEncoding;
 192             sampleSizeInBits = streamFormat.getSampleSizeInBits();
 193             if (streamEncoding.equals(AudioFormat.Encoding.ALAW)) {
 194                 waveType = WAVE_FORMAT_ALAW;
 195             } else {
 196                 waveType = WAVE_FORMAT_MULAW;
 197             }
 198         } else if ( streamFormat.getSampleSizeInBits()==8 ) {
 199             encoding = AudioFormat.Encoding.PCM_UNSIGNED;
 200             sampleSizeInBits=8;
 201         } else {
 202             encoding = AudioFormat.Encoding.PCM_SIGNED;
 203             sampleSizeInBits=streamFormat.getSampleSizeInBits();
 204         }
 205 
 206 
 207         format = new AudioFormat( encoding,
 208                                   streamFormat.getSampleRate(),
 209                                   sampleSizeInBits,
 210                                   streamFormat.getChannels(),
 211                                   streamFormat.getFrameSize(),
 212                                   streamFormat.getFrameRate(),
 213                                   false);       // WAVE is little endian
 214 
 215         if( stream.getFrameLength()!=AudioSystem.NOT_SPECIFIED ) {
 216             fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize()
 217                 + WaveFileFormat.getHeaderSize(waveType);
 218         } else {
 219             fileSize = AudioSystem.NOT_SPECIFIED;
 220         }
 221 
 222         fileFormat = new WaveFileFormat( AudioFileFormat.Type.WAVE,
 223                                          fileSize,
 224                                          format,
 225                                          (int)stream.getFrameLength() );
 226 
 227         return fileFormat;
 228     }
 229 
 230 
 231     private int writeWaveFile(InputStream in, WaveFileFormat waveFileFormat, OutputStream out) throws IOException {
 232 
 233         int bytesRead = 0;
 234         int bytesWritten = 0;
 235         InputStream fileStream = getFileStream(waveFileFormat, in);
 236         byte buffer[] = new byte[bisBufferSize];
 237         int maxLength = waveFileFormat.getByteLength();
 238 
 239         while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
 240 
 241             if (maxLength>0) {
 242                 if( bytesRead < maxLength ) {
 243                     out.write( buffer, 0, bytesRead );
 244                     bytesWritten += bytesRead;
 245                     maxLength -= bytesRead;
 246                 } else {
 247                     out.write( buffer, 0, maxLength );
 248                     bytesWritten += maxLength;
 249                     maxLength = 0;
 250                     break;
 251                 }
 252             } else {
 253                 out.write( buffer, 0, bytesRead );
 254                 bytesWritten += bytesRead;
 255             }
 256         }
 257 
 258         return bytesWritten;
 259     }
 260 
 261     private InputStream getFileStream(WaveFileFormat waveFileFormat, InputStream audioStream) throws IOException {
 262         // private method ... assumes audioFileFormat is a supported file type
 263 
 264         // WAVE header fields
 265         AudioFormat audioFormat = waveFileFormat.getFormat();
 266         int headerLength       = waveFileFormat.getHeaderSize();
 267         int riffMagic          = WaveFileFormat.RIFF_MAGIC;
 268         int waveMagic          = WaveFileFormat.WAVE_MAGIC;
 269         int fmtMagic           = WaveFileFormat.FMT_MAGIC;
 270         int fmtLength          = WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType());
 271         short wav_type         = (short) waveFileFormat.getWaveType();
 272         short channels         = (short) audioFormat.getChannels();
 273         short sampleSizeInBits = (short) audioFormat.getSampleSizeInBits();
 274         int sampleRate         = (int) audioFormat.getSampleRate();
 275         int frameSizeInBytes   = audioFormat.getFrameSize();
 276         int frameRate              = (int) audioFormat.getFrameRate();
 277         int avgBytesPerSec     = channels * sampleSizeInBits * sampleRate / 8;;
 278         short blockAlign       = (short) ((sampleSizeInBits / 8) * channels);
 279         int dataMagic              = WaveFileFormat.DATA_MAGIC;
 280         int dataLength             = waveFileFormat.getFrameLength() * frameSizeInBytes;
 281         int length                         = waveFileFormat.getByteLength();
 282         int riffLength = dataLength + headerLength - 8;
 283 
 284         byte header[] = null;
 285         ByteArrayInputStream headerStream = null;
 286         ByteArrayOutputStream baos = null;
 287         DataOutputStream dos = null;
 288         SequenceInputStream waveStream = null;
 289 
 290         AudioFormat audioStreamFormat = null;
 291         AudioFormat.Encoding encoding = null;
 292         InputStream codedAudioStream = audioStream;
 293 
 294         // if audioStream is an AudioInputStream and we need to convert, do it here...
 295         if(audioStream instanceof AudioInputStream) {
 296             audioStreamFormat = ((AudioInputStream)audioStream).getFormat();
 297 
 298             encoding = audioStreamFormat.getEncoding();
 299 
 300             if(AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) {
 301                 if( sampleSizeInBits==8 ) {
 302                     wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
 303                     // plug in the transcoder to convert from PCM_SIGNED to PCM_UNSIGNED
 304                     codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat(
 305                                                                                         AudioFormat.Encoding.PCM_UNSIGNED,
 306                                                                                         audioStreamFormat.getSampleRate(),
 307                                                                                         audioStreamFormat.getSampleSizeInBits(),
 308                                                                                         audioStreamFormat.getChannels(),
 309                                                                                         audioStreamFormat.getFrameSize(),
 310                                                                                         audioStreamFormat.getFrameRate(),
 311                                                                                         false),
 312                                                                         (AudioInputStream)audioStream);
 313                 }
 314             }
 315             if( (AudioFormat.Encoding.PCM_SIGNED.equals(encoding) && audioStreamFormat.isBigEndian()) ||
 316                 (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) && !audioStreamFormat.isBigEndian()) ||
 317                 (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) && audioStreamFormat.isBigEndian()) ) {
 318                 if( sampleSizeInBits!=8) {
 319                     wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
 320                     // plug in the transcoder to convert to PCM_SIGNED_LITTLE_ENDIAN
 321                     codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat(
 322                                                                                         AudioFormat.Encoding.PCM_SIGNED,
 323                                                                                         audioStreamFormat.getSampleRate(),
 324                                                                                         audioStreamFormat.getSampleSizeInBits(),
 325                                                                                         audioStreamFormat.getChannels(),
 326                                                                                         audioStreamFormat.getFrameSize(),
 327                                                                                         audioStreamFormat.getFrameRate(),
 328                                                                                         false),
 329                                                                         (AudioInputStream)audioStream);
 330                 }
 331             }
 332         }
 333 
 334 
 335         // Now push the header into a stream, concat, and return the new SequenceInputStream
 336 
 337         baos = new ByteArrayOutputStream();
 338         dos = new DataOutputStream(baos);
 339 
 340         // we write in littleendian...
 341         dos.writeInt(riffMagic);
 342         dos.writeInt(big2little( riffLength ));
 343         dos.writeInt(waveMagic);
 344         dos.writeInt(fmtMagic);
 345         dos.writeInt(big2little(fmtLength));
 346         dos.writeShort(big2littleShort(wav_type));
 347         dos.writeShort(big2littleShort(channels));
 348         dos.writeInt(big2little(sampleRate));
 349         dos.writeInt(big2little(avgBytesPerSec));
 350         dos.writeShort(big2littleShort(blockAlign));
 351         dos.writeShort(big2littleShort(sampleSizeInBits));
 352         //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
 353         if (wav_type != WaveFileFormat.WAVE_FORMAT_PCM) {
 354             // add length 0 for "codec specific data length"
 355             dos.writeShort(0);
 356         }
 357 
 358         dos.writeInt(dataMagic);
 359         dos.writeInt(big2little(dataLength));
 360 
 361         dos.close();
 362         header = baos.toByteArray();
 363         headerStream = new ByteArrayInputStream( header );
 364         waveStream = new SequenceInputStream(headerStream,
 365                             new NoCloseInputStream(codedAudioStream));
 366 
 367         return (InputStream)waveStream;
 368     }
 369 }