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.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 != WaveFileFormat.WAVE_FORMAT_IEEE_FLOAT) { 71 throw new UnsupportedAudioFileException(); 72 } 73 channels = chunk.readUnsignedShort(); 74 samplerate = chunk.readUnsignedInt(); 75 /* framerate = */chunk.readUnsignedInt(); 76 framesize = chunk.readUnsignedShort(); 77 bits = chunk.readUnsignedShort(); 78 } 79 if (chunk.getFormat().equals("data")) { 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 AudioFileFormat(AudioFileFormat.Type.WAVE, audioformat, 91 AudioSystem.NOT_SPECIFIED); 92 } 93 94 @Override 95 public AudioInputStream getAudioInputStream(final InputStream stream) 96 throws UnsupportedAudioFileException, IOException { 97 98 final AudioFileFormat format = getAudioFileFormat(stream); 99 // we've got everything, the stream is supported and it is at the 100 // beginning of the header, so find the data chunk again and return an 101 // AudioInputStream 102 final RIFFReader riffiterator = new RIFFReader(stream); 103 while (riffiterator.hasNextChunk()) { 104 RIFFReader chunk = riffiterator.nextChunk(); 105 if (chunk.getFormat().equals("data")) { 106 final AudioFormat af = format.getFormat(); 107 final long length = chunk.getSize() / af.getFrameSize(); 108 return new AudioInputStream(chunk, af, length); 109 } 110 } 111 throw new UnsupportedAudioFileException(); 112 } 113 }