Manipulating Sounds
Looping a Sound
To cause a sound to loop (i.e., cause it to repeat once it is finished playing) do the following:
mySound.setLoop(True)
mySound.play()
To stop a sound from looping pass False in the set_loop()
function.
mySound.setLoop(False)
Sounds can also be looped for a certain number of times:
mySound.setLoopCount(n)
Where ‘n’ can be any positive integer. 0 will cause a sound to loop forever. 1 will cause a sound to play only once. >1 will cause a sound to loop that many times.
Note
Setting a sound’s loop count to 0 or >1 will automatically set a sound’s loop flag to true.
Notes on Looping Sounds Seamlessly
Looping a sound seamlessly should be as simple as loading the sound, then
calling set_loop()
and play()
.
However, occasionally Panda users have had difficulty getting sounds to loop
seamlessly. The problems have been traced to two(!) different causes:
Some MP3 encoders contain a bug where they add blank space at the end of the sound. This causes a skip during looping. Try using a wav instead.
Some have tried using Sound Intervals to create a loop. Unfortunately, sound intervals depend on Panda’s Thread to restart the sound, and if the CPU is is busy, there’s a skip. This is not a seamless method, in general. Use
set_loop()
instead.
So the easiest way to get a reliable looping sound is to use wav files, and to
use set_loop()
, not sound intervals. Of course, when it
comes time to ship your game, you can convert your sounds to mp3, but before you
do, test your mp3 encoder to see if it contains the blank-space bug.
Cueing Time
There are get_time()
, set_time()
and
length()
functions for sounds. These will respectively,
report the current time position, set the current time position and report the
length. All these are in seconds.
mySound.length()
will return the length of a sound file in seconds.
mySound.getTime()
will get the current time the ‘playback head’ of a sound is at in seconds.
mySound.setTime(n)
will set the ‘playhead head’ of a sound to n (where is seconds).
Caution
When using the default OpenAL back-end, setting the time will not take
effect immediately. You will need to call play()
to
restart the sound at the configured position.
Changing Playback Speed
To change a sound’s playback speed, use:
mySound.setPlayRate(n)
Where n
is any float.
Negative numbers will play a sound backwards. Passing the value 0 will pause the sound.
You can also get a sound’s play rate with:
mySound.getPlayRate()