< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/AiffFileWriter.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2017, 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


 217             } else {
 218                 fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AiffFileFormat.AIFF_HEADERSIZE;
 219             }
 220         } else {
 221             fileSize = AudioSystem.NOT_SPECIFIED;
 222         }
 223 
 224         fileFormat = new AiffFileFormat( AudioFileFormat.Type.AIFF,
 225                                          fileSize,
 226                                          format,
 227                                          (int)stream.getFrameLength() );
 228 
 229         return fileFormat;
 230     }
 231 
 232     private int writeAiffFile(InputStream in, AiffFileFormat aiffFileFormat, OutputStream out) throws IOException {
 233 
 234         int bytesRead = 0;
 235         int bytesWritten = 0;
 236         InputStream fileStream = getFileStream(aiffFileFormat, in);
 237         byte buffer[] = new byte[bisBufferSize];
 238         int maxLength = aiffFileFormat.getByteLength();
 239 
 240         while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
 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 
 253             } else {
 254                 out.write( buffer, 0, bytesRead );
 255                 bytesWritten += bytesRead;
 256             }
 257         }


 271         int headerSize          = aiffFileFormat.getHeaderSize();
 272         //int fverChunkSize       = 0;
 273         int fverChunkSize       = aiffFileFormat.getFverChunkSize();
 274         int commChunkSize       = aiffFileFormat.getCommChunkSize();
 275         int aiffLength          = -1;
 276         int ssndChunkSize       = -1;
 277         int ssndOffset                  = aiffFileFormat.getSsndChunkOffset();
 278         short channels = (short) format.getChannels();
 279         short sampleSize = (short) format.getSampleSizeInBits();
 280         int ssndBlockSize = channels * ((sampleSize + 7) / 8);
 281         int numFrames = aiffFileFormat.getFrameLength();
 282         long dataSize = -1;
 283         if( numFrames != AudioSystem.NOT_SPECIFIED) {
 284             dataSize = (long) numFrames * ssndBlockSize;
 285             ssndChunkSize = (int)dataSize + 16;
 286             aiffLength = (int)dataSize+headerSize;
 287         }
 288         float sampleFramesPerSecond = format.getSampleRate();
 289         int compCode = AiffFileFormat.AIFC_PCM;
 290 
 291         byte header[] = null;
 292         InputStream codedAudioStream = audioStream;
 293 
 294         // if we need to do any format conversion, do it here....
 295 
 296         if( audioStream instanceof AudioInputStream ) {
 297 
 298             streamFormat = ((AudioInputStream)audioStream).getFormat();
 299             encoding = streamFormat.getEncoding();
 300 
 301 
 302             // $$jb: Note that AIFF samples are ALWAYS signed
 303             if( (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ||
 304                 ( (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) && !streamFormat.isBigEndian() ) ) {
 305 
 306                 // plug in the transcoder to convert to PCM_SIGNED. big endian
 307                 codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
 308                                                                                      AudioFormat.Encoding.PCM_SIGNED,
 309                                                                                      streamFormat.getSampleRate(),
 310                                                                                      streamFormat.getSampleSizeInBits(),
 311                                                                                      streamFormat.getChannels(),


   1 /*
   2  * Copyright (c) 1999, 2018, 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


 217             } else {
 218                 fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AiffFileFormat.AIFF_HEADERSIZE;
 219             }
 220         } else {
 221             fileSize = AudioSystem.NOT_SPECIFIED;
 222         }
 223 
 224         fileFormat = new AiffFileFormat( AudioFileFormat.Type.AIFF,
 225                                          fileSize,
 226                                          format,
 227                                          (int)stream.getFrameLength() );
 228 
 229         return fileFormat;
 230     }
 231 
 232     private int writeAiffFile(InputStream in, AiffFileFormat aiffFileFormat, OutputStream out) throws IOException {
 233 
 234         int bytesRead = 0;
 235         int bytesWritten = 0;
 236         InputStream fileStream = getFileStream(aiffFileFormat, in);
 237         byte[] buffer = new byte[bisBufferSize];
 238         int maxLength = aiffFileFormat.getByteLength();
 239 
 240         while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
 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 
 253             } else {
 254                 out.write( buffer, 0, bytesRead );
 255                 bytesWritten += bytesRead;
 256             }
 257         }


 271         int headerSize          = aiffFileFormat.getHeaderSize();
 272         //int fverChunkSize       = 0;
 273         int fverChunkSize       = aiffFileFormat.getFverChunkSize();
 274         int commChunkSize       = aiffFileFormat.getCommChunkSize();
 275         int aiffLength          = -1;
 276         int ssndChunkSize       = -1;
 277         int ssndOffset                  = aiffFileFormat.getSsndChunkOffset();
 278         short channels = (short) format.getChannels();
 279         short sampleSize = (short) format.getSampleSizeInBits();
 280         int ssndBlockSize = channels * ((sampleSize + 7) / 8);
 281         int numFrames = aiffFileFormat.getFrameLength();
 282         long dataSize = -1;
 283         if( numFrames != AudioSystem.NOT_SPECIFIED) {
 284             dataSize = (long) numFrames * ssndBlockSize;
 285             ssndChunkSize = (int)dataSize + 16;
 286             aiffLength = (int)dataSize+headerSize;
 287         }
 288         float sampleFramesPerSecond = format.getSampleRate();
 289         int compCode = AiffFileFormat.AIFC_PCM;
 290 
 291         byte[] header = null;
 292         InputStream codedAudioStream = audioStream;
 293 
 294         // if we need to do any format conversion, do it here....
 295 
 296         if( audioStream instanceof AudioInputStream ) {
 297 
 298             streamFormat = ((AudioInputStream)audioStream).getFormat();
 299             encoding = streamFormat.getEncoding();
 300 
 301 
 302             // $$jb: Note that AIFF samples are ALWAYS signed
 303             if( (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ||
 304                 ( (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) && !streamFormat.isBigEndian() ) ) {
 305 
 306                 // plug in the transcoder to convert to PCM_SIGNED. big endian
 307                 codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
 308                                                                                      AudioFormat.Encoding.PCM_SIGNED,
 309                                                                                      streamFormat.getSampleRate(),
 310                                                                                      streamFormat.getSampleSizeInBits(),
 311                                                                                      streamFormat.getChannels(),


< prev index next >