Mercurial > boomslang
annotate pyntnclick/sound.py @ 581:7e4a20eb78b8 pyntnclick
Pass game_description to all screens
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 11 Feb 2012 16:28:41 +0200 |
parents | ccc26c23d2c1 |
children | 66c2e084b8b3 |
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.resources import ResourceNotFound | |
22 | |
154 | 23 import albow.music |
107 | 24 |
564 | 25 |
26 class DummySound(object): | |
27 """A dummy sound object. | |
28 | |
29 This is a placeholder object with the same API as | |
30 pygame.mixer.Sound which does nothing. Used when | |
31 sounds are disabled so scense don't need to worry | |
32 about the details. | |
33 | |
34 Inpsired by the same idea in Albow (by Greg Ewing)""" | |
35 | |
565 | 36 def play(self, *args): |
37 pass | |
564 | 38 |
565 | 39 def stop(self): |
40 pass | |
564 | 41 |
565 | 42 def get_length(self): |
43 return 0.0 | |
564 | 44 |
565 | 45 def get_num_channel(self): |
46 return 0 | |
564 | 47 |
565 | 48 def get_volume(self): |
49 return 0.0 | |
564 | 50 |
565 | 51 def fadeout(self, *args): |
52 pass | |
107 | 53 |
534 | 54 |
564 | 55 class Sound(object): |
56 """Global sound management and similiar useful things""" | |
57 | |
58 def __init__(self, resource_finder): | |
59 self.sound_enabled = False | |
60 self.sound_cache = {} | |
61 self._resource_finder = resource_finder | |
62 | |
576
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
63 def enable_sound(self, constants): |
564 | 64 """Attempt to initialise the sound system""" |
65 if pygame_Sound is None: | |
66 self.disable_sound(pygame_import_error) | |
67 return | |
107 | 68 try: |
576
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
69 pygame.mixer.init(constants.snd_freq, |
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
70 constants.snd_bitsize, |
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
71 constants.snd_channels, |
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
72 constants.snd_buffer) |
564 | 73 self.sound_enabled = True |
74 except pygame.error, exc: | |
75 self.disable_sound(exc) | |
76 | |
77 def disable_sound(self, exc=None): | |
78 """Disable the sound system""" | |
79 self.sound_enabled = False | |
80 if exc is not None: | |
81 print 'Failed to initialise sound system' | |
82 print 'Error: %s' % exc | |
83 print 'Sound disabled' | |
84 | |
85 def get_sound(self, *names): | |
86 if not self.sound_enabled: | |
565 | 87 return DummySound() |
564 | 88 sound = None |
89 try: | |
577 | 90 path = self._resource_finder.get_resource_path("sounds", *names) |
565 | 91 sound = self.sound_cache.get(path, None) |
564 | 92 except ResourceNotFound: |
577 | 93 print "Sound file not found: %s" % names |
564 | 94 # Cache failed lookup |
95 sound = DummySound() | |
96 self.sound_cache[path] = sound | |
97 if sound is None: | |
98 try: | |
99 sound = pygame_Sound(path) | |
570
9c3528c2cbe5
Bug fixes for sound hook-up.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
565
diff
changeset
|
100 except pygame.error: |
577 | 101 print "Sound file not found: %s" % names |
564 | 102 sound = DummySound() |
103 self.sound_cache[path] = sound | |
104 return sound | |
107 | 105 |
564 | 106 def get_playlist(self, pieces, random=False, repeat=False): |
107 return albow.music.PlayList(pieces, random, repeat) | |
534 | 108 |
564 | 109 def get_music(self, name, prefix): |
577 | 110 if self.sound_enabled: |
111 return albow.music.get_music(name, prefix=prefix) | |
107 | 112 |
564 | 113 def change_playlist(self, new_playlist): |
577 | 114 if self.sound_enabled: |
115 albow.music.change_playlist(new_playlist) | |
154 | 116 |
577 | 117 def get_current_playlist(self): |
118 if self.sound_enabled and albow.music.music_enabled and \ | |
119 albow.music.current_playlist: | |
572
e393954e3749
Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents:
570
diff
changeset
|
120 return albow.music.current_playlist |
e393954e3749
Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents:
570
diff
changeset
|
121 |
565 | 122 |
154 | 123 def start_next_music(): |
124 """Start playing the next item from the current playlist immediately.""" | |
125 if albow.music.music_enabled and albow.music.current_playlist: | |
126 next_music = albow.music.current_playlist.next() | |
127 if next_music: | |
128 #print "albow.music: loading", repr(next_music) | |
129 music.load(next_music) | |
130 music.play() | |
131 albow.music.next_change_delay = albow.music.change_delay | |
132 albow.music.current_music = next_music | |
133 | |
534 | 134 |
154 | 135 # Monkey patch |
136 albow.music.start_next_music = start_next_music |