JOAL v2.6.0-rc-20250706
JOAL, OpenAL® API Binding for Java™ (public API).
WAVLoader.java
Go to the documentation of this file.
1/**
2* Copyright (c) 2013 JogAmp Community. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are met:
6*
7* -Redistribution of source code must retain the above copyright notice,
8* this list of conditions and the following disclaimer.
9*
10* -Redistribution in binary form must reproduce the above copyright notice,
11* this list of conditions and the following disclaimer in the documentation
12* and/or other materials provided with the distribution.
13*
14* Neither the name of Sun Microsystems, Inc. or the names of contributors may
15* be used to endorse or promote products derived from this software without
16* specific prior written permission.
17*
18* This software is provided "AS IS," without a warranty of any kind.
19* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
20* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
21* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS
22* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
23* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
24* IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
25* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
26* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
27* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
28* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
29*
30* You acknowledge that this software is not designed or intended for use in the
31* design, construction, operation or maintenance of any nuclear facility.
32*
33* Note: Rewrite started by Julien Goussej 2013-03-27 commit 6292ed9712b17f849f22221aea9d586c3a363c09
34*/
35
36package com.jogamp.openal.util;
37
38import java.io.File;
39import java.io.FileInputStream;
40import java.io.IOException;
41import java.io.InputStream;
42import java.nio.ByteOrder;
43
44import com.jogamp.common.util.Bitstream;
45import com.jogamp.openal.ALException;
46
47/**
48 * A Loader utility for (.wav) files. Creates a WAVData object containing the
49 * data used by the AL.alBufferData method.
50 */
51public class WAVLoader {
52
53 /**
54 * This method loads a (.wav) file into a WAVData object.
55 *
56 * @param filename The name of the (.wav) file
57 *
58 * @return a WAVData object containing the audio data
59 *
60 * @throws ALException if the format of the audio if not supported.
61 * @throws IOException If the file can no be found or some other IO error
62 * occurs
63 */
64 public static WAVData loadFromFile(final String filename) throws ALException, IOException {
65 final File soundFile = new File(filename);
66 final InputStream is = new FileInputStream(soundFile);
67 return loadFromStreamImpl(is);
68 }
69
70 /**
71 * This method loads a (.wav) file into a WAVData object.
72 *
73 * @param stream An InputStream for the .WAV stream.
74 *
75 * @return a WAVData object containing the audio data
76 *
77 * @throws ALException if the format of the audio if not supported.
78 * @throws IOException If the file can no be found or some other IO error
79 * occurs
80 */
81 public static WAVData loadFromStream(final InputStream stream) throws ALException, IOException {
82 return loadFromStreamImpl(stream);
83 }
84
85 private static final int RIFF = 0x52494646;
86 private static final int RIFX = 0x52494658;
87 private static final int WAVE = 0x57415645;
88 private static final int FACT = 0x66616374;
89 private static final int FMT = 0x666D7420;
90 private static final int DATA = 0x64617461;
91
92 private static WAVData loadFromStreamImpl(final InputStream aIn) throws ALException, IOException {
93 /**
94 * references:
95 * http://www.sonicspot.com/guide/wavefiles.html
96 * https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
97 * http://stackoverflow.com/questions/1111539/is-the-endianness-of-format-params-guaranteed-in-riff-wav-files
98 * http://sharkysoft.com/archive/lava/docs/javadocs/lava/riff/wave/doc-files/riffwave-content.htm
99 */
100 final Bitstream.ByteInputStream bis = new Bitstream.ByteInputStream(aIn);
101 final Bitstream<InputStream> bs = new Bitstream<InputStream>(bis, false);
102 bs.setThrowIOExceptionOnEOF(true);
103 try {
104 final boolean bigEndian; // FIXME: for all data incl. signatures ?
105
106 final long riffMarker = bs.readUInt32(true /* bigEndian */);
107 if ( RIFF == riffMarker ) {
108 bigEndian = false;
109 } else if( RIFX == riffMarker ) {
110 bigEndian = true;
111 } else {
112 throw new ALException("Invalid RIF header: 0x"+Integer.toHexString((int)riffMarker)+", "+bs);
113 }
114 final long riffLenL = bs.readUInt32(bigEndian);
115 final int riffLenI = Bitstream.uint32LongToInt(riffLenL);
116 final long wavMarker = bs.readUInt32(true /* bigEndian */);
117 if ( WAVE != wavMarker ) {
118 throw new ALException("Invalid WAV header: 0x"+Integer.toHexString((int)wavMarker)+", "+bs);
119 }
120 boolean foundFmt = false;
121 boolean foundData = false;
122
123 short sChannels = 0, sSampleSizeInBits = 0;
124 long sampleRate = 0;
125 long chunkLength = 0;
126 int dataLength = 0;
127
128 while (!foundData) {
129 final int chunkId = (int)bs.readUInt32(true /* bigEndian */);
130 chunkLength = bs.readUInt32(bigEndian);
131 switch (chunkId) {
132 case FMT:
133 foundFmt = true;
134 @SuppressWarnings("unused")
135 final int compressionCode = bs.readUInt16(bigEndian);
136 sChannels = (short)bs.readUInt16(bigEndian);
137 sampleRate = bs.readUInt32(bigEndian);
138 @SuppressWarnings("unused")
139 final long bytesPerSeconds = bs.readUInt32(bigEndian);
140 @SuppressWarnings("unused")
141 final short blockAlignment = (short) bs.readUInt16(bigEndian);
142 sSampleSizeInBits = (short) bs.readUInt16(bigEndian);
143 bs.skip( 8 * ( chunkLength - 16 ) );
144 break;
145 case FACT:
146 // FIXME: compression format dependent data?
147 bs.skip( 8 * chunkLength );
148 break;
149 case DATA:
150 if (!foundFmt) {
151 throw new ALException("WAV fmt chunks must be before data chunks: "+bs);
152 }
153 foundData = true;
154 dataLength = Bitstream.uint32LongToInt(chunkLength);
155 break;
156 default:
157 // unrecognized chunk, skips it
158 bs.skip( 8 * chunkLength );
159 }
160 }
161
162 final int channels = sChannels;
163 final int sampleSizeInBits = sSampleSizeInBits;
164 final float fSampleRate = sampleRate;
165 return WAVData.loadFromStream(bs.getSubStream(), dataLength, channels, sampleSizeInBits,
166 Math.round(fSampleRate), bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN, false);
167 } finally {
168 bs.close();
169 }
170 }
171
172}
A generic exception for OpenAL errors used throughout the binding as a substitute for RuntimeExceptio...
This class is a holder for WAV (.wav ) file Data returned from the WavLoader, or directly via loadFro...
Definition: WAVData.java:53
A Loader utility for (.wav) files.
Definition: WAVLoader.java:51
static WAVData loadFromStream(final InputStream stream)
This method loads a (.wav) file into a WAVData object.
Definition: WAVLoader.java:81
static WAVData loadFromFile(final String filename)
This method loads a (.wav) file into a WAVData object.
Definition: WAVLoader.java:64