openalbridge/wrappers.c
changeset 2210 1cb7118a77dd
parent 2200 8192be6e3aef
child 2211 288360b78f30
--- a/openalbridge/wrappers.c	Mon Jun 29 00:58:32 2009 +0000
+++ b/openalbridge/wrappers.c	Mon Jun 29 03:47:39 2009 +0000
@@ -22,8 +22,9 @@
 extern "C" {
 #endif 
 	
-	void *Malloc (size_t nbytes)
-	{
+	extern ALint *Sources;
+	
+	void *Malloc (size_t nbytes){
 		void *aptr;
 		if ( (aptr = malloc(nbytes)) == NULL) {
 			fprintf(stderr, "ERROR: not enough memory! malloc() failed");
@@ -32,14 +33,15 @@
 		return aptr;
 	}
 	
-	FILE *Fopen (const char *fname, char *mode)
-	{
+	
+	FILE *Fopen (const char *fname, char *mode)	{
 		FILE *fp;
 		if ((fp=fopen(fname,mode)) == NULL)
 			fprintf (stderr, "ERROR: can't open file %s in mode '%s'", fname, mode);
 		return fp;
 	}
 	
+	
 	ALint AlGetError (const char *str) {
 		ALenum error;
 		
@@ -51,6 +53,97 @@
 			return AL_TRUE;
 	}
 	
+	
+#ifndef _WIN32
+	void *helper_fadein(void *tmp) 
+#else
+	void WINAPI helper_fadein(void *tmp) 
+#endif
+	{
+		ALfloat gain;
+		ALfloat target_gain;
+		fade_t *fade;
+		int index; 
+		unsigned int quantity; 
+		
+		fade = tmp;
+		index = fade->index;
+		quantity = fade->quantity;
+		free (fade);
+		
+#ifdef DEBUG
+		fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity);
+#endif
+		
+		//save the volume desired after the fade
+		alGetSourcef(Sources[index], AL_GAIN, &target_gain);
+		if (target_gain > 1.0f || target_gain <= 0.0f)
+			target_gain = 1.0f;
+		
+		alSourcePlay(Sources[index]);
+		
+		for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
+#ifdef DEBUG
+			fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
+#endif
+			alSourcef(Sources[index], AL_GAIN, gain);
+			usleep(10000);
+		}
+		
+		AlGetError("ERROR %d: Setting fade in volume\n");
+		
+#ifndef _WIN32
+		pthread_exit(NULL);
+#else
+		_endthread();
+#endif
+	}
+	
+	
+#ifndef _WIN32
+	void *helper_fadeout(void *tmp) 
+#else
+	void WINAPI helper_fadeout(void *tmp) 	
+#endif
+	{
+		ALfloat gain;
+		ALfloat old_gain;
+		fade_t *fade;
+		int index; 
+		unsigned int quantity; 
+		
+		fade = tmp;
+		index = fade->index;
+		quantity = fade->quantity;
+		free(fade);
+		
+#ifdef DEBUG
+		fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity);
+#endif
+		
+		alGetSourcef(Sources[index], AL_GAIN, &old_gain);
+		
+		for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) {
+#ifdef DEBUG
+			fprintf(stderr, "Fade-out: Set gain to %f\n", gain);
+#endif
+			alSourcef(Sources[index], AL_GAIN, gain);
+			usleep(10000);
+		}
+		
+		AlGetError("ERROR %d: Setting fade out volume\n");
+		
+		//stop that sound and reset its volume
+		alSourceStop (Sources[index]);
+		alSourcef (Sources[index], AL_GAIN, old_gain);	
+		
+#ifndef _WIN32
+		pthread_exit(NULL);
+#else
+		_endthread();
+#endif
+	}
+	
 #ifdef __CPLUSPLUS
 }
 #endif
\ No newline at end of file