5 from pygame import mixer
7 from naja.options import options
8 from naja.resources import resources
9 from naja.constants import (
10 FREQ, BITSIZE, CHANNELS, BUFFER, DEFAULT_SOUND_VOLUME,
14 class DummySound(object):
18 def play_sound(self, name, volume=DEFAULT_SOUND_VOLUME, foreground=False):
21 def play_music(self, name, volume=DEFAULT_MUSIC_VOLUME):
24 def pause_music(self):
27 def resume_music(self):
34 class PygameSound(object):
39 mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
40 silence = self.load_sound("silence.ogg")
41 if silence.get_length() < 1:
42 raise RuntimeError("Sound load error - silence.ogg too short")
43 self.play_sound("silence.ogg")
45 def load_sound(self, name):
46 track_name = resources.get_resource_path("sounds", name)
47 sound = self._sounds.get(track_name)
49 sound = self._sounds[track_name] = mixer.Sound(track_name)
52 def play_sound(self, name, volume=DEFAULT_SOUND_VOLUME, foreground=False):
53 sound = self.load_sound(name)
55 sound.set_volume(volume)
58 time.sleep(sound.get_length())
60 def play_music(self, name, volume=DEFAULT_MUSIC_VOLUME):
63 track_name = resources.get_resource_path("music", name)
64 mixer.music.load(track_name)
65 mixer.music.set_volume(volume)
66 mixer.music.play(-1) # loop sound
68 def pause_music(self):
71 def unpause_music(self):
79 class SoundProxy(object):
81 self._sound = DummySound()
84 """Attempt to initialize the sound system."""
87 pyg_sound = PygameSound()
89 self._sound = pyg_sound
90 except Exception, err:
91 print "Failed to enable sound: %r" % (err,)
93 def __getattr__(self, name):
94 return getattr(self._sound, name)