openalbridge/wrappers.c
changeset 3353 a767dd3786b5
parent 3352 ac5d14a35482
child 3354 cb5d13ff4aae
equal deleted inserted replaced
3352:ac5d14a35482 3353:a767dd3786b5
     1 /*
       
     2  * OpenAL Bridge - a simple portable library for OpenAL interface
       
     3  * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU Lesser General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU Lesser General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Lesser General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #include "wrappers.h"
       
    20 
       
    21 
       
    22 #ifdef __CPLUSPLUS
       
    23 extern "C" {
       
    24 #endif 
       
    25         
       
    26         extern ALint *Sources;
       
    27         
       
    28         void *Malloc (size_t nbytes) {
       
    29                 void *aptr;
       
    30                 
       
    31                 if ((aptr = malloc(nbytes)) == NULL)
       
    32                         err_dump("(%s) FATAL - not enough memory");
       
    33                 
       
    34                 return aptr;
       
    35         }
       
    36         
       
    37         
       
    38         void *Realloc (void *aptr, size_t nbytes) {
       
    39                 aptr = realloc(aptr, nbytes);
       
    40                 
       
    41                 if (aptr == NULL) 
       
    42                         err_dump("(%s) FATAL - not enough memory");
       
    43                 
       
    44                 return aptr;
       
    45         }
       
    46         
       
    47         
       
    48         FILE *Fopen (const char *fname, char *mode)	{
       
    49                 FILE *fp;
       
    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                 
       
    55                 return fp;
       
    56         }
       
    57         
       
    58         /*TODO make a proper error reporting routine*/
       
    59         ALint AlGetError (const char *str) {
       
    60                 ALenum error;
       
    61                 
       
    62                 error = alGetError();
       
    63                 if (error != AL_NO_ERROR) {
       
    64                         err_msg(str, prog);
       
    65                         return error;
       
    66                 } else 
       
    67                         return AL_TRUE;
       
    68         }
       
    69         
       
    70         ALint AlGetError2 (const char *str, int num) {
       
    71                 ALenum error;
       
    72                 
       
    73                 error = alGetError();
       
    74                 if (error != AL_NO_ERROR) {
       
    75                         err_msg(str, prog, num);
       
    76                         return error;
       
    77                 } else 
       
    78                         return AL_TRUE;
       
    79         }
       
    80         
       
    81         void *helper_fadein(void *tmp) {
       
    82                 ALfloat gain;
       
    83                 ALfloat target_gain;
       
    84                 fade_t *fade;
       
    85                 uint32_t index; 
       
    86                 uint16_t quantity; 
       
    87                 
       
    88                 fade = tmp;
       
    89                 index = fade->index;
       
    90                 quantity = fade->quantity;
       
    91                 free (fade);
       
    92                 
       
    93 #ifdef DEBUG
       
    94                 err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity);
       
    95 #endif
       
    96                 
       
    97                 /*save the volume desired after the fade*/
       
    98                 alGetSourcef(Sources[index], AL_GAIN, &target_gain);
       
    99                 if (target_gain > 1.0f || target_gain <= 0.0f)
       
   100                         target_gain = 1.0f;
       
   101                 
       
   102                 alSourcePlay(Sources[index]);
       
   103                 
       
   104                 for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
       
   105 #ifdef TRACE
       
   106                         err_msg("(%s) DEBUG - Fade-in set gain to %f", gain);
       
   107 #endif
       
   108                         alSourcef(Sources[index], AL_GAIN, gain);
       
   109                         usleep(10000);
       
   110                 }
       
   111                 
       
   112                 AlGetError("(%s) WARN - Failed to set fade-in volume level");
       
   113                 
       
   114 #ifndef _WIN32
       
   115                 pthread_exit(NULL);
       
   116 #else
       
   117                 _endthread();
       
   118 #endif
       
   119                 return 0;
       
   120         }
       
   121         
       
   122         void *helper_fadeout(void *tmp) {
       
   123                 ALfloat gain;
       
   124                 ALfloat old_gain;
       
   125                 fade_t *fade;
       
   126                 uint32_t index; 
       
   127                 uint16_t quantity; 
       
   128                 
       
   129                 fade = tmp;
       
   130                 index = fade->index;
       
   131                 quantity = fade->quantity;
       
   132                 free(fade);
       
   133                 
       
   134 #ifdef DEBUG
       
   135                 err_msg("(%s) INFO - Fade-out in progress [index %d quantity %d]", prog, index, quantity);
       
   136 #endif
       
   137                 
       
   138                 alGetSourcef(Sources[index], AL_GAIN, &old_gain);
       
   139                 
       
   140                 for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) {
       
   141 #ifdef TRACE
       
   142                         err_msg("(%s) DEBUG - Fade-out set gain to %f", gain);
       
   143 #endif
       
   144                         alSourcef(Sources[index], AL_GAIN, gain);
       
   145                         usleep(10000);
       
   146                 }
       
   147                 
       
   148                 AlGetError("(%s) WARN - Failed to set fade-out volume level");
       
   149                 
       
   150                 /*stop that sound and reset its volume*/
       
   151                 alSourceStop (Sources[index]);
       
   152                 alSourcef (Sources[index], AL_GAIN, old_gain);	
       
   153                 
       
   154 #ifndef _WIN32
       
   155                 pthread_exit(NULL);
       
   156 #else
       
   157                 _endthread();
       
   158 #endif
       
   159                 return 0;
       
   160         }
       
   161         
       
   162         
       
   163 #ifdef __CPLUSPLUS
       
   164 }
       
   165 #endif