openalbridge/openalwrap.c
changeset 2210 1cb7118a77dd
parent 2209 2573d4ff78f9
child 2211 288360b78f30
equal deleted inserted replaced
2209:2573d4ff78f9 2210:1cb7118a77dd
    19 #include "openalwrap.h"
    19 #include "openalwrap.h"
    20 
    20 
    21 #ifdef __CPLUSPLUS
    21 #ifdef __CPLUSPLUS
    22 extern "C" {
    22 extern "C" {
    23 #endif 
    23 #endif 
    24 
       
    25 	typedef struct _fade_t {
       
    26 		int index;
       
    27 		unsigned int quantity;
       
    28 	} fade_t;
       
    29 	
    24 	
    30 	// Sources are points emitting sound.
    25 	// Sources are points emitting sound.
    31 	ALuint *Sources;
    26 	ALuint *Sources;
    32 	// Buffers hold sound data.
    27 	// Buffers hold sound data.
    33 	ALuint *Buffers;
    28 	ALuint *Buffers;
   167 		//copy pcm data in one buffer
   162 		//copy pcm data in one buffer
   168 		alBufferData(Buffers[globalindex], format, data, bitsize, freq);
   163 		alBufferData(Buffers[globalindex], format, data, bitsize, freq);
   169 		free(data);		//deallocate data to save memory
   164 		free(data);		//deallocate data to save memory
   170 		
   165 		
   171 		if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE)
   166 		if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE)
   172 			return -5;
   167 			return -6;
   173 		
   168 		
   174 		//memory allocation for source position and velocity
   169 		//memory allocation for source position and velocity
   175 		SourcePos[globalindex] = (ALfloat*) Malloc(sizeof(ALfloat)*3);
   170 		SourcePos[globalindex] = (ALfloat*) Malloc(sizeof(ALfloat)*3);
   176 		SourceVel[globalindex] = (ALfloat*) Malloc(sizeof(ALfloat)*3);
   171 		SourceVel[globalindex] = (ALfloat*) Malloc(sizeof(ALfloat)*3);
   177 		
   172 		
   178 		if (SourcePos[globalindex] == NULL || SourceVel[globalindex] == NULL)
   173 		if (SourcePos[globalindex] == NULL || SourceVel[globalindex] == NULL)
   179 			return -6;
   174 			return -7;
   180 			
   175 			
   181 		//source properties that it will use when it's in playback
   176 		//source properties that it will use when it's in playback
   182 		for (i = 0; i < 3; i++) {
   177 		for (i = 0; i < 3; i++) {
   183 			SourcePos[globalindex][i] = 0.0;
   178 			SourcePos[globalindex][i] = 0.0;
   184 			SourceVel[globalindex][i] = 0.1;
   179 			SourceVel[globalindex][i] = 0.1;
   189 		alSourcefv(Sources[globalindex], AL_POSITION, SourcePos[globalindex]);
   184 		alSourcefv(Sources[globalindex], AL_POSITION, SourcePos[globalindex]);
   190 		alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel[globalindex]);
   185 		alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel[globalindex]);
   191 		alSourcei (Sources[globalindex], AL_LOOPING,  0						);
   186 		alSourcei (Sources[globalindex], AL_LOOPING,  0						);
   192 		
   187 		
   193 		if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE)
   188 		if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE)
   194 			return -7;
   189 			return -8;
   195 		
   190 		
   196 		alGetError();  /* clear any AL errors beforehand */
   191 		alGetError();  /* clear any AL errors beforehand */
   197 		
   192 		
   198 		//returns the index of the source you just loaded, increments it and exits
   193 		//returns the index of the source you just loaded, increments it and exits
   199 		return globalindex++;
   194 		return globalindex++;
   269 		alGetError();  /* clear any AL errors beforehand */
   264 		alGetError();  /* clear any AL errors beforehand */
   270 
   265 
   271 		return AL_TRUE;
   266 		return AL_TRUE;
   272 	}
   267 	}
   273 	
   268 	
   274 #ifndef _WIN32
   269 
   275 	void *helper_fadeout(void *tmp) {
       
   276 #else
       
   277 	void WINAPI helper_fadeout(void *tmp) {	
       
   278 #endif
       
   279 		ALfloat gain;
       
   280 		fade_t *fade;
       
   281 		int index; 
       
   282 		unsigned int quantity; 
       
   283 		
       
   284 		fade = tmp;
       
   285 		index = fade->index;
       
   286 		quantity = fade->quantity;
       
   287 		free(fade);
       
   288 
       
   289 #ifdef DEBUG
       
   290 		fprintf(stderr, "Fade-out: index %d quantity %d", index, quantity);
       
   291 #endif
       
   292 		
       
   293 		alGetSourcef(Sources[index], AL_GAIN, &gain);
       
   294 		
       
   295 		for ( ; gain >= 0.00f; gain -= (float) quantity/10000){
       
   296 #ifdef DEBUG
       
   297 			fprintf(stderr, "Fade-out: Set gain to %f\n", gain);
       
   298 #endif
       
   299 			alSourcef(Sources[index], AL_GAIN, gain);
       
   300 			usleep(10000);
       
   301 		}
       
   302 
       
   303 		AlGetError("ERROR %d: Setting fade out volume\n");
       
   304 		
       
   305 		//stop that sound and reset its gain
       
   306 		alSourceStop (Sources[index]);
       
   307 		alSourcef (Sources[index], AL_GAIN, 1.0f);	
       
   308 		
       
   309 #ifndef _WIN32
       
   310 		pthread_exit(NULL);
       
   311 #else
       
   312 		_endthread();
       
   313 #endif
       
   314 	}
       
   315 	
       
   316 	ALint openal_fadeout(int index, unsigned int quantity) {
   270 	ALint openal_fadeout(int index, unsigned int quantity) {
   317 #ifndef _WIN32
   271 #ifndef _WIN32
   318 		pthread_t thread;
   272 		pthread_t thread;
   319 #else
   273 #else
   320 		HANDLE Thread;
   274 		HANDLE Thread;
   341 		alGetError();  /* clear any AL errors beforehand */
   295 		alGetError();  /* clear any AL errors beforehand */
   342 
   296 
   343 		return AL_TRUE;
   297 		return AL_TRUE;
   344 	}
   298 	}
   345 		
   299 		
   346 #ifndef _WIN32
       
   347 		void *helper_fadein(void *tmp) 
       
   348 #else
       
   349 		void WINAPI helper_fadein(void *tmp) 
       
   350 #endif
       
   351 		{
       
   352 			ALfloat gain;
       
   353 			fade_t *fade;
       
   354 			int index; 
       
   355 			unsigned int quantity; 
       
   356 			
       
   357 			fade = tmp;
       
   358 			index = fade->index;
       
   359 			quantity = fade->quantity;
       
   360 			free (fade);
       
   361 			
       
   362 			gain = 0.0f;
       
   363 			alSourcef(Sources[index], AL_GAIN, gain);
       
   364 			alSourcePlay(Sources[index]);
       
   365 			
       
   366 			for ( ; gain <= 1.00f; gain += (float) quantity/10000){
       
   367 #ifdef DEBUG
       
   368 				fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
       
   369 #endif
       
   370 				alSourcef(Sources[index], AL_GAIN, gain);
       
   371 				usleep(10000);
       
   372 			}
       
   373 			
       
   374 			AlGetError("ERROR %d: Setting fade in volume\n");
       
   375 			
       
   376 #ifndef _WIN32
       
   377 			pthread_exit(NULL);
       
   378 #else
       
   379 			_endthread();
       
   380 #endif
       
   381 		}
       
   382 			
       
   383 		
   300 		
   384 	ALint openal_fadein(int index, unsigned int quantity) {
   301 	ALint openal_fadein(int index, unsigned int quantity) {
   385 #ifndef _WIN32
   302 #ifndef _WIN32
   386 		pthread_t thread;
   303 		pthread_t thread;
   387 #else
   304 #else