1 /*
   2  * Copyright (c) 2007, 2016, 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.IOException;
  29 import java.io.InputStream;
  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.UnsupportedAudioFileException;
  36 
  37 /**
  38  * Floating-point encoded (format 3) WAVE file loader.
  39  *
  40  * @author Karl Helgason
  41  */
  42 public final class WaveFloatFileReader extends SunFileReader {
  43 
  44     @Override
  45     StandardFileFormat getAudioFileFormatImpl(final InputStream stream)
  46             throws UnsupportedAudioFileException, IOException {
  47 
  48         RIFFReader riffiterator = new RIFFReader(stream);
  49         if (!riffiterator.getFormat().equals("RIFF"))
  50             throw new UnsupportedAudioFileException();
  51         if (!riffiterator.getType().equals("WAVE"))
  52             throw new UnsupportedAudioFileException();
  53 
  54         boolean fmt_found = false;
  55         boolean data_found = false;
  56 
  57         int channels = 1;
  58         long samplerate = 1;
  59         int framesize = 1;
  60         int bits = 1;
  61         long dataSize = 0;
  62 
  63         while (riffiterator.hasNextChunk()) {
  64             RIFFReader chunk = riffiterator.nextChunk();
  65             if (chunk.getFormat().equals("fmt ")) {
  66                 fmt_found = true;
  67 
  68                 int format = chunk.readUnsignedShort();
  69                 if (format != WaveFileFormat.WAVE_FORMAT_IEEE_FLOAT) {
  70                     throw new UnsupportedAudioFileException();
  71                 }
  72                 channels = chunk.readUnsignedShort();
  73                 samplerate = chunk.readUnsignedInt();
  74                 /* framerate = */chunk.readUnsignedInt();
  75                 framesize = chunk.readUnsignedShort();
  76                 bits = chunk.readUnsignedShort();
  77             }
  78             if (chunk.getFormat().equals("data")) {
  79                 dataSize = chunk.getSize();
  80                 data_found = true;
  81                 break;
  82             }
  83         }
  84         if (!fmt_found || !data_found) {
  85             throw new UnsupportedAudioFileException();
  86         }
  87         AudioFormat audioformat = new AudioFormat(
  88                 Encoding.PCM_FLOAT, samplerate, bits, channels,
  89                 framesize, samplerate, false);
  90         return new StandardFileFormat(AudioFileFormat.Type.WAVE, audioformat,
  91                                       dataSize / audioformat.getFrameSize());
  92     }
  93 
  94     @Override
  95     public AudioInputStream getAudioInputStream(final InputStream stream)
  96             throws UnsupportedAudioFileException, IOException {
  97 
  98         final StandardFileFormat format = getAudioFileFormat(stream);
  99         final AudioFormat af = format.getFormat();
 100         final long length = format.getLongFrameLength();
 101         // we've got everything, the stream is supported and it is at the
 102         // beginning of the header, so find the data chunk again and return an
 103         // AudioInputStream
 104         final RIFFReader riffiterator = new RIFFReader(stream);
 105         while (riffiterator.hasNextChunk()) {
 106             RIFFReader chunk = riffiterator.nextChunk();
 107             if (chunk.getFormat().equals("data")) {
 108                 return new AudioInputStream(chunk, af, length);
 109             }
 110         }
 111         throw new UnsupportedAudioFileException();
 112     }
 113 }