annotate mamba/sound.py @ 36:908c9d5597f9

Provide more utility functions in sound.py
author Neil Muller <drnlmuller@gmail.com>
date Sun, 11 Sep 2011 14:46:58 +0200
parents cccf1675731c
children cad653e4f59c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 """Basic sound frame-work"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3 from pygame import mixer
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4 import pygame
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6 from mamba import data
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7 from mamba.constants import FREQ, BITSIZE, CHANNELS, BUFFER
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
8
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9 # Global constant for holding the initialised sound system
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
10 _SOUND_SYSTEM = None
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
11
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
12
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
13 # Utility functions, used elsewhere
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14 def load_sound(key, track_name, volume=None):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
15 if _SOUND_SYSTEM is not None:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
16 _SOUND_SYSTEM.load_sound(key, track_name, volume)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
17
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
18
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19 def play_sound(key):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 if _SOUND_SYSTEM is not None:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 _SOUND_SYSTEM.play_sound(key)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
24 def play_music(track_name, volume=1.0):
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
25 if _SOUND_SYSTEM is not None:
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
26 _SOUND_SYSTEM.play_background_music(track_name, volume)
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
27
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
28
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
29 def stop_sound():
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
30 """Stop sounds and music"""
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
31 if _SOUND_SYSTEM is not None:
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
32 _SOUND_SYSTEM.stop_sounds()
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
33 _SOUND_SYSTEM.stop_music()
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
34
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
35
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
36 def pause_music():
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
37 """Pause backgroun music"""
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
38 if _SOUND_SYSTEM is not None:
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
39 _SOUND_SYSTEM.pause_music()
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
40
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
41
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
42 def resume_music():
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
43 """Unpause music"""
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
44 if _SOUND_SYSTEM is not None:
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
45 _SOUND_SYSTEM.resume_music()
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
46
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
47
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
48 class SoundSystem(object):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50 def __init__(self, sound_on):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51 global _SOUND_SYSTEM
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52 self.sound_enabled = False
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53 if _SOUND_SYSTEM is not None:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
54 # We've been initialised, so skip out
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
55 return
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
56 if sound_on:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
57 try:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
58 mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
59 test_sound = mixer.Sound(data.filepath('sounds/silence.ogg'))
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
60 test_sound.play()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
61 self.sound_enabled = True
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
62 except pygame.error:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
63 print 'Unable to enable sound. Continuing without sound'
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
64 self.sound_enabled = False
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
65 self._sounds = {}
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
66 _SOUND_SYSTEM = self
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
67
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
68 def play_background_music(self, track_name, volume):
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
69 """Play a looping track using pygame's music support"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
70 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
71 try:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
72 mixer.music.load(data.filepath(track_name))
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
73 mixer.music.play(-1) # Loop sound
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
74 mixer.music.set_volume(volume)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
75 except:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
76 print 'Unable to load track %s. Skipping' % track_name
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
77
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
78 def stop_music(self):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
79 """Stop music"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
80 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
81 mixer.music.stop()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
82
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
83 def load_sound(self, key, track_name, volume):
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
84 """Load sounds so we can play them later"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
85 if key in self._sounds:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
86 # Ignore multiple keys
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
87 return
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
88 if not self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
89 self._sounds[key] = None
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
90 else:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
91 try:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
92 self._sounds[key] = pygame.mixer.Sound(
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
93 data.filepath(track_name))
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
94 if volume is not None:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
95 self._sounds[key].set_volume(volume)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
96 except:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
97 print 'Unable to load sound %s. Skipping' % track_name
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
98 self._sounds[key] = None
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
99
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
100 def play_sound(self, key):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
101 """Play the sound"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
102 sound = self._sounds.get(key, None)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
103 if sound:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
104 sound.play()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
105
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
106 def stop_sounds(self):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
107 """Stop any playing sounds"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
108 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
109 mixer.stop()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
110
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
111 def pause_music(self):
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
112 """Pause music"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
113 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
114 mixer.music.pause()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
115
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
116 def resume_music(self):
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
117 """Resume paused music"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
118 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
119 mixer.music.unpause()