openalbridge/ssound.c
author koda
Thu, 15 Oct 2009 15:54:48 +0000
changeset 2454 c8b1fb10003c
parent 2446 cbb3af76bcc0
child 2468 0b62498c201a
permissions -rw-r--r--
fix a potential bug when playing two sounds at the same time (or very near)
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
5033848d3afa committing some missing files
koda
parents:
diff changeset
    23
extern ALuint sources[MAX_SOURCES];
5033848d3afa committing some missing files
koda
parents:
diff changeset
    24
extern const ALfloat NV[3];
5033848d3afa committing some missing files
koda
parents:
diff changeset
    25
extern const ALfloat LO[6];
5033848d3afa committing some missing files
koda
parents:
diff changeset
    26
2446
cbb3af76bcc0 partial italian translation and other fixes to lib
koda
parents: 2445
diff changeset
    27
extern char *prog;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
    28
5033848d3afa committing some missing files
koda
parents:
diff changeset
    29
char SSound_load (SSound_t* pSound, const char* cFilename) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    30
        uint32_t magic;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    31
        ALenum format;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    32
        ALsizei bitsize, freq;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    33
        char *data;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    34
        FILE* fp;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    35
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    36
        snprintf(pSound->Filename, 256, "%s", cFilename);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    37
        pSound->source = -1;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    38
        alGenBuffers(1, &pSound->Buffer);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    39
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    40
        if(alGetError() != AL_NO_ERROR) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    41
                fprintf(stderr, "CSound: Couldn't create buffer.\n");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    42
                return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    43
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    44
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    45
        fp = fopen(pSound->Filename, "rb");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    46
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    47
        if(!fp) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    48
                fprintf(stderr, "CSound: Couldn't open file for reading.\n");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    49
                return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    50
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    51
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    52
        if(fread(&magic, sizeof(uint32_t), 1, fp) < 1)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    53
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    54
                fclose(fp);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    55
                fprintf(stderr, "CSound: Couldn't read file header.\n");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    56
                return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    57
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    58
        fclose(fp);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    59
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    60
        switch (ENDIAN_BIG_32(magic)) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    61
                case OGG_FILE_FORMAT:
5033848d3afa committing some missing files
koda
parents:
diff changeset
    62
                        load_oggvorbis (pSound->Filename, &format, &data, &bitsize, &freq);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    63
                        break;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    64
                case WAV_FILE_FORMAT:
5033848d3afa committing some missing files
koda
parents:
diff changeset
    65
                        load_wavpcm (pSound->Filename, &format, &data, &bitsize, &freq);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    66
                        break;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    67
                default:
5033848d3afa committing some missing files
koda
parents:
diff changeset
    68
                        errno = EINVAL;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    69
                        err_ret ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(magic));
5033848d3afa committing some missing files
koda
parents:
diff changeset
    70
                        return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    71
                        break;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    72
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    73
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    74
        alBufferData(pSound->Buffer, format, data, bitsize, freq);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    75
        if(alGetError() != AL_NO_ERROR)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    76
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    77
                fprintf(stderr, "CSound: Couldn't write buffer data.\n");
5033848d3afa committing some missing files
koda
parents:
diff changeset
    78
                return 0;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    79
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
    80
        free(data);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    81
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    82
        return 1;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    83
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
    84
5033848d3afa committing some missing files
koda
parents:
diff changeset
    85
void SSound_close(SSound_t* pSound)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    86
{
5033848d3afa committing some missing files
koda
parents:
diff changeset
    87
        SSound_stop(pSound);
5033848d3afa committing some missing files
koda
parents:
diff changeset
    88
        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
    89
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
    90
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
    91
5033848d3afa committing some missing files
koda
parents:
diff changeset
    92
void SSound_play(SSound_t* pSound, const char bLoop) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    93
        int i;
5033848d3afa committing some missing files
koda
parents:
diff changeset
    94
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    95
        if(pSound->source == -1) // need a new source
5033848d3afa committing some missing files
koda
parents:
diff changeset
    96
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
    97
                int i;
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();
5033848d3afa committing some missing files
koda
parents:
diff changeset
   109
                                break;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   110
                        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   111
                }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   112
                if(i == MAX_SOURCES) // no available source found; skip
