misc/openalbridge/wrappers.c
changeset 3362 8d3b4d19ce27
parent 3353 a767dd3786b5
child 3364 e5403e2bf02c
equal deleted inserted replaced
3361:cfc6cd502f85 3362:8d3b4d19ce27
     1 /*
     1 /*
     2  * OpenAL Bridge - a simple portable library for OpenAL interface
     2 * OpenAL Bridge - a simple portable library for OpenAL interface
     3  * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
     3 * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
     4  *
     4 *
     5  * This program is free software; you can redistribute it and/or modify
     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
     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
     7 * the Free Software Foundation; version 2 of the License
     8  *
     8 *
     9  * This program is distributed in the hope that it will be useful,
     9 * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU Lesser General Public License for more details.
    12 * GNU Lesser General Public License for more details.
    13  *
    13 *
    14  * You should have received a copy of the GNU Lesser General Public License
    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
    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
    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 #include "openalbridge_t.h"
       
    21 
       
    22 extern ALint *Sources;
       
    23 
       
    24 void *Malloc (size_t nbytes) {
       
    25     void *aptr;
       
    26 
       
    27     if ((aptr = malloc(nbytes)) == NULL)
       
    28         err_dump("(%s) FATAL - not enough memory");
       
    29 
       
    30     return aptr;
       
    31 }
    20 
    32 
    21 
    33 
    22 #ifdef __CPLUSPLUS
    34 void *Realloc (void *aptr, size_t nbytes) {
    23 extern "C" {
    35     aptr = realloc(aptr, nbytes);
    24 #endif 
    36 
    25     
    37     if (aptr == NULL)
    26     extern ALint *Sources;
    38         err_dump("(%s) FATAL - not enough memory");
    27     
    39 
    28     void *Malloc (size_t nbytes) {
    40     return aptr;
    29         void *aptr;
    41 }
    30         
    42 
    31         if ((aptr = malloc(nbytes)) == NULL)
    43 
    32             err_dump("(%s) FATAL - not enough memory");
    44 FILE *Fopen (const char *fname, char *mode)	{
    33         
    45     FILE *fp;
    34         return aptr;
    46 
    35     }
    47     fp = fopen(fname,mode);
    36     
    48     if (fp == NULL)
    37     
    49         err_ret("(%s) ERROR - can't open file %s in mode '%s'", prog, fname, mode);
    38     void *Realloc (void *aptr, size_t nbytes) {
    50 
    39         aptr = realloc(aptr, nbytes);
    51     return fp;
    40         
    52 }
    41         if (aptr == NULL) 
    53 
    42             err_dump("(%s) FATAL - not enough memory");
    54 
    43         
    55 void helper_fade(void *tmp) {
    44         return aptr;
    56     ALfloat gain;
    45     }
    57     ALfloat target_gain;
    46     
    58     fade_t *fade;
    47     
    59     uint32_t index;
    48     FILE *Fopen (const char *fname, char *mode)	{
    60     uint16_t quantity;
    49         FILE *fp;
    61     al_fade_t type;
    50         
    62 
    51         fp = fopen(fname,mode);
    63     fade = tmp;
    52         if (fp == NULL)
    64     index = fade->index;
    53             err_ret("(%s) ERROR - can't open file %s in mode '%s'", prog, fname, mode);
    65     quantity = fade->quantity;
    54         
    66     type = fade->type;
    55         return fp;
    67     free (fade);
    56     }
    68 
    57     
    69     if (type == AL_FADE_IN) {
    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
    70 #ifdef DEBUG
    94         err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity);
    71         err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity);
    95 #endif
    72 #endif
    96         
    73 
    97         /*save the volume desired after the fade*/
    74         // save the volume desired after the fade
    98         alGetSourcef(Sources[index], AL_GAIN, &target_gain);
    75         alGetSourcef(Sources[index], AL_GAIN, &target_gain);
    99         if (target_gain > 1.0f || target_gain <= 0.0f)
    76         if (target_gain > 1.0f || target_gain <= 0.0f)
   100             target_gain = 1.0f;
    77             target_gain = 1.0f;
   101         
    78 
   102         alSourcePlay(Sources[index]);
    79         alSourcePlay(Sources[index]);
   103         
    80 
   104         for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
    81         for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
   105 #ifdef TRACE
    82 #ifdef TRACE
   106             err_msg("(%s) DEBUG - Fade-in set gain to %f", gain);
    83             err_msg("(%s) DEBUG - Fade-in set gain to %f", gain);
   107 #endif
    84 #endif
   108             alSourcef(Sources[index], AL_GAIN, gain);
    85             alSourcef(Sources[index], AL_GAIN, gain);
   109             usleep(10000);
    86             usleep(10000);
   110         }
    87         }
   111         
    88     } else {
   112         AlGetError("(%s) WARN - Failed to set fade-in volume level");
    89         alGetSourcef(Sources[index], AL_GAIN, &target_gain);
   113         
    90 
   114 #ifndef _WIN32
    91         for (gain = target_gain; gain >= 0.00f; gain -= (float) quantity/10000) {
   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
    92 #ifdef TRACE
   142             err_msg("(%s) DEBUG - Fade-out set gain to %f", gain);
    93             err_msg("(%s) DEBUG - Fade-out set gain to %f", gain);
   143 #endif
    94 #endif
   144             alSourcef(Sources[index], AL_GAIN, gain);
    95             alSourcef(Sources[index], AL_GAIN, gain);
   145             usleep(10000);
    96             usleep(10000);
   146         }
    97         }
   147         
    98 
   148         AlGetError("(%s) WARN - Failed to set fade-out volume level");
    99         if (AL_NO_ERROR != alGetError())
   149         
   100             err_msg("(%s) WARN - Failed to set fade-out effect",prog);
   150         /*stop that sound and reset its volume*/
   101 
       
   102         // stop that sound and reset its volume
   151         alSourceStop (Sources[index]);
   103         alSourceStop (Sources[index]);
   152         alSourcef (Sources[index], AL_GAIN, old_gain);	
   104         alSourcef (Sources[index], AL_GAIN, target_gain);
   153         
   105     }
       
   106 
       
   107     if (AL_NO_ERROR != alGetError())
       
   108         err_msg("(%s) WARN - Failed to set fade effect",prog);
       
   109 
   154 #ifndef _WIN32
   110 #ifndef _WIN32
   155         pthread_exit(NULL);
   111     pthread_exit(NULL);
   156 #else
   112 #else
   157         _endthread();
   113     _endthread();
   158 #endif
   114 #endif
   159         return 0;
       
   160     }
       
   161     
       
   162     
       
   163 #ifdef __CPLUSPLUS
       
   164 }
   115 }
   165 #endif
   116