Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]
    1   /*
    2    * Copyright (c) 2007, 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.BufferedInputStream;
   28   import java.io.File;
   29   import java.io.FileInputStream;
   30   import java.io.IOException;
   31   import java.io.InputStream;
   32   import java.net.URL;
   33   
   34   import javax.sound.sampled.AudioFileFormat;
   35   import javax.sound.sampled.AudioFormat;
   36   import javax.sound.sampled.AudioFormat.Encoding;
   37   import javax.sound.sampled.AudioInputStream;
   38   import javax.sound.sampled.AudioSystem;
   39   import javax.sound.sampled.UnsupportedAudioFileException;
   40   import javax.sound.sampled.spi.AudioFileReader;
   41   
   42   /**
   43    * Floating-point encoded (format 3) WAVE file loader.
   44    *
   45    * @author Karl Helgason
   46    */
   47   public class WaveFloatFileReader extends AudioFileReader {
   48   
   49       public AudioFileFormat getAudioFileFormat(InputStream stream)
   50               throws UnsupportedAudioFileException, IOException {
   51   
   52           stream.mark(200);
   53           AudioFileFormat format;
   54           try {
   55               format = internal_getAudioFileFormat(stream);
   56           } finally {
   57               stream.reset();
   58           }
   59           return format;
   60       }
   61   
   62       private AudioFileFormat internal_getAudioFileFormat(InputStream stream)
   63               throws UnsupportedAudioFileException, IOException {
   64   
   65           RIFFReader riffiterator = new RIFFReader(stream);
   66           if (!riffiterator.getFormat().equals("RIFF"))
   67               throw new UnsupportedAudioFileException();
   68           if (!riffiterator.getType().equals("WAVE"))
   69               throw new UnsupportedAudioFileException();
   70   
   71           boolean fmt_found = false;
   72           boolean data_found = false;
   73   
   74           int channels = 1;
   75           long samplerate = 1;
   76           int framesize = 1;
   77           int bits = 1;
   78   
   79           while (riffiterator.hasNextChunk()) {
   80               RIFFReader chunk = riffiterator.nextChunk();
   81   
   82               if (chunk.getFormat().equals("fmt ")) {
   83                   fmt_found = true;
   84   
   85                   int format = chunk.readUnsignedShort();
   86                   if (format != 3) // WAVE_FORMAT_IEEE_FLOAT only
   87                       throw new UnsupportedAudioFileException();
   88                   channels = chunk.readUnsignedShort();
   89                   samplerate = chunk.readUnsignedInt();
   90                   /* framerate = */chunk.readUnsignedInt();
   91                   framesize = chunk.readUnsignedShort();
   92                   bits = chunk.readUnsignedShort();
   93               }
   94               if (chunk.getFormat().equals("data")) {
   95                   data_found = true;
   96                   break;
   97               }
   98           }
   99   
  100           if (!fmt_found)
  101               throw new UnsupportedAudioFileException();
  102           if (!data_found)
  103               throw new UnsupportedAudioFileException();
  104   
  105           AudioFormat audioformat = new AudioFormat(
  106                   Encoding.PCM_FLOAT, samplerate, bits, channels,
  107                   framesize, samplerate, false);
  108           AudioFileFormat fileformat = new AudioFileFormat(
  109                   AudioFileFormat.Type.WAVE, audioformat,
  110                   AudioSystem.NOT_SPECIFIED);
  111           return fileformat;
  112       }
  113   
  114       public AudioInputStream getAudioInputStream(InputStream stream)
  115               throws UnsupportedAudioFileException, IOException {
  116   
  117           AudioFileFormat format = getAudioFileFormat(stream);
  118           RIFFReader riffiterator = new RIFFReader(stream);
  119           if (!riffiterator.getFormat().equals("RIFF"))
  120               throw new UnsupportedAudioFileException();
  121           if (!riffiterator.getType().equals("WAVE"))
  122               throw new UnsupportedAudioFileException();
  123           while (riffiterator.hasNextChunk()) {
  124               RIFFReader chunk = riffiterator.nextChunk();
  125               if (chunk.getFormat().equals("data")) {
  126                   return new AudioInputStream(chunk, format.getFormat(),
  127                           chunk.getSize());
  128               }
  129           }
  130           throw new UnsupportedAudioFileException();
  131       }
  132   
  133       public AudioFileFormat getAudioFileFormat(URL url)
  134               throws UnsupportedAudioFileException, IOException {
  135           InputStream stream = url.openStream();
  136           AudioFileFormat format;
  137           try {
  138               format = getAudioFileFormat(new BufferedInputStream(stream));
  139           } finally {
  140               stream.close();
  141           }
  142           return format;
  143       }
  144   
  145       public AudioFileFormat getAudioFileFormat(File file)
  146               throws UnsupportedAudioFileException, IOException {
  147           InputStream stream = new FileInputStream(file);
  148           AudioFileFormat format;
  149           try {
  150               format = getAudioFileFormat(new BufferedInputStream(stream));
  151           } finally {
  152               stream.close();
  153           }
  154           return format;
  155       }
  156   
  157       public AudioInputStream getAudioInputStream(URL url)
  158               throws UnsupportedAudioFileException, IOException {
  159           return getAudioInputStream(new BufferedInputStream(url.openStream()));
  160       }
  161   
  162       public AudioInputStream getAudioInputStream(File file)
  163               throws UnsupportedAudioFileException, IOException {
  164           return getAudioInputStream(new BufferedInputStream(new FileInputStream(
  165                   file)));
  166       }
  167   }

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