5033848d3afa committing some missing files
koda
parents:
diff changeset
   113
                {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   114
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   115
                        printf("no source to play buffer %d!\n", i);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   116
#endif
5033848d3afa committing some missing files
koda
parents:
diff changeset
   117
                        return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   118
                }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   119
                pSound->source = i;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   120
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   121
        else // reuse already playing source
5033848d3afa committing some missing files
koda
parents:
diff changeset
   122
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   123
                alSourceStop(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   124
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   125
        alSourcei (sources[pSound->source], AL_BUFFER, pSound->Buffer);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   126
        alSourcef (sources[pSound->source], AL_PITCH,            1.0f);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   127
        alSourcef (sources[pSound->source], AL_GAIN,             1.0f);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   128
        alSourcefv(sources[pSound->source], AL_POSITION, NV          );
5033848d3afa committing some missing files
koda
parents:
diff changeset
   129
        alSourcefv(sources[pSound->source], AL_VELOCITY, NV          );
5033848d3afa committing some missing files
koda
parents:
diff changeset
   130
        alSourcei (sources[pSound->source], AL_LOOPING,  bLoop       );
5033848d3afa committing some missing files
koda
parents:
diff changeset
   131
        alSourcePlay(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   132
        
5033848d3afa committing some missing files
koda
parents:
diff changeset
   133
        if((i = alGetError()) != AL_NO_ERROR)
5033848d3afa committing some missing files
koda
parents:
diff changeset
   134
        {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   135
                fprintf(stderr, "CSound: SourcePlay error 0x%4x in source %d\n", i, pSound->source);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   136
        }
5033848d3afa committing some missing files
koda
parents:
diff changeset
   137
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   138
        fprintf(stderr, "play %s%s [%d]\n", pSound->Filename, bLoop ? " forever" : " once", pSound->source);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   139
#endif
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   140
        usleep(0);
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   141
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   142
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
   143
5033848d3afa committing some missing files
koda
parents:
diff changeset
   144
void SSound_pause(const SSound_t* pSound) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   145
        if(pSound->source == -1) // not playing
5033848d3afa committing some missing files
koda
parents:
diff changeset
   146
                return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   147
        alSourcePause(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   148
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   149
        fprintf(stderr, "pause %s\n", pSound->Filename);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   150
#endif
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   151
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   152
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
   153
5033848d3afa committing some missing files
koda
parents:
diff changeset
   154
void SSound_continue(const SSound_t* pSound) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   155
        if(pSound->source == -1) // not playing
5033848d3afa committing some missing files
koda
parents:
diff changeset
   156
                return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   157
        alSourcePlay(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   158
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   159
        fprintf(stderr, "pause %s\n", pSound->Filename);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   160
#endif
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   161
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   162
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
   163
5033848d3afa committing some missing files
koda
parents:
diff changeset
   164
void SSound_stop(SSound_t* pSound) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   165
        if(pSound->source == -1) // not playing
5033848d3afa committing some missing files
koda
parents:
diff changeset
   166
                return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   167
        alSourceStop(sources[pSound->source]);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   168
        pSound->source = -1;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   169
#ifdef DEBUG
5033848d3afa committing some missing files
koda
parents:
diff changeset
   170
        fprintf(stderr, "stop %s\n", pSound->Filename);
5033848d3afa committing some missing files
koda
parents:
diff changeset
   171
#endif
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2446
diff changeset
   172
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   173
}
5033848d3afa committing some missing files
koda
parents:
diff changeset
   174
5033848d3afa committing some missing files
koda
parents:
diff changeset
   175
void SSound_volume(const SSound_t* pSound, const float fPercentage) {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   176
        if(pSound->source == -1) // not playing
5033848d3afa committing some missing files
koda
parents:
diff changeset
   177
                return;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   178
        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
   179
        return;
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   180
}