Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/openal/AiffData.java
1461 views
1
/*
2
* Copyright (c) 2002-2004 LWJGL Project
3
* 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
7
* met:
8
*
9
* * Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
*
12
* * Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* * Neither the name of 'LWJGL' nor the names of
17
* its contributors may be used to endorse or promote products derived
18
* from this software without specific prior written permission.
19
*
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
*/
32
package org.newdawn.slick.openal;
33
34
import java.io.BufferedInputStream;
35
import java.io.ByteArrayInputStream;
36
import java.io.IOException;
37
import java.io.InputStream;
38
import java.net.URL;
39
import java.nio.ByteBuffer;
40
import java.nio.ByteOrder;
41
import java.nio.ShortBuffer;
42
43
import javax.sound.sampled.AudioFormat;
44
import javax.sound.sampled.AudioInputStream;
45
import javax.sound.sampled.AudioSystem;
46
import javax.sound.sampled.AudioFormat.Encoding;
47
48
import org.lwjgl.openal.AL10;
49
50
/**
51
*
52
* Utitlity class for loading wavefiles.
53
*
54
* @author Brian Matzon <[email protected]>
55
* @version $Revision: 2286 $
56
*/
57
public class AiffData {
58
/** actual AIFF data */
59
public final ByteBuffer data;
60
61
/** format type of data */
62
public final int format;
63
64
/** sample rate of data */
65
public final int samplerate;
66
67
/**
68
* Creates a new AiffData
69
*
70
* @param data actual Aiffdata
71
* @param format format of Aiff data
72
* @param samplerate sample rate of data
73
*/
74
private AiffData(ByteBuffer data, int format, int samplerate) {
75
this.data = data;
76
this.format = format;
77
this.samplerate = samplerate;
78
}
79
80
/**
81
* Disposes the Aiffdata
82
*/
83
public void dispose() {
84
data.clear();
85
}
86
87
/**
88
* Creates a AiffData container from the specified url
89
*
90
* @param path URL to file
91
* @return AiffData containing data, or null if a failure occured
92
*/
93
public static AiffData create(URL path) {
94
try {
95
return create(
96
AudioSystem.getAudioInputStream(
97
new BufferedInputStream(path.openStream())));
98
} catch (Exception e) {
99
org.lwjgl.LWJGLUtil.log("Unable to create from: " + path);
100
e.printStackTrace();
101
return null;
102
}
103
}
104
105
/**
106
* Creates a AiffData container from the specified in the classpath
107
*
108
* @param path path to file (relative, and in classpath)
109
* @return AiffData containing data, or null if a failure occured
110
*/
111
public static AiffData create(String path) {
112
return create(AiffData.class.getClassLoader().getResource(path));
113
}
114
115
/**
116
* Creates a AiffData container from the specified inputstream
117
*
118
* @param is InputStream to read from
119
* @return AiffData containing data, or null if a failure occured
120
*/
121
public static AiffData create(InputStream is) {
122
try {
123
return create(
124
AudioSystem.getAudioInputStream(is));
125
} catch (Exception e) {
126
org.lwjgl.LWJGLUtil.log("Unable to create from inputstream");
127
e.printStackTrace();
128
return null;
129
}
130
}
131
132
/**
133
* Creates a AiffData container from the specified bytes
134
*
135
* @param buffer array of bytes containing the complete Aiff file
136
* @return AiffData containing data, or null if a failure occured
137
*/
138
public static AiffData create(byte[] buffer) {
139
try {
140
return create(
141
AudioSystem.getAudioInputStream(
142
new BufferedInputStream(new ByteArrayInputStream(buffer))));
143
} catch (Exception e) {
144
e.printStackTrace();
145
return null;
146
}
147
}
148
149
/**
150
* Creates a AiffData container from the specified ByetBuffer.
151
* If the buffer is backed by an array, it will be used directly,
152
* else the contents of the buffer will be copied using get(byte[]).
153
*
154
* @param buffer ByteBuffer containing sound file
155
* @return AiffData containing data, or null if a failure occured
156
*/
157
public static AiffData create(ByteBuffer buffer) {
158
try {
159
byte[] bytes = null;
160
161
if(buffer.hasArray()) {
162
bytes = buffer.array();
163
} else {
164
bytes = new byte[buffer.capacity()];
165
buffer.get(bytes);
166
}
167
return create(bytes);
168
} catch (Exception e) {
169
e.printStackTrace();
170
return null;
171
}
172
}
173
174
/**
175
* Creates a AiffData container from the specified stream
176
*
177
* @param ais AudioInputStream to read from
178
* @return AiffData containing data, or null if a failure occured
179
*/
180
public static AiffData create(AudioInputStream ais) {
181
//get format of data
182
AudioFormat audioformat = ais.getFormat();
183
184
// get channels
185
int channels = 0;
186
if (audioformat.getChannels() == 1) {
187
if (audioformat.getSampleSizeInBits() == 8) {
188
channels = AL10.AL_FORMAT_MONO8;
189
} else if (audioformat.getSampleSizeInBits() == 16) {
190
channels = AL10.AL_FORMAT_MONO16;
191
} else {
192
throw new RuntimeException("Illegal sample size");
193
}
194
} else if (audioformat.getChannels() == 2) {
195
if (audioformat.getSampleSizeInBits() == 8) {
196
channels = AL10.AL_FORMAT_STEREO8;
197
} else if (audioformat.getSampleSizeInBits() == 16) {
198
channels = AL10.AL_FORMAT_STEREO16;
199
} else {
200
throw new RuntimeException("Illegal sample size");
201
}
202
} else {
203
throw new RuntimeException("Only mono or stereo is supported");
204
}
205
206
//read data into buffer
207
byte[] buf =
208
new byte[audioformat.getChannels()
209
* (int) ais.getFrameLength()
210
* audioformat.getSampleSizeInBits()
211
/ 8];
212
int read = 0, total = 0;
213
try {
214
while ((read = ais.read(buf, total, buf.length - total)) != -1
215
&& total < buf.length) {
216
total += read;
217
}
218
} catch (IOException ioe) {
219
return null;
220
}
221
222
//insert data into bytebuffer
223
ByteBuffer buffer = convertAudioBytes(audioformat, buf, audioformat.getSampleSizeInBits() == 16);
224
225
//create our result
226
AiffData Aiffdata =
227
new AiffData(buffer, channels, (int) audioformat.getSampleRate());
228
229
//close stream
230
try {
231
ais.close();
232
} catch (IOException ioe) {
233
}
234
235
return Aiffdata;
236
}
237
238
/**
239
* Convert the audio bytes into the stream
240
*
241
* @param format The audio format being decoded
242
* @param audio_bytes The audio byts
243
* @param two_bytes_data True if we using double byte data
244
* @return The byte bufer of data
245
*/
246
private static ByteBuffer convertAudioBytes(AudioFormat format, byte[] audio_bytes, boolean two_bytes_data) {
247
ByteBuffer dest = ByteBuffer.allocateDirect(audio_bytes.length);
248
dest.order(ByteOrder.nativeOrder());
249
ByteBuffer src = ByteBuffer.wrap(audio_bytes);
250
src.order(ByteOrder.BIG_ENDIAN);
251
if (two_bytes_data) {
252
ShortBuffer dest_short = dest.asShortBuffer();
253
ShortBuffer src_short = src.asShortBuffer();
254
while (src_short.hasRemaining())
255
dest_short.put(src_short.get());
256
} else {
257
while (src.hasRemaining()) {
258
byte b = src.get();
259
if (format.getEncoding() == Encoding.PCM_SIGNED) {
260
b = (byte) (b + 127);
261
}
262
dest.put(b);
263
}
264
}
265
dest.rewind();
266
return dest;
267
}
268
}
269
270