Mercurial > boomslang
annotate pyntnclick/sound.py @ 658:2703924c8c70 pyntnclick
Custom mouse cursors return
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 12 Feb 2012 14:25:27 +0200 |
parents | 4206da1f742d |
children |
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 |
629 | 3 from random import randrange |
4 | |
5 | |
123 | 6 import pygame |
564 | 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 | |
629 | 19 from pyntnclick.engine import MUSIC_ENDED |
564 | 20 |
629 | 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 | |
631
8b78fc07a862
Less buggy non-albow music support
Neil Muller <neil@dip.sun.ac.za>
parents:
629
diff
changeset
|
28 self._repeat = repeat |
629 | 29 |
30 def get_next(self): | |
31 # Get the next piece | |
631
8b78fc07a862
Less buggy non-albow music support
Neil Muller <neil@dip.sun.ac.za>
parents:
629
diff
changeset
|
32 if self._pieces: |
629 | 33 if self._random: |
631
8b78fc07a862
Less buggy non-albow music support
Neil Muller <neil@dip.sun.ac.za>
parents:
629
diff
changeset
|
34 if not self._repeat or len(self._pieces) < 3: |
8b78fc07a862
Less buggy non-albow music support
Neil Muller <neil@dip.sun.ac.za>
parents:
629
diff
changeset
|
35 i = randrange(0, len(self._pieces)) |
629 | 36 else: |
37 # Ignore the last entry, since we possibly just played it | |
631
8b78fc07a862
Less buggy non-albow music support
Neil Muller <neil@dip.sun.ac.za>
parents:
629
diff
changeset
|
38 i = randrange(0, len(self._pieces) - 1) |
629 | 39 else: |
40 i = 0 | |
631
8b78fc07a862
Less buggy non-albow music support
Neil Muller <neil@dip.sun.ac.za>
parents:
629
diff
changeset
|
41 result = self._pieces.pop(i) |
629 | 42 if self._repeat: |
631
8b78fc07a862
Less buggy non-albow music support
Neil Muller <neil@dip.sun.ac.za>
parents:
629
diff
changeset
|
43 self._pieces.append(result) |
629 | 44 return result |
45 return None | |
107 | 46 |
564 | 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 | |
565 | 58 def play(self, *args): |
59 pass | |
564 | 60 |
565 | 61 def stop(self): |
62 pass | |
564 | 63 |
565 | 64 def get_length(self): |
65 return 0.0 | |
564 | 66 |
565 | 67 def get_num_channel(self): |
68 return 0 | |
564 | 69 |
565 | 70 def get_volume(self): |
71 return 0.0 | |
564 | 72 |
565 | 73 def fadeout(self, *args): |
74 pass | |
107 | 75 |
534 | 76 |
564 | 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 | |
629 | 84 self._current_playlist = None |
564 | 85 |
576
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
86 def enable_sound(self, constants): |
564 | 87 """Attempt to initialise the sound system""" |
88 if pygame_Sound is None: | |
89 self.disable_sound(pygame_import_error) | |
90 return | |
107 | 91 try: |
576
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
92 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
|
93 constants.snd_bitsize, |
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
94 constants.snd_channels, |
1b1ab71535bd
Classify constants, which involves a whole bunch of XXX comments
Stefano Rivera <stefano@rivera.za.net>
parents:
572
diff
changeset
|
95 constants.snd_buffer) |
564 | 96 self.sound_enabled = True |
629 | 97 music.set_endevent(MUSIC_ENDED) |
564 | 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: | |
565 | 111 return DummySound() |
564 | 112 sound = None |
113 try: | |
577 | 114 path = self._resource_finder.get_resource_path("sounds", *names) |
565 | 115 sound = self.sound_cache.get(path, None) |
564 | 116 except ResourceNotFound: |
577 | 117 print "Sound file not found: %s" % names |
564 | 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) | |
570
9c3528c2cbe5
Bug fixes for sound hook-up.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
565
diff
changeset
|
124 except pygame.error: |
577 | 125 print "Sound file not found: %s" % names |
564 | 126 sound = DummySound() |
127 self.sound_cache[path] = sound | |
128 return sound | |
107 | 129 |
564 | 130 def get_playlist(self, pieces, random=False, repeat=False): |
629 | 131 return PlayList(pieces, random, repeat) |
534 | 132 |
629 | 133 def get_music(self, name): |
577 | 134 if self.sound_enabled: |
629 | 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() | |
107 | 144 |
564 | 145 def change_playlist(self, new_playlist): |
577 | 146 if self.sound_enabled: |
631
8b78fc07a862
Less buggy non-albow music support
Neil Muller <neil@dip.sun.ac.za>
parents:
629
diff
changeset
|
147 music.stop() |
629 | 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() | |
154 | 157 |
577 | 158 def get_current_playlist(self): |
629 | 159 return self._current_playlist |