Mercurial > mamba
changeset 30:cccf1675731c
Add sound framework
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sun, 11 Sep 2011 14:20:36 +0200 |
parents | b48c47af7801 |
children | 67218082cdbb |
files | mamba/__main__.py mamba/sound.py |
diffstat | 2 files changed, 97 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/mamba/__main__.py Sun Sep 11 14:20:20 2011 +0200 +++ b/mamba/__main__.py Sun Sep 11 14:20:36 2011 +0200 @@ -8,6 +8,7 @@ from mamba.constants import SCREEN, DEFAULTS from mamba.engine import Engine +from mamba.sound import SoundSystem from mamba.habitats.mainmenu import MainMenu from mamba.habitats.level import LevelHabitat @@ -35,9 +36,9 @@ def main(): """Launch the currently unnamed mamab game""" - options = DEFAULTS.copy() parse_args(sys.argv, options) + sound = SoundSystem(options['sound']) pygame.display.init() pygame.font.init() # TODO: Sound initialisation
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mamba/sound.py Sun Sep 11 14:20:36 2011 +0200 @@ -0,0 +1,95 @@ +"""Basic sound frame-work""" + +from pygame import mixer +import pygame + +from mamba import data +from mamba.constants import FREQ, BITSIZE, CHANNELS, BUFFER + +# Global constant for holding the initialised sound system +_SOUND_SYSTEM = None + + +# Utility functions, used elsewhere +def load_sound(key, track_name, volume=None): + if _SOUND_SYSTEM is not None: + _SOUND_SYSTEM.load_sound(key, track_name, volume) + + +def play_sound(key): + if _SOUND_SYSTEM is not None: + _SOUND_SYSTEM.play_sound(key) + + +class SoundSystem(object): + + def __init__(self, sound_on): + global _SOUND_SYSTEM + self.sound_enabled = False + if _SOUND_SYSTEM is not None: + # We've been initialised, so skip out + return + if sound_on: + try: + mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER) + test_sound = mixer.Sound(data.filepath('sounds/silence.ogg')) + test_sound.play() + self.sound_enabled = True + except pygame.error: + print 'Unable to enable sound. Continuing without sound' + self.sound_enabled = False + self._sounds = {} + _SOUND_SYSTEM = self + + def play_background_music(self, track_name, volume=1.0): + """Play a looping track using pygame's music support""" + if self.sound_enabled: + try: + mixer.music.load(data.filepath(track_name)) + mixer.music.play(-1) # Loop sound + mixer.music.set_volume(volume) + except: + print 'Unable to load track %s. Skipping' % track_name + + def stop_music(self): + """Stop music""" + if self.sound_enabled: + mixer.music.stop() + + def load_sound(self, key, track_name, volume=None): + """Load sounds so we can play them later""" + if key in self._sounds: + # Ignore multiple keys + return + if not self.sound_enabled: + self._sounds[key] = None + else: + try: + self._sounds[key] = pygame.mixer.Sound( + data.filepath(track_name)) + if volume is not None: + self._sounds[key].set_volume(volume) + except: + print 'Unable to load sound %s. Skipping' % track_name + self._sounds[key] = None + + def play_sound(self, key): + """Play the sound""" + sound = self._sounds.get(key, None) + if sound: + sound.play() + + def stop_sounds(self): + """Stop any playing sounds""" + if self.sound_enabled: + mixer.stop() + + def pause(self): + """Pause music""" + if self.sound_enabled: + mixer.music.pause() + + def resume(self): + """Resume paused music""" + if self.sound_enabled: + mixer.music.unpause()