misc/openalbridge/openalbridge.c
author nemo
Wed, 21 Apr 2010 21:38:07 +0000
changeset 3363 bcd6d76db4f7
parent 3362 8d3b4d19ce27
child 3364 e5403e2bf02c
permissions -rw-r--r--
More work on slovak locale from jose1711
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
     1
/*
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
     2
* OpenAL Bridge - a simple portable library for OpenAL interface
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
     3
* Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
     4
*
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
     5
* This program is free software; you can redistribute it and/or modify
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
     6
* it under the terms of the GNU Lesser General Public License as published by
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
     7
* the Free Software Foundation; version 2 of the License
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
     8
*
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
     9
* This program is distributed in the hope that it will be useful,
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    12
* GNU Lesser General Public License for more details.
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    13
*
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    14
* You should have received a copy of the GNU Lesser General Public License
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    15
* along with this program; if not, write to the Free Software
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    17
*/
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    18
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    19
#include "openalbridge.h"
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    20
#include "globals.h"
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    21
#include "wrappers.h"
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    22
#include "alc.h"
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    23
#include "loaders.h"
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    24
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
    25
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    26
/*Sources are points emitting sound*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    27
ALuint *Sources;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    28
/*Buffers hold sound data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    29
ALuint *Buffers;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    30
/*index for Sources and Buffers*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    31
ALuint globalindex, globalsize, increment;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    32
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    33
ALboolean openalReady = AL_FALSE;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    34
ALfloat old_gain;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
    35
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    36
int openal_init(int memorysize) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    37
    /*Initialize an OpenAL contex and allocate memory space for data and buffers*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    38
    ALCcontext *context;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    39
    ALCdevice *device;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
    40
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    41
    // set the memory dimentsion and the increment width when reallocating
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    42
    if (memorysize <= 0)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    43
        globalsize = 50;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    44
    else
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    45
        globalsize = memorysize;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    46
    increment = globalsize;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    47
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    48
    // reuse old context but keep the new value for increment
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    49
    if (openalReady == AL_TRUE) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    50
        err_msg("(%s) WARN - already initialized", prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    51
        return 0;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
    52
    }
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
    53
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    54
    // open hardware device if present
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    55
    device = alcOpenDevice(NULL);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    56
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    57
    if (device == NULL) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    58
        errno = ENODEV;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    59
        err_ret("(%s) WARN - failed to open sound device, using software renderer", prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    60
        device = alcOpenDevice("Generic Software");
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    61
        if (device == NULL) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    62
            err_ret("(%s) ERROR - failed to open sound software device, sound will be disabled", prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    63
            return -1;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    64
        }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    65
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    66
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    67
    err_msg("(%s) INFO - Output device: %s", prog, alcGetString(device, ALC_DEVICE_SPECIFIER));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    68
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    69
    context = alcCreateContext(device, NULL);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    70
    alcMakeContextCurrent(context);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    71
    alcProcessContext(context);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    72
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    73
    if (AL_NO_ERROR != alGetError()) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    74
        err_msg("(%s) ERROR - Failed to create a new contex",prog);
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    75
        alcMakeContextCurrent(NULL);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    76
        alcDestroyContext(context);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    77
        alcCloseDevice(device);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    78
        return -2;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    79
    }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    80
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    81
    // allocate memory space for buffers and sources
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    82
    Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    83
    Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    84
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    85
    // set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    86
    // Position, Velocity and Orientation of the listener
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    87
    ALfloat ListenerPos[] = {0.0, 0.0, 0.0};
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    88
    ALfloat ListenerVel[] = {0.0, 0.0, 0.0};
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    89
    ALfloat ListenerOri[] = {0.0, 0.0, -1.0,  0.0, 1.0, 0.0};
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    90
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    91
    alListenerf (AL_GAIN,        1.0f       );
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    92
    alListenerfv(AL_POSITION,    ListenerPos);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    93
    alListenerfv(AL_VELOCITY,    ListenerVel);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    94
    alListenerfv(AL_ORIENTATION, ListenerOri);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    95
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    96
    if (AL_NO_ERROR != alGetError()) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    97
        err_msg("(%s) ERROR - Failed to set Listener properties",prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    98
        return -3;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    99
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   100
    openalReady = AL_TRUE;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   101
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   102
    alGetError();  // clear any AL errors beforehand
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   103
    return AL_TRUE;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   104
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   105
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   106
void openal_close (void) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   107
    /*Stop all sounds, deallocate all memory and close OpenAL */
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   108
    ALCcontext *context;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   109
    ALCdevice  *device;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   110
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   111
    if (openalReady == AL_FALSE) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   112
        errno = EPERM;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   113
        err_ret("(%s) WARN - OpenAL not initialized", prog);
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   114
        return;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   115
    }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   116
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   117
    alSourceStopv	(globalsize, Sources);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   118
    alDeleteSources (globalsize, Sources);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   119
    alDeleteBuffers (globalsize, Buffers);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   120
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   121
    free(Sources);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   122
    free(Buffers);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   123
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   124
    context = alcGetCurrentContext();
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   125
    device  = alcGetContextsDevice(context);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   126
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   127
    alcMakeContextCurrent(NULL);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   128
    alcDestroyContext(context);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   129
    alcCloseDevice(device);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   130
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   131
    openalReady = AL_FALSE;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   132
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   133
    err_msg("(%s) INFO - closed", prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   134
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   135
    return;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   136
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   137
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   138
ALboolean openal_ready(void) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   139
    return openalReady;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   140
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   141
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   142
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   143
void helper_realloc (void) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   144
    /*expands allocated memory when loading more sound files than expected*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   145
    int oldsize = globalsize;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   146
    globalsize += increment;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   147
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   148
    err_msg("(%s) INFO - Realloc in process from %d to %d\n", prog, oldsize, globalsize);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   149
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   150
    Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   151
    Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   152
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   153
    return;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   154
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   155
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   156
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   157
int openal_loadfile (const char *filename){
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   158
    /*Open a file, load into memory and allocate the Source buffer for playing*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   159
    ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   160
    ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   161
    ALenum format;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   162
    ALsizei bitsize, freq;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   163
    char *data;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   164
    uint32_t fileformat;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   165
    ALenum error;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   166
    FILE *fp;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   167
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   168
    if (openalReady == AL_FALSE) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   169
        err_msg("(%s) WARN - not initialized", prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   170
        return -1;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   171
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   172
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   173
    /*when the buffers are all used, we can expand memory to accept new files*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   174
    if (globalindex == globalsize)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   175
        helper_realloc();
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   176
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   177
    /*detect the file format, as written in the first 4 bytes of the header*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   178
    fp = Fopen (filename, "rb");
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   179
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   180
    if (fp == NULL)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   181
        return -2;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   182
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   183
    error = fread (&fileformat, sizeof(uint32_t), 1, fp);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   184
    fclose (fp);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   185
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   186
    if (error < 0) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   187
        err_msg("(%s) ERROR - File %s is too short", prog, filename);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   188
        return -3;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   189
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   190
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   191
    /*prepare the buffer to receive data*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   192
    alGenBuffers(1, &Buffers[globalindex]);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   193
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   194
    if (AL_NO_ERROR != alGetError()) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   195
        err_msg("(%s) ERROR - Failed to allocate memory for buffers",prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   196
        return -4;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   197
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   198
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   199
    /*prepare the source to emit sound*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   200
    alGenSources(1, &Sources[globalindex]);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   201
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   202
    if (AL_NO_ERROR != alGetError()) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   203
        err_msg("(%s) ERROR - Failed to allocate memory for sources",prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   204
        return -5;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   205
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   206
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   207
    switch (ENDIAN_BIG_32(fileformat)) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   208
        case OGG_FILE_FORMAT:
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   209
            error = load_oggvorbis (filename, &format, &data, &bitsize, &freq);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   210
            break;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   211
        case WAV_FILE_FORMAT:
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   212
            error = load_wavpcm (filename, &format, &data, &bitsize, &freq);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   213
            break;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   214
        default:
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   215
            err_msg ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(fileformat));
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   216
            return -6;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   217
            break;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   218
    }
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   219
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   220
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   221
    //copy pcm data in one buffer and free it
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   222
    alBufferData(Buffers[globalindex], format, data, bitsize, freq);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   223
    free(data);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   224
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   225
    if (AL_NO_ERROR != alGetError()) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   226
        err_msg("(%s) ERROR - Failed to write data to buffers",prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   227
        return -6;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   228
    }
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   229
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   230
    /*set source properties that it will use when it's in playback*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   231
    alSourcei (Sources[globalindex], AL_BUFFER,   Buffers[globalindex]  );
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   232
    alSourcef (Sources[globalindex], AL_PITCH,    1.0f                  );
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   233
    alSourcef (Sources[globalindex], AL_GAIN,     1.0f                  );
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   234
    alSourcefv(Sources[globalindex], AL_POSITION, SourcePos             );
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   235
    alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel             );
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   236
    alSourcei (Sources[globalindex], AL_LOOPING,  0                     );
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   237
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   238
    if (AL_NO_ERROR != alGetError()) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   239
        err_msg("(%s) ERROR - Failed to set Source properties",prog);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   240
        return -7;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   241
    }
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   242
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   243
    alGetError();  /* clear any AL errors beforehand */
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   244
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   245
    /*returns the index of the source you just loaded, increments it and exits*/
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   246
    return globalindex++;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   247
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   248
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   249
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   250
void openal_playsound (uint32_t index) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   251
    openal_playsound_loop (index, 0);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   252
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   253
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   254
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   255
void openal_pausesound (uint32_t index) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   256
    if (openalReady == AL_TRUE && index < globalsize)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   257
        alSourcePause(Sources[index]);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   258
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   259
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   260
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   261
void openal_stopsound (uint32_t index) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   262
    openal_stopsound_free(index, 0);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   263
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   264
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   265
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   266
void openal_freesound (uint32_t index){
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   267
   if (openalReady == AL_TRUE && index < globalsize)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   268
        alSourceStop(Sources[index]);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   269
       // STUB
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   270
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   271
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   272
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   273
void openal_playsound_loop (unsigned int index, char loops) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   274
if (openalReady == AL_TRUE && index < globalsize) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   275
        alSourcePlay(Sources[index]);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   276
    if (loops != 0)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   277
            openal_toggleloop(index);
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   278
    }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   279
}
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   280
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   281
void openal_stopsound_free    (unsigned int index, char freesource) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   282
    if (openalReady == AL_TRUE && index < globalsize) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   283
        alSourceStop(Sources[index]);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   284
    if (freesource != 0)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   285
        openal_freesound(index);
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   286
    }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   287
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   288
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   289
void openal_toggleloop (uint32_t index) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   290
    ALint loop;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   291
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   292
    if (openalReady == AL_TRUE && index < globalsize) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   293
        alGetSourcei (Sources[index], AL_LOOPING, &loop);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   294
        alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   295
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   296
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   297
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   298
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   299
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   300
void openal_setvolume (uint32_t index, float gain) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   301
    if (openalReady == AL_TRUE && index < globalsize)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   302
        alSourcef (Sources[index], AL_GAIN, gain);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   303
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   304
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   305
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   306
void openal_setglobalvolume (float gain) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   307
    if (openalReady == AL_TRUE)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   308
        alListenerf (AL_GAIN, gain);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   309
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   310
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   311
void openal_togglemute () {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   312
    ALfloat gain;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   313
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   314
    if (openalReady == AL_TRUE) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   315
        alGetListenerf (AL_GAIN, &gain);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   316
        if (gain > 0) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   317
    old_gain = gain;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   318
            gain = 0;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   319
    } else
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   320
            gain = old_gain;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   321
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   322
    alListenerf (AL_GAIN, gain);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   323
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   324
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   325
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   326
// Fade in or out by calling a helper thread
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   327
void openal_fade (uint32_t index, uint16_t quantity, al_fade_t direction) {
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   328
#ifndef _WIN32
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   329
    pthread_t thread;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   330
#else
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   331
    HANDLE Thread;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   332
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   333
    fade_t *fade;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   334
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   335
    if (openalReady == AL_TRUE && index < globalsize) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   336
        fade = (fade_t*) Malloc(sizeof(fade_t));
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   337
        fade->index = index;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   338
        fade->quantity = quantity;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   339
        fade->type = direction;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   340
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   341
#ifndef _WIN32
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   342
        pthread_create(&thread, NULL, (void *)helper_fade, (void *)fade);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   343
        pthread_detach(thread);
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   344
#else
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   345
        Thread = (HANDLE) _beginthread((void *)helper_fade, 0, (void *)fade);
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   346
#endif
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   347
    }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   348
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   349
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   350
void openal_fadein (uint32_t index, uint16_t quantity) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   351
    openal_fade(index, quantity, AL_FADE_IN);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   352
}
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   353
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   354
void openal_fadeout (uint32_t index, uint16_t quantity) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   355
    openal_fade(index, quantity, AL_FADE_OUT);
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   356
}
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   357
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   358
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   359
void openal_setposition (uint32_t index, float x, float y, float z) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   360
    if (openalReady == AL_TRUE && index < globalsize)
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   361
        alSource3f(Sources[index], AL_POSITION, x, y, z);;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   362
}