annotate pyntnclick/sound.py @ 548:ded4324b236e pyntnclick

Moved stuff around, broke everything.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 11 Feb 2012 13:10:18 +0200
parents gamelib/sound.py@6df527142e69
children 2f7aa3cad77c
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
534
6df527142e69 PEP-8 sound.py
Jeremy Thurgood <firxen@gmail.com>
parents: 315
diff changeset
16
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
17 def get_sound(*names):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
18 if sound_cache is None:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
19 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
20 path = _resource_path("sounds", names)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
21 sound = sound_cache.get(path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
22 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
23 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
24 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
25 return dummy_sound
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
26 try:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
27 from pygame.mixer import Sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
28 except ImportError, e:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
29 no_sound(e)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
30 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
31 try:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
32 sound = Sound(path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
33 except pygame.error, e:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
34 missing_sound(e, path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
35 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
36 sound_cache[path] = sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
37 return sound
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
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
40 def no_sound(e):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
41 global sound_cache
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
42 print "get_sound: %s" % e
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
43 print "get_sound: Sound not available, continuing without it"
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
44 sound_cache = None
188
aebd4fd5b561 no_sound error handler should also disable music
Neil Muller <neil@dip.sun.ac.za>
parents: 154
diff changeset
45 albow.music.music_enabled = False
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
46
534
6df527142e69 PEP-8 sound.py
Jeremy Thurgood <firxen@gmail.com>
parents: 315
diff changeset
47
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
48 def disable_sound():
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
49 global sound_cache
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
50 sound_cache = None
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
51 albow.music.music_enabled = False
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
52
534
6df527142e69 PEP-8 sound.py
Jeremy Thurgood <firxen@gmail.com>
parents: 315
diff changeset
53
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
54 def missing_sound(e, name):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
55 print "albow.resource.get_sound: %s: %s" % (name, e)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
56
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
57
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
58 def start_next_music():
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
59 """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
60 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
61 next_music = albow.music.current_playlist.next()
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
62 if next_music:
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
63 #print "albow.music: loading", repr(next_music)
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
64 music.load(next_music)
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
65 music.play()
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
66 albow.music.next_change_delay = albow.music.change_delay
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
67 albow.music.current_music = next_music
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
68
534
6df527142e69 PEP-8 sound.py
Jeremy Thurgood <firxen@gmail.com>
parents: 315
diff changeset
69
315
7b07ffc37ec0 Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents: 188
diff changeset
70 def get_current_playlist():
7b07ffc37ec0 Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents: 188
diff changeset
71 if albow.music.music_enabled and albow.music.current_playlist:
7b07ffc37ec0 Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents: 188
diff changeset
72 return albow.music.current_playlist
7b07ffc37ec0 Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents: 188
diff changeset
73
154
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
74 # Monkey patch
d2f94f42edf3 Monkey patch albow
Neil Muller <neil@dip.sun.ac.za>
parents: 123
diff changeset
75 albow.music.start_next_music = start_next_music