Mercurial > boomslang
annotate pyntnclick/sound.py @ 624:1c5ef1e02e30 pyntnclick
Remove hacky start_game() that wasn't meant to be committed
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 11 Feb 2012 21:52:42 +0200 |
parents | 66c2e084b8b3 |
children | 660ef5793886 |
rev | line source |
---|---|
583
66c2e084b8b3
Remove unused import and fix comment.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
577
diff
changeset
|
1 # sound management for pyntnclick |
107 | 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 | |
123 | 7 import pygame |
564 | 8 |
9 try: | |
10 from pygame.mixer import Sound as pygame_Sound | |
11 from pygame.mixer import music | |
12 pygame_import_error = None | |
13 except ImportError, e: | |
14 # Save error, so we don't crash and can do the right thing later | |
15 pygame_import_error = e | |
16 pygame_Sound = None | |
17 music = None | |
18 | |
19 from pyntnclick.resources import ResourceNotFound | |
20 | |
154 | 21 import albow.music |
107 | 22 |
564 | 23 |
24 class DummySound(object): | |
25 """A dummy sound object. | |
26 | |
27 This is a placeholder object with the same API as | |
28 pygame.mixer.Sound which does nothing. Used when | |
29 sounds are disabled so scense don't need to worry | |
30 about the details. | |
31 | |
32 Inpsired by the same idea in Albow (by Greg Ewing)""" | |
33 | |
565 | 34 def play(self, *args): |
35 pass | |
564 | 36 |
565 | 37 def stop(self): |
38 pass | |
564 | 39 |
565 | 40 def get_length(self): |
41 return 0.0 | |
564 | 42 |
565 | 43 def get_num_channel(self): |
44 return 0 | |
564 | 45 |
565 | 46 def get_volume(self): |
47 return 0.0 | |
564 | 48 |
565 | 49 def fadeout(self, *args): |
50 pass | |
107 | 51 |
534 | 52 |
564 | 53 class Sound(object): |
54 """Global sound management and similiar useful things""" | |
55 | |
56 def __init__(self, resource_finder): | |
57 self.sound_enabled = False | |
58 self.sound_cache = {} | |
59 self._resource_finder = resource_finder | |
60 | |
576
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
61 def enable_sound(self, constants): |
564 | 62 """Attempt to initialise the sound system""" |
63 if pygame_Sound is None: | |
64 self.disable_sound(pygame_import_error) | |
65 return | |
107 | 66 try: |
576
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
67 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
|
68 constants.snd_bitsize, |
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
69 constants.snd_channels, |
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
70 constants.snd_buffer) |
564 | 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 sound = None |
87 try: | |
577 | 88 path = self._resource_finder.get_resource_path("sounds", *names) |
565 | 89 sound = self.sound_cache.get(path, None) |
564 | 90 except ResourceNotFound: |
577 | 91 print "Sound file not found: %s" % names |
564 | 92 # Cache failed lookup |
93 sound = DummySound() | |
94 self.sound_cache[path] = sound | |
95 if sound is None: | |
96 try: | |
97 sound = pygame_Sound(path) | |
570
9c3528c2cbe5
Bug fixes for sound hook-up.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
565
diff
changeset
|
98 except pygame.error: |
577 | 99 print "Sound file not found: %s" % names |
564 | 100 sound = DummySound() |
101 self.sound_cache[path] = sound | |
102 return sound | |
107 | 103 |
564 | 104 def get_playlist(self, pieces, random=False, repeat=False): |
105 return albow.music.PlayList(pieces, random, repeat) | |
534 | 106 |
564 | 107 def get_music(self, name, prefix): |
577 | 108 if self.sound_enabled: |
109 return albow.music.get_music(name, prefix=prefix) | |
107 | 110 |
564 | 111 def change_playlist(self, new_playlist): |
577 | 112 if self.sound_enabled: |
113 albow.music.change_playlist(new_playlist) | |
154 | 114 |
577 | 115 def get_current_playlist(self): |
116 if self.sound_enabled and albow.music.music_enabled and \ | |
117 albow.music.current_playlist: | |
572
e393954e3749
Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents:
570
diff
changeset
|
118 return albow.music.current_playlist |
e393954e3749
Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents:
570
diff
changeset
|
119 |
565 | 120 |
154 | 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 | |
534 | 132 |
154 | 133 # Monkey patch |
134 albow.music.start_next_music = start_next_music |