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