misc/libopenalbridge/openalbridge.c
author sheepluva
Wed, 02 May 2012 10:47:21 +0200
changeset 6987 e34415c77342
parent 3697 d5b30d6373fc
permissions -rw-r--r--
allow custom sorting of roomslist (by clicking on header sections)
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"
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    21
#include "al.h"
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    22
#include "alc.h"
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    23
#include "wrappers.h"
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    24
#include "loaders.h"
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    25
#include "string.h"
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
    26
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    27
// Sources are points emitting sound, their number is limited, but a single source can play many buffers
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    28
// Buffers hold sound data and are unlimited
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    29
ALuint *Sources;
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    30
ALuint cache_size, cache_index, sources_number;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    31
ALboolean instances_number;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    32
al_sound_t *the_sounds;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    33
ALint cache_pointer;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    34
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    35
// Initialize an OpenAL contex and allocate memory space for data and buffers
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    36
// It can be called twice to increase the cache size
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
    37
int openal_init (void) {
3362
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;
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    40
    int i;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
    41
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
    42
    // reuse old context and resize the existing
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    43
    if (openal_ready() == AL_TRUE) {
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
    44
        fprintf(stderr,"(Bridge Info) - already initialized\n");
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    45
        instances_number++;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    46
        return AL_TRUE;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    47
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
    48
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    49
    cache_pointer = 0;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    50
    instances_number++;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
    51
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
    52
    // initial memory size
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
    53
    cache_size = 50;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
    54
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    55
    // open hardware device if present
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    56
    device = alcOpenDevice(NULL);
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    57
    sources_number = 16;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    58
    if (device == NULL) {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    59
        fprintf(stderr,"(Bridge Warning) - failed to open sound device, using software renderer\n");
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    60
        device = alcOpenDevice("Generic Software");
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    61
        sources_number = 32;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    62
        if (device == NULL) {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    63
            fprintf(stderr,"(Bridge ERROR) - failed to start software renderer, sound will be disabled\n");
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    64
            return -1;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    65
        }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    66
    }
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    67
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    68
    fprintf(stderr,"(Bridge Info) - output device: %s\n", alcGetString(device, ALC_DEVICE_SPECIFIER));
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    69
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    70
    context = alcCreateContext(device, NULL);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    71
    alcMakeContextCurrent(context);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    72
    alcProcessContext(context);
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    73
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    74
    if (AL_NO_ERROR != alGetError()) {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    75
        fprintf(stderr,"(Bridge ERROR) - Failed to create a new contex\n");
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    76
        alcMakeContextCurrent(NULL);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    77
        alcDestroyContext(context);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    78
        alcCloseDevice(device);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    79
        return -2;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    80
    }
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    81
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    82
    Sources = (ALuint *)Malloc (sizeof(ALuint) * sources_number);
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    83
    alGenSources(sources_number, Sources);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
    84
3362
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};
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    89
    ALfloat ListenerOri[] = {0.0, 0.0, -1.0, 0.0, 1.0, 0.0};
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    90
3362
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);
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    95
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
    96
    if (AL_NO_ERROR != alGetError()) {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
    97
        fprintf(stderr,"(Bridge ERROR) - Failed to set Listener properties\n");
3362
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
    }
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   100
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   101
    the_sounds = (al_sound_t *)Malloc (sizeof(al_sound_t) * cache_size);
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   102
    for (i = 0; i < cache_size; i++)
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   103
        the_sounds[i] = new_sound_el();
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   104
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   105
    alGetError();
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   106
    return AL_TRUE;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   107
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   108
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   109
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   110
// Stop all sounds, deallocate all memory and close OpenAL context
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   111
void openal_close (void) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   112
    ALCcontext *context;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   113
    ALCdevice  *device;
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   114
    int i;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   115
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   116
    if (instances_number == 0) {
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   117
        fprintf(stderr,"(Bridge Warning) - OpenAL not initialized\n");
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   118
        return;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   119
    }
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   120
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   121
    instances_number--;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   122
    if (instances_number > 0) {
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   123
        // release memory only when last session ends
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   124
        return;
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   125
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   126
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   127
    for (i = 0; i < cache_size; i++) {
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   128
        openal_unloadfile(i);
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   129
    }
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   130
    free(the_sounds);
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   131
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   132
    alSourceStopv (sources_number, Sources);
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   133
    alDeleteSources (sources_number, Sources);
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   134
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   135
    free(Sources);
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   136
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   137
    context = alcGetCurrentContext();
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   138
    device  = alcGetContextsDevice(context);
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   139
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   140
    alcMakeContextCurrent(NULL);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   141
    alcDestroyContext(context);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   142
    alcCloseDevice(device);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   143
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   144
    fprintf(stderr,"(Bridge Info) - closed\n");
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   145
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   146
    return;
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
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   149
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   150
ALboolean openal_ready (void) {
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   151
    if (instances_number >= 1)
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   152
        return AL_TRUE;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   153
    else
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   154
        return AL_FALSE;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   155
}
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   156
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   157
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   158
// Open a file, load into memory and allocate the Source buffer for playing
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   159
int openal_loadfile (const char *filename){
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   160
    ALenum format, error;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   161
    ALsizei bitsize, freq;
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   162
    uint32_t fileformat;
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   163
    al_sound_t sound_data;
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   164
    int len, i, index = -1;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   165
    char *data;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   166
    FILE *fp;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   167
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   168
    if (openal_ready() == AL_FALSE) {
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   169
        fprintf(stderr,"(Bridge Warning) - not initialized\n");
3362
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
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   172
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   173
    // if this sound is already loaded return the index from the_sounds
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   174
    len = strlen(filename);
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   175
    for (i = 0; i < cache_size; i++) {
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   176
        if (the_sounds[i].filename != NULL && strncmp(the_sounds[i].filename, filename, len) == 0) {
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   177
#ifdef DEBUG
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   178
            fprintf(stderr,"(Bridge Debug) - sound %d is already loaded\n", i);
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   179
#endif
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   180
            return i;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   181
        }
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   182
        // if we don't have memory available search for a free element
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   183
        if (cache_pointer >= cache_size)
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   184
            if (the_sounds[i].is_used == AL_FALSE)
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   185
                index = i;
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   186
    }
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   187
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   188
    if (index == -1 && cache_pointer >= cache_size) {
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   189
        fprintf(stderr,"(Bridge Info) - No free spots found; doubling cache size\n", filename);
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   190
        cache_size *= 2;
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   191
        the_sounds = (al_sound_t *)Realloc (the_sounds, sizeof(al_sound_t) * cache_size);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   192
        for (i = cache_size - 50; i < cache_size; i++)
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   193
            the_sounds[i] = new_sound_el();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   194
    } else
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   195
        index = ++cache_pointer;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   196
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   197
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   198
    // detect the file format, as written in the first 4 bytes of the header
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   199
    fp = Fopen (filename, "rb");
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   200
    if (fp == NULL) {
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   201
        fprintf(stderr,"(Bridge ERROR) - File %s not loaded\n", filename);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   202
        return -3;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   203
    }
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   204
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   205
    error = fread (&fileformat, sizeof(uint32_t), 1, fp);
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   206
    fclose (fp);
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   207
    if (error < 0) {
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   208
        fprintf(stderr,"(Bridge ERROR) - File %s is too short\n", filename);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   209
        return -4;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   210
    }
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   211
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   212
    switch (ENDIAN_BIG_32(fileformat)) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   213
        case OGG_FILE_FORMAT:
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   214
            error = load_oggvorbis (filename, &format, &data, &bitsize, &freq);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   215
            break;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   216
        case WAV_FILE_FORMAT:
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   217
            error = load_wavpcm (filename, &format, &data, &bitsize, &freq);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   218
            break;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   219
        default:
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   220
            fprintf(stderr,"(Bridge ERROR) - File format (%08X) not supported\n", ENDIAN_BIG_32(fileformat));
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   221
            return -5;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   222
            break;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   223
    }
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   224
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   225
    if (error != 0) {
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   226
        fprintf(stderr,"(Bridge ERROR) - error loading file %s\n", filename);
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   227
        if(data)
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   228
            free(data);
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   229
        return -6;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   230
    }
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   231
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   232
    // alGenBuffers happens here
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   233
    sound_data = init_sound_el(filename);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   234
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   235
    if (AL_NO_ERROR != alGetError()) {
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   236
        fprintf(stderr,"(Bridge ERROR) - Failed to allocate memory for buffer %d\n", index);
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   237
        free(data);
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   238
        return -5;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   239
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   240
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   241
    // copy pcm data in one buffer and free it
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   242
    alBufferData(sound_data.buffer, format, data, bitsize, freq);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   243
    free(data);
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   244
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   245
    if (AL_NO_ERROR != alGetError()) {
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   246
        fprintf(stderr,"(Bridge ERROR) - Failed to write data to buffer %d\n", index);
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
   247
        return -8;
3360
717b4e46e855 updates to openalbridge (modified api and simplified some sections)
koda
parents: 3356
diff changeset
   248
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   249
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   250
    // clear any AL errors beforehand
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   251
    alGetError();
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   252
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   253
    fprintf(stderr,"(Bridge Info) - successfully loaded %s\n", filename);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   254
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3364
diff changeset
   255
    // returns the index of the source you just loaded, increments it and exits
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   256
    the_sounds[index] = sound_data;
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   257
    return index;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3361
diff changeset
   258
}
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   259
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   260
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   261
void openal_unloadfile (uint32_t index) {
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   262
    ALint state;
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   263
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   264
    if (openal_ready() == AL_TRUE && index < cache_size && the_sounds[index].is_used == AL_TRUE) {
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   265
        alGetSourcei (Sources[the_sounds[index].source_index], AL_SOURCE_STATE, &state);
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   266
        if (state == AL_PLAYING || state == AL_PAUSED)
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   267
            openal_stopsound(index);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   268
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   269
        // free memory and
3529
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   270
        alDeleteBuffers (1, &the_sounds[index].buffer);
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   271
        the_sounds[index] = new_sound_el();
0e968ba12a84 memory management for openalbridge
koda
parents: 3514
diff changeset
   272
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3529
diff changeset
   273
}