openalbridge/ssound.c
author nemo
Fri, 16 Oct 2009 15:35:41 +0000
changeset 2492 9e80b7fc4017
parent 2468 0b62498c201a
permissions -rw-r--r--
Tweak of molotov based on burp's play, a little bit more specificity in homerun.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
     1
/*
5033848d3afa committing some missing files
koda
parents:
diff changeset
     2
 * OpenAL Bridge - a simple portable library for OpenAL interface
5033848d3afa committing some missing files
koda
parents:
diff changeset
     3
 * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>,
5033848d3afa committing some missing files
koda
parents:
diff changeset
     4
 *                    Mario Liebisch <mario.liebisch+hw@googlemail.com>
5033848d3afa committing some missing files
koda
parents:
diff changeset
     5
 *
5033848d3afa committing some missing files
koda
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
5033848d3afa committing some missing files
koda
parents:
diff changeset
     7
 * it under the terms of the GNU Lesser General Public License as published by
5033848d3afa committing some missing files
koda
parents:
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
5033848d3afa committing some missing files
koda
parents:
diff changeset
     9
 *
5033848d3afa committing some missing files
koda
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
5033848d3afa committing some missing files
koda
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5033848d3afa committing some missing files
koda
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5033848d3afa committing some missing files
koda
parents:
diff changeset
    13
 * GNU Lesser General Public License for more details.
5033848d3afa committing some missing files
koda
parents:
diff changeset
    14
 *
5033848d3afa committing some missing files
koda
parents:
diff changeset
    15
 * You should have received a copy of the GNU Lesser General Public License
5033848d3afa committing some missing files
koda
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
5033848d3afa committing some missing files
koda
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
5033848d3afa committing some missing files
koda
parents:
diff changeset
    18
 */
5033848d3afa committing some missing files
koda
parents:
diff changeset
    19
2446
cbb3af76bcc0 partial italian translation and other fixes to lib
koda
parents: 2445
diff changeset
    20
#include "ssound.h"
cbb3af76bcc0 partial italian translation and other fixes to lib
koda
parents: 2445
diff changeset
    21
#include "loaders.h"
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
    22
2468
0b62498c201a openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents: 2454
diff changeset
    23
extern SSound_t aSounds[MAX_SOUNDS];
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
    24
extern ALuint sources[MAX_SOURCES];
5033848d3afa committing some missing files
koda
parents:
diff changeset
    25
extern const ALfloat NV[3];
5033848d3afa committing some missing files
koda
parents:
diff changeset
    26
extern const ALfloat LO[6];
2468
0b62498c201a openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents: 2454
diff changeset
    27
extern int iNumSounds;
2446
cbb3af76bcc0 partial italian translation and other fixes to lib
koda
parents: 2445
diff changeset
    28
extern char *prog;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
    29
5033848d3afa committing some missing files
koda
parents:
diff changeset
    30
char SSound_load (SSound_t* pSound, const char* cFilename) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    31
        uint32_t magic;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    32
        ALenum format;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    33
        ALsizei bitsize, freq;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    34
        char *data;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    35
        FILE* fp;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    36
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    37
        snprintf(pSound->Filename, 256, "%s", cFilename);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    38
        pSound->source = -1;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    39
        alGenBuffers(1, &pSound->Buffer);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    40
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    41
        if(alGetError() != AL_NO_ERROR) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    42
                fprintf(stderr, "CSound: Couldn't create buffer.\n");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    43
                return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    44
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    45
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    46
        fp = fopen(pSound->Filename, "rb");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    47
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    48
        if(!fp) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    49
                fprintf(stderr, "CSound: Couldn't open file for reading.\n");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    50
                return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    51
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    52
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    53
        if(fread(&magic, sizeof(uint32_t), 1, fp) < 1)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    54
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    55
                fclose(fp);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    56
                fprintf(stderr, "CSound: Couldn't read file header.\n");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    57
                return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    58
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    59
        fclose(fp);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    60
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    61
        switch (ENDIAN_BIG_32(magic)) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    62
                case OGG_FILE_FORMAT:
5033848d3afa committing some missing files
koda
parents:
diff changeset
    63
                        load_oggvorbis (pSound->Filename, &format, &data, &bitsize, &freq);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    64
                        break;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    65
                case WAV_FILE_FORMAT:
5033848d3afa committing some missing files
koda
parents:
diff changeset
    66
                        load_wavpcm (pSound->Filename, &format, &data, &bitsize, &freq);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    67
                        break;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    68
                default:
5033848d3afa committing some missing files
koda
parents:
diff changeset
    69
                        errno = EINVAL;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    70
                        err_ret ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(magic));
5033848d3afa committing some missing files
koda
parents:
diff changeset
    71
                        return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    72
                        break;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    73
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    74
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    75
        alBufferData(pSound->Buffer, format, data, bitsize, freq);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    76
        if(alGetError() != AL_NO_ERROR)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    77
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    78
                fprintf(stderr, "CSound: Couldn't write buffer data.\n");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    79
                return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    80
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    81
        free(data);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    82
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    83
        return 1;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    84
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
    85
5033848d3afa committing some missing files
koda
parents:
diff changeset
    86
