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