comparison pyntnclick/sound.py @ 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 66c2e084b8b3
children 8b78fc07a862
comparison
equal deleted inserted replaced
628:a3b82af01749 629:660ef5793886
1 # sound management for pyntnclick 1 # sound management for pyntnclick
2 2
3 # This re-implements some of the albow.resource code to 3 # This re-implements some of the albow.resource code to
4 # a) work around an annoying bugs 4 # a) work around an annoying bugs
5 # b) add some missing functionality (disable_sound) 5 # b) add some missing functionality (disable_sound)
6
7 from random import randrange
8
6 9
7 import pygame 10 import pygame
8 11
9 try: 12 try:
10 from pygame.mixer import Sound as pygame_Sound 13 from pygame.mixer import Sound as pygame_Sound
15 pygame_import_error = e 18 pygame_import_error = e
16 pygame_Sound = None 19 pygame_Sound = None
17 music = None 20 music = None
18 21
19 from pyntnclick.resources import ResourceNotFound 22 from pyntnclick.resources import ResourceNotFound
23 from pyntnclick.engine import MUSIC_ENDED
20 24
21 import albow.music 25
26 class PlayList(object):
27 """Hold a playlist of music filenames"""
28
29 def __init__(self, pieces, random, repeat):
30 self._pieces = pieces
31 self._random = random
32 self._repeate = repeat
33
34 def get_next(self):
35 # Get the next piece
36 if self.pieces:
37 if self._random:
38 if not self._repeat or len(self._items) < 3:
39 i = randrange(0, len(self.items))
40 else:
41 # Ignore the last entry, since we possibly just played it
42 i = randrange(0, len(self.items) - 1)
43 else:
44 i = 0
45 result = self.items.pop(i)
46 if self._repeat:
47 self.items.push(result)
48 return result
49 return None
22 50
23 51
24 class DummySound(object): 52 class DummySound(object):
25 """A dummy sound object. 53 """A dummy sound object.
26 54
55 83
56 def __init__(self, resource_finder): 84 def __init__(self, resource_finder):
57 self.sound_enabled = False 85 self.sound_enabled = False
58 self.sound_cache = {} 86 self.sound_cache = {}
59 self._resource_finder = resource_finder 87 self._resource_finder = resource_finder
88 self._current_playlist = None
60 89
61 def enable_sound(self, constants): 90 def enable_sound(self, constants):
62 """Attempt to initialise the sound system""" 91 """Attempt to initialise the sound system"""
63 if pygame_Sound is None: 92 if pygame_Sound is None:
64 self.disable_sound(pygame_import_error) 93 self.disable_sound(pygame_import_error)
67 pygame.mixer.init(constants.snd_freq, 96 pygame.mixer.init(constants.snd_freq,
68 constants.snd_bitsize, 97 constants.snd_bitsize,
69 constants.snd_channels, 98 constants.snd_channels,
70 constants.snd_buffer) 99 constants.snd_buffer)
71 self.sound_enabled = True 100 self.sound_enabled = True
101 music.set_endevent(MUSIC_ENDED)
72 except pygame.error, exc: 102 except pygame.error, exc:
73 self.disable_sound(exc) 103 self.disable_sound(exc)
74 104
75 def disable_sound(self, exc=None): 105 def disable_sound(self, exc=None):
76 """Disable the sound system""" 106 """Disable the sound system"""
100 sound = DummySound() 130 sound = DummySound()
101 self.sound_cache[path] = sound 131 self.sound_cache[path] = sound
102 return sound 132 return sound
103 133
104 def get_playlist(self, pieces, random=False, repeat=False): 134 def get_playlist(self, pieces, random=False, repeat=False):
105 return albow.music.PlayList(pieces, random, repeat) 135 return PlayList(pieces, random, repeat)
106 136
107 def get_music(self, name, prefix): 137 def get_music(self, name):
108 if self.sound_enabled: 138 if self.sound_enabled:
109 return albow.music.get_music(name, prefix=prefix) 139 music_file = self._resource_finder.get_resource_path("sounds",
140 name)
141 return music_file
142 return None
143
144 def music_ended(self):
145 if self._current_playlist:
146 # Try start the next tune
147 self.start_next_music()
110 148
111 def change_playlist(self, new_playlist): 149 def change_playlist(self, new_playlist):
112 if self.sound_enabled: 150 if self.sound_enabled:
113 albow.music.change_playlist(new_playlist) 151 music.stop_music()
152 self._current_playlist = new_playlist
153 self.start_next_music()
154
155 def start_next_music(self):
156 if self._current_playlist:
157 tune = self._current_playlist.get_next()
158 if tune:
159 music.load(tune)
160 music.play()
114 161
115 def get_current_playlist(self): 162 def get_current_playlist(self):
116 if self.sound_enabled and albow.music.music_enabled and \ 163 return self._current_playlist
117 albow.music.current_playlist:
118 return albow.music.current_playlist
119
120
121 def start_next_music():
122 """Start playing the next item from the current playlist immediately."""
123 if albow.music.music_enabled and albow.music.current_playlist:
124 next_music = albow.music.current_playlist.next()
125 if next_music:
126 #print "albow.music: loading", repr(next_music)
127 music.load(next_music)
128 music.play()
129 albow.music.next_change_delay = albow.music.change_delay
130 albow.music.current_music = next_music
131
132
133 # Monkey patch
134 albow.music.start_next_music = start_next_music