changeset 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 16c2b3ee59ce
files demos/shooter/scripts/scene.py demos/shooter/scripts/soundmanager.py
diffstat 2 files changed, 72 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/demos/shooter/scripts/scene.py	Wed Apr 28 17:28:29 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Wed Apr 28 21:33:11 2010 +0000
@@ -69,6 +69,8 @@
 		self._timemod = 0
 		
 		self._gameover = False
+		
+		self._music = None
 
 	def destroyScene(self):
 		nodestodelete = list()
@@ -175,7 +177,8 @@
 		#and finally add the player to the scene
 		self.addObjectToScene(self._player)
 		
-		self._soundmanager.playSound("music/waynesmind2.ogg")
+		self._music = self._soundmanager.createSoundEmitter("music/waynesmind2.ogg")
+		self._music.play(True)
 		
 		self.startCamera()
 		
@@ -192,11 +195,11 @@
 		
 	def gameOver(self):
 		self._gameover = True
-		self._soundmanager.stopSound("music/waynesmind2.ogg")		
+		self._music.stop()		
 		self._world.gameOver()
 		
 	def endLevel(self):
-		self._soundmanager.stopSound("music/waynesmind2.ogg")
+		self._music.stop()
 		self._world.endLevel()
 		
 	def queueObjectForRemoval(self, obj):
--- a/demos/shooter/scripts/soundmanager.py	Wed Apr 28 17:28:29 2010 +0000
+++ b/demos/shooter/scripts/soundmanager.py	Wed Apr 28 21:33:11 2010 +0000
@@ -24,6 +24,49 @@
 from fife import fife
 
 
+class SoundEmitter(object):
+	def __init__(self, fifeemitter, clipid, soundname ):
+		self._name = soundname
+		self._fifeemitter = fifeemitter
+		self._fifeclipid = clipid
+		self._gain = 255.0
+	
+	def play(self, looping=False):
+		self._fifeemitter.stop()
+		self._fifeemitter.setGain(float(self._gain)/255.0)
+		self._fifeemitter.setLooping(looping)
+		self._fifeemitter.play()
+		
+	def stop(self):
+		self._fifeemitter.stop()
+		
+	def _getGain(self):
+		return self._gain
+		
+	def _setGain(self, gain):
+		self._gain = float(gain)
+		
+	def _getClipID(self):
+		return self._fifeclipid
+	
+	def _getCallback(self):
+		return self._callback
+		
+	def _setCallback(self, cb):
+		self._callback = cb
+		
+	def _getFifeEmitter(self):
+		return self._fifeemitter
+		
+	def _setFifeEmitter(self, emitter):
+		self._fifeemitter = emitter
+	
+	clipid = property(_getClipID)
+	gain = property(_setGain, _getGain)
+	callback = property(_getCallback, _setCallback)
+	fifeemitter = property(_getFifeEmitter, _setFifeEmitter)
+	
+
 class SoundManager(object):
 	def __init__(self, engine):
 		self._engine = engine
@@ -31,33 +74,32 @@
 		self._soundmanager = self._engine.getSoundManager()
 		self._soundmanager.init()
 		
-		self._emitters = {}
+		self._emitters = list()
+		self._loadedclips = {}
 
-	def playSound(self, soundname, volume=255):
-		if soundname in self._emitters:
-			self._emitters[soundname].stop()
-			self._emitters[soundname].play()
+	def createSoundEmitter(self, filename):
+		if not filename in self._loadedclips:
+			clipid = self._engine.getSoundClipPool().addResourceFromFile(filename)
+			fifeemitter = self._soundmanager.createEmitter()
+			fifeemitter.setSoundClip(clipid)
+			self._loadedclips[filename] = clipid
+			emitter = SoundEmitter(fifeemitter, clipid, filename)
 		else:
-			#create an emitter for this sound
-			emitter = self._soundmanager.createEmitter()
-			clipid = self._engine.getSoundClipPool().addResourceFromFile(soundname)
-			emitter.setSoundClip(clipid)
-			emitter.setLooping(False)
-			
-			self._emitters[soundname] = emitter
-			self._emitters[soundname].thisown = 0
-			
-			self._emitters[soundname].play()
-			
-	def stopSound(self, soundname):
-		if soundname in self._emitters:
-			self._emitters[soundname].stop()
+			fifeemitter = self._soundmanager.createEmitter()
+			fifeemitter.setSoundClip(self._loadedclips[filename])
+			emitter = SoundEmitter(fifeemitter, self._loadedclips[filename], filename)
+		
+		self._emitters.append(emitter)
+		return emitter
+
+	def stopAllSounds(self):
+		for emitter in self._emitters:
+			emitter.stop()
 			
 	def destroy(self):
-		todelete = {}
 		for emitter in self._emitters:
-			self._emitters[emitter].stop()
-			clipid = self._emitters[emitter].getID()
-			self._soundmanager.releaseEmitter(clipid)
+			emitter.stop()
+			self._soundmanager.releaseEmitter(emitter.fifeemitter.getID())
+			emitter.fifeemitter = None			
 
-		self._emitters.clear()
\ No newline at end of file
+		self._loadedclips.clear()
\ No newline at end of file