misc/openalbridge/wrappers.c
changeset 3516 a8c673657b79
parent 3510 23145a950eae
parent 3515 3e8635f43972
child 3529 0e968ba12a84
equal deleted inserted replaced
3510:23145a950eae 3516:a8c673657b79
     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 #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         fprintf(stderr,"(Bridge Fatal Error) - not enough memory");
       
    29         abort();
       
    30     }
       
    31     
       
    32     return aptr;
       
    33 }
       
    34 
       
    35 
       
    36 void *Realloc (void *aptr, size_t nbytes) {
       
    37     aptr = realloc(aptr, nbytes);
       
    38 
       
    39     if (aptr == NULL) {
       
    40         fprintf(stderr,"(Bridge Fatal Error) - not enough memory");
       
    41         abort();
       
    42     }
       
    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         fprintf(stderr,"(Bridge Error) - can't open file %s in mode '%s'", fname, mode);
       
    54 
       
    55     return fp;
       
    56 }
       
    57 
       
    58 
       
    59 void helper_fade(void *tmp) {
       
    60     ALfloat gain;
       
    61     ALfloat target_gain;
       
    62     fade_t *fade;
       
    63     uint32_t index;
       
    64     uint16_t quantity;
       
    65     al_fade_t type;
       
    66 
       
    67     fade = tmp;
       
    68     index = fade->index;
       
    69     quantity = fade->quantity;
       
    70     type = fade->type;
       
    71     free (fade);
       
    72 
       
    73     if (type == AL_FADE_IN) {
       
    74 #ifdef DEBUG
       
    75         fprintf(stderr,"(Bridge Info) - Fade-in in progress [index %d quantity %d]", index, quantity);
       
    76 #endif
       
    77 
       
    78         // save the volume desired after the fade
       
    79         alGetSourcef(Sources[index], AL_GAIN, &target_gain);
       
    80         if (target_gain > 1.0f || target_gain <= 0.0f)
       
    81             target_gain = 1.0f;
       
    82 
       
    83         alSourcePlay(Sources[index]);
       
    84 
       
    85         for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
       
    86 #ifdef TRACE
       
    87             err_msg("(%s) DEBUG - Fade-in set gain to %f", gain);
       
    88 #endif
       
    89             alSourcef(Sources[index], AL_GAIN, gain);
       
    90             usleep(10000);
       
    91         }
       
    92     } else {
       
    93         alGetSourcef(Sources[index], AL_GAIN, &target_gain);
       
    94 
       
    95         for (gain = target_gain; gain >= 0.00f; gain -= (float) quantity/10000) {
       
    96 #ifdef TRACE
       
    97             err_msg("(%s) DEBUG - Fade-out set gain to %f", gain);
       
    98 #endif
       
    99             alSourcef(Sources[index], AL_GAIN, gain);
       
   100             usleep(10000);
       
   101         }
       
   102 
       
   103         if (AL_NO_ERROR != alGetError())
       
   104             fprintf(stderr,"(Bridge Warning) - Failed to set fade-out effect");
       
   105 
       
   106         // stop that sound and reset its volume
       
   107         alSourceStop (Sources[index]);
       
   108         alSourcef (Sources[index], AL_GAIN, target_gain);
       
   109     }
       
   110 
       
   111     if (AL_NO_ERROR != alGetError())
       
   112         fprintf(stderr,"(Bridge Warning) - Failed to set fade effect");
       
   113 
       
   114 #ifndef _WIN32
       
   115     pthread_exit(NULL);
       
   116 #else
       
   117     _endthread();
       
   118 #endif
       
   119 }
       
   120