annotate pyntnclick/sound.py @ 576:1b1ab71535bd pyntnclick

Classify constants, which involves a whole bunch of XXX comments
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 11 Feb 2012 16:02:06 +0200
parents e393954e3749
children ccc26c23d2c1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
1 # Sound management for Suspended Sentence
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
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
3b293e3b8829 Add missing pygame import
Neil Muller <neil@dip.sun.ac.za>
parents: 111
diff changeset
9 import pygame
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
10
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
11 try:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
12 from pygame.mixer import Sound as pygame_Sound
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
13 from pygame.mixer import music
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
14 pygame_import_error = None
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
15 except ImportError, e:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
16 # 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
17 pygame_import_error = e
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
18 pygame_Sound = None
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
19 music = None
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
20
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
21 from pyntnclick.resources import ResourceNotFound
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
22
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
23 import albow.music
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
24
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
25
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
26 class DummySound(object):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
27 """A dummy sound object.
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
28
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
29 This is a placeholder object with the same API as
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
30 pygame.mixer.Sound which does nothing. Used when
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
31 sounds are disabled so scense don't need to worry
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
32 about the details.
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
33
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
34 Inpsired by the same idea in Albow (by Greg Ewing)"""
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
35
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
36 def play(self, *args):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
37 pass
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
38
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
39 def stop(self):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
40 pass
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
41
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
42 def get_length(self):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
43 return 0.0
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
44
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
45 def get_num_channel(self):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
46 return 0
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
47
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
48 def get_volume(self):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
49 return 0.0
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
50
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
51 def fadeout(self, *args):
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
52 pass
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
53
534
6df527142e69 PEP-8 sound.py
Jeremy Thurgood <firxen@gmail.com>
parents: 315
diff changeset
54
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
55 class Sound(object):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
56 """Global sound management and similiar useful things"""
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
57
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
58 def __init__(self, resource_finder):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
59 self.sound_enabled = False
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
60 self.sound_cache = {}
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
61 self._resource_finder = resource_finder
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
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
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
64 """Attempt to initialise the sound system"""
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
65 if pygame_Sound is None:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
66 self.disable_sound(pygame_import_error)
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
67 return
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
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
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
73 self.sound_enabled = True
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
74 except pygame.error, exc:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
75 self.disable_sound(exc)
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
76
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
77 def disable_sound(self, exc=None):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
78 """Disable the sound system"""
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
79 self.sound_enabled = False
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
80 if exc is not None:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
81 print 'Failed to initialise sound system'
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
82 print 'Error: %s' % exc
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
83 print 'Sound disabled'
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
84
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
85 def get_sound(self, *names):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
86 if not self.sound_enabled:
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
87 return DummySound()
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
88 soundfile = os.path.join(names)
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
89 sound = None
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
90 try:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
91 path = self._resource_finder("sounds", soundfile)
565
88cffe418201 Pyflakes sound
Neil Muller <neil@dip.sun.ac.za>
parents: 564
diff changeset
92 sound = self.sound_cache.get(path, None)
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
93 except ResourceNotFound:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
94 print "Sound file not found: %s" % soundfile
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
95 # Cache failed lookup
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
96 sound = DummySound()
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
97 self.sound_cache[path] = sound
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
98 if sound is None:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
99 try:
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
100 sound = pygame_Sound(path)
570
9c3528c2cbe5 Bug fixes for sound hook-up.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 565
diff changeset
101 except pygame.error:
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
102 print "Sound file not found: %s" % soundfile
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
103 sound = DummySound()
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
104 self.sound_cache[path] = sound
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
105 return sound
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
106
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
107 def get_playlist(self, pieces, random=False, repeat=False):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
108 return albow.music.PlayList(pieces, random, repeat)
534
6df527142e69 PEP-8 sound.py
Jeremy Thurgood <firxen@gmail.com>
parents: 315
diff changeset
109
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
110 def get_music(self, name, prefix):
570
9c3528c2cbe5 Bug fixes for sound hook-up.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 565
diff changeset
111 return albow.music.get_music(name, prefix=prefix)
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
112
564
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
113 def change_playlist(self, new_playlist):
2f7aa3cad77c Sound hackery
Neil Muller <neil@dip.sun.ac.za>
parents: 548
diff changeset
114 albow.music.change_playlist(new_playlist)
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
115
572
e393954e3749 Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents: 570
diff changeset
116 def get_current_playlist():
e393954e3749 Move get_current_playlist onto sound object
Neil Muller <neil@dip.sun.ac.za>
parents: 570
diff changeset
117 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
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