openalbridge/openalbridge.c
author nemo
Sat, 27 Mar 2010 17:03:27 +0000
changeset 3114 3a3d1ee2ebd0
parent 2529 51e5df1c8462
permissions -rw-r--r--
Tidier way to handle crosshair drawing. show/hide based on CurAmmoGear for alt attack weps
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
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
     3
 * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
2191
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: 2220
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: 2220
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: 2220
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
2326
0ddf641fddee rename dpr -> pas, restores ammos on two columns, minor fixes to openalbridge
koda
parents: 2261
diff changeset
    19
#include "openalbridge.h"
2418
538a777f90c4 fix build and partially moves messages to errlib
koda
parents: 2415
diff changeset
    20
2443
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
    21
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    22
#ifdef __CPLUSPLUS
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    23
extern "C" {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    24
#endif 
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    25
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    26
        /*Sources are points emitting sound*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    27
        ALuint *Sources;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    28
        /*Buffers hold sound data*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    29
        ALuint *Buffers;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    30
        /*index for Sources and Buffers*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    31
        ALuint globalindex, globalsize, increment;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    32
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    33
        ALboolean openalReady = AL_FALSE;
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
    34
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    35
        ALboolean openal_close (void) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    36
                /*Stop all sounds, deallocate all memory and close OpenAL */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    37
                ALCcontext *context;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    38
                ALCdevice  *device;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    39
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    40
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    41
                        errno = EPERM;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    42
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    43
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    44
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    45
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    46
                alSourceStopv	(globalsize, Sources);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    47
                alDeleteSources (globalsize, Sources);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    48
                alDeleteBuffers (globalsize, Buffers);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    49
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    50
                free(Sources);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    51
                free(Buffers);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    52
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    53
                context = alcGetCurrentContext();
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    54
                device  = alcGetContextsDevice(context);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    55
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    56
                alcMakeContextCurrent(NULL);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    57
                alcDestroyContext(context);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    58
                alcCloseDevice(device);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    59
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    60
                openalReady = AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    61
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    62
                return AL_TRUE;
2419
dbaaba09146d really fix build and completes transition to errlib
koda
parents: 2418
diff changeset
    63
        }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
    64
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    65
        ALboolean openal_ready(void) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    66
                return openalReady;
