changeset 629:660ef5793886 pyntnclick

Remove albow from sound
author Neil Muller <neil@dip.sun.ac.za>
date Sat, 11 Feb 2012 22:20:26 +0200
parents a3b82af01749
children a5f573002fb0
files gamelib/scenes/bridge.py gamelib/scenes/cryo.py pyntnclick/engine.py pyntnclick/sound.py
diffstat 4 files changed, 62 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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)
--- 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):
--- 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