comparison pyntnclick/sound.py @ 564:2f7aa3cad77c pyntnclick

Sound hackery
author Neil Muller <neil@dip.sun.ac.za>
date Sat, 11 Feb 2012 15:35:06 +0200
parents ded4324b236e
children 88cffe418201
comparison
equal deleted inserted replaced
563:18396b937647 564:2f7aa3cad77c
5 # b) add some missing functionality (disable_sound) 5 # b) add some missing functionality (disable_sound)
6 6
7 import os 7 import os
8 8
9 import pygame 9 import pygame
10 from pygame.mixer import music 10
11 from albow.resource import _resource_path, dummy_sound 11 try:
12 from pygame.mixer import Sound as pygame_Sound
13 from pygame.mixer import music
14 pygame_import_error = None
15 except ImportError, e:
16 # Save error, so we don't crash and can do the right thing later
17 pygame_import_error = e
18 pygame_Sound = None
19 music = None
20
21 from pyntnclick.constants import FREQ, BITSIZE, CHANNELS, BUFFER
22 from pyntnclick.resources import ResourceNotFound
23
12 import albow.music 24 import albow.music
13 25
14 sound_cache = {} 26
27 class DummySound(object):
28 """A dummy sound object.
29
30 This is a placeholder object with the same API as
31 pygame.mixer.Sound which does nothing. Used when
32 sounds are disabled so scense don't need to worry
33 about the details.
34
35 Inpsired by the same idea in Albow (by Greg Ewing)"""
36
37 def play(self, *args):
38 pass
39
40 def stop(self):
41 pass
42
43 def get_length(self):
44 return 0.0
45
46 def get_num_channel(self):
47 return 0
48
49 def get_volume(self):
50 return 0.0
51
52 def fadeout(self, *args):
53 pass
15 54
16 55
17 def get_sound(*names): 56 class Sound(object):
18 if sound_cache is None: 57 """Global sound management and similiar useful things"""
19 return dummy_sound 58
20 path = _resource_path("sounds", names) 59 def __init__(self, resource_finder):
21 sound = sound_cache.get(path) 60 self.sound_enabled = False
22 if not sound: 61 self.sound_cache = {}
23 if not os.path.isfile(path): 62 self._resource_finder = resource_finder
24 missing_sound("File does not exist", path) 63
64 def enable_sound(self):
65 """Attempt to initialise the sound system"""
66 if pygame_Sound is None:
67 self.disable_sound(pygame_import_error)
68 return
69 try:
70 pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
71 self.sound_enabled = True
72 except pygame.error, exc:
73 self.disable_sound(exc)
74
75 def disable_sound(self, exc=None):
76 """Disable the sound system"""
77 self.sound_enabled = False
78 if exc is not None:
79 print 'Failed to initialise sound system'
80 print 'Error: %s' % exc
81 print 'Sound disabled'
82
83 def get_sound(self, *names):
84 if not self.sound_enabled:
25 return dummy_sound 85 return dummy_sound
86 soundfile = os.path.join(names)
87 sound = None
26 try: 88 try:
27 from pygame.mixer import Sound 89 path = self._resource_finder("sounds", soundfile)
28 except ImportError, e: 90 sound = sound_cache.get(path, None)
29 no_sound(e) 91 except ResourceNotFound:
30 return dummy_sound 92 print "Sound file not found: %s" % soundfile
31 try: 93 # Cache failed lookup
32 sound = Sound(path) 94 sound = DummySound()
33 except pygame.error, e: 95 self.sound_cache[path] = sound
34 missing_sound(e, path) 96 if sound is None:
35 return dummy_sound 97 try:
36 sound_cache[path] = sound 98 sound = pygame_Sound(path)
37 return sound 99 except pygame.error, e:
100 print "Sound file not found: %s" % soundfile
101 sound = DummySound()
102 self.sound_cache[path] = sound
103 return sound
38 104
105 def get_playlist(self, pieces, random=False, repeat=False):
106 return albow.music.PlayList(pieces, random, repeat)
39 107
40 def no_sound(e): 108 def get_music(self, name, prefix):
41 global sound_cache 109 return albow.music.get_music(name, prefix)
42 print "get_sound: %s" % e
43 print "get_sound: Sound not available, continuing without it"
44 sound_cache = None
45 albow.music.music_enabled = False
46 110
47 111 def change_playlist(self, new_playlist):
48 def disable_sound(): 112 albow.music.change_playlist(new_playlist)
49 global sound_cache
50 sound_cache = None
51 albow.music.music_enabled = False
52
53
54 def missing_sound(e, name):
55 print "albow.resource.get_sound: %s: %s" % (name, e)
56
57 113
58 def start_next_music(): 114 def start_next_music():
59 """Start playing the next item from the current playlist immediately.""" 115 """Start playing the next item from the current playlist immediately."""
60 if albow.music.music_enabled and albow.music.current_playlist: 116 if albow.music.music_enabled and albow.music.current_playlist:
61 next_music = albow.music.current_playlist.next() 117 next_music = albow.music.current_playlist.next()