2443
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
    67
        }
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
    68
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    69
        ALboolean openal_init(char* programname, ALboolean usehardware, uint32_t memorysize) {	
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    70
                /*Initialize an OpenAL contex and allocate memory space for data and buffers*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    71
                ALCcontext *context;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    72
                ALCdevice *device;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    73
                const ALCchar *default_device;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    74
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    75
                prog = programname;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    76
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    77
                /*Position of the listener*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    78
                ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    79
                /*Velocity of the listener*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    80
                ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    81
                /*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    82
                ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    83
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    84
                if (openalReady == AL_TRUE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    85
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    86
                        err_ret("(%s) WARN - OpenAL already initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    87
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    88
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    89
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    90
                if (usehardware == AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    91
                        device = alcOpenDevice(NULL);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    92
                else
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    93
                        device = alcOpenDevice("Generic Software");
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    94
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    95
                if (device == NULL) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    96
                        errno = ENODEV;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    97
                        err_ret("(%s) WARN - Failed to open sound device", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    98
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
    99
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   100
                err_msg("(%s) INFO - Output device: %s", prog, alcGetString(device, ALC_DEVICE_SPECIFIER));
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   101
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   102
                context = alcCreateContext(device, NULL);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   103
                alcMakeContextCurrent(context);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   104
                alcProcessContext(context);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   105
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   106
                if (AlGetError("(%s) WARN - Failed to create a new contex") != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   107
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   108
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   109
                /*allocate memory space for buffers and sources*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   110
                if (memorysize == 0)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   111
                        globalsize = 50;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   112
                else
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   113
                        globalsize = memorysize;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   114
                increment  = globalsize;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   115
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   116
                Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   117
                Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   118
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   119
                /*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   120
                alListenerf (AL_GAIN,        1.0f       );
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   121
                alListenerfv(AL_POSITION,    ListenerPos);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   122
                alListenerfv(AL_VELOCITY,    ListenerVel);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   123
                alListenerfv(AL_ORIENTATION, ListenerOri);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   124
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   125
                if (AlGetError("(%s) WARN - Failed to set Listener properties") != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   126
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   127
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   128
                openalReady = AL_TRUE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   129
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   130
                alGetError();  /* clear any AL errors beforehand */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   131
                return AL_TRUE;
2443
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   132
        }
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   133
        
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   134
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   135
        ALboolean helper_realloc (void) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   136
                /*expands allocated memory when loading more sound files than expected*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   137
                int oldsize = globalsize;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   138
                globalsize += increment;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   139
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   140
#ifdef DEBUG
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   141
                err_msg("(%s) INFO - Realloc in process from %d to %d\n", prog, oldsize, globalsize);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   142
#endif
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   143
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   144
                Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   145
                Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   146
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   147
                return AL_TRUE;
2443
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   148
        }
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   149
        
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   150
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   151
        ALint openal_loadfile (const char *filename){
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   152
                /*Open a file, load into memory and allocate the Source buffer for playing*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   153
                ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   154
                ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   155
                ALenum format;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   156
                ALsizei bitsize, freq;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   157
                char *data;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   158
                uint32_t fileformat;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   159
                ALenum error;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   160
                FILE *fp;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   161
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   162
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   163
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   164
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   165
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   166
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   167
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   168
                /*when the buffers are all used, we can expand memory to accept new files*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   169
                if (globalindex == globalsize)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   170
                        helper_realloc();
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   171
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   172
                /*detect the file format, as written in the first 4 bytes of the header*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   173
                fp = Fopen (filename, "rb");
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   174
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   175
                if (fp == NULL)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   176
                        return -1;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   177
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   178
                error = fread (&fileformat, sizeof(uint32_t), 1, fp);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   179
                fclose (fp);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   180
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   181
                if (error < 0) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   182
                        errno = EIO;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   183
                        err_ret("(%s) ERROR - File %s is too short", prog, filename);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   184
                        return -2;
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   185
                }
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   186
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   187
                /*prepare the buffer to receive data*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   188
                alGenBuffers(1, &Buffers[globalindex]);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   189
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   190
                if (AlGetError("(%s) ERROR - Failed to allocate memory for buffers") != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   191
                        return -3;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   192
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   193
                /*prepare the source to emit sound*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   194
                alGenSources(1, &Sources[globalindex]);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   195
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   196
                if (AlGetError("(%s) ERROR - Failed to allocate memory for sources") != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   197
                        return -4;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   198
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   199
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   200
                switch (ENDIAN_BIG_32(fileformat)) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   201
                        case OGG_FILE_FORMAT:
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   202
                                error = load_oggvorbis (filename, &format, &data, &bitsize, &freq);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   203
                                break;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   204
                        case WAV_FILE_FORMAT:
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   205
                                error = load_wavpcm (filename, &format, &data, &bitsize, &freq);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   206
                                break;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   207
                        default:
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   208
                                errno = EINVAL;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   209
                                err_ret ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(fileformat));
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   210
                                return -5;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   211
                                break;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   212
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   213
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   214
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   215
                /*copy pcm data in one buffer*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   216
                alBufferData(Buffers[globalindex], format, data, bitsize, freq);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   217
                free(data);		/*deallocate data to save memory*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   218
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   219
                if (AlGetError("(%s) ERROR - Failed to write data to buffers") != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   220
                        return -6;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   221
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   222
                /*set source properties that it will use when it's in playback*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   223
                alSourcei (Sources[globalindex], AL_BUFFER,   Buffers[globalindex]  );
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   224
                alSourcef (Sources[globalindex], AL_PITCH,    1.0f                  );
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   225
                alSourcef (Sources[globalindex], AL_GAIN,     1.0f                  );
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   226
                alSourcefv(Sources[globalindex], AL_POSITION, SourcePos             );
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   227
                alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel             );
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   228
                alSourcei (Sources[globalindex], AL_LOOPING,  0                     );
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   229
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   230
                if (AlGetError("(%s) ERROR - Failed to set Source properties") != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   231
                        return -7;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   232
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   233
                alGetError();  /* clear any AL errors beforehand */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   234
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   235
                /*returns the index of the source you just loaded, increments it and exits*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   236
                return globalindex++;
2443
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   237
        }
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   238
        
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   239
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   240
        ALboolean openal_toggleloop (uint32_t index){
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   241
                /*Set or unset looping mode*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   242
                ALint loop;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   243
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   244
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   245
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   246
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   247
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   248
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   249
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   250
                if (index >= globalsize) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   251
                        errno = EINVAL;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   252
                        err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   253
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   254
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   255
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   256
                alGetSourcei (Sources[index], AL_LOOPING, &loop);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   257
                alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   258
                if (AlGetError("(%s) ERROR - Failed to get or set loop property") != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   259
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   260
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   261
                alGetError();  /* clear any AL errors beforehand */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   262
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   263
                return AL_TRUE;
