Game Development Reference
In-Depth Information
public Sound(int channel)
{
Channel = channel;
}
}
Now that the sound wrapper class is properly defined a PlaySound method can
be added. A sound has to be played on a channel. There are only a limited
number of channels so it is possible that all the channels are full. In this case the
sound won't be played. This is a very simple heuristic, but for games with hun-
dreds of sounds going off at once, it may be better to give each sound a priority
and then the sounds of high priority can take over the channels of those sounds
with a lower priority. The priority value would probably be linked to the sound's
volume and distance from the player.
A method needs to be written that will determine if a given channel is free or not.
An easy way to determine if a channel is free is to ask OpenAL if it is currently
playing anything on that channel. If nothing is being played on the channel then
it is free. This new IsChannelPlaying method should be added to the
SoundManager class.
private bool IsChannelPlaying(int channel)
{
int value = 0;
Al.alGetSourcei(channel, Al.AL_SOURCE_STATE, out value);
return (value == Al.AL_PLAYING);
}
The OpenAL function alGetSourcei queries a particular channel about some
property. The property is determined by the second argument; in this case we're
asking what the current state of the source is. The size and speed of the sound on
the channel can also be queried in this way. The IsChannelPlaying function
checks to see if the channel's current state is set to playing; if so it returns true,
otherwise false.
With the IsChannelPlaying function defined we can now use it to build up
another function that will return a free channel. This new function will be called
GetNextFreeChannel and will iterate through the list of channels that the
function DicoverSoundChannels made in the constructor. If it can't find a
sound channel free it will return minus one as an error flag.
 
 
Search WWH ::




Custom Search