80 ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; |
80 ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; |
81 /*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/ |
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 }; |
82 ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; |
83 |
83 |
84 if(openalReady == AL_TRUE) { |
84 if(openalReady == AL_TRUE) { |
85 fprintf(stderr, "ERROR: OpenAL already initialized\n"); |
85 fprintf(stderr, "ERROR 'openal_init()': OpenAL already initialized\n"); |
86 return AL_FALSE; |
86 return AL_FALSE; |
87 } |
87 } |
88 |
88 |
89 if(usehardware) |
89 if(usehardware) |
90 { |
90 { |
91 if ((device = alcOpenDevice(NULL)) == NULL) { |
91 if ((device = alcOpenDevice(NULL)) == NULL) { |
92 fprintf(stderr, "ERROR: Failed to open sound device\n"); |
92 fprintf(stderr, "ERROR 'openal_init()': Failed to open sound device\n"); |
93 return AL_FALSE; |
93 return AL_FALSE; |
94 } |
94 } |
95 } |
95 } |
96 else |
96 else |
97 { |
97 { |
98 if ((device = alcOpenDevice("Generic Software")) == NULL) { |
98 if ((device = alcOpenDevice("Generic Software")) == NULL) { |
99 fprintf(stderr, "ERROR: Failed to open sound device\n"); |
99 fprintf(stderr, "ERROR 'openal_init()': Failed to open sound device\n"); |
100 return AL_FALSE; |
100 return AL_FALSE; |
101 } |
101 } |
102 } |
102 } |
103 fprintf(stderr, "Using default device: %s\n", alcGetString(device, ALC_DEVICE_SPECIFIER)); |
103 fprintf(stderr, "Using default device: %s\n", alcGetString(device, ALC_DEVICE_SPECIFIER)); |
104 |
104 |
105 context = alcCreateContext(device, NULL); |
105 context = alcCreateContext(device, NULL); |
106 alcMakeContextCurrent(context); |
106 alcMakeContextCurrent(context); |
107 alcProcessContext(context); |
107 alcProcessContext(context); |
108 |
108 |
109 if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE) |
109 if (AlGetError("ERROR %d in 'openal_init()': Creating a new contex\n") != AL_TRUE) |
110 return AL_FALSE; |
110 return AL_FALSE; |
111 |
111 |
112 /*allocate memory space for buffers and sources*/ |
112 /*allocate memory space for buffers and sources*/ |
113 if (memorysize == 0) |
113 if (memorysize == 0) |
114 globalsize = 50; |
114 globalsize = 50; |
115 else |
115 else |
116 globalsize = memorysize; |
116 globalsize = memorysize; |
117 increment = globalsize; |
117 increment = globalsize; |
|
118 |
118 Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
119 Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
119 Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
120 Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
120 |
121 |
121 /*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/ |
122 /*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/ |
122 alListenerf (AL_GAIN, 1.0f ); |
123 alListenerf (AL_GAIN, 1.0f ); |
176 return -1; |
177 return -1; |
177 error = fread (&fileformat, sizeof(uint32_t), 1, fp); |
178 error = fread (&fileformat, sizeof(uint32_t), 1, fp); |
178 fclose (fp); |
179 fclose (fp); |
179 |
180 |
180 if (error < 0) { |
181 if (error < 0) { |
181 fprintf(stderr, "ERROR: file %s is too short \n", filename); |
182 fprintf(stderr, "ERROR 'openal_loadfile()': file %s is too short \n", filename); |
182 return -2; |
183 return -2; |
183 } |
184 } |
184 |
185 |
185 /*prepare the buffer to receive data*/ |
186 /*prepare the buffer to receive data*/ |
186 alGenBuffers(1, &Buffers[globalindex]); |
187 alGenBuffers(1, &Buffers[globalindex]); |
187 |
188 |
188 if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE) |
189 if (AlGetError("ERROR %d in 'openal_loadfile()': Allocating memory for buffers\n") != AL_TRUE) |
189 return -3; |
190 return -3; |
190 |
191 |
191 /*prepare the source to emit sound*/ |
192 /*prepare the source to emit sound*/ |
192 alGenSources(1, &Sources[globalindex]); |
193 alGenSources(1, &Sources[globalindex]); |
193 |
194 |
194 if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE) |
195 if (AlGetError("ERROR %d in 'openal_loadfile()': Allocating memory for sources\n") != AL_TRUE) |
195 return -4; |
196 return -4; |
196 |
197 |
197 |
198 |
198 if (fileformat == 0x5367674F) /*check if ogg*/ |
199 switch (ENDIAN_BIG_32(fileformat)) { |
199 error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); |
200 case OGG_FILE_FORMAT: |
200 else { |
201 error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); |
201 if (fileformat == 0x46464952) /*check if wav*/ |
202 break; |
202 error = load_wavpcm (filename, &format, &data, &bitsize, &freq); |
203 case WAV_FILE_FORMAT: |
203 else { |
204 error = load_wavpcm (filename, &format, &data, &bitsize, &freq); |
204 fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat)); |
205 break; |
205 return -5; |
206 default: |
206 } |
207 fprintf(stderr, "ERROR 'openal_loadfile()': File format (%08X) not supported!\n", ENDIAN_BIG_32(fileformat)); |
207 } |
208 return -5; |
208 |
209 break; |
|
210 } |
|
211 |
|
212 |
209 /*copy pcm data in one buffer*/ |
213 /*copy pcm data in one buffer*/ |
210 alBufferData(Buffers[globalindex], format, data, bitsize, freq); |
214 alBufferData(Buffers[globalindex], format, data, bitsize, freq); |
211 free(data); /*deallocate data to save memory*/ |
215 free(data); /*deallocate data to save memory*/ |
212 |
216 |
213 if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE) |
217 if (AlGetError("ERROR %d in 'openal_loadfile()': Writing data to buffer\n") != AL_TRUE) |
214 return -6; |
218 return -6; |
215 |
219 |
216 /*set source properties that it will use when it's in playback*/ |
220 /*set source properties that it will use when it's in playback*/ |
217 alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); |
221 alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); |
218 alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); |
222 alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); |
219 alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); |
223 alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); |
220 alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); |
224 alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); |
221 alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); |
225 alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); |
222 alSourcei (Sources[globalindex], AL_LOOPING, 0 ); |
226 alSourcei (Sources[globalindex], AL_LOOPING, 0 ); |
223 |
227 |
224 if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE) |
228 if (AlGetError("ERROR %d in 'openal_loadfile()': Setting source properties\n") != AL_TRUE) |
225 return -7; |
229 return -7; |
226 |
230 |
227 alGetError(); /* clear any AL errors beforehand */ |
231 alGetError(); /* clear any AL errors beforehand */ |
228 |
232 |
229 /*returns the index of the source you just loaded, increments it and exits*/ |
233 /*returns the index of the source you just loaded, increments it and exits*/ |
234 ALboolean openal_toggleloop (uint32_t index){ |
238 ALboolean openal_toggleloop (uint32_t index){ |
235 /*Set or unset looping mode*/ |
239 /*Set or unset looping mode*/ |
236 ALint loop; |
240 ALint loop; |
237 |
241 |
238 if(openalReady == AL_FALSE) { |
242 if(openalReady == AL_FALSE) { |
239 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
243 fprintf(stderr, "ERROR 'openal_toggleloop()': OpenAL not initialized\n"); |
240 return AL_FALSE; |
244 return AL_FALSE; |
241 } |
245 } |
242 |
246 |
243 if (index >= globalsize) { |
247 if (index >= globalsize) { |
244 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
248 fprintf(stderr, "ERROR 'openal_toggleloop()': index out of bounds (got %d, max %d)\n", index, globalindex); |
245 return AL_FALSE; |
249 return AL_FALSE; |
246 } |
250 } |
247 |
251 |
248 alGetSourcei (Sources[index], AL_LOOPING, &loop); |
252 alGetSourcei (Sources[index], AL_LOOPING, &loop); |
249 alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); |
253 alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); |
250 if (AlGetError("ERROR %d: Getting or setting loop property\n") != AL_TRUE) |
254 if (AlGetError("ERROR %d in 'openal_toggleloop()': Getting or setting loop property\n") != AL_TRUE) |
251 return AL_FALSE; |
255 return AL_FALSE; |
252 |
256 |
253 alGetError(); /* clear any AL errors beforehand */ |
257 alGetError(); /* clear any AL errors beforehand */ |
254 |
258 |
255 return AL_TRUE; |
259 return AL_TRUE; |
256 } |
260 } |
257 |
261 |
258 |
262 |
259 ALboolean openal_setvolume (uint32_t index, uint8_t percentage) { |
263 ALboolean openal_setvolume (uint32_t index, uint8_t percentage) { |
260 if(openalReady == AL_FALSE) { |
264 if(openalReady == AL_FALSE) { |
261 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
265 fprintf(stderr, "ERROR 'openal_setvolume()': OpenAL not initialized\n"); |
262 return AL_FALSE; |
266 return AL_FALSE; |
263 } |
267 } |
264 |
268 |
265 /*Set volume for sound number index*/ |
269 /*Set volume for sound number index*/ |
266 if (index >= globalindex) { |
270 if (index >= globalindex) { |
267 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
271 fprintf(stderr, "ERROR 'openal_setvolume()': index out of bounds (got %d, max %d)\n", index, globalindex); |
268 return AL_FALSE; |
272 return AL_FALSE; |
269 } |
273 } |
270 |
274 |
271 if (percentage > 100) |
275 if (percentage > 100) |
272 percentage = 100; |
276 percentage = 100; |
273 alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f); |
277 alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f); |
274 if (AlGetError2("ERROR %d: setting volume for sound %d\n", index) != AL_TRUE) |
278 if (AlGetError2("ERROR %d in 'openal_setvolume()': setting volume for sound %d\n", index) != AL_TRUE) |
275 return AL_FALSE; |
279 return AL_FALSE; |
276 |
280 |
277 alGetError(); /* clear any AL errors beforehand */ |
281 alGetError(); /* clear any AL errors beforehand */ |
278 |
282 |
279 return AL_TRUE; |
283 return AL_TRUE; |
280 } |
284 } |
281 |
285 |
282 |
286 |
283 ALboolean openal_setglobalvolume (uint8_t percentage) { |
287 ALboolean openal_setglobalvolume (uint8_t percentage) { |
284 if(openalReady == AL_FALSE) { |
288 if(openalReady == AL_FALSE) { |
285 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
289 fprintf(stderr, "ERROR 'openal_setglobalvolume()': OpenAL not initialized\n"); |
286 return AL_FALSE; |
290 return AL_FALSE; |
287 } |
291 } |
288 |
292 |
289 /*Set volume for all sounds*/ |
293 /*Set volume for all sounds*/ |
290 if (percentage > 100) |
294 if (percentage > 100) |
291 percentage = 100; |
295 percentage = 100; |
292 alListenerf (AL_GAIN, (float) percentage/100.0f); |
296 alListenerf (AL_GAIN, (float) percentage/100.0f); |
293 if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE) |
297 if (AlGetError("ERROR %d in 'openal_setglobalvolume()': Setting global volume\n") != AL_TRUE) |
294 return AL_FALSE; |
298 return AL_FALSE; |
295 |
299 |
296 alGetError(); /* clear any AL errors beforehand */ |
300 alGetError(); /* clear any AL errors beforehand */ |
297 |
301 |
298 return AL_TRUE; |
302 return AL_TRUE; |
332 DWORD threadID; |
336 DWORD threadID; |
333 #endif |
337 #endif |
334 fade_t *fade; |
338 fade_t *fade; |
335 |
339 |
336 if(openalReady == AL_FALSE) { |
340 if(openalReady == AL_FALSE) { |
337 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
341 fprintf(stderr, "ERROR 'openal_fade()': OpenAL not initialized\n"); |
338 return AL_FALSE; |
342 return AL_FALSE; |
339 } |
343 } |
340 |
344 |
341 fade = (fade_t*) Malloc(sizeof(fade_t)); |
345 fade = (fade_t*) Malloc(sizeof(fade_t)); |
342 fade->index = index; |
346 fade->index = index; |
343 fade->quantity = quantity; |
347 fade->quantity = quantity; |
344 |
348 |
345 if (index >= globalindex) { |
349 if (index >= globalindex) { |
346 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
350 fprintf(stderr, "ERROR 'openal_fade()': index out of bounds (got %d, max %d)\n", index, globalindex); |
347 return AL_FALSE; |
351 return AL_FALSE; |
348 } |
352 } |
349 |
353 |
350 if (direction == FADE_IN) |
354 switch (direction) { |
|
355 case FADE_IN: |
351 #ifndef _WIN32 |
356 #ifndef _WIN32 |
352 pthread_create(&thread, NULL, helper_fadein, (void*) fade); |
357 pthread_create(&thread, NULL, helper_fadein, (void*) fade); |
353 #else |
358 #else |
354 Thread = _beginthread(&helper_fadein, 0, (void*) fade); |
359 Thread = _beginthread(&helper_fadein, 0, (void*) fade); |
355 #endif |
360 #endif |
356 else { |
361 break; |
357 if (direction == FADE_OUT) |
362 case FADE_OUT: |
358 #ifndef _WIN32 |
363 #ifndef _WIN32 |
359 pthread_create(&thread, NULL, helper_fadeout, (void*) fade); |
364 pthread_create(&thread, NULL, helper_fadeout, (void*) fade); |
360 #else |
365 #else |
361 Thread = _beginthread(&helper_fadeout, 0, (void*) fade); |
366 Thread = _beginthread(&helper_fadeout, 0, (void*) fade); |
362 #endif |
367 #endif |
363 else { |
368 break; |
364 fprintf(stderr, "ERROR: unknown direction for fade (%d)\n", direction); |
369 default: |
365 free(fade); |
370 fprintf(stderr, "ERROR 'openal_fade()': unknown direction for fade (%d)\n", direction); |
366 return AL_FALSE; |
371 free(fade); |
|
372 return AL_FALSE; |
|
373 break; |
367 } |
374 } |
368 } |
|
369 |
375 |
370 #ifndef _WIN32 |
376 #ifndef _WIN32 |
371 pthread_detach(thread); |
377 pthread_detach(thread); |
372 #endif |
378 #endif |
373 |
379 |
389 } |
395 } |
390 |
396 |
391 |
397 |
392 ALboolean openal_setposition (uint32_t index, float x, float y, float z) { |
398 ALboolean openal_setposition (uint32_t index, float x, float y, float z) { |
393 if(openalReady == AL_FALSE) { |
399 if(openalReady == AL_FALSE) { |
394 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
400 fprintf(stderr, "ERROR 'openal_setposition()': OpenAL not initialized\n"); |
395 return AL_FALSE; |
401 return AL_FALSE; |
396 } |
402 } |
397 if (index >= globalindex) { |
403 |
398 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
404 if (index >= globalindex) { |
|
405 fprintf(stderr, "ERROR 'openal_setposition()': index out of bounds (got %d, max %d)\n", index, globalindex); |
399 return AL_FALSE; |
406 return AL_FALSE; |
400 } |
407 } |
401 |
408 |
402 alSource3f(Sources[index], AL_POSITION, x, y, z); |
409 alSource3f(Sources[index], AL_POSITION, x, y, z); |
403 if (AlGetError2("ERROR %d: setting position for sound %d\n", index) != AL_TRUE) |
410 if (AlGetError2("ERROR %d in 'openal_setposition()': setting position for sound %d\n", index) != AL_TRUE) |
404 return AL_FALSE; |
411 return AL_FALSE; |
405 |
412 |
406 return AL_TRUE; |
413 return AL_TRUE; |
407 } |
414 } |
408 |
415 |
409 |
416 |
410 ALboolean openal_playsound (uint32_t index){ |
417 ALboolean openal_playsound (uint32_t index){ |
411 if(openalReady == AL_FALSE) { |
418 if(openalReady == AL_FALSE) { |
412 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
419 fprintf(stderr, "ERROR 'openal_playsound()': OpenAL not initialized\n"); |
413 return AL_FALSE; |
420 return AL_FALSE; |
414 } |
421 } |
415 |
422 |
416 /*Play sound number index*/ |
423 /*Play sound number index*/ |
417 if (index >= globalindex) { |
424 if (index >= globalindex) { |
418 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
425 fprintf(stderr, "ERROR 'openal_playsound()': index out of bounds (got %d, max %d)\n", index, globalindex); |
419 return AL_FALSE; |
426 return AL_FALSE; |
420 } |
427 } |
421 alSourcePlay(Sources[index]); |
428 alSourcePlay(Sources[index]); |
422 if (AlGetError2("ERROR %d: Playing sound %d\n", index) != AL_TRUE) |
429 if (AlGetError2("ERROR %d in 'openal_playsound()': Playing sound %d\n", index) != AL_TRUE) |
423 return AL_FALSE; |
430 return AL_FALSE; |
424 |
431 |
425 alGetError(); /* clear any AL errors beforehand */ |
432 alGetError(); /* clear any AL errors beforehand */ |
426 |
433 |
427 return AL_TRUE; |
434 return AL_TRUE; |
428 } |
435 } |
429 |
436 |
430 |
437 |
431 ALboolean openal_pausesound(uint32_t index){ |
438 ALboolean openal_pausesound(uint32_t index){ |
432 if(openalReady == AL_FALSE) { |
439 if(openalReady == AL_FALSE) { |
433 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
440 fprintf(stderr, "ERROR 'openal_pausesound()': OpenAL not initialized\n"); |
434 return AL_FALSE; |
441 return AL_FALSE; |
435 } |
442 } |
436 |
443 |
437 /*Pause sound number index*/ |
444 /*Pause sound number index*/ |
438 if (index >= globalindex) { |
445 if (index >= globalindex) { |
439 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
446 fprintf(stderr, "ERROR 'openal_pausesound()': index out of bounds (got %d, max %d)\n", index, globalindex); |
440 return AL_FALSE; |
447 return AL_FALSE; |
441 } |
448 } |
442 alSourcePause(Sources[index]); |
449 alSourcePause(Sources[index]); |
443 if (AlGetError2("ERROR %d: Pausing sound %d\n", index) != AL_TRUE) |
450 if (AlGetError2("ERROR %d in 'openal_pausesound()': Pausing sound %d\n", index) != AL_TRUE) |
444 return AL_FALSE; |
451 return AL_FALSE; |
445 |
452 |
446 return AL_TRUE; |
453 return AL_TRUE; |
447 } |
454 } |
448 |
455 |
449 |
456 |
450 ALboolean openal_stopsound(uint32_t index){ |
457 ALboolean openal_stopsound(uint32_t index){ |
451 if(openalReady == AL_FALSE) { |
458 if(openalReady == AL_FALSE) { |
452 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
459 fprintf(stderr, "ERROR 'openal_stopsound()': OpenAL not initialized\n"); |
453 return AL_FALSE; |
460 return AL_FALSE; |
454 } |
461 } |
455 |
462 |
456 /*Stop sound number index*/ |
463 /*Stop sound number index*/ |
457 if (index >= globalindex) { |
464 if (index >= globalindex) { |
458 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
465 fprintf(stderr, "ERROR 'openal_stopsound()': index out of bounds (got %d, max %d)\n", index, globalindex); |
459 return AL_FALSE; |
466 return AL_FALSE; |
460 } |
467 } |
461 alSourceStop(Sources[index]); |
468 alSourceStop(Sources[index]); |
462 if (AlGetError2("ERROR %d: Stopping sound %d\n", index) != AL_TRUE) |
469 if (AlGetError2("ERROR %d in 'openal_stopsound()': Stopping sound %d\n", index) != AL_TRUE) |
463 return AL_FALSE; |
470 return AL_FALSE; |
464 |
471 |
465 alGetError(); /* clear any AL errors beforehand */ |
472 alGetError(); /* clear any AL errors beforehand */ |
466 |
473 |
467 return AL_TRUE; |
474 return AL_TRUE; |