Ensure color_pos is always -1 when the lights go out, so behaviour is always consistent
[tabakrolletjie.git] / tabakrolletjie / sound.py
1
2 from pygame import mixer
3
4 from .constants import (FREQ, BITSIZE, CHANNELS, BUFFER, DEFAULT_VOLUME,
5                         NO_SOUND)
6 from .loader import loader
7
8
9 class SoundManager(object):
10
11     def init(self):
12         """This is not in __init__, because we want to delay until after
13            other pygame initialistion"""
14         self._init = False
15         if not NO_SOUND:
16             mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
17             silence = loader.load_sound("silence.ogg")
18             if silence.get_length() < 1:
19                 raise RuntimeError("Sound load error - silence.ogg too short")
20             try:
21                 self.play_sound("silence.ogg")
22                 self._init = True
23             except Exception, err:
24                 print "Failed to enable sound: %r" % err
25
26     def play_sound(self, name, volume=DEFAULT_VOLUME):
27         if self._init:
28             sound = loader.load_sound(name)
29             if sound is not None:
30                 sound.set_volume(volume)
31                 sound.play()
32
33     def play_music(self, name, volume=DEFAULT_VOLUME):
34         pass
35
36     def stop(self):
37         if self._init:
38             mixer.fadeout(1000)
39             mixer.music.stop()
40
41
42 sound = SoundManager()