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