Mercurial > fife-parpg
comparison demos/shooter/scripts/soundmanager.py @ 490:939a4dc12ca1
Starting to add some comments.
Cleaned up some old commented out code.
The SoundManager now creates a new SoundClip rather than only creating one per FIFE SoundEmitter.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Wed, 05 May 2010 21:39:31 +0000 |
parents | 0324a3533988 |
children | c4168eb47a44 |
comparison
equal
deleted
inserted
replaced
489:0324a3533988 | 490:939a4dc12ca1 |
---|---|
25 | 25 |
26 from scripts.common.helpers import Timer | 26 from scripts.common.helpers import Timer |
27 from fife.extensions.pychan.tools import callbackWithArguments as cbwa | 27 from fife.extensions.pychan.tools import callbackWithArguments as cbwa |
28 | 28 |
29 class SoundClip(object): | 29 class SoundClip(object): |
30 """ | |
31 SoundClip | |
32 | |
33 This class stores an instance of a FIFE SoundEmitter class along | |
34 with some information about a sound clip (like gain and if its | |
35 looping). All instances of SoundClip should be created by SoundManager. | |
36 """ | |
30 def __init__(self, soundmanager, clipid, soundname, emitter): | 37 def __init__(self, soundmanager, clipid, soundname, emitter): |
31 self._soundmanager = soundmanager | 38 self._soundmanager = soundmanager |
32 self._name = soundname | 39 self._name = soundname |
33 self._fifeclipid = clipid | 40 self._fifeclipid = clipid |
34 self._fifeemitter = emitter | 41 self._fifeemitter = emitter |
91 name = property(_getName) | 98 name = property(_getName) |
92 callback = property(_getCallback, _setCallback) | 99 callback = property(_getCallback, _setCallback) |
93 duration = property(_getDuration, _setDuration) | 100 duration = property(_getDuration, _setDuration) |
94 | 101 |
95 class SoundManager(object): | 102 class SoundManager(object): |
103 """ | |
104 SoundManger | |
105 | |
106 This class manages and plays all the sounds of the game. | |
107 It creates SoundClips and ensures that there is only one | |
108 FIFE SoundEmitter per unique sound. | |
109 """ | |
96 def __init__(self, engine): | 110 def __init__(self, engine): |
97 self._engine = engine | 111 self._engine = engine |
98 | 112 |
99 self._fifesoundmanager = self._engine.getSoundManager() | 113 self._fifesoundmanager = self._engine.getSoundManager() |
100 self._fifesoundmanager.init() | 114 self._fifesoundmanager.init() |
101 | 115 |
102 self._emitters = [] | |
103 self._loadedclips = {} | 116 self._loadedclips = {} |
104 | 117 |
105 def loadSoundClip(self, filename): | 118 def loadSoundClip(self, filename): |
119 """ | |
120 Returns a valid SoundClip instance. | |
121 """ | |
106 if not self._loadedclips.has_key(filename): | 122 if not self._loadedclips.has_key(filename): |
107 clipid = self._engine.getSoundClipPool().addResourceFromFile(filename) | 123 clipid = self._engine.getSoundClipPool().addResourceFromFile(filename) |
108 fifeemitter = self._fifesoundmanager.createEmitter() | 124 fifeemitter = self._fifesoundmanager.createEmitter() |
109 fifeemitter.thisown = 0 | 125 fifeemitter.thisown = 0 |
110 fifeemitter.setSoundClip(clipid) | 126 fifeemitter.setSoundClip(clipid) |
111 | 127 |
112 time = fifeemitter.getDuration() | 128 self._loadedclips[filename] = fifeemitter |
113 | 129 clip = SoundClip(self, clipid, filename, fifeemitter) |
114 self._loadedclips[filename] = SoundClip(self, clipid, filename, fifeemitter) | 130 clip.duration = fifeemitter.getDuration() |
115 self._loadedclips[filename].duration = time | 131 else: |
116 self._emitters.append(fifeemitter) | 132 fifeemitter = self._loadedclips[filename] |
133 clip = SoundClip(self, fifeemitter.getID(), filename, fifeemitter) | |
134 clip.duration = fifeemitter.getDuration() | |
117 | 135 |
118 return self._loadedclips[filename] | 136 return clip |
119 | 137 |
120 def playClip(self, clip): | 138 def playClip(self, clip): |
121 if clip.fifeemitter: | 139 if clip.fifeemitter: |
122 if clip.callback: | 140 if clip.callback: |
123 if clip.timer: | 141 if clip.timer: |
124 clip.timer.stop() | 142 clip.timer.stop() |
125 timer = None | 143 timer = None |
126 | 144 |
127 if clip.looping: | 145 if clip.looping: |
128 repeat = 0 | 146 repeat = 0 |
129 def real_callback(c, e): | 147 def real_callback(c, e, g): |
130 c() | 148 c() |
131 e.stop() | 149 e.stop() |
150 e.setGain(g) | |
132 e.play() | 151 e.play() |
133 | 152 |
134 clip.callback = cbwa(real_callback, clip.callback, clip.fifeemitter) | 153 clip.callback = cbwa(real_callback, clip.callback, clip.fifeemitter, clip.gain) |
135 | 154 |
136 else: | 155 else: |
137 repeat = 1 | 156 repeat = 1 |
138 | 157 |
139 clip.timer = Timer(self._engine.getTimeManager(), clip.duration, clip.callback, repeat) | 158 clip.timer = Timer(self._engine.getTimeManager(), clip.duration, clip.callback, repeat) |
140 clip.timer.start() | 159 clip.timer.start() |
141 | 160 |
142 clip.fifeemitter.setGain(clip.gain) | 161 clip.fifeemitter.setGain(clip.gain) |
143 #clip.fifeemitter.setLooping(clip.looping) | |
144 clip.fifeemitter.play() | 162 clip.fifeemitter.play() |
145 else: | 163 else: |
146 clip = self.loadSoundClip(clip.name) | 164 clip = self.loadSoundClip(clip.name) |
147 self.playClip(clip) | 165 self.playClip(clip) |
148 | 166 |
153 if clip.timer: | 171 if clip.timer: |
154 clip.timer.stop() | 172 clip.timer.stop() |
155 clip.timer = None | 173 clip.timer = None |
156 | 174 |
157 def stopAllSounds(self): | 175 def stopAllSounds(self): |
158 for clip in self._loadedclips.values(): | 176 for emitter in self._loadedclips.values(): |
159 self.stopClip(clip) | 177 emitter.stop() |
160 | 178 |
161 def destroy(self): | 179 def destroy(self): |
162 self.stopAllSounds() | 180 self.stopAllSounds() |
163 | 181 |
164 for emitter in self._emitters[:]: | 182 for emitter in self._loadedclips.values(): |
165 self._fifesoundmanager.releaseEmitter(emitter.getID()) | 183 self._fifesoundmanager.releaseEmitter(emitter.getID()) |
166 self._emitters.remove(emitter) | 184 emitter = None |
167 | 185 |
168 for clip in self._loadedclips.values(): | |
169 clip.fifeemitter = None | |
170 | |
171 | |
172 self._emitters = list() | |
173 self._loadedclips.clear() | 186 self._loadedclips.clear() |