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