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