+
+from pygame import mixer
+
+from .constants import FREQ, BITSIZE, CHANNELS, BUFFER, DEFAULT_VOLUME, NO_SOUND
+from .loader import loader
+
+class SoundManager(object):
+
+ def init(self):
+ """This is not in __init__, because we want to delay until after
+ other pygame initialistion"""
+ self._init = False
+ if not NO_SOUND:
+ mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
+ silence = loader.load_sound("silence.ogg")
+ if silence.get_length() < 1:
+ raise RuntimeError("Sound load error - silence.ogg too short")
+ try:
+ self.play_sound("silence.ogg")
+ self._init = True
+ except Exception, err:
+ print "Failed to enable sound: %r" % err
+
+ def play_sound(self, name, volume=DEFAULT_VOLUME):
+ if self._init:
+ sound = loader.load_sound(name)
+ if sound is not None:
+ sound.set_volume(volume)
+ sound.play()
+
+ def play_music(self, name, volume=DEFAULT_VOLUME):
+ pass
+
+ def stop(self):
+ if self._init:
+ mixer.fadeout(1000)
+ mixer.music.stop()
+
+
+sound = SoundManager()