openalbridge/openalbridge.c
changeset 2445 5033848d3afa
parent 2444 ace11b7d8eab
child 2446 cbb3af76bcc0
equal deleted inserted replaced
2444:ace11b7d8eab 2445:5033848d3afa
    38 
    38 
    39 ALCcontext *context;
    39 ALCcontext *context;
    40 ALCdevice *device;
    40 ALCdevice *device;
    41 ALuint sources[MAX_SOURCES];
    41 ALuint sources[MAX_SOURCES];
    42 
    42 
    43 char SSound_load        (SSound_t* pSound, const char* cFilename);
    43 
    44 void SSound_close       (SSound_t* pSound);
       
    45 void SSound_play        (SSound_t* pSound, const char bLoop);
       
    46 void SSound_pause       (const SSound_t* pSound);
       
    47 void SSound_continue    (const SSound_t* pSound);
       
    48 void SSound_stop        (SSound_t* pSound);
       
    49 void SSound_volume      (const SSound_t* pSound, const float fPercentage);
       
    50 
    44 
    51 
    45 
    52 /**
    46 /**
    53  * const char oalb_init (const char* programname, const char usehardware) 
    47  * const char oalb_init (const char* programname, const char usehardware) 
    54  *
    48  *
   448         
   442         
   449         alGetError();  /* clear any AL errors beforehand */
   443         alGetError();  /* clear any AL errors beforehand */
   450         
   444         
   451         return;
   445         return;
   452 }
   446 }
   453 
       
   454 
       
   455 /*SSOUND STUFF HERE*/
       
   456 
       
   457 char SSound_load (SSound_t* pSound, const char* cFilename) {
       
   458         uint32_t magic;
       
   459         ALenum format;
       
   460         ALsizei bitsize, freq;
       
   461         char *data;
       
   462         FILE* fp;
       
   463         
       
   464         snprintf(pSound->Filename, 256, "%s", cFilename);
       
   465         pSound->source = -1;
       
   466         alGenBuffers(1, &pSound->Buffer);
       
   467         
       
   468         if(alGetError() != AL_NO_ERROR) {
       
   469                 fprintf(stderr, "CSound: Couldn't create buffer.\n");
       
   470                 return 0;
       
   471         }
       
   472         
       
   473         fp = fopen(pSound->Filename, "rb");
       
   474         
       
   475         if(!fp) {
       
   476                 fprintf(stderr, "CSound: Couldn't open file for reading.\n");
       
   477                 return 0;
       
   478         }
       
   479         
       
   480         if(fread(&magic, sizeof(uint32_t), 1, fp) < 1)
       
   481         {
       
   482                 fclose(fp);
       
   483                 fprintf(stderr, "CSound: Couldn't read file header.\n");
       
   484                 return 0;
       
   485         }
       
   486         fclose(fp);
       
   487         
       
   488         switch (ENDIAN_BIG_32(magic)) {
       
   489                 case OGG_FILE_FORMAT:
       
   490                         load_oggvorbis (pSound->Filename, &format, &data, &bitsize, &freq);
       
   491                         break;
       
   492                 case WAV_FILE_FORMAT:
       
   493                         load_wavpcm (pSound->Filename, &format, &data, &bitsize, &freq);
       
   494                         break;
       
   495                 default:
       
   496                         errno = EINVAL;
       
   497                         err_ret ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(magic));
       
   498                         return 0;
       
   499                         break;
       
   500         }
       
   501         
       
   502         alBufferData(pSound->Buffer, format, data, bitsize, freq);
       
   503         if(alGetError() != AL_NO_ERROR)
       
   504         {
       
   505                 fprintf(stderr, "CSound: Couldn't write buffer data.\n");
       
   506                 return 0;
       
   507         }
       
   508         free(data);
       
   509         
       
   510         return 1;
       
   511 }
       
   512 
       
   513 void SSound_close(SSound_t* pSound)
       
   514 {
       
   515         SSound_stop(pSound);
       
   516         alDeleteBuffers(1, &pSound->Buffer);
       
   517 }
       
   518 
       
   519 void SSound_play(SSound_t* pSound, const char bLoop) {
       
   520         int i;
       
   521         
       
   522         if(pSound->source == -1) // need a new source
       
   523         {
       
   524                 int i;
       
   525                 for(i = 0; i < MAX_SOURCES; i++)
       
   526                 {
       
   527                         ALint state;
       
   528                         alGetSourcei(sources[i], AL_SOURCE_STATE, &state);
       
   529                         if(state != AL_PLAYING && state != AL_PAUSED)
       
   530                         {
       
   531 #ifdef DEBUG
       
   532                                 printf("using source %d (state 0x%x) for buffer.\n", i, state);
       
   533 #endif
       
   534                                 alSourceStop(sources[pSound->source]);
       
   535                                 alGetError();
       
   536                                 break;
       
   537                         }
       
   538                 }
       
   539                 if(i == MAX_SOURCES) // no available source found; skip
       
   540                 {
       
   541 #ifdef DEBUG
       
   542                         printf("no source to play buffer %d!\n", i);
       
   543 #endif
       
   544                         return;
       
   545                 }
       
   546                 pSound->source = i;
       
   547         }
       
   548         else // reuse already playing source
       
   549         {
       
   550                 alSourceStop(sources[pSound->source]);
       
   551         }
       
   552         alSourcei (sources[pSound->source], AL_BUFFER, pSound->Buffer);
       
   553         alSourcef (sources[pSound->source], AL_PITCH,            1.0f);
       
   554         alSourcef (sources[pSound->source], AL_GAIN,             1.0f);
       
   555         alSourcefv(sources[pSound->source], AL_POSITION, NV          );
       
   556         alSourcefv(sources[pSound->source], AL_VELOCITY, NV          );
       
   557         alSourcei (sources[pSound->source], AL_LOOPING,  bLoop       );
       
   558         alSourcePlay(sources[pSound->source]);
       
   559         
       
   560         if((i = alGetError()) != AL_NO_ERROR)
       
   561         {
       
   562                 fprintf(stderr, "CSound: SourcePlay error 0x%4x in source %d\n", i, pSound->source);
       
   563         }
       
   564 #ifdef DEBUG
       
   565         fprintf(stderr, "play %s%s [%d]\n", pSound->Filename, bLoop ? " forever" : " once", pSound->source);
       
   566 #endif
       
   567 }
       
   568 
       
   569 void SSound_pause(const SSound_t* pSound) {
       
   570         if(pSound->source == -1) // not playing
       
   571                 return;
       
   572         alSourcePause(sources[pSound->source]);
       
   573 #ifdef DEBUG
       
   574         fprintf(stderr, "pause %s\n", pSound->Filename);
       
   575 #endif
       
   576 }
       
   577 
       
   578 void SSound_continue(const SSound_t* pSound) {
       
   579         if(pSound->source == -1) // not playing
       
   580                 return;
       
   581         alSourcePlay(sources[pSound->source]);
       
   582 #ifdef DEBUG
       
   583         fprintf(stderr, "pause %s\n", pSound->Filename);
       
   584 #endif
       
   585 }
       
   586 
       
   587 void SSound_stop(SSound_t* pSound) {
       
   588         if(pSound->source == -1) // not playing
       
   589                 return;
       
   590         alSourceStop(sources[pSound->source]);
       
   591         pSound->source = -1;
       
   592 #ifdef DEBUG
       
   593         fprintf(stderr, "stop %s\n", pSound->Filename);
       
   594 #endif
       
   595 }
       
   596 
       
   597 void SSound_volume(const SSound_t* pSound, const float fPercentage) {
       
   598         if(pSound->source == -1) // not playing
       
   599                 return;
       
   600         alSourcef(sources[pSound->source], AL_GAIN, fPercentage);
       
   601 }        
       
   602