PDA

View Full Version : sound in opengl



popapez
05-07-2009, 11:52 AM
hi, i was wondering if there is an easy way to add a wav file to play when a certain event happens?

I have done some research and found openAL, and code that should work. it is not working though.



#include <AL/al.h>
#include <AL/alc.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


typedef struct {

char wave[4];//'WAVE'
unsigned short format;
unsigned short channels;
unsigned int samplesPerSec;
unsigned int bytesPerSec;
unsigned short blockAlign;
unsigned short bitsPerSample;
char data[4];//'data'
unsigned int dataSize;
}BasicWAVEHeader;

int initOpenAL(ALCdevice** device,ALCcontext** context){
printf("Initialising Open AL...\n");
*device = alcOpenDevice(0);
if (*device){
*context = alcCreateContext(*device,0);
if (*context){
alcMakeContextCurrent(*context);
return 1;
}
alcCloseDevice(*device);
}
return 0;
}

//WARNING: This Doesn't Check To See If These Pointers Are Valid
void deinitOpenAL(ALCdevice* device,ALCcontext* context){
alcMakeContextCurrent(0);
alcDestroyContext(context);
alcCloseDevice(device);
}

//WARNING: This Doesn't Check To See If These Pointers Are Valid
char* readWAVE(char* filename,BasicWAVEHeader* header){
printf("Reading File...\n");
char* buffer = 0;
FILE* file = fopen(filename,"rb");
if (!file) return 0;

if (fread(header,sizeof(BasicWAVEHeader),1,file)){
if (!(//these things *must* be valid with this basic header
memcmp("RIFF",header->riff,4) ||
memcmp("WAVE",header->wave,4) ||
memcmp("fmt ",header->fmt,4) ||
memcmp("data",header->data,4)
)){
buffer = (char*)malloc(header->dataSize);
if (buffer){
if (fread(buffer,header->dataSize,1,file)){
fclose(file);
return buffer;
}
free(buffer);
}
}
}
fclose(file);
return 0;
}

ALuint createBufferFromWave(char* data,BasicWAVEHeader header){
printf("Creating Buffer...\n");
ALuint buffer = 0;

ALuint format = 0;
switch (header.bitsPerSample){
case 8:
format = (header.channels == 1) ? AL_FORMAT_MONO8 : AL_FORMAT_STEREO8;
break;
case 16:
format = (header.channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
break;
default:
//last time I checked, Open AL didn't support anything else
return 0;
}

alGenBuffers(1,&amp;buffer);
alBufferData(buffer,format,data,header.dataSize,he ader.samplesPerSec);
return buffer;
}

int main(){
ALCdevice* device = 0;
ALCcontext* context = 0;
if (!initOpenAL(&amp;device,&amp;context)){
printf("FAILED TO INIT OPEN AL\n");
return 0;
}

BasicWAVEHeader header;
char* data = readWAVE("MyWave.wav",&amp;header);
if (data){
//Now We've Got A Wave In Memory, Time To Turn It Into A Usable Buffer
ALuint buffer = createBufferFromWave(data,header);
if (buffer){
//You can usually free the data here, but in this example, it's done later

//Time to create a source to play the wave
ALuint source;
alGenSources(1,&amp;source);
//and now we queue the buffer
alSourceQueueBuffers(source,1,&amp;buffer);
//and play
printf("PLAYING...\n");
alSourcePlay(source);
//now we do nothing for 10 seconds but let it play
time_t t = time(0) + 10;
while (time(0) < t){
Sleep(100);
}
//now we stop it (assuming it's still playing...)
printf("STOPPING...\n");
alSourceStop(source);
//unqueue the buffer
alSourceUnqueueBuffers(source,1,&amp;buffer);
//clean up
alDeleteSources(1,&amp;source);
alDeleteBuffers(1,&amp;buffer);
printf("ALL DONE!\n");
}else printf("FAILED TO CREATE BUFFER!\n");
free(data);
}else printf("FAILED TO READ FILE!\n");
deinitOpenAL(device,context);

return 1;
}





its having problems playing the sound. is prints this line:
FAILED TO READ FILE!
which is at the end of that code segment
whats wrong??

popapez
05-07-2009, 01:10 PM
so it doesnt like what's in here:

char* data = readWAVE("MyWave.wav",&header);
if (data){

.......
}

dletozeun
05-07-2009, 01:55 PM
You would have more chances on the right forum:

http://connect.creativelabs.com/openal/default.aspx

OpenAL is not OpenGL.