# HG changeset patch # User Neil Muller # Date 1328991626 -7200 # Node ID 660ef5793886a4c3d2d2f53d67ec2263f519b3b6 # Parent a3b82af0174901113f45d3e02065caf05331c18d Remove albow from sound diff -r a3b82af01749 -r 660ef5793886 gamelib/scenes/bridge.py --- a/gamelib/scenes/bridge.py Sat Feb 11 22:16:43 2012 +0200 +++ b/gamelib/scenes/bridge.py Sat Feb 11 22:20:26 2012 +0200 @@ -73,7 +73,7 @@ self.add_thing(self.doctor) def enter(self): - pieces = [self.sound.get_music(x, prefix='sounds') for x in self.MUSIC] + pieces = [self.sound.get_music(x) for x in self.MUSIC] self.background_playlist = self.sound.get_playlist(pieces, random=True, repeat=True) self.sound.change_playlist(self.background_playlist) diff -r a3b82af01749 -r 660ef5793886 gamelib/scenes/cryo.py --- a/gamelib/scenes/cryo.py Sat Feb 11 22:16:43 2012 +0200 +++ b/gamelib/scenes/cryo.py Sat Feb 11 22:20:26 2012 +0200 @@ -125,7 +125,7 @@ def enter(self): # Setup music - pieces = [self.sound.get_music(x, prefix='sounds') for x in self.MUSIC] + pieces = [self.sound.get_music(x) for x in self.MUSIC] background_playlist = self.sound.get_playlist(pieces, random=True, repeat=True) self.sound.change_playlist(background_playlist) diff -r a3b82af01749 -r 660ef5793886 pyntnclick/engine.py --- a/pyntnclick/engine.py Sat Feb 11 22:16:43 2012 +0200 +++ b/pyntnclick/engine.py Sat Feb 11 22:20:26 2012 +0200 @@ -6,6 +6,11 @@ import pygame.time from pygame.locals import QUIT, USEREVENT +# We can't do this via our usual UserEvent trickey +# as it gets generated by pygame.music, which only +# takes an event type +MUSIC_ENDED = USEREVENT + 1 + class Engine(object): def __init__(self, game_description): @@ -34,6 +39,8 @@ for ev in events: if ev.type == QUIT: return + elif ev.type == MUSIC_ENDED: + self._game_description.sound.music_ended() elif ScreenChangeEvent.matches(ev): self.set_screen(ev.screen_name) elif ScreenEvent.matches(ev): diff -r a3b82af01749 -r 660ef5793886 pyntnclick/sound.py --- a/pyntnclick/sound.py Sat Feb 11 22:16:43 2012 +0200 +++ b/pyntnclick/sound.py Sat Feb 11 22:20:26 2012 +0200 @@ -4,6 +4,9 @@ # a) work around an annoying bugs # b) add some missing functionality (disable_sound) +from random import randrange + + import pygame try: @@ -17,8 +20,33 @@ music = None from pyntnclick.resources import ResourceNotFound +from pyntnclick.engine import MUSIC_ENDED -import albow.music + +class PlayList(object): + """Hold a playlist of music filenames""" + + def __init__(self, pieces, random, repeat): + self._pieces = pieces + self._random = random + self._repeate = repeat + + def get_next(self): + # Get the next piece + if self.pieces: + if self._random: + if not self._repeat or len(self._items) < 3: + i = randrange(0, len(self.items)) + else: + # Ignore the last entry, since we possibly just played it + i = randrange(0, len(self.items) - 1) + else: + i = 0 + result = self.items.pop(i) + if self._repeat: + self.items.push(result) + return result + return None class DummySound(object): @@ -57,6 +85,7 @@ self.sound_enabled = False self.sound_cache = {} self._resource_finder = resource_finder + self._current_playlist = None def enable_sound(self, constants): """Attempt to initialise the sound system""" @@ -69,6 +98,7 @@ constants.snd_channels, constants.snd_buffer) self.sound_enabled = True + music.set_endevent(MUSIC_ENDED) except pygame.error, exc: self.disable_sound(exc) @@ -102,33 +132,32 @@ return sound def get_playlist(self, pieces, random=False, repeat=False): - return albow.music.PlayList(pieces, random, repeat) + return PlayList(pieces, random, repeat) - def get_music(self, name, prefix): + def get_music(self, name): if self.sound_enabled: - return albow.music.get_music(name, prefix=prefix) + music_file = self._resource_finder.get_resource_path("sounds", + name) + return music_file + return None + + def music_ended(self): + if self._current_playlist: + # Try start the next tune + self.start_next_music() def change_playlist(self, new_playlist): if self.sound_enabled: - albow.music.change_playlist(new_playlist) + music.stop_music() + self._current_playlist = new_playlist + self.start_next_music() + + def start_next_music(self): + if self._current_playlist: + tune = self._current_playlist.get_next() + if tune: + music.load(tune) + music.play() def get_current_playlist(self): - if self.sound_enabled and albow.music.music_enabled and \ - albow.music.current_playlist: - return albow.music.current_playlist - - -def start_next_music(): - """Start playing the next item from the current playlist immediately.""" - if albow.music.music_enabled and albow.music.current_playlist: - next_music = albow.music.current_playlist.next() - if next_music: - #print "albow.music: loading", repr(next_music) - music.load(next_music) - music.play() - albow.music.next_change_delay = albow.music.change_delay - albow.music.current_music = next_music - - -# Monkey patch -albow.music.start_next_music = start_next_music + return self._current_playlist