Plays up to NUM_SIMULTANEOUS_SOUNDS .wav/.ogg files simultaneously.
Allows pause/unpause on any of these sounds. (.ogg untested)
Theory of operation: The class uses the pygame module. This module
involves Sound instances and Channel instances. A Sound is created from a
.wav or .ogg file, and can play on one or more Channel instances.
All channels can simultaneously play, one Sound per channel. The
number of channels can be set via the module-global constant
NUM_SIMULTANEOUS_SOUNDS. Default is 8. Channels and Sounds are managed by
pygame.mixer.
This class uses parameter polymorphism to unify all these concepts as
much as possible. The only public methods are <code>play(), stop(),
pause(), unpause(),set_volume(), get_volume()</code>. Each of these
methods takes either the path to a sound file, a Sound instances, or a
Channel instance. The SoundPlayer takes care of creating Sound instances
by loading them from files, caching sounds, and tracking which Sounds are
playing on which Channel at any time. Callers of these methods need to
deal only with the sound file paths. These paths, when passed to, say,
the pause() method, will do the right thing; Sound instances are cached,
so they will not be loaded repeatedly.
Note that this wonderful collapsing of complex underlying pygame
concepts comes at a price in code ugliness. Since Python does not have
parameter based polymorphism, methods must figure out incoming parameter
types via duck typing methods: Treat the parameters as some type and see
whether an error occurs. Terrible, but the 'pythonic' way.
As background: The pygame API provides methods three entities: Mixer,
Channel, and Sound. The main methods are:
|
|
|
play(self,
whatToPlay,
blockTillDone=False,
volume=None)
Play a file, or an already loaded Sound instance. |
source code
|
|
|
stop(self,
whatToStop=None)
Stop either all currently playing sounds, or a particular sound. |
source code
|
|
|
pause(self,
whatToPause=None)
Pause either all currently playing sounds, or a particular sound. |
source code
|
|
|
unpause(self,
whatToUnPause=None)
Unpause either all currently playing sounds, or a particular sound. |
source code
|
|
|
|
|
|
boolean
|
|
|
|
|
|
|
|
|
|
(Sound, Channel)
|
playFile(self,
filename,
volume=None)
Private Method! Called by play(); Given a filename, load it into a
Sound instance, if it is not already cached. |
source code
|
|
|
stopFile(self,
filename)
Private Method! Called by stop(); Stops currently playing sound from
the given filename. |
source code
|
|
|
pauseFile(self,
filename)
Private Method! Called by pause(); Pauses currently playing sound
from the given filename. |
source code
|
|
Sound
|
|
{Sound | NoneType}
|
|
|
|
{[Channel] | NoneType}
|
|
|
|
|
cleanupSoundChannelBindings(self)
Runs through the sound-to-channel bindings and removes the entries of
channels that are done playing. |
source code
|
|
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__sizeof__ ,
__str__ ,
__subclasshook__
|