openAL sound class problem

whotaces 101 Apr 09, 2011 at 15:20

Hi! I am pretty new to programming, and i am trying to make a simple sound class for my project using openAL. The problem is that i cant seem to get the sounds to play when i split the class =S

It is supposed to have more audio files, but i’ve shorted it down for more simplicity. Does anyone know what i am doing wrong?

thanks in advance =)

/////////////////////////// sound.h ///////////////////////////
#ifndef SOUND_H
#define SOUND_H

#include <iostream>
#include <al.h>
#include <alc.h>
#include <alut.h>

#define NUM_BUFFERS 1
#define NUM_SOURCES 1

#define game 0

class cSound
void SetListenerValues();

int format;
int size;
void* data;
int freq;
char loop;

unsigned int Buffers[NUM_BUFFERS];
unsigned int Sources[NUM_SOURCES];

/////////////////////////// sound.cpp ///////////////////////////

#include “sound.h”

float SourcesPos[NUM_SOURCES][3];
float SourcesVel[NUM_SOURCES][3];
float ListenerPos[] = { 0.0, 0.0, 0.0 };
float ListenerVel[] = { 0.0, 0.0, 0.0 };
float ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 };

alGenBuffers(NUM_BUFFERS, Buffers);

alutLoadWAVFile(“jump.wav”, &format, &data, &size, &freq, &loop);
alBufferData(Buffers[game], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);

alGenSources(NUM_SOURCES, Sources);

alSourcei (Sources[game], AL_BUFFER, Buffers[game] );
alSourcef (Sources[game], AL_PITCH, 1.0f );
alSourcefv(Sources[game], AL_VELOCITY, SourcesVel[game]);
alSourcef (Sources[game], AL_GAIN, 1.0f );
alSourcefv(Sources[game], AL_POSITION, SourcesPos[game]);
alSourcei (Sources[game], AL_LOOPING, AL_TRUE );

alDeleteBuffers(NUM_BUFFERS, Buffers);
alDeleteSources(NUM_SOURCES, Sources);

void cSound::SetListenerValues()
alListenerfv(AL_POSITION, ListenerPos);
alListenerfv(AL_VELOCITY, ListenerVel);
alListenerfv(AL_ORIENTATION, ListenerOri);

/////////////////////////// main.cpp ///////////////////////////

#include “sound.h”

int main()

cSound cS;

alutInit(NULL, 0);


//random code


//more random code
return 0;

3 Replies

Please log in or register to post a reply.

TheNut 179 Apr 09, 2011 at 22:53

What do you mean by “when I split the class”?

Despite your bizarre coding structure, a quick glance over your code shows your audio buffer initialization routine is fine. Check that OpenAL is correctly generating IDs for you. I would also advise against using ALUT because the API is deprecated, and I think there was even problems with their WAV loading method. It’s been a while so I can’t remember exact details, but I remember staying far away from it. You should be initializing OpenAL using the official API methods alcOpenDevice, alcCreateContext, and alcMakeContextCurrent.

Also, just a heads up, but if you’re going to be using velocity, make sure to set the doppler value (alDopplerFactor).

mmakrzem 101 Feb 06, 2012 at 11:15

I’m not sure what you mean by “split the class” either. Have you looked in the [OpenAL SDK install folder]\samples directory for some sample code to compare against?

Anonymous Mar 27, 2012 at 07:09

Can you clarify it more?