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