• Advertisement
Sign in to follow this  

OpenAL: 3d sound not working

This topic is 3890 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have OpenAL playing sounds. However the 3d sounds are not working. ie if i set the distance of the source from the listener to 10000.0, it has the same volume with a distance of 0.0. Do I need to turn on the 3d sound feature somehow? Maybe define a distance where the sound fades to 0 volume? I have made a unit test. I have cut down the code as much as possible and am just making simple OpenAL calls. The following needs to be configured: std::string filename = "testsound.wav"; float closeDistance = 0.0; float farDistance = 100000.0; OpenAL_UnitTest() - initialise openal - play close distance - play far distance (sounds sound the same!!) - (no cleanup. not important for unit test) - (only call this unit test once) - (debug asserts to ensure openal state OK)
void OpenAL_UnitTest()
{
	std::string filename = "testsound.wav";
	float closeDistance = 0.0;
	float farDistance = 100000.0;

	// load device
	ALCdevice* Device = alcOpenDevice(NULL); // select the "preferred device"
	if (Device) {
		// load context
		ALCcontext* Context=alcCreateContext(Device, NULL);
		if (Context) {
			alcMakeContextCurrent(Context);
		}
	}
	//assert(alGetError() == AL_NO_ERROR);

	//alutInit(0, NULL); //initialise openAL

	// load buffer
	ALuint nBufferID;
	alGenBuffers(1, &nBufferID); // create 1 buffer
	assert(alGetError() == AL_NO_ERROR);

	// load wav file
	ALenum format;
	ALvoid*data;
	ALsizei size;
	ALsizei freq;
	char al_bool;
	alutLoadWAVFile((ALbyte*)filename.c_str(), &format, &data, &size, &freq, &al_bool);
	alBufferData(nBufferID, format, data, size, freq);
	assert(alGetError() == AL_NO_ERROR);
	alutUnloadWAV(format, data, size, freq);

	// set listener to (0,0,0)
	alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
	alListener3f(AL_VELOCITY, 0.0, 0.0, 0.0);

	// set listen orientation
	ALfloat pt[6];
	pt[0] = 0.0;
	pt[1] = 0.0;
	pt[2] = 1.0;
	pt[3] = 1.0;
	pt[4] = 0.0;
	pt[5] = 0.0;
	alListenerfv(AL_ORIENTATION, pt);
	assert(alGetError() == AL_NO_ERROR);

	// load source
	ALuint nSourceID;
	alGenSources(1, &nSourceID);
	assert(alGetError() == AL_NO_ERROR);

	// ////////////////////////////////////////////////////
	// play the close distance

	// configure the source
	alSourcef(nSourceID, AL_PITCH, 1.0f);
	alSourcef(nSourceID, AL_GAIN, 1.0f);
	alSource3f(nSourceID, AL_POSITION, closeDistance, closeDistance, closeDistance);
	alSource3f(nSourceID, AL_VELOCITY, 0.0, 0.0, 0.0); 
	alSourcei(nSourceID, AL_BUFFER, nBufferID);
	alSourcei(nSourceID, AL_LOOPING, AL_FALSE);
	assert(alGetError() == AL_NO_ERROR);
	// play the source
	alSourcePlay(nSourceID);
	assert(alGetError() == AL_NO_ERROR);

	// wait until source stopped
	ALint value;
	do {
		alGetSourcei(nSourceID, AL_SOURCE_STATE, &value);
	} while (value == AL_PLAYING);

	// ////////////////////////////////////////////////////
	// play the far distance

	// configure the source
	alSourcef(nSourceID, AL_PITCH, 1.0f);
	alSourcef(nSourceID, AL_GAIN, 1.0f);
	alSource3f(nSourceID, AL_POSITION, farDistance, farDistance, farDistance);
	alSource3f(nSourceID, AL_VELOCITY, 0.0, 0.0, 0.0); 
	alSourcei(nSourceID, AL_BUFFER, nBufferID);
	alSourcei(nSourceID, AL_LOOPING, AL_FALSE);
	assert(alGetError() == AL_NO_ERROR);
	// play the source
	alSourcePlay(nSourceID);
	assert(alGetError() == AL_NO_ERROR);
}

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Yes, It's stero.
Damn, that's a pain!

Is there a simple way to convert?
Sounds like I'll need more tools :S

Share this post


Link to post
Share on other sites
Hey, I know this is an old post, but I ran into the same problem. Figured out that it was the whole stereo/mono thing, but I'm wondering if anyone knows why that is. Or if its possible to get around it somehow, just so my program isn't limited. Maybe there's something about the stereo format that I don't know but it seems like this kinda restriction should not be there. Thanks...

Share this post


Link to post
Share on other sites
Just wanted to follow up on what I found out. You cannot use stereo because by definition these files are set to play in the left and right speaker, so OpenAL can't know where to set them in relation to each other... thanks, to anyone that tried to help.

Share this post


Link to post
Share on other sites
For what it's worth, here's a little more info: It seems like AL could merge the channels on the fly... I haven't confirmed that this is the case, but according to this thread other platforms will do 3D effects on stereo buffers. I did find that it is mentioned in the OpenAL 1.1 spec though. 5.3.4. Specifying Buffer Content says "Buffers containing audio data with more than one channel will be played without 3D spatialization features - these formats are normally used for background music."

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement