Fundamentals of Audio and Video Programming for Games (Pro-Developer)
It is simple to play ambient sounds. The only essential new method needed is the following.
bool playAmbientSound(soundBufferRef* sID, bool fLooping, int percentVolume, bool fFreeOnCompletion = false);
So the complete code needed to play background rain is the following.
soundBufferRef sIDRain; if (Concertina.getSoundID(ID_rain,&sIDRain)) Concertina.playAmbientSound(&sIDRain,true,100);
The first statement declares the soundBufferRef structure, the second fills in that structure, and the third plays it looping at 100 percent of its recorded volume.
Note the optional fFreeOnCompletion flag at the end of the playAmbientSound call. If you leave this at false, then the sound buffer will be attached until the end of the program, or a call to clearSoundBuffer , whichever comes sooner. However, if you set it to true, then the sound buffer will be returned to the pool as soon as the sound stops playing. As most ambient sounds are looping, this will occur when you make a specific call to stop the sound.
It is also possible to change the volume of any sound, ambient or 3-D, using the following method.
bool setBufferVolume(soundBufferRef* sID, float distanceRatio);
The distanceRatio is the relative distance of the listener from the sound source, compared with the distance of the audio recorder from the sound source when the wave file was recorded. So a value of 1.0 will give the maximum volume, values less than 1.0 are not allowed, and a value of 3.0 will give a volume that sounds as if the object is three times further away than when the wave file was recorded.
When a sound buffer is playing, this call can be made repeatedly to gradually (or dramatically) increase or decrease the volume.