14 * You should have received a copy of the GNU General Public License |
14 * You should have received a copy of the GNU General Public License |
15 * along with this program; if not, write to the Free Software |
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 |
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
17 */ |
17 */ |
18 |
18 |
19 #include "openalwrap.h" |
19 #include "globals.h" |
|
20 #include "wrappers.h" |
|
21 #include "alc.h" |
|
22 #include "loaders.h" |
|
23 #include "endianness.h" |
20 |
24 |
21 #ifdef __CPLUSPLUS |
25 #ifdef __CPLUSPLUS |
22 extern "C" { |
26 extern "C" { |
23 #endif |
27 #endif |
24 |
28 |
25 /*Sources are points emitting sound*/ |
29 /*Sources are points emitting sound*/ |
26 ALuint *Sources; |
30 ALuint *Sources; |
27 /*Buffers hold sound data*/ |
31 /*Buffers hold sound data*/ |
28 ALuint *Buffers; |
32 ALuint *Buffers; |
29 /*index for Sources and Buffers*/ |
33 /*index for Sources and Buffers*/ |
30 ALuint globalindex, globalsize; |
34 ALuint globalindex, globalsize, increment; |
31 /*Position of the source sound*/ |
35 /*Position of the source sound*/ |
32 ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; |
36 ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; |
33 /*Velocity of the source sound*/ |
37 /*Velocity of the source sound*/ |
34 ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; |
38 ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; |
35 |
39 |
36 int increment; |
|
37 |
40 |
38 ALint openal_close(void) { |
41 ALint openal_close(void) { |
39 /* This function stops all the sounds, deallocates all memory and closes OpenAL */ |
42 /*Stop all sounds, deallocate all memory and close OpenAL */ |
40 ALCcontext *context; |
43 ALCcontext *context; |
41 ALCdevice *device; |
44 ALCdevice *device; |
42 |
45 |
43 alSourceStopv (globalsize, Sources); |
46 alSourceStopv (globalsize, Sources); |
44 alDeleteSources (globalsize, Sources); |
47 alDeleteSources (globalsize, Sources); |
102 |
105 |
103 alGetError(); /* clear any AL errors beforehand */ |
106 alGetError(); /* clear any AL errors beforehand */ |
104 return AL_TRUE; |
107 return AL_TRUE; |
105 } |
108 } |
106 |
109 |
|
110 |
107 int helper_realloc (void) { |
111 int helper_realloc (void) { |
|
112 /*expands allocated memory when loading more sound files than expected*/ |
108 globalsize += increment; |
113 globalsize += increment; |
109 #ifdef DEBUG |
114 #ifdef DEBUG |
110 fprintf(stderr, "OpenAL: Realloc in process %d\n", globalsize); |
115 fprintf(stderr, "OpenAL: Realloc in process %d\n", globalsize); |
111 #endif |
116 #endif |
112 Buffers = (ALuint*) reallocf(Buffers, sizeof(ALuint)*globalsize); |
117 Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize); |
113 Sources = (ALuint*) reallocf(Sources, sizeof(ALuint)*globalsize); |
118 Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize); |
114 if (Buffers == NULL || Sources == NULL) { |
119 |
115 fprintf(stderr, "ERROR: not enough memory! realloc() failed\n"); |
120 return 0; |
116 exit(-1); |
|
117 } else { |
|
118 return 0; |
|
119 } |
|
120 } |
121 } |
121 |
122 |
122 |
123 |
123 int openal_loadfile (const char *filename){ |
124 int openal_loadfile (const char *filename){ |
124 /* This function opens a file, loads into memory and allocates the Source buffer for playing*/ |
125 /*Open a file, load into memory and allocate the Source buffer for playing*/ |
125 ALenum format; |
126 ALenum format; |
126 ALsizei bitsize; |
127 ALsizei bitsize; |
127 ALsizei freq; |
128 ALsizei freq; |
128 uint8_t *data; |
129 char *data; |
129 uint32_t fileformat; |
130 uint32_t fileformat; |
130 int error; |
131 int error; |
131 FILE *fp; |
132 FILE *fp; |
132 |
133 |
133 |
134 |
|
135 /*when the buffers are all used, we can expand memory to accept new files*/ |
134 if (globalindex == globalsize) |
136 if (globalindex == globalsize) |
135 helper_realloc(); |
137 helper_realloc(); |
136 |
138 |
137 /*detect the file format, as written in the first 4 bytes of the header*/ |
139 /*detect the file format, as written in the first 4 bytes of the header*/ |
138 fp = Fopen (filename, "rb"); |
140 fp = Fopen (filename, "rb"); |
222 return AL_FALSE; |
224 return AL_FALSE; |
223 } |
225 } |
224 |
226 |
225 if (percentage > 100) |
227 if (percentage > 100) |
226 percentage = 100; |
228 percentage = 100; |
227 alSourcef (Sources[index], AL_GAIN, (ALfloat) percentage/100.0f); |
229 alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f); |
228 if (AlGetError("ERROR %d: Setting volume for last sound\n") != AL_TRUE) |
230 if (AlGetError("ERROR %d: Setting volume for last sound\n") != AL_TRUE) |
229 return AL_FALSE; |
231 return AL_FALSE; |
230 |
232 |
231 alGetError(); /* clear any AL errors beforehand */ |
233 alGetError(); /* clear any AL errors beforehand */ |
232 |
234 |
236 |
238 |
237 ALint openal_setglobalvolume (unsigned char percentage) { |
239 ALint openal_setglobalvolume (unsigned char percentage) { |
238 /*Set volume for all sounds*/ |
240 /*Set volume for all sounds*/ |
239 if (percentage > 100) |
241 if (percentage > 100) |
240 percentage = 100; |
242 percentage = 100; |
241 alListenerf (AL_GAIN, (ALfloat) percentage/100.0f); |
243 alListenerf (AL_GAIN, (float) percentage/100.0f); |
242 if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE) |
244 if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE) |
243 return AL_FALSE; |
245 return AL_FALSE; |
244 |
246 |
245 alGetError(); /* clear any AL errors beforehand */ |
247 alGetError(); /* clear any AL errors beforehand */ |
246 |
248 |
283 if (index >= globalindex) { |
286 if (index >= globalindex) { |
284 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
287 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
285 return AL_FALSE; |
288 return AL_FALSE; |
286 } |
289 } |
287 |
290 |
288 if (inout == FADE_IN) |
291 if (direction == FADE_IN) |
289 #ifndef _WIN32 |
292 #ifndef _WIN32 |
290 pthread_create(&thread, NULL, helper_fadein, (void*) fade); |
293 pthread_create(&thread, NULL, helper_fadein, (void*) fade); |
291 #else |
294 #else |
292 Thread = _beginthread(&helper_fadein, 0, (void*) fade); |
295 Thread = _beginthread(&helper_fadein, 0, (void*) fade); |
293 #endif |
296 #endif |
294 else { |
297 else { |
295 if (inout == FADE_OUT) |
298 if (direction == FADE_OUT) |
296 #ifndef _WIN32 |
299 #ifndef _WIN32 |
297 pthread_create(&thread, NULL, helper_fadeout, (void*) fade); |
300 pthread_create(&thread, NULL, helper_fadeout, (void*) fade); |
298 #else |
301 #else |
299 Thread = _beginthread(&helper_fadeout, 0, (void*) fade); |
302 Thread = _beginthread(&helper_fadeout, 0, (void*) fade); |
300 #endif |
303 #endif |
301 else { |
304 else { |
302 fprintf(stderr, "ERROR: unknown direction for fade (%d)\n", inout); |
305 fprintf(stderr, "ERROR: unknown direction for fade (%d)\n", direction); |
303 free(fade); |
306 free(fade); |
304 return AL_FALSE; |
307 return AL_FALSE; |
305 } |
308 } |
306 } |
309 } |
307 |
310 |
314 return AL_TRUE; |
317 return AL_TRUE; |
315 } |
318 } |
316 |
319 |
317 |
320 |
318 ALint openal_fadeout(int index, unsigned int quantity) { |
321 ALint openal_fadeout(int index, unsigned int quantity) { |
|
322 /*wrapper for fadeout*/ |
319 return openal_fade(index, quantity, FADE_OUT); |
323 return openal_fade(index, quantity, FADE_OUT); |
320 } |
324 } |
321 |
325 |
322 |
326 |
323 ALint openal_fadein(int index, unsigned int quantity) { |
327 ALint openal_fadein(int index, unsigned int quantity) { |
|
328 /*wrapper for fadein*/ |
324 return openal_fade(index, quantity, FADE_IN); |
329 return openal_fade(index, quantity, FADE_IN); |
325 } |
330 } |
326 |
331 |
327 |
332 |
328 ALint openal_playsound(int index){ |
333 ALint openal_playsound(int index){ |