openAL sound class problem

84bbadd59291a18598d55da7b416a821
0
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
{
public:
cSound();
\~cSound();
void SetListenerValues();

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

unsigned int Buffers[NUM_BUFFERS];
unsigned int Sources[NUM_SOURCES];
};
#endif

/////////////////////////// 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 };

cSound::cSound()
{
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 );
}

cSound::\~cSound()
{
alDeleteBuffers(NUM_BUFFERS, Buffers);
alDeleteSources(NUM_SOURCES, Sources);
alutExit();
}

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);
alGetError();

cS.SetListenerValues();

while(true)
{
//random code

alSourcePlay(cS.Sources[game]);

//more random code
}
system(“pause”);
return 0;
}

3 Replies

Please log in or register to post a reply.

6837d514b487de395be51432d9cdd078
0
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).

500367065665a05a847242a39a0bc69e
0
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?

00000000000000000000000000000000
0
Anonymous Mar 27, 2012 at 07:09

Can you clarify it more?