2444
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
   264
        }
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
   265
        
ace11b7d8eab code cleanup and restore the mute call
koda
parents: 2443
diff changeset
   266
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   267
        ALboolean openal_setvolume (uint32_t index, uint8_t percentage) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   268
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   269
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   270
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   271
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   272
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   273
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   274
                /*Set volume for sound number index*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   275
                if (index >= globalsize) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   276
                        errno = EINVAL;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   277
                        err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   278
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   279
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   280
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   281
                if (percentage > 100)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   282
                        percentage = 100;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   283
                alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   284
                if (AlGetError2("(%s) ERROR -  Failed to set volume for sound %d\n", index) != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   285
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   286
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   287
                alGetError();  /* clear any AL errors beforehand */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   288
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   289
                return AL_TRUE;
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   290
        }
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   291
        
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   292
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   293
        ALboolean openal_setglobalvolume (uint8_t percentage) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   294
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   295
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   296
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   297
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   298
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   299
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   300
                /*Set volume for all sounds*/		
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   301
                if (percentage > 100)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   302
                        percentage = 100;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   303
                alListenerf (AL_GAIN, (float) percentage/100.0f);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   304
                if (AlGetError("(%s) ERROR -  Failed to set global volume") != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   305
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   306
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   307
                alGetError();  /* clear any AL errors beforehand */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   308
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   309
                return AL_TRUE;
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   310
        }
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   311
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   312
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   313
        ALboolean openal_togglemute () {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   314
                /*Mute or unmute sound*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   315
                ALfloat mute;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   316
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   317
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   318
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   319
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   320
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   321
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   322
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   323
                alGetListenerf (AL_GAIN, &mute);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   324
                if (mute > 0) 
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   325
                        mute = 0;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   326
                else
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   327
                        mute = 1.0;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   328
                alListenerf (AL_GAIN, mute);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   329
                if (AlGetError("(%s) ERROR -  Failed to set mute property") != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   330
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   331
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   332
                alGetError();  /* clear any AL errors beforehand */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   333
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   334
                return AL_TRUE;
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   335
        }
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   336
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   337
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   338
        ALboolean openal_fade (uint32_t index, uint16_t quantity, ALboolean direction) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   339
                /*Fade in or out by calling a helper thread*/
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   340
#ifndef _WIN32
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   341
                pthread_t thread;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   342
#else
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   343
                HANDLE Thread;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   344
                DWORD threadID;
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   345
#endif
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   346
                fade_t *fade;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   347
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   348
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   349
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   350
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   351
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   352
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   353
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   354
                fade = (fade_t*) Malloc(sizeof(fade_t));
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   355
                fade->index = index;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   356
                fade->quantity = quantity;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   357
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   358
                if (index >= globalsize) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   359
                        errno = EINVAL;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   360
                        err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   361
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   362
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   363
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   364
                switch (direction) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   365
                        case FADE_IN:
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   366
#ifndef _WIN32
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   367
                                pthread_create(&thread, NULL, helper_fadein, (void*) fade);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   368
#else
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   369
                                Thread = _beginthread(&helper_fadein, 0, (void*) fade);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   370
#endif
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   371
                                break;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   372
                        case FADE_OUT:
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   373
#ifndef _WIN32
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   374
                                pthread_create(&thread, NULL, helper_fadeout, (void*) fade);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   375
#else
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   376
                                Thread = _beginthread(&helper_fadeout, 0, (void*) fade);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   377
#endif	
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   378
                                break;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   379
                        default:
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   380
                                errno = EINVAL;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   381
                                err_ret("(%s) ERROR - Unknown direction for fading", prog, index, globalindex);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   382
                                free(fade);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   383
                                return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   384
                                break;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   385
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   386
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   387
#ifndef _WIN32
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   388
                pthread_detach(thread);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   389
