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