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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
2
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
3 # This re-implements some of the albow.resource code to
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
4 # a) work around an annoying bugs
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
5 # b) add some missing functionality (disable_sound)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
6
123
3b293e3b8829 Add missing pygame import
Neil Muller <neil@dip.sun.ac.za>
parents: 111
diff changeset
7 import pygame
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
8
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
9 try:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
10 from pygame.mixer import Sound as pygame_Sound
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
11 from pygame.mixer import music
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
12 pygame_import_error = None
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
13 except ImportError, e:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
14 # Save error, so we don't crash and can do the right thing later
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
15 pygame_import_error = e
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
16 pygame_Sound = None
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
17 music = None
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
18
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
19 from pyntnclick.resources import ResourceNotFound
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
20
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
21 import albow.music
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
22
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
23
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
24 class DummySound(object):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
25 """A dummy sound object.
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
26
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
27 This is a placeholder object with the same API as
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
28 pygame.mixer.Sound which does nothing. Used when
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
29 sounds are disabled so scense don't need to worry
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
30 about the details.
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
31
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
32 Inpsired by the same idea in Albow (by Greg Ewing)"""
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
33
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
34 def play(self, *args):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
35 pass
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
36
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
37 def stop(self):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
38 pass
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
39
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
40 def get_length(self):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
41 return 0.0
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
42
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
43 def get_num_channel(self):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
44 return 0
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
45
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
46 def get_volume(self):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
47 return 0.0
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
48
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
49 def fadeout(self, *args):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
50 pass
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
51
534
6df527142e69 PEP-8 sound.py
Jeremy Thurgood <firxen@gmail.com>
parents: 315
diff changeset
52
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
53 class Sound(object):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
54 """Global sound management and similiar useful things"""
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
55
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
56 def __init__(self, resource_finder):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
57 self.sound_enabled = False
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
58 self.sound_cache = {}
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
59 self._resource_finder = resource_finder
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
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
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
62 """Attempt to initialise the sound system"""
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
63 if pygame_Sound is None:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
64 self.disable_sound(pygame_import_error)
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
65 return
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
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
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
71 self.sound_enabled = True
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
72 except pygame.error, exc:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
73 self.disable_sound(exc)
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
74
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
75 def disable_sound(self, exc=None):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
76 """Disable the sound system"""
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
77 self.sound_enabled = False
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
78 if exc is not None:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
79 print 'Failed to initialise sound system'
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
80 print 'Error: %s' % exc
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
81 print 'Sound disabled'
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
82
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
83 def get_sound(self, *names):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
84 if not self.sound_enabled:
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
85 return DummySound()
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
86 sound = None
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
87 try:
577
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
88 path = self._resource_finder.get_resource_path("sounds", *names)
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
89 sound = self.sound_cache.get(path, None)
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
90 except ResourceNotFound:
577
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
91 print "Sound file not found: %s" % names
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
92 # Cache failed lookup
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
93 sound = DummySound()
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
94 self.sound_cache[path] = sound
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
95 if sound is None:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
96 try:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
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
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
99 print "Sound file not found: %s" % names
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
100 sound = DummySound()
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
101 self.sound_cache[path] = sound
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
102 return sound
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
103
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
104 def get_playlist(self, pieces, random=False, repeat=False):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
105 return albow.music.PlayList(pieces, random, repeat)
534
6df527142e69 PEP-8 sound.py
Jeremy Thurgood <firxen@gmail.com>
parents: 315
diff changeset
106
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
107 def get_music(self, name, prefix):
577
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
108 if self.sound_enabled:
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
109 return albow.music.get_music(name, prefix=prefix)
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
110
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
111 def change_playlist(self, new_playlist):
577
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
112 if self.sound_enabled:
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
113 albow.music.change_playlist(new_playlist)
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
114
577
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
115 def get_current_playlist(self):
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
116 if self.sound_enabled and albow.music.music_enabled and \
ccc26c23d2c1 Fix sound loading
Neil Muller <neil@dip.sun.ac.za>
parents: 576
diff changeset
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
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
120
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
121 def start_next_music():
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
122 """Start playing the next item from the current playlist immediately."""
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
123 if albow.music.music_enabled and albow.music.current_playlist:
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
124 next_music = albow.music.current_playlist.next()
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
125 if next_music:
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
126 #print "albow.music: loading", repr(next_music)
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
127 music.load(next_music)
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
128 music.play()
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
129 albow.music.next_change_delay = albow.music.change_delay
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
130 albow.music.current_music = next_music
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
131
534
6df527142e69 PEP-8 sound.py
Jeremy Thurgood <firxen@gmail.com>
parents: 315
diff changeset
132
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
133 # Monkey patch
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
134 albow.music.start_next_music = start_next_music