3 from pygame import mixer
5 from naja.options import options
6 from naja.resources import resources
7 from naja.constants import (
8 FREQ, BITSIZE, CHANNELS, BUFFER, DEFAULT_SOUND_VOLUME,
12 class DummySound(object):
16 def play_sound(self, name, volume=DEFAULT_SOUND_VOLUME):
19 def play_music(self, name, volume=DEFAULT_MUSIC_VOLUME):
22 def pause_music(self):
25 def resume_music(self):
32 class PygameSound(object):
37 mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
38 silence = self.load_sound("silence.ogg")
39 if silence.get_length() < 1:
40 raise RuntimeError("Sound load error - silence.ogg too short")
41 self.play_sound("silence.ogg")
43 def load_sound(self, name):
44 track_name = resources.get_resource_path("sounds", name)
45 sound = self._sounds.get(track_name)
47 sound = self._sounds[track_name] = mixer.Sound(track_name)
50 def play_sound(self, name, volume=DEFAULT_SOUND_VOLUME):
51 sound = self.load_sound(name)
53 sound.set_volume(volume)
56 def play_music(self, name, volume=DEFAULT_MUSIC_VOLUME):
59 track_name = resources.get_resource_path("music", name)
60 mixer.music.load(track_name)
61 mixer.music.set_volume(volume)
62 mixer.music.play(-1) # loop sound
64 def pause_music(self):
67 def unpause_music(self):
75 class SoundProxy(object):
77 self._sound = DummySound()
80 """Attempt to initialize the sound system."""
83 pyg_sound = PygameSound()
85 self._sound = pyg_sound
86 except Exception, err:
87 print "Failed to enable sound: %r" % (err,)
89 def __getattr__(self, name):
90 return getattr(self._sound, name)