openalbridge/wrappers.c
author unc0rr
Fri, 16 Oct 2009 19:52:09 +0000
changeset 2502 f67c603c23b5
parent 2494 1e10a47cabea
child 2515 51d3f4b6293a
permissions -rw-r--r--
- Fix build - Replace 01:07 'Remaining' with '%1 remaining'
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     1
/*
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     2
 * OpenAL Bridge - a simple portable library for OpenAL interface
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     4
 *
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
2257
7eb31efcfb9b updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents: 2218
diff changeset
     6
 * it under the terms of the GNU Lesser General Public License as published by
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     8
 *
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2257
7eb31efcfb9b updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents: 2218
diff changeset
    12
 * GNU Lesser General Public License for more details.
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    13
 *
2257
7eb31efcfb9b updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents: 2218
diff changeset
    14
 * You should have received a copy of the GNU Lesser General Public License
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    17
 */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    18
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    19
#include "wrappers.h"
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    20
2443
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2419
diff changeset
    21
extern char *prog;
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    22
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2444
diff changeset
    23
extern ALint Sources[MAX_SOURCES];
2444
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    24
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    25
void *Malloc (size_t nbytes) {
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    26
        void *aptr;
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    27
        
2444
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    28
        if ((aptr = malloc(nbytes)) == NULL)
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    29
                err_dump("(%s) FATAL - not enough memory");
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    30
        
2444
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    31
        return aptr;
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    32
}
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    33
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    34
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    35
void *Realloc (void *aptr, size_t nbytes) {
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    36
        aptr = realloc(aptr, nbytes);
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    37
        
2444
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    38
        if (aptr == NULL) 
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    39
                err_dump("(%s) FATAL - not enough memory");
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    40
        
2444
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    41
        return aptr;
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    42
}
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    43
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    44
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    45
FILE *Fopen (const char *fname, char *mode)	{
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    46
        FILE *fp;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    47
        
2444
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    48
        fp = fopen(fname,mode);
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    49
        if (fp == NULL)
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    50
                err_ret("(%s) ERROR - can't open file %s in mode '%s'", prog, fname, mode);
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    51
        
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    52
        return fp;
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    53
}
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    54
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    55
/*TODO make a proper error reporting routine*/
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    56
ALint AlGetError (const char *str) {
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    57
        ALenum error;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    58
        
2444
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    59
        error = alGetError();
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    60
        if (error != AL_NO_ERROR) {
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    61
                err_msg(str, prog);
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    62
                return error;
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    63
        } else 
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    64
                return AL_TRUE;
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    65
}
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    66
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    67
ALint AlGetError2 (const char *str, int num) {
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    68
        ALenum error;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    69
        
2444
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    70
        error = alGetError();
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    71
        if (error != AL_NO_ERROR) {
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    72
                err_msg(str, prog, num);
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    73
                return error;
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    74
        } else 
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    75
                return AL_TRUE;
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    76
}
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
    77
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2444
diff changeset
    78
       void *helper_fadein(void *tmp) {
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    79
                ALfloat gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    80
                ALfloat target_gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    81
                fade_t *fade;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    82
                uint32_t index; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    83
                uint16_t quantity; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    84
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    85
                fade = tmp;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    86
                index = fade->index;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    87
                quantity = fade->quantity;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    88
                free (fade);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    89
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    90
#ifdef DEBUG
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    91
                err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity);
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
    92
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    93
                
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2444
diff changeset
    94
                /*save the volume desired after the fade*/
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    95
                alGetSourcef(Sources[index], AL_GAIN, &target_gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    96
                if (target_gain > 1.0f || target_gain <= 0.0f)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    97
                        target_gain = 1.0f;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    98
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    99
                alSourcePlay(Sources[index]);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   100
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   101
                for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   102
#ifdef TRACE
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   103
                        err_msg("(%s) DEBUG - Fade-in set gain to %f", gain);
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   104
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   105
                        alSourcef(Sources[index], AL_GAIN, gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   106
                        usleep(10000);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   107
                }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   108
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   109
                AlGetError("(%s) WARN - Failed to set fade-in volume level");
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   110
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   111
#ifndef _WIN32
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   112
                pthread_exit(NULL);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   113
#else
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   114
                _endthread();
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   115
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   116
                return 0;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   117
        }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   118
        
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   119
        void *helper_fadeout(void *tmp) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   120
                ALfloat gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   121
                ALfloat old_gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   122
                fade_t *fade;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   123
                uint32_t index; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   124
                uint16_t quantity; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   125
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   126
                fade = tmp;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   127
                index = fade->index;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   128
                quantity = fade->quantity;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   129
                free(fade);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   130
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   131
#ifdef DEBUG
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   132
                err_msg("(%s) INFO - Fade-out in progress [index %d quantity %d]", prog, index, quantity);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   133
#endif
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   134
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   135
                alGetSourcef(Sources[index], AL_GAIN, &old_gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   136
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   137
                for (gain = old_gain; gain >= 0.00f; gain -= (float) quantity/10000) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   138
#ifdef TRACE
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   139
                        err_msg("(%s) DEBUG - Fade-out set gain to %f", gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   140
#endif
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   141
                        alSourcef(Sources[index], AL_GAIN, gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   142
                        usleep(10000);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   143
                }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   144
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   145
                AlGetError("(%s) WARN - Failed to set fade-out volume level");
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   146
                
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2444
diff changeset
   147
                /*stop that sound and reset its volume*/
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   148
                alSourceStop (Sources[index]);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   149
                alSourcef (Sources[index], AL_GAIN, old_gain);	
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   150
                
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   151
#ifndef _WIN32
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   152
                pthread_exit(NULL);
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   153
#else
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   154
                _endthread();
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   155
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   156
                return 0;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   157
        }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   158