comparison demos/shooter/scripts/soundmanager.py @ 481:1f37adc9a685

Added a SoundEmitter class that encapsulates the FIFE sound emitter. The SoundManager is now a factory for SoundEmitters. Also working on getting a callback going for when the sound is finished playing.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 28 Apr 2010 21:33:11 +0000
parents 85fa5fa486a8
children 82d44c471959
comparison
equal deleted inserted replaced
480:85fa5fa486a8 481:1f37adc9a685
22 # #################################################################### 22 # ####################################################################
23 23
24 from fife import fife 24 from fife import fife
25 25
26 26
27 class SoundEmitter(object):
28 def __init__(self, fifeemitter, clipid, soundname ):
29 self._name = soundname
30 self._fifeemitter = fifeemitter
31 self._fifeclipid = clipid
32 self._gain = 255.0
33
34 def play(self, looping=False):
35 self._fifeemitter.stop()
36 self._fifeemitter.setGain(float(self._gain)/255.0)
37 self._fifeemitter.setLooping(looping)
38 self._fifeemitter.play()
39
40 def stop(self):
41 self._fifeemitter.stop()
42
43 def _getGain(self):
44 return self._gain
45
46 def _setGain(self, gain):
47 self._gain = float(gain)
48
49 def _getClipID(self):
50 return self._fifeclipid
51
52 def _getCallback(self):
53 return self._callback
54
55 def _setCallback(self, cb):
56 self._callback = cb
57
58 def _getFifeEmitter(self):
59 return self._fifeemitter
60
61 def _setFifeEmitter(self, emitter):
62 self._fifeemitter = emitter
63
64 clipid = property(_getClipID)
65 gain = property(_setGain, _getGain)
66 callback = property(_getCallback, _setCallback)
67 fifeemitter = property(_getFifeEmitter, _setFifeEmitter)
68
69
27 class SoundManager(object): 70 class SoundManager(object):
28 def __init__(self, engine): 71 def __init__(self, engine):
29 self._engine = engine 72 self._engine = engine
30 73
31 self._soundmanager = self._engine.getSoundManager() 74 self._soundmanager = self._engine.getSoundManager()
32 self._soundmanager.init() 75 self._soundmanager.init()
33 76
34 self._emitters = {} 77 self._emitters = list()
78 self._loadedclips = {}
35 79
36 def playSound(self, soundname, volume=255): 80 def createSoundEmitter(self, filename):
37 if soundname in self._emitters: 81 if not filename in self._loadedclips:
38 self._emitters[soundname].stop() 82 clipid = self._engine.getSoundClipPool().addResourceFromFile(filename)
39 self._emitters[soundname].play() 83 fifeemitter = self._soundmanager.createEmitter()
84 fifeemitter.setSoundClip(clipid)
85 self._loadedclips[filename] = clipid
86 emitter = SoundEmitter(fifeemitter, clipid, filename)
40 else: 87 else:
41 #create an emitter for this sound 88 fifeemitter = self._soundmanager.createEmitter()
42 emitter = self._soundmanager.createEmitter() 89 fifeemitter.setSoundClip(self._loadedclips[filename])
43 clipid = self._engine.getSoundClipPool().addResourceFromFile(soundname) 90 emitter = SoundEmitter(fifeemitter, self._loadedclips[filename], filename)
44 emitter.setSoundClip(clipid) 91
45 emitter.setLooping(False) 92 self._emitters.append(emitter)
46 93 return emitter
47 self._emitters[soundname] = emitter 94
48 self._emitters[soundname].thisown = 0 95 def stopAllSounds(self):
49 96 for emitter in self._emitters:
50 self._emitters[soundname].play() 97 emitter.stop()
51
52 def stopSound(self, soundname):
53 if soundname in self._emitters:
54 self._emitters[soundname].stop()
55 98
56 def destroy(self): 99 def destroy(self):
57 todelete = {}
58 for emitter in self._emitters: 100 for emitter in self._emitters:
59 self._emitters[emitter].stop() 101 emitter.stop()
60 clipid = self._emitters[emitter].getID() 102 self._soundmanager.releaseEmitter(emitter.fifeemitter.getID())
61 self._soundmanager.releaseEmitter(clipid) 103 emitter.fifeemitter = None
62 104
63 self._emitters.clear() 105 self._loadedclips.clear()