Game Development Reference
In-Depth Information
commands to the sound driver to play the sound. If it has been lost, it will call the
VRestore() method, and everything will be back to normal. Hopefully, anyway.
The next four methods can control the play status on an individual sound effect.
VPlay() gets a volume from 0
100 and a Boolean looping , which you set to
true if you want the sound to loop. VPause() , VStop() , VResume() , and
VPause() let you control the progress of a sound.
The volume methods do exactly what you
-
d think they do: set and retrieve the cur-
rent volume of the sound. The method that sets the volume will do so instantly, or
nearly so. If you want a gradual fade, on the other hand, you ' ll have to use something
a little higher level. Luckily, we
'
ll do exactly that later on in this chapter.
The last method, VGetProgress() , returns a floating-point number between 0.0f
and 1.0f and is meant to track the progress of a sound as it is being played. If the
sound effect is one-fourth of the way through playing, this method will return 0.25f.
'
All Things Go from 0.0 to 1.0
Measuring things like sound effects in terms of a coefficient ranging from 0.0
to 1.0 instead of a number of milliseconds is a nice trick. This abstraction gives
you some flexibility if the actual length of the sound effect changes, especially
if it is timed with animations, or animations are tied to sound, which is very
frequently the case. If either the sound changes or the animation changes, it is
easy to track one versus the other.
With the interface defined, we can write a little platform-agnostic code and create the
AudioBuffer class. The real meat of this class is the management of the smart
pointer to a SoundResource . This guarantees that the memory for your sound
effect can
'
t go out of scope while the sound effect is being played.
class AudioBuffer : public IAudioBuffer
{
public:
virtual shared_ptr<ResHandle> VGetResource() { return m_Resource; }
virtual bool VIsLooping() const { return m_isLooping; }
virtual int VGetVolume() const { return m_Volume; }
protected:
AudioBuffer(shared_ptr<ResHandle >resource)
{
m_Resource = resource;
m_isPaused = false;
Search WWH ::




Custom Search