changeset 483:82d44c471959

Modified the SoundManager to not create unique FIFE sound emitters for each object. There is now only one FIFE emitter per unique sound. Added some sound effects.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 29 Apr 2010 16:09:56 +0000
parents 16c2b3ee59ce
children e584b0b8b4a2
files demos/shooter/scripts/powerups.py demos/shooter/scripts/scene.py demos/shooter/scripts/ships/enemies.py demos/shooter/scripts/ships/shipbase.py demos/shooter/scripts/soundmanager.py demos/shooter/scripts/weapons.py demos/shooter/sounds/bossexplode.ogg demos/shooter/sounds/cannon.ogg demos/shooter/sounds/explode.ogg demos/shooter/sounds/fireball.ogg demos/shooter/sounds/hit.ogg demos/shooter/sounds/pickup.ogg
diffstat 12 files changed, 103 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/demos/shooter/scripts/powerups.py	Thu Apr 29 13:51:45 2010 +0000
+++ b/demos/shooter/scripts/powerups.py	Thu Apr 29 16:09:56 2010 +0000
@@ -47,9 +47,12 @@
 		self._time = 1500
 		self._velocity.x = -0.25
 		self._velocity.y = 0
+		
+		self._pickupclip = self._scene.soundmanager.loadSoundClip("sounds/pickup.ogg")		
 	
 	def applyPowerUp(self, ship):
 		ship.weapon = CannonSpread5(self._scene, ship, 300)
+		self._scene.soundmanager.playClip(self._pickupclip)
 		self.destroy()
 		self._scene.queueObjectForRemoval(self)
 		
--- a/demos/shooter/scripts/scene.py	Thu Apr 29 13:51:45 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Thu Apr 29 16:09:56 2010 +0000
@@ -177,8 +177,9 @@
 		#and finally add the player to the scene
 		self.addObjectToScene(self._player)
 		
-		self._music = self._soundmanager.createSoundEmitter("music/waynesmind2.ogg")
-		self._music.play(True)
+		self._music = self._soundmanager.loadSoundClip("music/waynesmind2.ogg")
+		self._music.looping = True
+		self._soundmanager.playClip(self._music)
 		
 		self.startCamera()
 		
@@ -195,11 +196,11 @@
 		
 	def gameOver(self):
 		self._gameover = True
-		self._music.stop()		
+		self._soundmanager.stopClip(self._music)		
 		self._world.gameOver()
 		
 	def endLevel(self):
-		self._music.stop()
+		self._soundmanager.stopClip(self._music)
 		self._world.endLevel()
 		
 	def queueObjectForRemoval(self, obj):
@@ -383,7 +384,10 @@
 		
 	def _getPaused(self):
 		return self._paused
-		
+
+	def _getSoundManager(self):
+		return self._soundmanager
+	
 	player = property(_getPlayer)
 	keystate = property(_getKeyState)
 	camera = property(_getCamera)
@@ -391,4 +395,5 @@
 	model = property(_getModel)
 	time = property(_getTime)
 	timedelta = property(_getTimeDelta)
-	paused = property(_getPaused)	
+	paused = property(_getPaused)
+	soundmanager = property(_getSoundManager)
--- a/demos/shooter/scripts/ships/enemies.py	Thu Apr 29 13:51:45 2010 +0000
+++ b/demos/shooter/scripts/ships/enemies.py	Thu Apr 29 16:09:56 2010 +0000
@@ -211,6 +211,8 @@
 		self.hitpoints = 30
 		self.scorevalue = 1000
 		
+		self._explodclip = self._scene.soundmanager.loadSoundClip("sounds/bossexplode.ogg")
+		
 	def endLevel(self):
 		self._scene.endLevel()
 		
--- a/demos/shooter/scripts/ships/shipbase.py	Thu Apr 29 13:51:45 2010 +0000
+++ b/demos/shooter/scripts/ships/shipbase.py	Thu Apr 29 16:09:56 2010 +0000
@@ -58,6 +58,9 @@
 		
 		self._hitpoints = 0
 		self._scorevalue = 0
+		
+		self._hitclip = self._scene.soundmanager.loadSoundClip("sounds/hit.ogg")
+		self._explodclip = self._scene.soundmanager.loadSoundClip("sounds/explode.ogg")
 	
 	def _setWeapon(self, weapon):
 		self._weapon = weapon
@@ -81,10 +84,13 @@
 		self._hitpoints -= hp
 		if self._hitpoints <= 0:
 			self.destroy()
+		else:
+			self._scene.soundmanager.playClip(self._hitclip)
 		
 	def destroy(self):
 		if self._running:
 			self._instance.act('explode', self._instance.getFacingLocation())
