openalbridge/wrappers.c
changeset 2210 1cb7118a77dd
parent 2200 8192be6e3aef
child 2211 288360b78f30
equal deleted inserted replaced
2209:2573d4ff78f9 2210:1cb7118a77dd
    20 
    20 
    21 #ifdef __CPLUSPLUS
    21 #ifdef __CPLUSPLUS
    22 extern "C" {
    22 extern "C" {
    23 #endif 
    23 #endif 
    24 	
    24 	
    25 	void *Malloc (size_t nbytes)
    25 	extern ALint *Sources;
    26 	{
    26 	
       
    27 	void *Malloc (size_t nbytes){
    27 		void *aptr;
    28 		void *aptr;
    28 		if ( (aptr = malloc(nbytes)) == NULL) {
    29 		if ( (aptr = malloc(nbytes)) == NULL) {
    29 			fprintf(stderr, "ERROR: not enough memory! malloc() failed");
    30 			fprintf(stderr, "ERROR: not enough memory! malloc() failed");
    30 			exit(-1);
    31 			exit(-1);
    31 		}
    32 		}
    32 		return aptr;
    33 		return aptr;
    33 	}
    34 	}
    34 	
    35 	
    35 	FILE *Fopen (const char *fname, char *mode)
    36 	
    36 	{
    37 	FILE *Fopen (const char *fname, char *mode)	{
    37 		FILE *fp;
    38 		FILE *fp;
    38 		if ((fp=fopen(fname,mode)) == NULL)
    39 		if ((fp=fopen(fname,mode)) == NULL)
    39 			fprintf (stderr, "ERROR: can't open file %s in mode '%s'", fname, mode);
    40 			fprintf (stderr, "ERROR: can't open file %s in mode '%s'", fname, mode);
    40 		return fp;
    41 		return fp;
    41 	}
    42 	}
       
    43 	
    42 	
    44 	
    43 	ALint AlGetError (const char *str) {
    45 	ALint AlGetError (const char *str) {
    44 		ALenum error;
    46 		ALenum error;
    45 		
    47 		
    46 		error = alGetError();
    48 		error = alGetError();
    49 			return -2;
    51 			return -2;
    50 		} else 
    52 		} else 
    51 			return AL_TRUE;
    53 			return AL_TRUE;
    52 	}
    54 	}
    53 	
    55 	
       
    56 	
       
    57 #ifndef _WIN32
       
    58 	void *helper_fadein(void *tmp) 
       
    59 #else
       
    60 	void WINAPI helper_fadein(void *tmp) 
       
    61 #endif
       
    62 	{
       
    63 		ALfloat gain;
       
    64 		ALfloat target_gain;
       
    65 		fade_t *fade;
       
    66 		int index; 
       
    67 		unsigned int quantity; 
       
    68 		
       
    69 		fade = tmp;
       
    70 		index = fade->index;
       
    71 		quantity = fade->quantity;
       
    72 		free (fade);
       
    73 		
       
    74 #ifdef DEBUG
       
    75 		fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity);
       
    76 #endif
       
    77 		
       
    78 		//save the volume desired after the fade
       
    79 		alGetSourcef(Sources[index], AL_GAIN, &target_gain);
       
    80 		if (target_gain > 1.0f || target_gain <= 0.0f)
       
    81 			target_gain = 1.0f;
       
    82 		
       
    83 		alSourcePlay(Sources[index]);
       
    84 		
       
    85 		for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
       
    86 #ifdef DEBUG
       
    87 			fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
       
    88 #endif
       
    89 			alSourcef(Sources[index], AL_GAIN, gain);
       
    90 			usleep(10000);
       
    91 		}
       
    92 		
       
    93 		AlGetError("ERROR %d: Setting fade in volume\n");
       
    94 		
       
    95 #ifndef _WIN32
       
    96 		pthread_exit(NULL);
       
    97 #else
       
    98 		_endthread();
       
    99 #endif
       
   100 	}
       
   101 	
       
   102 	
       
   103 #ifndef _WIN32
       
   104 	void *helper_fadeout(void *tmp) 
       
   105 #else
       
   106 	void WINAPI helper_fadeout(void *tmp) 	
       
   107 #endif
       
   108 	{
       
   109 		ALfloat gain;
       
   110 		ALfloat old_gain;
       
   111 		fade_t *fade;
       
   112 		int index; 
       
   113 		unsigned int quantity; 
       
   114 		
       
   115 		fade = tmp;
       
   116 		index = fade->index;
       
   117 		quantity = fade->quantity;
       
   118 		free(fade);
       
   119 		
       
   120 #ifdef DEBUG
       
   121 		fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity);
       
   122 #endif
       
   123 		
       
   124 		alGetSourcef(Sources[index], AL_GAIN, &old_gain);
       
   125 		
       
   126 		for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) {
       
   127 #ifdef DEBUG
       
   128 			fprintf(stderr, "Fade-out: Set gain to %f\n", gain);
       
   129 #endif
       
   130 			alSourcef(Sources[index], AL_GAIN, gain);
       
   131 			usleep(10000);
       
   132 		}
       
   133 		
       
   134 		AlGetError("ERROR %d: Setting fade out volume\n");
       
   135 		
       
   136 		//stop that sound and reset its volume
       
   137 		alSourceStop (Sources[index]);
       
   138 		alSourcef (Sources[index], AL_GAIN, old_gain);	
       
   139 		
       
   140 #ifndef _WIN32
       
   141 		pthread_exit(NULL);
       
   142 #else
       
   143 		_endthread();
       
   144 #endif
       
   145 	}
       
   146 	
    54 #ifdef __CPLUSPLUS
   147 #ifdef __CPLUSPLUS
    55 }
   148 }
    56 #endif
   149 #endif