< prev index next >

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

Print this page




  63     public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
  64 
  65         AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
  66         System.arraycopy(types, 0, filetypes, 0, types.length);
  67 
  68         // make sure we can write this stream
  69         AudioFormat format = stream.getFormat();
  70         AudioFormat.Encoding encoding = format.getEncoding();
  71 
  72         if( AudioFormat.Encoding.ALAW.equals(encoding) ||
  73             AudioFormat.Encoding.ULAW.equals(encoding) ||
  74             AudioFormat.Encoding.PCM_SIGNED.equals(encoding) ||
  75             AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) ) {
  76 
  77             return filetypes;
  78         }
  79 
  80         return new AudioFileFormat.Type[0];
  81     }
  82 
  83 
  84     @Override
  85     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
  86         Objects.requireNonNull(stream);
  87         Objects.requireNonNull(fileType);
  88         Objects.requireNonNull(out);
  89 
  90         //$$fb the following check must come first ! Otherwise
  91         // the next frame length check may throw an IOException and
  92         // interrupt iterating File Writers. (see bug 4351296)
  93 
  94         // throws IllegalArgumentException if not supported
  95         WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
  96 
  97         //$$fb when we got this far, we are committed to write this file
  98 
  99         // we must know the total data length to calculate the file length
 100         if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
 101             throw new IOException("stream length not specified");
 102         }
 103 
 104         return writeWaveFile(stream, waveFileFormat, out);
 105     }
 106 
 107 
 108     @Override
 109     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
 110         Objects.requireNonNull(stream);
 111         Objects.requireNonNull(fileType);
 112         Objects.requireNonNull(out);
 113 
 114         // throws IllegalArgumentException if not supported
 115         WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
 116 
 117         // first write the file without worrying about length fields
 118         FileOutputStream fos = new FileOutputStream( out );     // throws IOException
 119         BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
 120         int bytesWritten = writeWaveFile(stream, waveFileFormat, bos );
 121         bos.close();
 122 
 123         // now, if length fields were not specified, calculate them,
 124         // open as a random access file, write the appropriate fields,
 125         // close again....
 126         if( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
 127 


 240 
 241         return bytesWritten;
 242     }
 243 
 244     private InputStream getFileStream(WaveFileFormat waveFileFormat, InputStream audioStream) throws IOException {
 245         // private method ... assumes audioFileFormat is a supported file type
 246 
 247         // WAVE header fields
 248         AudioFormat audioFormat = waveFileFormat.getFormat();
 249         int headerLength       = waveFileFormat.getHeaderSize();
 250         int riffMagic          = WaveFileFormat.RIFF_MAGIC;
 251         int waveMagic          = WaveFileFormat.WAVE_MAGIC;
 252         int fmtMagic           = WaveFileFormat.FMT_MAGIC;
 253         int fmtLength          = WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType());
 254         short wav_type         = (short) waveFileFormat.getWaveType();
 255         short channels         = (short) audioFormat.getChannels();
 256         short sampleSizeInBits = (short) audioFormat.getSampleSizeInBits();
 257         int sampleRate         = (int) audioFormat.getSampleRate();
 258         int frameSizeInBytes   = audioFormat.getFrameSize();
 259         int frameRate              = (int) audioFormat.getFrameRate();
 260         int avgBytesPerSec     = channels * sampleSizeInBits * sampleRate / 8;;
 261         short blockAlign       = (short) ((sampleSizeInBits / 8) * channels);
 262         int dataMagic              = WaveFileFormat.DATA_MAGIC;
 263         int dataLength             = waveFileFormat.getFrameLength() * frameSizeInBytes;
 264         int length                         = waveFileFormat.getByteLength();
 265         int riffLength = dataLength + headerLength - 8;
 266 
 267         byte header[] = null;
 268         ByteArrayInputStream headerStream = null;
 269         ByteArrayOutputStream baos = null;
 270         DataOutputStream dos = null;
 271         SequenceInputStream waveStream = null;
 272 
 273         AudioFormat audioStreamFormat = null;
 274         AudioFormat.Encoding encoding = null;
 275         InputStream codedAudioStream = audioStream;
 276 
 277         // if audioStream is an AudioInputStream and we need to convert, do it here...
 278         if(audioStream instanceof AudioInputStream) {
 279             audioStreamFormat = ((AudioInputStream)audioStream).getFormat();
 280 


 330         dos.writeShort(big2littleShort(channels));
 331         dos.writeInt(big2little(sampleRate));
 332         dos.writeInt(big2little(avgBytesPerSec));
 333         dos.writeShort(big2littleShort(blockAlign));
 334         dos.writeShort(big2littleShort(sampleSizeInBits));
 335         //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
 336         if (wav_type != WaveFileFormat.WAVE_FORMAT_PCM) {
 337             // add length 0 for "codec specific data length"
 338             dos.writeShort(0);
 339         }
 340 
 341         dos.writeInt(dataMagic);
 342         dos.writeInt(big2little(dataLength));
 343 
 344         dos.close();
 345         header = baos.toByteArray();
 346         headerStream = new ByteArrayInputStream( header );
 347         waveStream = new SequenceInputStream(headerStream,
 348                             new NoCloseInputStream(codedAudioStream));
 349 
 350         return (InputStream)waveStream;
 351     }
 352 }


  63     public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
  64 
  65         AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
  66         System.arraycopy(types, 0, filetypes, 0, types.length);
  67 
  68         // make sure we can write this stream
  69         AudioFormat format = stream.getFormat();
  70         AudioFormat.Encoding encoding = format.getEncoding();
  71 
  72         if( AudioFormat.Encoding.ALAW.equals(encoding) ||
  73             AudioFormat.Encoding.ULAW.equals(encoding) ||
  74             AudioFormat.Encoding.PCM_SIGNED.equals(encoding) ||
  75             AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) ) {
  76 
  77             return filetypes;
  78         }
  79 
  80         return new AudioFileFormat.Type[0];
  81     }
  82 

  83     @Override
  84     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
  85         Objects.requireNonNull(stream);
  86         Objects.requireNonNull(fileType);
  87         Objects.requireNonNull(out);
  88 
  89         //$$fb the following check must come first ! Otherwise
  90         // the next frame length check may throw an IOException and
  91         // interrupt iterating File Writers. (see bug 4351296)
  92 
  93         // throws IllegalArgumentException if not supported
  94         WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
  95 
  96         //$$fb when we got this far, we are committed to write this file
  97 
  98         // we must know the total data length to calculate the file length
  99         if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
 100             throw new IOException("stream length not specified");
 101         }
 102 
 103         return writeWaveFile(stream, waveFileFormat, out);
 104     }
 105 

 106     @Override
 107     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
 108         Objects.requireNonNull(stream);
 109         Objects.requireNonNull(fileType);
 110         Objects.requireNonNull(out);
 111 
 112         // throws IllegalArgumentException if not supported
 113         WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);
 114 
 115         // first write the file without worrying about length fields
 116         FileOutputStream fos = new FileOutputStream( out );     // throws IOException
 117         BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
 118         int bytesWritten = writeWaveFile(stream, waveFileFormat, bos );
 119         bos.close();
 120 
 121         // now, if length fields were not specified, calculate them,
 122         // open as a random access file, write the appropriate fields,
 123         // close again....
 124         if( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
 125 


 238 
 239         return bytesWritten;
 240     }
 241 
 242     private InputStream getFileStream(WaveFileFormat waveFileFormat, InputStream audioStream) throws IOException {
 243         // private method ... assumes audioFileFormat is a supported file type
 244 
 245         // WAVE header fields
 246         AudioFormat audioFormat = waveFileFormat.getFormat();
 247         int headerLength       = waveFileFormat.getHeaderSize();
 248         int riffMagic          = WaveFileFormat.RIFF_MAGIC;
 249         int waveMagic          = WaveFileFormat.WAVE_MAGIC;
 250         int fmtMagic           = WaveFileFormat.FMT_MAGIC;
 251         int fmtLength          = WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType());
 252         short wav_type         = (short) waveFileFormat.getWaveType();
 253         short channels         = (short) audioFormat.getChannels();
 254         short sampleSizeInBits = (short) audioFormat.getSampleSizeInBits();
 255         int sampleRate         = (int) audioFormat.getSampleRate();
 256         int frameSizeInBytes   = audioFormat.getFrameSize();
 257         int frameRate              = (int) audioFormat.getFrameRate();
 258         int avgBytesPerSec     = channels * sampleSizeInBits * sampleRate / 8;
 259         short blockAlign       = (short) ((sampleSizeInBits / 8) * channels);
 260         int dataMagic              = WaveFileFormat.DATA_MAGIC;
 261         int dataLength             = waveFileFormat.getFrameLength() * frameSizeInBytes;
 262         int length                         = waveFileFormat.getByteLength();
 263         int riffLength = dataLength + headerLength - 8;
 264 
 265         byte header[] = null;
 266         ByteArrayInputStream headerStream = null;
 267         ByteArrayOutputStream baos = null;
 268         DataOutputStream dos = null;
 269         SequenceInputStream waveStream = null;
 270 
 271         AudioFormat audioStreamFormat = null;
 272         AudioFormat.Encoding encoding = null;
 273         InputStream codedAudioStream = audioStream;
 274 
 275         // if audioStream is an AudioInputStream and we need to convert, do it here...
 276         if(audioStream instanceof AudioInputStream) {
 277             audioStreamFormat = ((AudioInputStream)audioStream).getFormat();
 278 


 328         dos.writeShort(big2littleShort(channels));
 329         dos.writeInt(big2little(sampleRate));
 330         dos.writeInt(big2little(avgBytesPerSec));
 331         dos.writeShort(big2littleShort(blockAlign));
 332         dos.writeShort(big2littleShort(sampleSizeInBits));
 333         //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
 334         if (wav_type != WaveFileFormat.WAVE_FORMAT_PCM) {
 335             // add length 0 for "codec specific data length"
 336             dos.writeShort(0);
 337         }
 338 
 339         dos.writeInt(dataMagic);
 340         dos.writeInt(big2little(dataLength));
 341 
 342         dos.close();
 343         header = baos.toByteArray();
 344         headerStream = new ByteArrayInputStream( header );
 345         waveStream = new SequenceInputStream(headerStream,
 346                             new NoCloseInputStream(codedAudioStream));
 347 
 348         return waveStream;
 349     }
 350 }
< prev index next >