Mercurial > boomslang
annotate pyntnclick/sound.py @ 572:e393954e3749 pyntnclick
Move get_current_playlist onto sound object
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Sat, 11 Feb 2012 15:53:14 +0200 |
parents | 9c3528c2cbe5 |
children | 1b1ab71535bd |
rev | line source |
---|---|
107 | 1 # Sound management for Suspended Sentence |
2 | |
3 # This re-implements some of the albow.resource code to | |
4 # a) work around an annoying bugs | |
5 # b) add some missing functionality (disable_sound) | |
6 | |
111
18ffaaaa27e7
Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents:
107
diff
changeset
|
7 import os |
18ffaaaa27e7
Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents:
107
diff
changeset
|
8 |
123 | 9 import pygame |
564 | 10 |
11 try: | |
12 from pygame.mixer import Sound as pygame_Sound | |
13 from pygame.mixer import music | |
14 pygame_import_error = None | |
15 except ImportError, e: | |
16 # Save error, so we don't crash and can do the right thing later | |
17 pygame_import_error = e | |
18 pygame_Sound = None | |
19 music = None | |
20 | |
21 from pyntnclick.constants import FREQ, BITSIZE, CHANNELS, BUFFER | |
22 from pyntnclick.resources import ResourceNotFound | |
23 | |
154 | 24 import albow.music |
107 | 25 |
564 | 26 |
27 class DummySound(object): | |
28 """A dummy sound object. | |
29 | |
30 This is a placeholder object with the same API as | |
31 pygame.mixer.Sound which does nothing. Used when | |
32 sounds are disabled so scense don't need to worry | |
33 about the details. | |
34 | |
35 Inpsired by the same idea in Albow (by Greg Ewing)""" | |
36 | |
565 | 37 def play(self, *args): |
38 pass | |
564 | 39 |
565 | 40 def stop(self): |
41 pass | |
564 | 42 |
565 | 43 def get_length(self): |
44 return 0.0 | |
564 | 45 |
565 | 46 def get_num_channel(self): |
47 return 0 | |
564 | 48 |
565 | 49 def get_volume(self): |
50 return 0.0 | |
564 | 51 |
565 | 52 def fadeout(self, *args): |
53 pass | |
107 | 54 |
534 | 55 |
564 | 56 class Sound(object): |
57 """Global sound management and similiar useful things""" | |
58 | |
59 def __init__(self, resource_finder): | |
60 self.sound_enabled = False | |
61 self.sound_cache = {} | |
62 self._resource_finder = resource_finder | |
63 | |
64 def enable_sound(self): | |
65 """Attempt to initialise the sound system""" | |
66 if pygame_Sound is None: | |
67 self.disable_sound(pygame_import_error) | |
68 return | |
107 | 69 try: |
564 | 70 pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER) |
71 self.sound_enabled = True | |
72 except pygame.error, exc: | |
73 self.disable_sound(exc) | |
74 | |
75 def disable_sound(self, exc=None): | |
76 """Disable the sound system""" | |
77 self.sound_enabled = False | |
78 if exc is not None: | |
79 print 'Failed to initialise sound system' | |
80 print 'Error: %s' % exc | |
81 print 'Sound disabled' | |
82 | |
83 def get_sound(self, *names): | |
84 if not self.sound_enabled: | |
565 | 85 return DummySound() |
564 | 86 soundfile = os.path.join(names) |
87 sound = None | |
88 try: | |
89 path = self._resource_finder("sounds", soundfile) | |
565 | 90 sound = self.sound_cache.get(path, None) |
564 | 91 except ResourceNotFound: |
92 print "Sound file not found: %s" % soundfile | |
93 # Cache failed lookup | |
94 sound = DummySound() | |
95 self.sound_cache[path] = sound | |
96 if sound is None: | |
97 try: | |
98 sound = pygame_Sound(path) | |
570
9c3528c2cbe5
Bug fixes for sound hook-up.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
565
diff
changeset
|
99 except pygame.error: |
564 | 100 print "Sound file not found: %s" % soundfile |
101 sound = DummySound() | |
102 self.sound_cache[path] = sound | |
103 return sound | |
107 | 104 |
564 | 105 def get_playlist(self, pieces, random=False, repeat=False): |
106 return albow.music.PlayList(pieces, random, repeat) | |
534 | 107 |
564 | 108 def get_music(self, name, prefix): |
570
9c3528c2cbe5
Bug fixes for sound hook-up.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
565
diff
changeset
|
109 return albow.music.get_music(name, prefix=prefix) |
107 | 110 |
564 | 111 def change_playlist(self, new_playlist): |
112 albow.music.change_playlist(new_playlist) | |
154 | 113 |
572
e393954e3749
Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents:
570
diff
changeset
|
114 def get_current_playlist(): |
e393954e3749
Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents:
570
diff
changeset
|
115 if albow.music.music_enabled and albow.music.current_playlist: |
e393954e3749
Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents:
570
diff
changeset
|
116 return albow.music.current_playlist |
e393954e3749
Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents:
570
diff
changeset
|
117 |
565 | 118 |
154 | 119 def start_next_music(): |
120 """Start playing the next item from the current playlist immediately.""" | |
121 if albow.music.music_enabled and albow.music.current_playlist: | |
122 next_music = albow.music.current_playlist.next() | |
123 if next_music: | |
124 #print "albow.music: loading", repr(next_music) | |
125 music.load(next_music) | |
126 music.play() | |
127 albow.music.next_change_delay = albow.music.change_delay | |
128 albow.music.current_music = next_music | |
129 | |
534 | 130 |
154 | 131 # Monkey patch |
132 albow.music.start_next_music = start_next_music |