Panda3D Manual: Loading and Playing Sounds and Music
<<prev | top | next>> |
Note: setPlayRate is not implemented under fmod The implementation of the sound system in Panda3d allows for a division of audio into two categories - Sound Effects and Music. This division is only a convenience for programmers as Panda3d allows these two audio groups to be treated individually. These differences are explained on the next page. Loading sound is done through the Loader class. (Loading sounds through the 'base' builtin is deprecated and is only present for backwards compatibility.) In a normal Panda3D environment, loader is a builtin when you import DirectStart like this:
import direct.directbase.DirectStart
Load the sound, by supplying the path to the sound. Here's an example:
mySound1 = loader.loadSfx("SoundFile.wav")
These will return an object of the type AudioSound. It is necessary to put the extension in the sound filename. To play sounds you can do the following:
mySound.play()
To loop a sound do the following:
mySound.setLoop(True) mySound.play()
To unset the looping status pass False in the setLoop() function. Sounds can be looped for a certain number of times:
mySound.setLoopCount(n)
where 'n' can be any number. 0 will cause it to loop forever and any other number will loop it that many times. The volume can be set between 0 and 1 and will linearly scale between these.
mySound.setVolume(0.5)
You can change the balance of a sound. The range is between -1.0 to 1.0. Hard left is -1.0 and hard right is 1.0.
mySound.setBalance(-0.5)
To stop a sound:
mySound.stop()
There are getTime(), setTime() 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. A lot of these properties can be set in the constructor of the AudioSound object. A note here about Midi files in Panda3D. Midi support is not complete in any way. Midi sounds will not loop (There are ways to get around this. Check the Sound Interval documentation for this). As of this time, they are semi-implemented. ## Looping Sounds Seamlessly Looping a sound seamlessly should be as simple as loading the sound, then calling `setLoop` and `play`. However, in the past, panda users have had a great deal of difficulty getting sounds to loop seamlessly. The problems have been traced to four (!) 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. Panda uses fmod for its audio output. Midi looping is not implemented in fmod. Convert your midi to wav. Some have tried using Sound Intervals to create a loop. Unfortunately, sound intervals depend on the CPU to restart the sound, and if the CPU is busy, there's a skip. This is not a seamless method, in general. Use `setLoop` instead. There is a bug in Miles sound system, which requires a workaround in Panda3D. At one time, the workaround was causing problems with fmod, until we devised a new workaround. This bug no longer exists, you can ignore it. So the easiest way to get a reliable looping sound is to use wav files, and to use `setLoop`, 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.
<<prev | top | next>> |