# OpenAL Questions

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

## Recommended Posts

Hi, Glad that Gamedev is back online! Well, my first question is, is their a OpenAL (not "GL") newsgroup/forum somewhere? Or maybe there is a better place to ask technical questions about sound libraries on Gamedev? What I wanted to ask, is this: 1- OpenAL has a couple of settings to adjust the volume so that I can make sound sources very loud. However, I don't hear any difference. When I place the listener ~10 units (what are those units anyway?) from a source, I can barely hear any sound. So I tried to spice it up:
  AlSourcef ( source, AL_GAIN, 1.0 );
AlSourcei ( source, AL_REFERENCE_DISTANCE, 50 );
AlSourcei ( source, AL_MAX_DISTANCE, 100 );

But wether the distance is default or a billion for that matter, I still can't hear my noise when being 10 units away. Anything I forget? 2- Is their a maximum count of sources? I was playing around with a gun. For every shot, an extra source was created and binded to a buffer. Not very smart of course, but anyway, after ~30 shots, I the sound stopped... 3- To come back to question 2, what's the best way to deal with alot of sounds? For example, I have a machinegun that sprays bullets like mad. When a bullet collides somewhere, I want an impact sound on that point. Do I need to have a source for each bullet? greetings, Rick

##### Share on other sites
Post the code where you are:

1) changing the listener position
2) changing the source position

As far as many sounds goes, yes there is an upper limit, no i don't know what that is. Generally you create an abstraction layer between the game code and the audio code. Basically you create a mixer.

The mixer gets told when sounds events happen. Then it manages it's list of "these should be playing now" and picks the top X number of sounds (sorting by importance, etc) and plays only those. For instance particularly with unit voice callouts you only ever have one playing at a time or the audio will sound crap with a lot of voices overlaid. The mixer handles deciding which voice audio to play, again giving higher priority to certain sounds.

-me

##### Share on other sites
Well, the code is simple. I just played around in a OpenAL demo that playes 1 single looped sound. Listener and source are both at (0,0,0). But I changed the source position to try it out. It's Delphi code by the way.
< variables >  buffer     : TALuint;  source     : TALuint;  sourcepos  : array [0..2] of TALfloat= ( 0.0, 0.0, 0.0 );  sourcevel  : array [0..2] of TALfloat= ( 0.0, 0.0, 0.0 );  listenerpos: array [0..2] of TALfloat= ( 0.0, 0.0, 5.0); //Changed the Z coordinate of the listener, or source to test  listenervel: array [0..2] of TALfloat= ( 0.0, 0.0, 0.0);  listenerori: array [0..5] of TALfloat= ( 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);<init procedure>  AlutInit(nil,argv);  // Load buffer  AlGenBuffers(1, @buffer);  AlutLoadWavFile('ding.wav', format, data, size, freq, loop);  AlBufferData(buffer, format, data, size, freq);  AlutUnloadWav(format, data, size, freq);  // Create 1 source  AlGenSources(1, @source);  AlSourcei ( source, AL_BUFFER, buffer);  AlSourcef ( source, AL_PITCH, 1.0 );  AlSourcef ( source, AL_GAIN , 1.0 );  AlSourcefv( source, AL_POSITION, @sourcepos);  AlSourcefv( source, AL_VELOCITY, @sourcevel);  AlSourcei ( source, AL_LOOPING, AL_TRUE);  // Create the listener  AlListenerfv ( AL_POSITION, @listenerpos);  AlListenerfv ( AL_VELOCITY, @listenervel);  AlListenerfv ( AL_ORIENTATION, @listenerori);

I added a "trackbar" to change the distances. The bar can be shifted from 0 to 500. When the bar-position has changed, this event is called:
begin  AlSourcei ( source, AL_REFERENCE_DISTANCE, trackbar1.Position);  AlSourcei ( source, AL_MAX_DISTANCE      , trackbar1.Position*2);

