annotate mamba/sound.py @ 285:cacc03748580

Move world initialisation to restart
author Stefano Rivera <stefano@rivera.za.net>
date Thu, 15 Sep 2011 23:02:47 +0200
parents cad653e4f59c
children cd544a311f11
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
280
cad653e4f59c pygame.mixer.Sound, I mildly dislikes you, I does
Neil Muller <drnlmuller@gmail.com>
parents: 36
diff changeset
5 import os.path
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7 from mamba import data
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
8 from mamba.constants import FREQ, BITSIZE, CHANNELS, BUFFER
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
10 # Global constant for holding the initialised sound system
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
11 _SOUND_SYSTEM = None
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
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14 # Utility functions, used elsewhere
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
15 def load_sound(key, track_name, volume=None):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
16 if _SOUND_SYSTEM is not None:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
17 _SOUND_SYSTEM.load_sound(key, track_name, volume)
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
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 def play_sound(key):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 if _SOUND_SYSTEM is not None:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22 _SOUND_SYSTEM.play_sound(key)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
24
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
25 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
26 if _SOUND_SYSTEM is not None:
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
27 _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
28
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
29
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
30 def stop_sound():
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
31 """Stop sounds and music"""
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
32 if _SOUND_SYSTEM is not None:
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
33 _SOUND_SYSTEM.stop_sounds()
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
34 _SOUND_SYSTEM.stop_music()
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
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
37 def pause_music():
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
38 """Pause backgroun music"""
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
39 if _SOUND_SYSTEM is not None:
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
40 _SOUND_SYSTEM.pause_music()
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
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
43 def resume_music():
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
44 """Unpause music"""
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
45 if _SOUND_SYSTEM is not None:
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
46 _SOUND_SYSTEM.resume_music()
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
47
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
48
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49 class SoundSystem(object):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51 def __init__(self, sound_on):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52 global _SOUND_SYSTEM
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53 self.sound_enabled = False
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
54 if _SOUND_SYSTEM is not None:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
55 # We've been initialised, so skip out
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
56 return
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
57 if sound_on:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
58 try:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
59 mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
60 test_sound = mixer.Sound(data.filepath('sounds/silence.ogg'))
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
61 test_sound.play()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
62 self.sound_enabled = True
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
63 except pygame.error:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
64 print 'Unable to enable sound. Continuing without sound'
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
65 self.sound_enabled = False
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
66 self._sounds = {}
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
67 _SOUND_SYSTEM = self
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
68
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
69 def play_background_music(self, track_name, volume):
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
70 """Play a looping track using pygame's music support"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
71 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
72 try:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
73 mixer.music.load(data.filepath(track_name))
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
74 mixer.music.play(-1) # Loop sound
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
75 mixer.music.set_volume(volume)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
76 except:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
77 print 'Unable to load track %s. Skipping' % track_name
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
78
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
79 def stop_music(self):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
80 """Stop music"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
81 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
82 mixer.music.stop()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
83
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
84 def load_sound(self, key, track_name, volume):
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
85 """Load sounds so we can play them later"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
86 if key in self._sounds:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
87 # Ignore multiple keys
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
88 return
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
89 if not self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
90 self._sounds[key] = None
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
91 else:
280
cad653e4f59c pygame.mixer.Sound, I mildly dislikes you, I does
Neil Muller <drnlmuller@gmail.com>
parents: 36
diff changeset
92 if not os.path.exists(data.filepath(track_name)):
cad653e4f59c pygame.mixer.Sound, I mildly dislikes you, I does
Neil Muller <drnlmuller@gmail.com>
parents: 36
diff changeset
93 # Pygame doesn't error out for missing files, so
cad653e4f59c pygame.mixer.Sound, I mildly dislikes you, I does
Neil Muller <drnlmuller@gmail.com>
parents: 36
diff changeset
94 # handle that ourselves
cad653e4f59c pygame.mixer.Sound, I mildly dislikes you, I does
Neil Muller <drnlmuller@gmail.com>
parents: 36
diff changeset
95 print "Can't find sound %s. Skipping" % track_name
cad653e4f59c pygame.mixer.Sound, I mildly dislikes you, I does
Neil Muller <drnlmuller@gmail.com>
parents: 36
diff changeset
96 self._sounds[key] = None
cad653e4f59c pygame.mixer.Sound, I mildly dislikes you, I does
Neil Muller <drnlmuller@gmail.com>
parents: 36
diff changeset
97 return
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
98 try:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
99 self._sounds[key] = pygame.mixer.Sound(
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
100 data.filepath(track_name))
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
101 if volume is not None:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
102 self._sounds[key].set_volume(volume)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
103 except:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
104 print 'Unable to load sound %s. Skipping' % track_name
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
105 self._sounds[key] = None
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
106
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
107 def play_sound(self, key):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
108 """Play the sound"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
109 sound = self._sounds.get(key, None)
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
110 if sound:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
111 sound.play()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
112
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
113 def stop_sounds(self):
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
114 """Stop any playing sounds"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
115 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
116 mixer.stop()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
117
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
118 def pause_music(self):
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
119 """Pause music"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
120 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
121 mixer.music.pause()
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
122
36
908c9d5597f9 Provide more utility functions in sound.py
Neil Muller <drnlmuller@gmail.com>
parents: 30
diff changeset
123 def resume_music(self):
30
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
124 """Resume paused music"""
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
125 if self.sound_enabled:
cccf1675731c Add sound framework
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
126 mixer.music.unpause()