sound in opengl

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...
");
	*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...
");
	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...
");
	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,&buffer);
	alBufferData(buffer,format,data,header.dataSize,header.samplesPerSec);
	return buffer;
}

int main(){
	ALCdevice* device = 0;
	ALCcontext* context = 0;
	if (!initOpenAL(&device,&context)){
		printf("FAILED TO INIT OPEN AL
");
		return 0;
	}
	
	BasicWAVEHeader header;
	char* data = readWAVE("MyWave.wav",&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,&source);
			//and now we queue the buffer
			alSourceQueueBuffers(source,1,&buffer);
			//and play
			printf("PLAYING...
");
			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...
");
			alSourceStop(source);
			//unqueue the buffer
			alSourceUnqueueBuffers(source,1,&buffer);
			//clean up
			alDeleteSources(1,&source);
			alDeleteBuffers(1,&buffer);
			printf("ALL DONE!
");
		}else printf("FAILED TO CREATE BUFFER!
");
		free(data);
	}else printf("FAILED TO READ FILE!
");
	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??

so it doesnt like what’s in here:

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

       .......
     }

You would have more chances on the right forum:

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

OpenAL is not OpenGL.