+			self._scene.soundmanager.playClip(self._explodclip)
 			super(Ship, self).destroy()
 	
 	def _getHitPoints(self):
--- a/demos/shooter/scripts/soundmanager.py	Thu Apr 29 13:51:45 2010 +0000
+++ b/demos/shooter/scripts/soundmanager.py	Thu Apr 29 16:09:56 2010 +0000
@@ -23,49 +23,47 @@
 
 from fife import fife
 
-
-class SoundEmitter(object):
-	def __init__(self, fifeemitter, clipid, soundname ):
+class SoundClip(object):
+	def __init__(self, soundmanager, clipid, soundname, emitter):
+		self._soundmanager = soundmanager
 		self._name = soundname
-		self._fifeemitter = fifeemitter
 		self._fifeclipid = clipid
+		self._fifeemitter = emitter
+		self._fifeemitter.thisown = 0
 		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()
+		self._looping = False
 		
-	def stop(self):
-		self._fifeemitter.stop()
-		
+	def _getClipID(self):
+		return self._fifeclipid
+
 	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 _getLooping(self):
+		return self._looping
 		
-	def _setCallback(self, cb):
-		self._callback = cb
+	def _setLooping(self, looping):
+		self._looping = looping
 		
 	def _getFifeEmitter(self):
 		return self._fifeemitter
 		
 	def _setFifeEmitter(self, emitter):
 		self._fifeemitter = emitter
-	
+		if self._fifeemitter:
+			self._fifeemitter.thisown = 0
+
+	def _getName(self):
+		return self._name
+
 	clipid = property(_getClipID)
-	gain = property(_setGain, _getGain)
-	callback = property(_getCallback, _setCallback)
+	gain = property(_getGain, _setGain)
+	looping = property(_getLooping, _setLooping)
 	fifeemitter = property(_getFifeEmitter, _setFifeEmitter)
-	
+	name = property(_getName)
 
 class SoundManager(object):
 	def __init__(self, engine):
@@ -74,32 +72,51 @@
 		self._soundmanager = self._engine.getSoundManager()
 		self._soundmanager.init()
 		
-		self._emitters = list()
+		self._emitters = []
 		self._loadedclips = {}
-
-	def createSoundEmitter(self, filename):
-		if not filename in self._loadedclips:
+		
+	def loadSoundClip(self, filename):
+		if not self._loadedclips.has_key(filename):
 			clipid = self._engine.getSoundClipPool().addResourceFromFile(filename)
 			fifeemitter = self._soundmanager.createEmitter()
+			fifeemitter.thisown = 0
 			fifeemitter.setSoundClip(clipid)
-			self._loadedclips[filename] = clipid
-			emitter = SoundEmitter(fifeemitter, clipid, filename)
+			self._loadedclips[filename] = SoundClip(self, clipid, filename, fifeemitter)
+			self._emitters.append(fifeemitter)
+		
+		return self._loadedclips[filename]
+		
+	def playClip(self, clip):
+		if clip.fifeemitter:
+			clip.fifeemitter.setGain(clip.gain)
+			clip.fifeemitter.setLooping(clip.looping)
+			clip.fifeemitter.play()	
 		else:
-			fifeemitter = self._soundmanager.createEmitter()
-			fifeemitter.setSoundClip(self._loadedclips[filename])
-			emitter = SoundEmitter(fifeemitter, self._loadedclips[filename], filename)
-		
-		self._emitters.append(emitter)
-		return emitter
+			self.loadSoundClip(clip.name)
+				
+	def stopClip(self, clip):
+		if clip.fifeemitter:
+			clip.fifeemitter.stop()
+		else:
+			self.loadSoundClip(clip.name)
 
 	def stopAllSounds(self):
 		for emitter in self._emitters:
 			emitter.stop()
 			
 	def destroy(self):
-		for emitter in self._emitters:
+		print "Destroying " + str(len(self._emitters)) + " sound emitters!\n"
+		print "There are " + str(len(self._loadedclips)) + " unique sounds!\n"
+	
+		for emitter in self._emitters[:]:
 			emitter.stop()
-			self._soundmanager.releaseEmitter(emitter.fifeemitter.getID())
-			emitter.fifeemitter = None			
-
-		self._loadedclips.clear()
\ No newline at end of file
+			self._soundmanager.releaseEmitter(emitter.getID())
+			self._emitters.remove(emitter)
+		
+		for clip in self._loadedclips.values():
+			clip.fifeemitter = None
+		
+			
+		self._emitters = list()
+		self._loadedclips.clear()
+		
\ No newline at end of file
--- a/demos/shooter/scripts/weapons.py	Thu Apr 29 13:51:45 2010 +0000
+++ b/demos/shooter/scripts/weapons.py	Thu Apr 29 16:09:56 2010 +0000
@@ -24,6 +24,7 @@
 from fife import fife
 from scripts.common.baseobject import *
 from scripts.common.helpers import normalize, rotatePoint
