Mercurial > mamba
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 |
rev | line source |
---|---|
30 | 1 """Basic sound frame-work""" |
2 | |
3 from pygame import mixer | |
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 | 6 |
7 from mamba import data | |
8 from mamba.constants import FREQ, BITSIZE, CHANNELS, BUFFER | |
9 | |
10 # Global constant for holding the initialised sound system | |
11 _SOUND_SYSTEM = None | |
12 | |
13 | |
14 # Utility functions, used elsewhere | |
15 def load_sound(key, track_name, volume=None): | |
16 if _SOUND_SYSTEM is not None: | |
17 _SOUND_SYSTEM.load_sound(key, track_name, volume) | |
18 | |
19 | |
20 def play_sound(key): | |
21 if _SOUND_SYSTEM is not None: | |
22 _SOUND_SYSTEM.play_sound(key) | |
23 | |
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 | 49 class SoundSystem(object): |
50 | |
51 def __init__(self, sound_on): | |
52 global _SOUND_SYSTEM | |
53 self.sound_enabled = False | |
54 if _SOUND_SYSTEM is not None: | |
55 # We've been initialised, so skip out | |
56 return | |
57 if sound_on: | |
58 try: | |
59 mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER) | |
60 test_sound = mixer.Sound(data.filepath('sounds/silence.ogg')) | |
61 test_sound.play() | |
62 self.sound_enabled = True | |
63 except pygame.error: | |
64 print 'Unable to enable sound. Continuing without sound' | |
65 self.sound_enabled = False | |
66 self._sounds = {} | |
67 _SOUND_SYSTEM = self | |
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 | 70 """Play a looping track using pygame's music support""" |
71 if self.sound_enabled: | |
72 try: | |
73 mixer.music.load(data.filepath(track_name)) | |
74 mixer.music.play(-1) # Loop sound | |
75 mixer.music.set_volume(volume) | |
76 except: | |
77 print 'Unable to load track %s. Skipping' % track_name | |
78 | |
79 def stop_music(self): | |
80 """Stop music""" | |
81 if self.sound_enabled: | |
82 mixer.music.stop() | |
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 | 85 """Load sounds so we can play them later""" |
86 if key in self._sounds: | |
87 # Ignore multiple keys | |
88 return | |
89 if not self.sound_enabled: | |
90 self._sounds[key] = None | |
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 | 98 try: |
99 self._sounds[key] = pygame.mixer.Sound( | |
100 data.filepath(track_name)) | |
101 if volume is not None: | |
102 self._sounds[key].set_volume(volume) | |
103 except: | |
104 print 'Unable to load sound %s. Skipping' % track_name | |
105 self._sounds[key] = None | |
106 | |
107 def play_sound(self, key): | |
108 """Play the sound""" | |
109 sound = self._sounds.get(key, None) | |
110 if sound: | |
111 sound.play() | |
112 | |
113 def stop_sounds(self): | |
114 """Stop any playing sounds""" | |
115 if self.sound_enabled: | |
116 mixer.stop() | |
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 | 119 """Pause music""" |
120 if self.sound_enabled: | |
121 mixer.music.pause() | |
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 | 124 """Resume paused music""" |
125 if self.sound_enabled: | |
126 mixer.music.unpause() |