Results 1 to 3 of 3

Thread: sound in opengl

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    13

    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.

    Code :
    #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,header.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??

  2. #2
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    13

    Re: sound in opengl

    so it doesnt like what's in here:

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

    .......
    }

  3. #3
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: sound in opengl

    You would have more chances on the right forum:

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

    OpenAL is not OpenGL.

Similar Threads

  1. Playing Sound Or adding Sound to OpenGl file
    By GangaPatil in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 04-26-2014, 12:17 PM
  2. Sound in OpenGL
    By dvwood in forum OpenGL: Basic Coding
    Replies: 13
    Last Post: 11-15-2008, 06:14 AM
  3. Sound in Opengl
    By zadeh79 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 06-01-2007, 03:14 PM
  4. sound in OpenGL
    By caophong in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 05-09-2004, 10:35 PM
  5. OpenGL and Sound.
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-23-2002, 06:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean