openalbridge/wrappers.c
author nemo
Sun, 24 Jan 2010 16:46:06 +0000
changeset 2712 8f4527c9137c
parent 2529 51e5df1c8462
permissions -rw-r--r--
Minor tweak, try to make long flavour text last longer, move the hurt self messages to unused messages group, so they don't get wiped by crate an instant later.
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
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    22
#ifdef __CPLUSPLUS
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    23
extern "C" {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    24
#endif 
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    25
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    26
        extern ALint *Sources;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    27
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    28
        void *Malloc (size_t nbytes) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    29
                void *aptr;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    30
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    31
                if ((aptr = malloc(nbytes)) == NULL)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    32
                        err_dump("(%s) FATAL - not enough memory");
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    33
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    34
                return aptr;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    35
        }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    36
        
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    37
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    38
        void *Realloc (void *aptr, size_t nbytes) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    39
                aptr = realloc(aptr, nbytes);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    40
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    41
                if (aptr == NULL) 
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    42
                        err_dump("(%s) FATAL - not enough memory");
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    43
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    44
                return aptr;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    45
        }
2418
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
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    48
        FILE *Fopen (const char *fname, char *mode)	{
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    49
                FILE *fp;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    50
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    51
                fp = fopen(fname,mode);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    52
                if (fp == NULL)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    53
                        err_ret("(%s) ERROR - can't open file %s in mode '%s'", prog, fname, mode);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    54
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    55
                return fp;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    56
        }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    57
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    58
        /*TODO make a proper error reporting routine*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    59
        ALint AlGetError (const char *str) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    60
                ALenum error;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    61
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    62
                error = alGetError();
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    63
                if (error != AL_NO_ERROR) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    64
                        err_msg(str, prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    65
                        return error;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    66
                } else 
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    67
                        return AL_TRUE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    68
        }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    69
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    70
        ALint AlGetError2 (const char *str, int num) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    71
                ALenum error;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    72
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    73
                error = alGetError();
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    74
                if (error != AL_NO_ERROR) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    75
                        err_msg(str, prog, num);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    76
                        return error;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    77
                } else 
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    78
                        return AL_TRUE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    79
        }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    80
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    81
        void *helper_fadein(void *tmp) {
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    82
                ALfloat gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    83
                ALfloat target_gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    84
                fade_t *fade;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    85
                uint32_t index; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    86
                uint16_t quantity; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    87
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    88
                fade = tmp;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    89
                index = fade->index;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    90
                quantity = fade->quantity;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    91
                free (fade);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    92
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    93
#ifdef DEBUG
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    94
                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
    95
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    96
                
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2444
diff changeset
    97
                /*save the volume desired after the fade*/
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    98
                alGetSourcef(Sources[index], AL_GAIN, &target_gain);
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
    99
                if (target_gain > 1.0f || target_gain <= 0.0f)
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   100
                        target_gain = 1.0f;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   101
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   102
                alSourcePlay(Sources[index]);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   103
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   104
                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
   105
#ifdef TRACE
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   106
                        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
   107
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   108
                        alSourcef(Sources[index], AL_GAIN, gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   109
                        usleep(10000);
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
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   112
                AlGetError("(%s) WARN - Failed to set fade-in volume level");
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   113
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   114
#ifndef _WIN32
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   115
                pthread_exit(NULL);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   116
#else
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   117
                _endthread();
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   118
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   119
                return 0;
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   120
        }
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   121
        
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   122
        void *helper_fadeout(void *tmp) {
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   123
                ALfloat gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   124
                ALfloat old_gain;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   125
                fade_t *fade;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   126
                uint32_t index; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   127
                uint16_t quantity; 
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   128
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   129
                fade = tmp;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   130
                index = fade->index;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   131
                quantity = fade->quantity;
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   132
                free(fade);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   133
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   134
#ifdef DEBUG
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   135
                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
   136
#endif
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   137
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   138
                alGetSourcef(Sources[index], AL_GAIN, &old_gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   139
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   140
                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
   141
#ifdef TRACE
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   142
                        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
   143
#endif
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   144
                        alSourcef(Sources[index], AL_GAIN, gain);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   145
                        usleep(10000);
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
                
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   148
                AlGetError("(%s) WARN - Failed to set fade-out volume level");
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   149
                
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2444
diff changeset
   150
                /*stop that sound and reset its volume*/
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   151
                alSourceStop (Sources[index]);
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   152
                alSourcef (Sources[index], AL_GAIN, old_gain);	
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   153
                
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   154
#ifndef _WIN32
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   155
                pthread_exit(NULL);
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   156
#else
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   157
                _endthread();
2210
1cb7118a77dd initial bugfix for volume setup
koda
parents: 2200
diff changeset
   158
#endif
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
   159
                return 0;
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
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
   162
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
   163
#ifdef __CPLUSPLUS
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
   164
}
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2515
diff changeset
   165
#endif