void SSound_close(SSound_t* pSound)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    87
{
5033848d3afa committing some missing files
koda
parents:
diff changeset
    88
        SSound_stop(pSound);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    89
        alDeleteBuffers(1, &pSound->Buffer);
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
    90
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
    91
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
    92
5033848d3afa committing some missing files
koda
parents:
diff changeset
    93
void SSound_play(SSound_t* pSound, const char bLoop) {
2468
0b62498c201a openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents: 2454
diff changeset
    94
        int i, j;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
    95
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    96
        if(pSound->source == -1) // need a new source
5033848d3afa committing some missing files
koda
parents:
diff changeset
    97
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    98
                for(i = 0; i < MAX_SOURCES; i++)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    99
                {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   100
                        ALint state;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   101
                        alGetSourcei(sources[i], AL_SOURCE_STATE, &state);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   102
                        if(state != AL_PLAYING && state != AL_PAUSED)
5033848d3afa committing some missing files
koda
parents:
diff changeset
   103
                        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   104
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   105
                                printf("using source %d (state 0x%x) for buffer.\n", i, state);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   106
#endif
5033848d3afa committing some missing files
koda
parents:
diff changeset
   107
                                alSourceStop(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   108
                                alGetError();
2468
0b62498c201a openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents: 2454
diff changeset
   109
0b62498c201a openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents: 2454
diff changeset
   110
								// lookup buffers associated with this source and reset them
0b62498c201a openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents: 2454
diff changeset
   111
								for(j = 0; j < iNumSounds; j++)
0b62498c201a openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents: 2454
diff changeset
   112
									if(aSounds[j].source == i)
0b62498c201a openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents: 2454
diff changeset
   113
										aSounds[j].source = -1;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   114
                                break;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   115
                        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   116
                }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   117
                if(i == MAX_SOURCES) // no available source found; skip
5033848d3afa committing some missing files
koda
parents:
diff changeset
   118
                {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   119
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   120
                        printf("no source to play buffer %d!\n", i);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   121
#endif
5033848d3afa committing some missing files
koda
parents:
diff changeset
   122
                        return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   123
                }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   124
                pSound->source = i;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   125
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   126
        else // reuse already playing source
5033848d3afa committing some missing files
koda
parents:
diff changeset
   127
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   128
                alSourceStop(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   129
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   130
        alSourcei (sources[pSound->source], AL_BUFFER, pSound->Buffer);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   131
        alSourcef (sources[pSound->source], AL_PITCH,            1.0f);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   132
        alSourcef (sources[pSound->source], AL_GAIN,             1.0f);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   133
        alSourcefv(sources[pSound->source], AL_POSITION, NV          );
5033848d3afa committing some missing files
koda
parents:
diff changeset
   134
        alSourcefv(sources[pSound->source], AL_VELOCITY, NV          );
5033848d3afa committing some missing files
koda
parents:
diff changeset
   135
        alSourcei (sources[pSound->source], AL_LOOPING,  bLoop       );
5033848d3afa committing some missing files
koda
parents:
diff changeset
   136
        alSourcePlay(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   137
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
   138
        if((i = alGetError()) != AL_NO_ERROR)
5033848d3afa committing some missing files
koda
parents:
diff changeset
   139
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   140
                fprintf(stderr, "CSound: SourcePlay error 0x%4x in source %d\n", i, pSound->source);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   141
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   142
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   143
        fprintf(stderr, "play %s%s [%d]\n", pSound->Filename, bLoop ? " forever" : " once", pSound->source);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   144
#endif
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   145
        usleep(0);
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   146
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   147
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
   148
5033848d3afa committing some missing files
koda
parents:
diff changeset
   149
void SSound_pause(const SSound_t* pSound) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   150
        if(pSound->source == -1) // not playing
5033848d3afa committing some missing files
koda
parents:
diff changeset
   151
                return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   152
        alSourcePause(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   153
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   154
        fprintf(stderr, "pause %s\n", pSound->Filename);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   155
#endif
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   156
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   157
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
   158
5033848d3afa committing some missing files
koda
parents:
diff changeset
   159
void SSound_continue(const SSound_t* pSound) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   160
        if(pSound->source == -1) // not playing
5033848d3afa committing some missing files
koda
parents:
diff changeset
   161
                return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   162
        alSourcePlay(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   163
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   164
        fprintf(stderr, "pause %s\n", pSound->Filename);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   165
#endif
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   166
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   167
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
   168
5033848d3afa committing some missing files
koda
parents:
diff changeset
   169
void SSound_stop(SSound_t* pSound) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   170
        if(pSound->source == -1) // not playing
5033848d3afa committing some missing files
koda
parents:
diff changeset
   171
                return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   172
        alSourceStop(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   173
        pSound->source = -1;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   174
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   175
        fprintf(stderr, "stop %s\n", pSound->Filename);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   176
#endif
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   177
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   178
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
   179
5033848d3afa committing some missing files
koda
parents:
diff changeset
   180
void SSound_volume(const SSound_t* pSound, const float fPercentage) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   181
        if(pSound->source == -1) // not playing
5033848d3afa committing some missing files
koda
parents:
diff changeset
   182
                return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   183
        alSourcef(sources[pSound->source], AL_GAIN, fPercentage);
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   184
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   185
}