As for the mixer idea, I should implement such thing indeed. But in my case all those sounds weren't played at the same time. I clicked the mouse to trigger a "shot" sound. I repeated that ~30 times or something, and then it was quiet. But maybe those sources are still active? Does OpenAL automatically stop a source when the sound is finished (they were not looped)? If not, how to check if a sound effect has been finished?

Thanks for helping,
Rick

##### Share on other sites
OpenAL doesn't automatically release sources, even if a source has finished playing and is not to be looped. To check if a source is finished playing and to remove it:

     ALint IsPlaying;     alGetSourcei(SourceID, AL_SOURCE_STATE, &IsPlaying);     if (IsPlaying != AL_PLAYING)     {          alDeleteSources(1, SourceID);     }

Note that this will remove sources that were stopped or paused by you, since that is considered not AL_PLAYING.

##### Share on other sites
Ok, that's probably the reason that the sound suddenly stops after shooting my gun x times. The way you described would be a check to see if it's finish, but that means I have to poll every sound, every time. But I guess I can also get the length of a sound, and stop it after x milliseconds. In that case I wouldn't have to check it. But thanks for the tips!

That leaves me with the other problem, the volume thingy. The code I posted is all the OpenAL related code, except for buttons that start/pause/stop playing the (looped) sound. Maybe the volume can't be changed while a sound is playing? But I also tried to stop the sound, change the volume, and play it again. No effect either though. After ~10 metres distance, the sound is gone, no matter how loud its set.

Thanks for helping,
Rick

##### Share on other sites
You shouldn't think of an openAL source as an individual sound effect, but as a "speaker" or channel through which you can play any number of sounds.

What I often do is I create N sources at the beginning of the program, then whenever I need to play a sound I look for a source that's currently not playing anything or is playing a lower priority sound effect. Then I just tell that source to play the effect. This way I can set the max number of simultaneous sound channels to something like 16 or 32, never worry about running out of sources, and do clever things like sound effect priorities or cutting off a sound when you re-trigger the same sound effect (e.g. when shooting a machinegun you can use just one channel instead of opening a new one for each shot).

##### Share on other sites
Yes, that was my plan too. But becuase of a stupid bug, I created a new source everytime I played a sound effect. Which made the sound suddenly stop, so I wondered if there was a max number of sources.

I think that mystery is solved, but I still can't make my sources any louder. My game is going to be pretty quiet when the listener can only catch sounds withing 8 openAL "units" (I just call the metres, since 1 OpenGL unit is a metre in my game as well). The distance settings don't seem to have effect.

Greetings,
Rick

##### Share on other sites
You might need to adjust the rolloff factor. I can't remember what the exact call is sorry.

As to the other problem, I've found it a right pain getting a good prioritising system in place. Things like looping sounds and streaming music complicate things, but I did eventually manage to get it all working in the one system so I didn't have to manage 2 seperate source pools and waste half of them half the time. I didn't bother keeping track of the position of looping sounds though, just started them from the start if they became important again. Not to mention the api itself is fairly touchy. As far as I can tell, even if you delete sources properly you can never use them again, and run out after creating 30. (EDIT: In retrospect that could have been my fault.. not sure, it was a while back.) And you can't delete a buffer which is pointed to by a source, even if the source is stopped. You have to set it's buffer to 0 first. And don't even think about passing the api INF or NAN for a position or velocity (bug of mine). It'll kindly reset your pc for you in 0 seconds flat (Triple fault I assume).

##### Share on other sites
I shall look into that. But maybe I already solved it... if you look at the code, you see that I use "AlSourcei" to set those properties. Now I tried "AlSourcef", and damn, it seems to work. Oops.

>It'll kindly reset your pc for you in 0 seconds flat (Triple fault I assume).
Holy sh*t! Thanks for those warnings. These kind of things are good to know, especially because I haven't seen much demo's or documentation on OpenAL yet.

greetings,
Rick

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633672
• Total Posts
3013273
×