openalbridge/openalwrap.c
changeset 2194 1597710c6118
parent 2191 20c62f787a4d
child 2200 8192be6e3aef
equal deleted inserted replaced
2193:4d5d78aa7ca4 2194:1597710c6118
    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 <stdio.h>
       
    20 #include <stdlib.h>
       
    21 #include <stdint.h>
       
    22 #include "al.h"
       
    23 #include "alc.h"
       
    24 #include "openalwrap.h"
    19 #include "openalwrap.h"
    25 #include "loaders.h"
       
    26 #include "wrappers.h"
       
    27 #include "endianness.h"
       
    28 
    20 
    29 #ifdef __CPLUSPLUS
    21 #ifdef __CPLUSPLUS
    30 extern "C" {
    22 extern "C" {
    31 #endif 
    23 #endif 
       
    24 
       
    25 	typedef struct _fade_t {
       
    26 		int index;
       
    27 		unsigned int quantity;
       
    28 	} fade_t;
    32 	
    29 	
    33 	// Sources are points emitting sound.
    30 	// Sources are points emitting sound.
    34 	ALuint *Sources;
    31 	ALuint *Sources;
    35 	// Buffers hold sound data.
    32 	// Buffers hold sound data.
    36 	ALuint *Buffers;
    33 	ALuint *Buffers;
   273 		alGetError();  /* clear any AL errors beforehand */
   270 		alGetError();  /* clear any AL errors beforehand */
   274 
   271 
   275 		return AL_TRUE;
   272 		return AL_TRUE;
   276 	}
   273 	}
   277 	
   274 	
   278 	
   275 #ifndef _WIN32
   279 	ALint openal_fadeout(int index, unsigned int quantity) {
   276 	void *helper_fadeout(void* tmp) {
       
   277 #else
       
   278 	VOID WINAPI helper_fadeout(LPVOID tmp) {	
       
   279 #endif
   280 		ALfloat gain;
   280 		ALfloat gain;
   281 		
   281 		fade_t *fade;
   282 		if (index >= globalindex) {
   282 		int index; 
   283 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
   283 		unsigned int quantity; 
   284 			return AL_FALSE;
   284 		
   285 		}
   285 		fade = tmp;
       
   286 		index = fade->index;
       
   287 		quantity = fade->quantity;
   286 		
   288 		
   287 		alGetSourcef(Sources[index], AL_GAIN, &gain);
   289 		alGetSourcef(Sources[index], AL_GAIN, &gain);
   288 
   290 		
   289 		for ( ; gain >= 0.00f; gain -= (float) quantity/10000){
   291 		for ( ; gain >= 0.00f; gain -= (float) quantity/10000){
   290 #ifdef DEBUG
   292 #ifdef DEBUG
   291 			fprintf(stderr, "Fade-out: Set gain to: %f\n", gain);
   293 			fprintf(stderr, "Fade-out: Set gain to %f\n", gain);
   292 #endif
   294 #endif
   293 			alSourcef(Sources[index], AL_GAIN, gain);
   295 			alSourcef(Sources[index], AL_GAIN, gain);
   294 			usleep(10000);
   296 			usleep(10000);
   295 		}
   297 		}
   296 		
   298 		
   297 		if (AlGetError("ERROR %d: Setting fade out volume\n") != AL_TRUE)
   299 		AlGetError("ERROR %d: Setting fade out volume\n");
   298 			return AL_FALSE;
       
   299 		
   300 		
   300 		//stop that sound and reset its gain
   301 		//stop that sound and reset its gain
   301 		alSourceStop (Sources[index]);
   302 		alSourceStop (Sources[index]);
   302 		alSourcef (Sources[index], AL_GAIN, 1.0f);
   303 		alSourcef (Sources[index], AL_GAIN, 1.0f);	
   303 
   304 		
   304 		alGetError();  /* clear any AL errors beforehand */
   305 #ifndef _WIN32
   305 
   306 		pthread_exit(NULL);
   306 		return AL_TRUE;
   307 #else
   307 	}
   308 		ThreadExit();
   308 	
   309 #endif
   309 	
   310 	}
       
   311 	
       
   312 	ALint openal_fadeout(int index, unsigned int quantity) {
       
   313 #ifndef _WIN32
       
   314 		pthread_t thread;
       
   315 #else
       
   316 		HANDLE Thread;
       
   317 		DWORD threadID;
       
   318 #endif
       
   319 		fade_t fade;
       
   320 		
       
   321 		if (index >= globalindex) {
       
   322 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
       
   323 			return AL_FALSE;
       
   324 		}
       
   325 		
       
   326 		fade.index = index;
       
   327 		fade.quantity = quantity;
       
   328 		
       
   329 #ifndef _WIN32
       
   330 		pthread_create(&thread, NULL, helper_fadeout, (void*) &fade);
       
   331 		pthread_detach(thread);
       
   332 #else
       
   333 		Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) helper_fadeout, (void*) &fade, 0, threadID);
       
   334 		CloseHandle(Thread);
       
   335 #endif
       
   336 		
       
   337 		alGetError();  /* clear any AL errors beforehand */
       
   338 
       
   339 		return AL_TRUE;
       
   340 	}
       
   341 		
       
   342 #ifndef _WIN32
       
   343 		void *helper_fadein(void* tmp) 
       
   344 #else
       
   345 		VOID WINAPI helper_fadein(LPVOID tmp) 
       
   346 #endif
       
   347 		{
       
   348 			ALfloat gain;
       
   349 			fade_t *fade;
       
   350 			int index; 
       
   351 			unsigned int quantity; 
       
   352 			
       
   353 			fade = tmp;
       
   354 			index = fade->index;
       
   355 			quantity = fade->quantity;
       
   356 			
       
   357 			gain = 0.0f;
       
   358 			alSourcef(Sources[index], AL_GAIN, gain);
       
   359 			alSourcePlay(Sources[index]);
       
   360 			
       
   361 			for ( ; gain <= 1.00f; gain += (float) quantity/10000){
       
   362 #ifdef DEBUG
       
   363 				fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
       
   364 #endif
       
   365 				alSourcef(Sources[index], AL_GAIN, gain);
       
   366 				usleep(10000);
       
   367 			}
       
   368 			
       
   369 			if (AlGetError("ERROR %d: Setting fade in volume\n") != AL_TRUE)
       
   370 				return AL_FALSE;
       
   371 			
       
   372 #ifndef _WIN32
       
   373 			pthread_exit(NULL);
       
   374 #else
       
   375 			ThreadExit();
       
   376 #endif
       
   377 		}
       
   378 			
       
   379 		
   310 	ALint openal_fadein(int index, unsigned int quantity) {
   380 	ALint openal_fadein(int index, unsigned int quantity) {
   311 		ALfloat gain;
   381 #ifndef _WIN32
   312 		
   382 		pthread_t thread;
   313 		if (index >= globalindex) {
   383 #else
   314 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
   384 		HANDLE Thread;
   315 			return AL_FALSE;
   385 		DWORD threadID;
   316 		}
   386 #endif
   317 		
   387 		fade_t fade;
   318 		gain = 0.0f;
   388 		
   319 		alSourcef(Sources[index], AL_GAIN, gain);
   389 		if (index >= globalindex) {
   320 		alSourcePlay(Sources[index]);
   390 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
   321 		
   391 			return AL_FALSE;
   322 		for ( ; gain <= 1.00f; gain += (float) quantity/10000){
   392 		}
   323 #ifdef DEBUG
   393 		
   324 			fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
   394 		fade.index = index;
   325 #endif
   395 		fade.quantity = quantity;
   326 			alSourcef(Sources[index], AL_GAIN, gain);
   396 		
   327 			usleep(10000);
   397 #ifndef _WIN32
   328 		}
   398 		pthread_create(&thread, NULL, helper_fadein, (void*) &fade);
   329 		
   399 		pthread_detach(thread);
   330 		if (AlGetError("ERROR %d: Setting fade in volume\n") != AL_TRUE)
   400 #else
   331 			return AL_FALSE;
   401 		Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) helper_fadein, (void*) &fade, 0, threadID);
       
   402 		CloseHandle(Thread);
       
   403 #endif
   332 		
   404 		
   333 		alGetError();  /* clear any AL errors beforehand */
   405 		alGetError();  /* clear any AL errors beforehand */
   334 
   406 
   335 		return AL_TRUE;
   407 		return AL_TRUE;
   336 	}
   408 	}