openalbridge/wrappers.c
author koda
Sun, 11 Oct 2009 20:14:55 +0000
changeset 2418 538a777f90c4
parent 2415 35d09cbf819a
child 2419 dbaaba09146d
permissions -rw-r--r--
fix build and partially moves messages to errlib
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
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    21
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    22
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    23
extern "C" {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    24
#endif 
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    25
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    26
        extern ALint *Sources;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    27
        
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    28
        void *Malloc (size_t nbytes) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    29
                void *aptr;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    30
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    31
                if ((aptr = malloc(nbytes)) == NULL)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    32
                        err_dump("(%s) FATAL - not enough memory");
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    33
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    34
                return aptr;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    35
        }
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    36
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    37
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    38
        void *Realloc (void *aptr, size_t nbytes) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    39
                aptr = realloc(aptr, nbytes);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    40
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    41
                if (aptr == NULL) 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    42
                        err_dump("(%s) FATAL - not enough memory");
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    43
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    44
                return aptr;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    45
        }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    46
        
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    47
        
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    48
        FILE *Fopen (const char *fname, char *mode)	{
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    49
                FILE *fp;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    50
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    51
                fp = fopen(fname,mode);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    52
                if (fp == NULL)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    53
                        err_ret("(%s) ERROR - can't open file %s in mode '%s'", prog, fname, mode);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    54
                return fp;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    55
        }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    56
        
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    57
        /*TODO make a proper error reporting routine*/
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    58
        ALint AlGetError (const char *str) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    59
                ALenum error;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    60
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    61
                error = alGetError();
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    62
                if (error != AL_NO_ERROR) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    63
                        err_msg(str, prog);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    64
                        return error;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    65
                } else 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    66
                        return AL_TRUE;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    67
        }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    68
        
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    69
        ALint AlGetError2 (const char *str, int num) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    70
                ALenum error;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    71
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    72
                error = alGetError();
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    73
                if (error != AL_NO_ERROR) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    74
                        fprintf(stderr, str, error, num);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    75
                        return -2;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    76
                } else 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    77
                        return AL_TRUE;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    78
        }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    79
        
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    80
        void *helper_fadein(void *tmp) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    81
                ALfloat gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    82
                ALfloat target_gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    83
                fade_t *fade;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    84
                uint32_t index; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    85
                uint16_t quantity; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    86
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    87
                fade = tmp;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    88
                index = fade->index;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    89
                quantity = fade->quantity;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    90
                free (fade);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    91
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    92
#ifdef DEBUG
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    93
                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
    94
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    95
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    96
                /*save the volume desired after the fade*/
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    97
                alGetSourcef(Sources[index], AL_GAIN, &target_gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    98
                if (target_gain > 1.0f || target_gain <= 0.0f)
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    99
                        target_gain = 1.0f;
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
                alSourcePlay(Sources[index]);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   102
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   103
                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
   104
#ifdef TRACE
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   105
                        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
   106
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   107
                        alSourcef(Sources[index], AL_GAIN, gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   108
                        usleep(10000);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   109
                }
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
                AlGetError("(%s) WARN - Failed to set fade-in volume level");
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   112
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   113
#ifndef _WIN32
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   114
                pthread_exit(NULL);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   115
#else
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   116
                _endthread();
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   117
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   118
                return 0;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   119
        }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   120
        
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   121
        void *helper_fadeout(void *tmp) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   122
                ALfloat gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   123
                ALfloat old_gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   124
                fade_t *fade;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   125
                uint32_t index; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   126
                uint16_t quantity; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   127
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   128
                fade = tmp;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   129
                index = fade->index;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   130
                quantity = fade->quantity;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   131
                free(fade);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   132
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   133
#ifdef DEBUG
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   134
                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
   135
#endif
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
                alGetSourcef(Sources[index], AL_GAIN, &old_gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   138
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   139
                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
   140
#ifdef TRACE
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   141
                        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
   142
#endif
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   143
                        alSourcef(Sources[index], AL_GAIN, gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   144
                        usleep(10000);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   145
                }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   146
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   147
                AlGetError("(%s) WARN - Failed to set fade-out volume level");
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   148
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   149
                /*stop that sound and reset its volume*/
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   150
                alSourceStop (Sources[index]);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   151
                alSourcef (Sources[index], AL_GAIN, old_gain);	
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   152
                
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   153
#ifndef _WIN32
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   154
                pthread_exit(NULL);
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   155
#else
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   156
                _endthread();
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   157
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   158
                return 0;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   159
        }
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   160
        
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   161
        
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   162
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   163
}
2216
82e7da49c26a -Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents: 2213
diff changeset
   164
#endif