annotate gamelib/sound.py @ 154:d2f94f42edf3

Monkey patch albow
author Neil Muller <neil@dip.sun.ac.za>
date Tue, 24 Aug 2010 23:42:07 +0200
parents 3b293e3b8829
children aebd4fd5b561
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
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
10 from pygame.mixer import music
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
11 from albow.resource import _resource_path, dummy_sound
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
12 import albow.music
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
13
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
14 sound_cache = {}
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
15
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
16 def get_sound(*names):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
17 if sound_cache is None:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
18 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
19 path = _resource_path("sounds", names)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
20 sound = sound_cache.get(path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
21 if not sound:
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
22 if not os.path.isfile(path):
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
23 missing_sound("File does not exist", path)
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
24 return dummy_sound
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
25 try:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
26 from pygame.mixer import Sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
27 except ImportError, e:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
28 no_sound(e)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
29 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
30 try:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
31 sound = Sound(path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
32 except pygame.error, e:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
33 missing_sound(e, path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
34 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
35 sound_cache[path] = sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
36 return sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
37
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
38
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
39 def no_sound(e):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
40 global sound_cache
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
41 print "get_sound: %s" % e
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
42 print "get_sound: Sound not available, continuing without it"
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
43 sound_cache = None
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
44
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
45 def disable_sound():
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
46 global sound_cache
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
47 sound_cache = None
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
48 albow.music.music_enabled = False
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
49
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
50 def missing_sound(e, name):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
51 print "albow.resource.get_sound: %s: %s" % (name, e)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
52
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
53
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
54 def start_next_music():
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
55 """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
56 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
57 next_music = albow.music.current_playlist.next()
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
58 if next_music:
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
59 #print "albow.music: loading", repr(next_music)
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
60 music.load(next_music)
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
61 music.play()
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
62 albow.music.next_change_delay = albow.music.change_delay
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
63 albow.music.current_music = next_music
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
64
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
65 # Monkey patch
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
66 albow.music.start_next_music = start_next_music