+from scripts.soundmanager import *
 
 class Projectile(SpaceObject):
 	def __init__(self, scene, owner, projectileName, timeToLive):
@@ -92,9 +93,10 @@
 		self._firerate = firerate
 		self._lastfired = 0
 		self._projectileVelocity = 0.75
+		self._soundclip = None
 		
 	def fire(self, direction):
-		return None
+		pass
 		
 	def _getProjectileVelocity(self):
 		return self._projectileVelocity
@@ -116,6 +118,7 @@
 		super(Cannon, self).__init__(scene, ship, firerate)
 		
 		self._projectileVelocity = 0.75
+		self._soundclip = scene.soundmanager.loadSoundClip("sounds/cannon.ogg")
 
 		
 	def fire(self, direction):
@@ -128,14 +131,16 @@
 			pjctl.run(velocity, self._ship.location)
 			self._lastfired = self._scene.time
 			self._scene.addObjectToScene(pjctl)
+			if self._soundclip:
+				self._scene.soundmanager.playClip(self._soundclip)
 
 class FireBall(Weapon):
 	def __init__(self, scene, ship, firerate):
 		super(FireBall, self).__init__(scene, ship, firerate)
 		
 		self._projectileVelocity = 0.50
+		self._soundclip = scene.soundmanager.loadSoundClip("sounds/fireball.ogg")		
 
-		
 	def fire(self, direction):
 		velocity = normalize(direction)
 		velocity.x = velocity.x * self._projectileVelocity
@@ -146,13 +151,16 @@
 			pjctl.run(velocity, self._ship.location)
 			self._lastfired = self._scene.time
 			self._scene.addObjectToScene(pjctl)	
+			if self._soundclip:
+				self._scene.soundmanager.playClip(self._soundclip)
 			
 class FireBallBurst(Weapon):
 	def __init__(self, scene, ship, firerate, burstrate, burstnumber):
 		super(FireBallBurst, self).__init__(scene, ship, firerate)
 		
 		self._projectileVelocity = 0.50
-		
+		self._soundclip = scene.soundmanager.loadSoundClip("sounds/fireball.ogg")	
+
 		self._burstrate = burstrate
 		self._burstnumber = int(burstnumber)
 		self._burstcount = int(burstnumber)
@@ -170,6 +178,9 @@
 				pjctl = Projectile(self._scene, self._ship, "fireball", 6000 )
 				pjctl.run(velocity, self._ship.location)
 				self._scene.addObjectToScene(pjctl)
+			
+				if self._soundclip:
+					self._scene.soundmanager.playClip(self._soundclip)		
 				
 				self._lastburstfired = self._scene.time
 				self._burstcount -= 1
@@ -179,12 +190,13 @@
 				self._burstcount = int(self._burstnumber)
 				self._lastburstfired = 0
 				
+				
 class FireBallSpread(Weapon):
 	def __init__(self, scene, ship, firerate):
 		super(FireBallSpread, self).__init__(scene, ship, firerate)
 		
 		self._projectileVelocity = 0.50
-
+		self._soundclip = scene.soundmanager.loadSoundClip("sounds/fireball.ogg")
 		
 	def fire(self, direction):
 	
@@ -231,6 +243,9 @@
 			pjctl7.run(p7, self._ship.location)
 			self._scene.addObjectToScene(pjctl7)
 			
+			if self._soundclip:
+				self._scene.soundmanager.playClip(self._soundclip)
+
 			self._lastfired = self._scene.time
 			
 class CannonSpread5(Weapon):
@@ -238,7 +253,8 @@
 		super(CannonSpread5, self).__init__(scene, ship, firerate)
 		
 		self._projectileVelocity = 1
-		
+		self._soundclip = scene.soundmanager.loadSoundClip("sounds/cannon.ogg")
+
 	def fire(self, direction):
 	
 		if (self._scene.time - self._lastfired) > self._firerate:
@@ -274,6 +290,8 @@
 			pjctl6.run(p6, self._ship.location)
 			self._scene.addObjectToScene(pjctl6)
 			
+			if self._soundclip:
+				self._scene.soundmanager.playClip(self._soundclip)
 			
 			self._lastfired = self._scene.time
 
Binary file demos/shooter/sounds/bossexplode.ogg has changed
Binary file demos/shooter/sounds/cannon.ogg has changed
Binary file demos/shooter/sounds/explode.ogg has changed
Binary file demos/shooter/sounds/fireball.ogg has changed
Binary file demos/shooter/sounds/hit.ogg has changed
Binary file demos/shooter/sounds/pickup.ogg has changed