openalbridge/wrappers.c
changeset 2415 35d09cbf819a
parent 2259 ca42efdce3ce
child 2418 538a777f90c4
equal deleted inserted replaced
2414:fd9ca82077d8 2415:35d09cbf819a
    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             
    29         if ((aptr = malloc(nbytes)) == NULL) {
    30         if ((aptr = malloc(nbytes)) == NULL) {
    30             fprintf(stderr, "ERROR: not enough memory! malloc() failed\n");
    31             fprintf(stderr, "ERROR 'malloc()': not enough memory\n");
    31             exit(-1);
    32             exit(-1);
    32         }
    33         }
    33         return aptr;
    34         return aptr;
    34     }
    35     }
    35     
    36     
    36     
    37     
    37     void *Realloc (void *aptr, size_t nbytes) {
    38     void *Realloc (void *aptr, size_t nbytes) {
    38         aptr = realloc(aptr, nbytes);
    39         aptr = realloc(aptr, nbytes);
    39         
    40         
    40         if (aptr == NULL) {
    41         if (aptr == NULL) {
    41             fprintf(stderr, "ERROR: not enough memory! realloc() failed\n");
    42             fprintf(stderr, "ERROR 'realloc()': not enough memory\n");
    42             free(aptr);
    43             free(aptr);
    43             exit(-1);
    44             exit(-1);
    44         }
    45         }
    45         return aptr;
    46         return aptr;
    46     }
    47     }
    47     
    48     
    48     
    49     
    49     FILE *Fopen (const char *fname, char *mode)	{
    50     FILE *Fopen (const char *fname, char *mode)	{
    50         FILE *fp;
    51         FILE *fp;
    51         if ((fp=fopen(fname,mode)) == NULL)
    52         if ((fp=fopen(fname,mode)) == NULL)
    52             fprintf (stderr, "ERROR: can't open file %s in mode '%s'\n", fname, mode);
    53             fprintf (stderr, "ERROR 'fopen()': can't open file %s in mode '%s'\n", fname, mode);
    53         return fp;
    54         return fp;
    54     }
    55     }
    55     
    56     
    56     
    57     /*TODO make a proper error reporting routine*/
    57     ALint AlGetError (const char *str) {
    58     ALint AlGetError (const char *str) {
    58         ALenum error;
    59         ALenum error;
    59         
    60         
    60         error = alGetError();
    61         error = alGetError();
    61         if (error != AL_NO_ERROR) {
    62         if (error != AL_NO_ERROR) {
    74             return -2;
    75             return -2;
    75         } else 
    76         } else 
    76             return AL_TRUE;
    77             return AL_TRUE;
    77     }
    78     }
    78     
    79     
    79 #ifndef _WIN32
    80     void *helper_fadein(void *tmp) {
    80     void *helper_fadein(void *tmp) 
       
    81 #else
       
    82     void *helper_fadein(void *tmp) 
       
    83 #endif
       
    84     {
       
    85         ALfloat gain;
    81         ALfloat gain;
    86         ALfloat target_gain;
    82         ALfloat target_gain;
    87         fade_t *fade;
    83         fade_t *fade;
    88         uint32_t index; 
    84         uint32_t index; 
    89         uint16_t quantity; 
    85         uint16_t quantity; 
    92         index = fade->index;
    88         index = fade->index;
    93         quantity = fade->quantity;
    89         quantity = fade->quantity;
    94         free (fade);
    90         free (fade);
    95         
    91         
    96 #ifdef DEBUG
    92 #ifdef DEBUG
    97         fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity);
    93         fprintf(stderr, "Fade-in: index %d quantity %d\n", index, quantity);
    98 #endif
    94 #endif
    99         
    95         
   100         /*save the volume desired after the fade*/
    96         /*save the volume desired after the fade*/
   101         alGetSourcef(Sources[index], AL_GAIN, &target_gain);
    97         alGetSourcef(Sources[index], AL_GAIN, &target_gain);
   102         if (target_gain > 1.0f || target_gain <= 0.0f)
    98         if (target_gain > 1.0f || target_gain <= 0.0f)
   104         
   100         
   105         alSourcePlay(Sources[index]);
   101         alSourcePlay(Sources[index]);
   106         
   102         
   107         for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
   103         for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
   108 #ifdef DEBUG
   104 #ifdef DEBUG
   109             fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
   105             fprintf(stderr, "Fade-in: set gain to: %f\n", gain);
   110 #endif
   106 #endif
   111             alSourcef(Sources[index], AL_GAIN, gain);
   107             alSourcef(Sources[index], AL_GAIN, gain);
   112             usleep(10000);
   108             usleep(10000);
   113         }
   109         }
   114         
   110         
   115         AlGetError("ERROR %d: Setting fade in volume\n");
   111         AlGetError("ERROR %d in 'helper_fadein()': Setting fade in volume\n");
   116         
   112         
   117 #ifndef _WIN32
   113 #ifndef _WIN32
   118         pthread_exit(NULL);
   114         pthread_exit(NULL);
   119 #else
   115 #else
   120         _endthread();
   116         _endthread();
   121 #endif
   117 #endif
   122         return 0;
   118         return 0;
   123     }
   119     }
   124     
   120     
   125     
   121     void *helper_fadeout(void *tmp) {
   126 #ifndef _WIN32
       
   127     void *helper_fadeout(void *tmp) 
       
   128 #else
       
   129     void *helper_fadeout(void *tmp) 	
       
   130 #endif
       
   131     {
       
   132         ALfloat gain;
   122         ALfloat gain;
   133         ALfloat old_gain;
   123         ALfloat old_gain;
   134         fade_t *fade;
   124         fade_t *fade;
   135         uint32_t index; 
   125         uint32_t index; 
   136         uint16_t quantity; 
   126         uint16_t quantity; 
   152 #endif
   142 #endif
   153             alSourcef(Sources[index], AL_GAIN, gain);
   143             alSourcef(Sources[index], AL_GAIN, gain);
   154             usleep(10000);
   144             usleep(10000);
   155         }
   145         }
   156         
   146         
   157         AlGetError("ERROR %d: Setting fade out volume\n");
   147         AlGetError("ERROR %d in 'helper_fadeout()': Setting fade out volume\n");
   158         
   148         
   159         /*stop that sound and reset its volume*/
   149         /*stop that sound and reset its volume*/
   160         alSourceStop (Sources[index]);
   150         alSourceStop (Sources[index]);
   161         alSourcef (Sources[index], AL_GAIN, old_gain);	
   151         alSourcef (Sources[index], AL_GAIN, old_gain);	
   162         
   152