author | koda |
Sun, 12 Jul 2009 15:42:54 +0000 | |
changeset 2259 | ca42efdce3ce |
parent 2257 | 7eb31efcfb9b |
child 2260 | 31756e21c436 |
permissions | -rw-r--r-- |
2191 | 1 |
/* |
2 |
* OpenAL Bridge - a simple portable library for OpenAL interface |
|
3 |
* Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
6 |
* it under the terms of the GNU Lesser General Public License as published by |
2191 | 7 |
* the Free Software Foundation; version 2 of the License |
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
12 |
* GNU Lesser General Public License for more details. |
2191 | 13 |
* |
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
14 |
* You should have received a copy of the GNU Lesser General Public License |
2191 | 15 |
* along with this program; if not, write to the Free Software |
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
2213 | 19 |
#include "globals.h" |
20 |
#include "wrappers.h" |
|
21 |
#include "alc.h" |
|
22 |
#include "loaders.h" |
|
23 |
#include "endianness.h" |
|
2191 | 24 |
|
25 |
#ifdef __CPLUSPLUS |
|
26 |
extern "C" { |
|
27 |
#endif |
|
2259 | 28 |
|
29 |
/*Sources are points emitting sound*/ |
|
30 |
ALuint *Sources; |
|
31 |
/*Buffers hold sound data*/ |
|
32 |
ALuint *Buffers; |
|
33 |
/*index for Sources and Buffers*/ |
|
34 |
ALuint globalindex, globalsize, increment; |
|
35 |
||
36 |
ALboolean openalReady = AL_FALSE; |
|
37 |
||
38 |
ALboolean openal_close (void) { |
|
39 |
/*Stop all sounds, deallocate all memory and close OpenAL */ |
|
40 |
ALCcontext *context; |
|
41 |
ALCdevice *device; |
|
42 |
||
43 |
if(openalReady == AL_FALSE) { |
|
44 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
45 |
return AL_FALSE; |
|
46 |
} |
|
47 |
||
48 |
alSourceStopv (globalsize, Sources); |
|
49 |
alDeleteSources (globalsize, Sources); |
|
50 |
alDeleteBuffers (globalsize, Buffers); |
|
51 |
||
52 |
free(Sources); |
|
53 |
free(Buffers); |
|
54 |
||
55 |
context = alcGetCurrentContext(); |
|
56 |
device = alcGetContextsDevice(context); |
|
57 |
||
58 |
alcMakeContextCurrent(NULL); |
|
59 |
alcDestroyContext(context); |
|
60 |
alcCloseDevice(device); |
|
61 |
||
62 |
openalReady = AL_FALSE; |
|
63 |
||
64 |
return AL_TRUE; |
|
65 |
} |
|
66 |
||
67 |
ALboolean openal_ready(void) { |
|
68 |
return openalReady; |
|
69 |
} |
|
70 |
||
71 |
ALboolean openal_init(uint32_t memorysize) { |
|
72 |
/*Initialize an OpenAL contex and allocate memory space for data and buffers*/ |
|
73 |
ALCcontext *context; |
|
74 |
ALCdevice *device; |
|
75 |
const ALCchar *default_device; |
|
76 |
||
77 |
/*Position of the listener*/ |
|
78 |
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; |
|
79 |
/*Velocity of the listener*/ |
|
80 |
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; |
|
81 |
/*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/ |
|
82 |
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; |
|
83 |
||
84 |
if(openalReady == AL_TRUE) { |
|
85 |
fprintf(stderr, "ERROR: OpenAL already initialized\n"); |
|
86 |
return AL_FALSE; |
|
87 |
} |
|
88 |
||
89 |
default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); |
|
90 |
fprintf(stderr, "Using default device: %s\n", default_device); |
|
91 |
||
92 |
if ((device = alcOpenDevice(default_device)) == NULL) { |
|
93 |
fprintf(stderr, "ERROR: Failed to open sound device\n"); |
|
94 |
return AL_FALSE; |
|
95 |
} |
|
96 |
||
97 |
context = alcCreateContext(device, NULL); |
|
98 |
alcMakeContextCurrent(context); |
|
99 |
alcProcessContext(context); |
|
100 |
||
101 |
if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE) |
|
102 |
return AL_FALSE; |
|
103 |
||
104 |
/*allocate memory space for buffers and sources*/ |
|
105 |
globalsize = memorysize; |
|
106 |
increment = memorysize; |
|
107 |
Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
|
108 |
Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
|
109 |
||
110 |
/*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/ |
|
111 |
alListenerf (AL_GAIN, 1.0f ); |
|
112 |
alListenerfv(AL_POSITION, ListenerPos); |
|
113 |
alListenerfv(AL_VELOCITY, ListenerVel); |
|
114 |
alListenerfv(AL_ORIENTATION, ListenerOri); |
|
115 |
||
116 |
if (AlGetError("ERROR %d: Setting Listener properties\n") != AL_TRUE) |
|
117 |
return AL_FALSE; |
|
118 |
||
119 |
openalReady = AL_TRUE; |
|
120 |
||
121 |
alGetError(); /* clear any AL errors beforehand */ |
|
122 |
return AL_TRUE; |
|
123 |
} |
|
124 |
||
125 |
||
126 |
ALboolean helper_realloc (void) { |
|
127 |
/*expands allocated memory when loading more sound files than expected*/ |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
128 |
#ifdef DEBUG |
2259 | 129 |
fprintf(stderr, "OpenAL: Realloc in process %d\n", globalsize); |
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
130 |
#endif |
2259 | 131 |
globalsize += increment; |
2191 | 132 |
|
2259 | 133 |
Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize); |
134 |
Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize); |
|
135 |
||
136 |
return AL_TRUE; |
|
137 |
} |
|
138 |
||
139 |
||
140 |
ALint openal_loadfile (const char *filename){ |
|
141 |
/*Open a file, load into memory and allocate the Source buffer for playing*/ |
|
142 |
ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/ |
|
143 |
ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/ |
|
144 |
ALenum format; |
|
145 |
ALsizei bitsize, freq; |
|
146 |
char *data; |
|
147 |
uint32_t fileformat; |
|
148 |
ALenum error; |
|
149 |
FILE *fp; |
|
150 |
||
151 |
if(openalReady == AL_FALSE) { |
|
152 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
153 |
return AL_FALSE; |
|
154 |
} |
|
155 |
||
156 |
/*when the buffers are all used, we can expand memory to accept new files*/ |
|
157 |
if (globalindex == globalsize) |
|
158 |
helper_realloc(); |
|
159 |
||
160 |
/*detect the file format, as written in the first 4 bytes of the header*/ |
|
161 |
fp = Fopen (filename, "rb"); |
|
162 |
if (fp == NULL) |
|
163 |
return -1; |
|
164 |
error = fread (&fileformat, sizeof(uint32_t), 1, fp); |
|
165 |
fclose (fp); |
|
166 |
||
167 |
if (error < 0) { |
|
168 |
fprintf(stderr, "ERROR: file %s is too short \n", filename); |
|
169 |
return -2; |
|
170 |
} |
|
171 |
||
172 |
/*prepare the buffer to receive data*/ |
|
173 |
alGenBuffers(1, &Buffers[globalindex]); |
|
174 |
||
175 |
if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE) |
|
176 |
return -3; |
|
177 |
||
178 |
/*prepare the source to emit sound*/ |
|
179 |
alGenSources(1, &Sources[globalindex]); |
|
180 |
||
181 |
if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE) |
|
182 |
return -4; |
|
183 |
||
184 |
||
185 |
if (fileformat == 0x5367674F) /*check if ogg*/ |
|
186 |
error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); |
|
187 |
else { |
|
188 |
if (fileformat == 0x46464952) /*check if wav*/ |
|
189 |
error = load_wavpcm (filename, &format, &data, &bitsize, &freq); |
|
190 |
else { |
|
191 |
fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat)); |
|
192 |
return -5; |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
/*copy pcm data in one buffer*/ |
|
197 |
alBufferData(Buffers[globalindex], format, data, bitsize, freq); |
|
198 |
free(data); /*deallocate data to save memory*/ |
|
199 |
||
200 |
if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE) |
|
201 |
return -6; |
|
202 |
||
203 |
/*set source properties that it will use when it's in playback*/ |
|
204 |
alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); |
|
205 |
alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); |
|
206 |
alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); |
|
207 |
alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); |
|
208 |
alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); |
|
209 |
alSourcei (Sources[globalindex], AL_LOOPING, 0 ); |
|
210 |
||
211 |
if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE) |
|
212 |
return -7; |
|
213 |
||
214 |
alGetError(); /* clear any AL errors beforehand */ |
|
215 |
||
216 |
/*returns the index of the source you just loaded, increments it and exits*/ |
|
217 |
return globalindex++; |
|
218 |
} |
|
219 |
||
220 |
||
221 |
ALboolean openal_toggleloop (uint32_t index){ |
|
222 |
/*Set or unset looping mode*/ |
|
223 |
ALint loop; |
|
224 |
||
225 |
if(openalReady == AL_FALSE) { |
|
226 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
227 |
return AL_FALSE; |
|
228 |
} |
|
229 |
||
230 |
if (index >= globalsize) { |
|
231 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
232 |
return AL_FALSE; |
|
233 |
} |
|
234 |
||
235 |
alGetSourcei (Sources[index], AL_LOOPING, &loop); |
|
236 |
alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); |
|
237 |
if (AlGetError("ERROR %d: Getting or setting loop property\n") != AL_TRUE) |
|
238 |
return AL_FALSE; |
|
239 |
||
240 |
alGetError(); /* clear any AL errors beforehand */ |
|
241 |
||
242 |
return AL_TRUE; |
|
243 |
} |
|
244 |
||
245 |
||
246 |
ALboolean openal_setvolume (uint32_t index, uint8_t percentage) { |
|
247 |
if(openalReady == AL_FALSE) { |
|
248 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
249 |
return AL_FALSE; |
|
250 |
} |
|
251 |
||
252 |
/*Set volume for sound number index*/ |
|
253 |
if (index >= globalindex) { |
|
254 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
255 |
return AL_FALSE; |
|
256 |
} |
|
257 |
||
258 |
if (percentage > 100) |
|
259 |
percentage = 100; |
|
260 |
alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f); |
|
261 |
if (AlGetError2("ERROR %d: setting volume for sound %d\n", index) != AL_TRUE) |
|
262 |
return AL_FALSE; |
|
263 |
||
264 |
alGetError(); /* clear any AL errors beforehand */ |
|
265 |
||
266 |
return AL_TRUE; |
|
267 |
} |
|
268 |
||
269 |
||
270 |
ALboolean openal_setglobalvolume (uint8_t percentage) { |
|
271 |
if(openalReady == AL_FALSE) { |
|
272 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
273 |
return AL_FALSE; |
|
274 |
} |
|
275 |
||
276 |
/*Set volume for all sounds*/ |
|
277 |
if (percentage > 100) |
|
278 |
percentage = 100; |
|
279 |
alListenerf (AL_GAIN, (float) percentage/100.0f); |
|
280 |
if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE) |
|
281 |
return AL_FALSE; |
|
282 |
||
283 |
alGetError(); /* clear any AL errors beforehand */ |
|
284 |
||
285 |
return AL_TRUE; |
|
286 |
} |
|
287 |
||
288 |
||
289 |
ALboolean openal_togglemute () { |
|
290 |
/*Mute or unmute sound*/ |
|
291 |
ALfloat mute; |
|
292 |
||
293 |
if(openalReady == AL_FALSE) { |
|
294 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
295 |
return AL_FALSE; |
|
296 |
} |
|
297 |
||
298 |
alGetListenerf (AL_GAIN, &mute); |
|
299 |
if (mute > 0) |
|
300 |
mute = 0; |
|
301 |
else |
|
302 |
mute = 1.0; |
|
303 |
alListenerf (AL_GAIN, mute); |
|
304 |
if (AlGetError("ERROR %d: Setting mute property\n") != AL_TRUE) |
|
305 |
return AL_FALSE; |
|
306 |
||
307 |
alGetError(); /* clear any AL errors beforehand */ |
|
308 |
||
309 |
return AL_TRUE; |
|
310 |
} |
|
311 |
||
312 |
||
313 |
ALboolean openal_fade (uint32_t index, uint16_t quantity, bool direction) { |
|
314 |
/*Fade in or out by calling a helper thread*/ |
|
2194
1597710c6118
koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents:
2191
diff
changeset
|
315 |
#ifndef _WIN32 |
2259 | 316 |
pthread_t thread; |
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
317 |
#else |
2259 | 318 |
HANDLE Thread; |
319 |
DWORD threadID; |
|
320 |
#endif |
|
321 |
fade_t *fade; |
|
322 |
||
323 |
if(openalReady == AL_FALSE) { |
|
324 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
325 |
return AL_FALSE; |
|
326 |
} |
|
327 |
||
328 |
fade = (fade_t*) Malloc(sizeof(fade_t)); |
|
329 |
fade->index = index; |
|
330 |
fade->quantity = quantity; |
|
331 |
||
332 |
if (index >= globalindex) { |
|
333 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
334 |
return AL_FALSE; |
|
335 |
} |
|
336 |
||
337 |
if (direction == FADE_IN) |
|
338 |
#ifndef _WIN32 |
|
339 |
pthread_create(&thread, NULL, helper_fadein, (void*) fade); |
|
340 |
#else |
|
341 |
Thread = _beginthread(&helper_fadein, 0, (void*) fade); |
|
342 |
#endif |
|
343 |
else { |
|
344 |
if (direction == FADE_OUT) |
|
345 |
#ifndef _WIN32 |
|
346 |
pthread_create(&thread, NULL, helper_fadeout, (void*) fade); |
|
347 |
#else |
|
348 |
Thread = _beginthread(&helper_fadeout, 0, (void*) fade); |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
349 |
#endif |
2259 | 350 |
else { |
351 |
fprintf(stderr, "ERROR: unknown direction for fade (%d)\n", direction); |
|
352 |
free(fade); |
|
353 |
return AL_FALSE; |
|
354 |
} |
|
355 |
} |
|
356 |
||
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
357 |
#ifndef _WIN32 |
2259 | 358 |
pthread_detach(thread); |
2191 | 359 |
#endif |
2259 | 360 |
|
361 |
alGetError(); /* clear any AL errors beforehand */ |
|
362 |
||
363 |
return AL_TRUE; |
|
364 |
} |
|
365 |
||
366 |
||
367 |
ALboolean openal_fadeout (uint32_t index, uint16_t quantity) { |
|
368 |
/*wrapper for fadeout*/ |
|
369 |
return openal_fade(index, quantity, FADE_OUT); |
|
370 |
} |
|
371 |
||
372 |
||
373 |
ALboolean openal_fadein (uint32_t index, uint16_t quantity) { |
|
374 |
/*wrapper for fadein*/ |
|
375 |
return openal_fade(index, quantity, FADE_IN); |
|
376 |
} |
|
377 |
||
378 |
||
379 |
ALboolean openal_setposition (uint32_t index, float x, float y, float z) { |
|
380 |
if(openalReady == AL_FALSE) { |
|
381 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
382 |
return AL_FALSE; |
|
383 |
} |
|
384 |
if (index >= globalindex) { |
|
385 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
386 |
return AL_FALSE; |
|
387 |
} |
|
388 |
||
389 |
alSource3f(Sources[index], AL_POSITION, x, y, z); |
|
390 |
if (AlGetError2("ERROR %d: setting position for sound %d\n", index) != AL_TRUE) |
|
391 |
return AL_FALSE; |
|
392 |
||
393 |
return AL_TRUE; |
|
394 |
} |
|
395 |
||
396 |
||
397 |
ALboolean openal_playsound (uint32_t index){ |
|
398 |
if(openalReady == AL_FALSE) { |
|
399 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
400 |
return AL_FALSE; |
|
401 |
} |
|
402 |
||
403 |
/*Play sound number index*/ |
|
404 |
if (index >= globalindex) { |
|
405 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
406 |
return AL_FALSE; |
|
407 |
} |
|
408 |
alSourcePlay(Sources[index]); |
|
409 |
if (AlGetError2("ERROR %d: Playing sound %d\n", index) != AL_TRUE) |
|
410 |
return AL_FALSE; |
|
411 |
||
412 |
alGetError(); /* clear any AL errors beforehand */ |
|
413 |
||
414 |
return AL_TRUE; |
|
415 |
} |
|
416 |
||
417 |
||
418 |
ALboolean openal_pausesound(uint32_t index){ |
|
419 |
if(openalReady == AL_FALSE) { |
|
420 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
421 |
return AL_FALSE; |
|
422 |
} |
|
423 |
||
424 |
/*Pause sound number index*/ |
|
425 |
if (index >= globalindex) { |
|
426 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
427 |
return AL_FALSE; |
|
428 |
} |
|
429 |
alSourcePause(Sources[index]); |
|
430 |
if (AlGetError2("ERROR %d: Pausing sound %d\n", index) != AL_TRUE) |
|
431 |
return AL_FALSE; |
|
432 |
||
433 |
return AL_TRUE; |
|
434 |
} |
|
435 |
||
436 |
||
437 |
ALboolean openal_stopsound(uint32_t index){ |
|
438 |
if(openalReady == AL_FALSE) { |
|
439 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
440 |
return AL_FALSE; |
|
441 |
} |
|
442 |
||
443 |
/*Stop sound number index*/ |
|
444 |
if (index >= globalindex) { |
|
445 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
446 |
return AL_FALSE; |
|
447 |
} |
|
448 |
alSourceStop(Sources[index]); |
|
449 |
if (AlGetError2("ERROR %d: Stopping sound %d\n", index) != AL_TRUE) |
|
450 |
return AL_FALSE; |
|
451 |
||
452 |
alGetError(); /* clear any AL errors beforehand */ |
|
453 |
||
454 |
return AL_TRUE; |
|
455 |
} |
|
456 |
||
2191 | 457 |
#ifdef __CPLUSPLUS |
458 |
} |
|
459 |
#endif |