#endif
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   390
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   391
                alGetError();  /* clear any AL errors beforehand */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   392
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   393
                return AL_TRUE;
2419
dbaaba09146d really fix build and completes transition to errlib
koda
parents: 2418
diff changeset
   394
        }
dbaaba09146d really fix build and completes transition to errlib
koda
parents: 2418
diff changeset
   395
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   396
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   397
        ALboolean openal_fadeout (uint32_t index, uint16_t quantity) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   398
                /*wrapper for fadeout*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   399
                return openal_fade(index, quantity, FADE_OUT);
2419
dbaaba09146d really fix build and completes transition to errlib
koda
parents: 2418
diff changeset
   400
        }
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   401
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   402
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   403
        ALboolean openal_fadein (uint32_t index, uint16_t quantity) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   404
                /*wrapper for fadein*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   405
                return openal_fade(index, quantity, FADE_IN);
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   406
        }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   407
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   408
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   409
        ALboolean openal_setposition (uint32_t index, float x, float y, float z) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   410
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   411
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   412
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   413
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   414
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   415
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   416
                if (index >= globalsize) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   417
                        errno = EINVAL;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   418
                        err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   419
                        return AL_FALSE;
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   420
                }
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   421
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   422
                alSource3f(Sources[index], AL_POSITION, x, y, z);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   423
                if (AlGetError2("(%s) ERROR - Failed to set position for sound %d)", index) != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   424
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   425
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   426
                return AL_TRUE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   427
        }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   428
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   429
        
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   430
        ALboolean openal_playsound (uint32_t index){
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   431
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   432
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   433
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   434
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   435
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   436
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   437
                /*Play sound number index*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   438
                if (index >= globalsize) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   439
                        errno = EINVAL;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   440
                        err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   441
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   442
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   443
                alSourcePlay(Sources[index]);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   444
                if (AlGetError2("(%s) ERROR - Failed to play sound %d)", index) != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   445
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   446
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   447
                alGetError();  /* clear any AL errors beforehand */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   448
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   449
                return AL_TRUE;
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   450
        }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   451
        
2494
1e10a47cabea THE fix for openalbridge
koda
parents: 2446
diff changeset
   452
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   453
        ALboolean openal_pausesound(uint32_t index){
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   454
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   455
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   456
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   457
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   458
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   459
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   460
                /*Pause sound number index*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   461
                if (index >= globalsize) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   462
                        errno = EINVAL;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   463
                        err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   464
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   465
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   466
                alSourcePause(Sources[index]);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   467
                if (AlGetError2("(%s) ERROR - Failed to pause sound %d)", index) != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   468
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   469
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   470
                return AL_TRUE;
2443
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   471
        }
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   472
        
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   473
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   474
        ALboolean openal_stopsound(uint32_t index){
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   475
                if (openalReady == AL_FALSE) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   476
                        errno = EPERM;                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   477
                        err_ret("(%s) WARN - OpenAL not initialized", prog);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   478
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   479
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   480
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   481
                /*Stop sound number index*/
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   482
                if (index >= globalsize) {
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   483
                        errno = EINVAL;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   484
                        err_ret("(%s) ERROR - Index out of bounds (got %d, max %d)", prog, index, globalindex);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   485
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   486
                }
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   487
                alSourceStop(Sources[index]);
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   488
                if (AlGetError2("(%s) ERROR - Failed to stop sound %d)", index) != AL_TRUE)
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   489
                        return AL_FALSE;
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   490
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   491
                alGetError();  /* clear any AL errors beforehand */
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   492
                
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   493
                return AL_TRUE;
2419
dbaaba09146d really fix build and completes transition to errlib
koda
parents: 2418
diff changeset
   494
        }
2259
ca42efdce3ce fix indentation and other cosmetic changes to lib
koda
parents: 2257
diff changeset
   495
        
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   496
#ifdef __CPLUSPLUS
2443
fececcbc2189 Smaxx patch for fixing all sound related issues on any system
koda
parents: 2437
diff changeset
   497
}
2529
51e5df1c8462 restore openalbridge to r2489
koda
parents: 2501
diff changeset
   498
#endif