JOAL v2.6.0-rc-20250712
JOAL, OpenAL® API Binding for Java™ (public API).
WAVData.java
Go to the documentation of this file.
1/**
2* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3* Copyright (c) 2011 JogAmp Community. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* -Redistribution of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10*
11* -Redistribution in binary form must reproduce the above copyright notice,
12* this list of conditions and the following disclaimer in the documentation
13* and/or other materials provided with the distribution.
14*
15* Neither the name of Sun Microsystems, Inc. or the names of contributors may
16* be used to endorse or promote products derived from this software without
17* specific prior written permission.
18*
19* This software is provided "AS IS," without a warranty of any kind.
20* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
22* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS
23* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
24* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
25* IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
26* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
27* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
28* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
29* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30*
31* You acknowledge that this software is not designed or intended for use in the
32* design, construction, operation or maintenance of any nuclear facility.
33*/
34
35package com.jogamp.openal.util;
36
37import java.io.BufferedInputStream;
38import java.io.IOException;
39import java.io.InputStream;
40import java.nio.ByteBuffer;
41import java.nio.ByteOrder;
42
43import com.jogamp.common.util.IOUtil;
44import com.jogamp.openal.ALConstants;
45import com.jogamp.openal.UnsupportedAudioFileException;
46
47/**
48 * This class is a holder for WAV (.wav ) file Data returned from the WavLoader,
49 * or directly via {@link #loadFromStream(InputStream, int, int, int)}.
50 *
51 * @author Athomas Goldberg, et.al.
52 */
53public final class WAVData {
54 /** The audio data */
55 public final ByteBuffer data;
56
57 /** the format of the Data. One of:
58 * <pre>
59 * AL.AL_FORMAT_MONO8
60 * AL.AL_FORMAT_MONO16
61 * AL.AL_FORMAT_STEREO8
62 * AL.AL_FORMAT_STEREO16
63 * </pre>
64 *
65 */
66 public final int format;
67
68 /** Size (in bytes) of the data */
69 public final int size;
70
71 /** The frequency of the data */
72 public final int freq;
73
74 /** flag indicating whether or not the sound in the data should loop */
75 public final boolean loop;
76
77 public WAVData(final ByteBuffer data, final int format, final int size, final int freq, final boolean loop) {
78 this.data = data;
79 this.format = format;
80 this.size = size;
81 this.freq = freq;
82 this.loop = loop;
83 }
84
85 /**
86 * This method loads a (.wav) file into a WAVData object.
87 * @param aIn An InputStream for the .WAV stream
88 * @param byteCount byte count of expected wav data to be read
89 * @param numChannels
90 * @param bits
91 * @param sampleRate
92 * @param byteOrder
93 * @return a WAVData object containing the audio data
94 *
95 * @throws UnsupportedAudioFileException if the format of the audio if not
96 * supported.
97 * @throws IOException If the file can no be found or some other IO error
98 * occurs
99 */
100 public static WAVData loadFromStream(InputStream aIn, final int byteCount, final int numChannels, final int bits, final int sampleRate, final ByteOrder byteOrder, final boolean loop)
101 throws IOException {
102 if( !(aIn instanceof BufferedInputStream) ) {
103 aIn = new BufferedInputStream(aIn);
104 }
105 // ReadableByteChannel aChannel = Channels.newChannel(aIn);
107
108 if ((bits == 8) && (numChannels == 1)) {
110 } else if ((bits == 16) && (numChannels == 1)) {
112 } else if ((bits == 8) && (numChannels == 2)) {
114 } else if ((bits == 16) && (numChannels == 2)) {
116 }
117 final ByteBuffer buffer = IOUtil.copyStreamChunk2ByteBuffer(aIn, 0, byteCount);
118 final int actualSize = buffer.limit();
119
120 // Must byte swap in case endianess mismatch
121 if ( bits == 16 && ByteOrder.nativeOrder() != byteOrder ) {
122 final int len = buffer.remaining();
123 for (int i = 0; i < len; i += 2) {
124 final byte a = buffer.get(i);
125 final byte b = buffer.get(i+1);
126 buffer.put(i, b);
127 buffer.put(i+1, a);
128 }
129 }
130
131 final WAVData result = new WAVData(buffer, format, actualSize, sampleRate, loop);
132 aIn.close();
133
134 return result;
135 }
136
137}
This class is a holder for WAV (.wav ) file Data returned from the WavLoader, or directly via loadFro...
Definition: WAVData.java:53
final int format
the format of the Data.
Definition: WAVData.java:66
WAVData(final ByteBuffer data, final int format, final int size, final int freq, final boolean loop)
Definition: WAVData.java:77
final ByteBuffer data
The audio data.
Definition: WAVData.java:55
final boolean loop
flag indicating whether or not the sound in the data should loop
Definition: WAVData.java:75
final int size
Size (in bytes) of the data.
Definition: WAVData.java:69
final int freq
The frequency of the data.
Definition: WAVData.java:72
static WAVData loadFromStream(InputStream aIn, final int byteCount, final int numChannels, final int bits, final int sampleRate, final ByteOrder byteOrder, final boolean loop)
This method loads a (.wav) file into a WAVData object.
Definition: WAVData.java:100
static final int AL_FORMAT_MONO8
Define "AL_FORMAT_MONO8" with expression '0x1100', CType: int.
static final int AL_FORMAT_STEREO16
Define "AL_FORMAT_STEREO16" with expression '0x1103', CType: int.
static final int AL_FORMAT_STEREO8
Define "AL_FORMAT_STEREO8" with expression '0x1102', CType: int.
static final int AL_FORMAT_MONO16
Define "AL_FORMAT_MONO16" with expression '0x1101', CType: int.