Mercurial > boomslang
comparison pyntnclick/sound.py @ 854:79b5c1be9a5e default tip
Remove pyntnclick, it's its own library, now
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 21 Jun 2014 22:06:09 +0200 |
parents | f95830b58336 |
children |
comparison
equal
deleted
inserted
replaced
852:f95830b58336 | 854:79b5c1be9a5e |
---|---|
1 # sound management for pyntnclick | |
2 | |
3 from random import randrange | |
4 | |
5 | |
6 import pygame | |
7 | |
8 try: | |
9 from pygame.mixer import Sound as pygame_Sound | |
10 from pygame.mixer import music | |
11 pygame_import_error = None | |
12 except ImportError, e: | |
13 # Save error, so we don't crash and can do the right thing later | |
14 pygame_import_error = e | |
15 pygame_Sound = None | |
16 music = None | |
17 | |
18 from pyntnclick.resources import ResourceNotFound | |
19 from pyntnclick.engine import MUSIC_ENDED | |
20 | |
21 | |
22 class PlayList(object): | |
23 """Hold a playlist of music filenames""" | |
24 | |
25 def __init__(self, pieces, random, repeat): | |
26 self._pieces = pieces | |
27 self._random = random | |
28 self._repeat = repeat | |
29 | |
30 def get_next(self): | |
31 # Get the next piece | |
32 if self._pieces: | |
33 if self._random: | |
34 if not self._repeat or len(self._pieces) < 3: | |
35 i = randrange(0, len(self._pieces)) | |
36 else: | |
37 # Ignore the last entry, since we possibly just played it | |
38 i = randrange(0, len(self._pieces) - 1) | |
39 else: | |
40 i = 0 | |
41 result = self._pieces.pop(i) | |
42 if self._repeat: | |
43 self._pieces.append(result) | |
44 return result | |
45 return None | |
46 | |
47 | |
48 class DummySound(object): | |
49 """A dummy sound object. | |
50 | |
51 This is a placeholder object with the same API as | |
52 pygame.mixer.Sound which does nothing. Used when | |
53 sounds are disabled so scense don't need to worry | |
54 about the details. | |
55 | |
56 Inpsired by the same idea in Albow (by Greg Ewing)""" | |
57 | |
58 def play(self, *args): | |
59 pass | |
60 | |
61 def stop(self): | |
62 pass | |
63 | |
64 def get_length(self): | |
65 return 0.0 | |
66 | |
67 def get_num_channel(self): | |
68 return 0 | |
69 | |
70 def get_volume(self): | |
71 return 0.0 | |
72 | |
73 def fadeout(self, *args): | |
74 pass | |
75 | |
76 | |
77 class Sound(object): | |
78 """Global sound management and similiar useful things""" | |
79 | |
80 def __init__(self, resource_finder): | |
81 self.sound_enabled = False | |
82 self.sound_cache = {} | |
83 self._resource_finder = resource_finder | |
84 self._current_playlist = None | |
85 | |
86 def enable_sound(self, constants): | |
87 """Attempt to initialise the sound system""" | |
88 if pygame_Sound is None: | |
89 self.disable_sound(pygame_import_error) | |
90 return | |
91 try: | |
92 pygame.mixer.init(constants.snd_freq, | |
93 constants.snd_bitsize, | |
94 constants.snd_channels, | |
95 constants.snd_buffer) | |
96 self.sound_enabled = True | |
97 music.set_endevent(MUSIC_ENDED) | |
98 except pygame.error, exc: | |
99 self.disable_sound(exc) | |
100 | |
101 def disable_sound(self, exc=None): | |
102 """Disable the sound system""" | |
103 self.sound_enabled = False | |
104 if exc is not None: | |
105 print 'Failed to initialise sound system' | |
106 print 'Error: %s' % exc | |
107 print 'Sound disabled' | |
108 | |
109 def get_sound(self, *names): | |
110 if not self.sound_enabled: | |
111 return DummySound() | |
112 sound = None | |
113 try: | |
114 path = self._resource_finder.get_resource_path("sounds", *names) | |
115 sound = self.sound_cache.get(path, None) | |
116 except ResourceNotFound: | |
117 print "Sound file not found: %s" % names | |
118 # Cache failed lookup | |
119 sound = DummySound() | |
120 self.sound_cache[path] = sound | |
121 if sound is None: | |
122 try: | |
123 sound = pygame_Sound(path) | |
124 except pygame.error: | |
125 print "Sound file not found: %s" % names | |
126 sound = DummySound() | |
127 self.sound_cache[path] = sound | |
128 return sound | |
129 | |
130 def get_playlist(self, pieces, random=False, repeat=False): | |
131 return PlayList(pieces, random, repeat) | |
132 | |
133 def get_music(self, name): | |
134 if self.sound_enabled: | |
135 music_file = self._resource_finder.get_resource_path("sounds", | |
136 name) | |
137 return music_file | |
138 return None | |
139 | |
140 def music_ended(self): | |
141 if self._current_playlist: | |
142 # Try start the next tune | |
143 self.start_next_music() | |
144 | |
145 def change_playlist(self, new_playlist): | |
146 if self.sound_enabled: | |
147 music.stop() | |
148 self._current_playlist = new_playlist | |
149 self.start_next_music() | |
150 | |
151 def start_next_music(self): | |
152 if self._current_playlist: | |
153 tune = self._current_playlist.get_next() | |
154 if tune: | |
155 music.load(tune) | |
156 music.play() | |
157 | |
158 def get_current_playlist(self): | |
159 return self._current_playlist |