Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]
    1   /*
    2    * Copyright (c) 2008, 2010, 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   package com.sun.media.sound;
   26   
   27   import java.io.File;
   28   import java.io.IOException;
   29   import java.io.OutputStream;
   30   
   31   import javax.sound.sampled.AudioFileFormat;
   32   import javax.sound.sampled.AudioFormat;
   33   import javax.sound.sampled.AudioFormat.Encoding;
   34   import javax.sound.sampled.AudioInputStream;
   35   import javax.sound.sampled.AudioSystem;
   36   import javax.sound.sampled.AudioFileFormat.Type;
   37   import javax.sound.sampled.spi.AudioFileWriter;
   38   
   39   /**
   40    * Floating-point encoded (format 3) WAVE file writer.
   41    *
   42    * @author Karl Helgason
   43    */
   44   public class WaveFloatFileWriter extends AudioFileWriter {
   45   
   46       public Type[] getAudioFileTypes() {
   47           return new Type[] { Type.WAVE };
   48       }
   49   
   50       public Type[] getAudioFileTypes(AudioInputStream stream) {
   51   
   52           if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
   53               return new Type[0];
   54           return new Type[] { Type.WAVE };
   55       }
   56   
   57       private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
   58           if (!Type.WAVE.equals(type))
   59               throw new IllegalArgumentException("File type " + type
   60                       + " not supported.");
   61           if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
   62               throw new IllegalArgumentException("File format "
   63                       + stream.getFormat() + " not supported.");
   64       }
   65   
   66       public void write(AudioInputStream stream, RIFFWriter writer)
   67               throws IOException {
   68   
   69           RIFFWriter fmt_chunk = writer.writeChunk("fmt ");
   70   
   71           AudioFormat format = stream.getFormat();
   72           fmt_chunk.writeUnsignedShort(3); // WAVE_FORMAT_IEEE_FLOAT
   73           fmt_chunk.writeUnsignedShort(format.getChannels());
   74           fmt_chunk.writeUnsignedInt((int) format.getSampleRate());
   75           fmt_chunk.writeUnsignedInt(((int) format.getFrameRate())
   76                   * format.getFrameSize());
   77           fmt_chunk.writeUnsignedShort(format.getFrameSize());
   78           fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits());
   79           fmt_chunk.close();
   80           RIFFWriter data_chunk = writer.writeChunk("data");
   81           byte[] buff = new byte[1024];
   82           int len;
   83           while ((len = stream.read(buff, 0, buff.length)) != -1)
   84               data_chunk.write(buff, 0, len);
   85           data_chunk.close();
   86       }
   87   
   88       private static class NoCloseOutputStream extends OutputStream {
   89           OutputStream out;
   90   
   91           public NoCloseOutputStream(OutputStream out) {
   92               this.out = out;
   93           }
   94   
   95           public void write(int b) throws IOException {
   96               out.write(b);
   97           }
   98   
   99           public void flush() throws IOException {
  100               out.flush();
  101           }
  102   
  103           public void write(byte[] b, int off, int len) throws IOException {
  104               out.write(b, off, len);
  105           }
  106   
  107           public void write(byte[] b) throws IOException {
  108               out.write(b);
  109           }
  110       }
  111   
  112       private AudioInputStream toLittleEndian(AudioInputStream ais) {
  113           AudioFormat format = ais.getFormat();
  114           AudioFormat targetFormat = new AudioFormat(format.getEncoding(), format
  115                   .getSampleRate(), format.getSampleSizeInBits(), format
  116                   .getChannels(), format.getFrameSize(), format.getFrameRate(),
  117                   false);
  118           return AudioSystem.getAudioInputStream(targetFormat, ais);
  119       }
  120   
  121       public int write(AudioInputStream stream, Type fileType, OutputStream out)
  122               throws IOException {
  123   
  124           checkFormat(fileType, stream);
  125           if (stream.getFormat().isBigEndian())
  126               stream = toLittleEndian(stream);
  127           RIFFWriter writer = new RIFFWriter(new NoCloseOutputStream(out), "WAVE");
  128           write(stream, writer);
  129           int fpointer = (int) writer.getFilePointer();
  130           writer.close();
  131           return fpointer;
  132       }
  133   
  134       public int write(AudioInputStream stream, Type fileType, File out)
  135               throws IOException {
  136           checkFormat(fileType, stream);
  137           if (stream.getFormat().isBigEndian())
  138               stream = toLittleEndian(stream);
  139           RIFFWriter writer = new RIFFWriter(out, "WAVE");
  140           write(stream, writer);
  141           int fpointer = (int) writer.getFilePointer();
  142           writer.close();
  143           return fpointer;
  144       }
  145   
  146   }

Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]