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):
16 self.playing_music = False
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):
37 self.playing_music = False
40 mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
41 silence = self.load_sound("silence.ogg")
42 if silence.get_length() < 1:
43 raise RuntimeError("Sound load error - silence.ogg too short")
44 self.play_sound("silence.ogg")
46 def load_sound(self, name):
47 track_name = resources.get_resource_path("sounds", name)
48 sound = self._sounds.get(track_name)
50 sound = self._sounds[track_name] = mixer.Sound(track_name)
53 def play_sound(self, name, volume=DEFAULT_SOUND_VOLUME, foreground=False):
54 sound = self.load_sound(name)
56 sound.set_volume(volume)
59 time.sleep(sound.get_length())
61 def play_music(self, name, volume=DEFAULT_MUSIC_VOLUME):
64 track_name = resources.get_resource_path("music", name)
65 mixer.music.load(track_name)
66 mixer.music.set_volume(volume)
67 mixer.music.play(-1) # loop sound
68 self.playing_music = True
70 def pause_music(self):
72 self.playing_music = False
74 def unpause_music(self):
76 self.playing_music = True
81 self.playing_music = False
84 class SoundProxy(object):
86 self._sound = DummySound()
90 """Attempt to initialize the sound system."""
93 pyg_sound = PygameSound()
95 self._sound = pyg_sound
96 except Exception, err:
97 print "Failed to enable sound: %r" % (err,)
99 def __getattr__(self, name):
100 return getattr(self._sound, name)