changeset 173:454ed60ad7b2

* Added a setRoleoff() function to the SoundEmitter class This function sets AL_ROLEOFF_FACTOR. Rolloff factor judges the strength of attenuation over distance. It effect's the volume of the emitter over distance to the listener. You'll have to do plenty of testing to find a good value.
author nihathrael@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 24 Jan 2009 22:26:46 +0000
parents 3fe0e68f4269
children 362fe23920b9
files engine/core/audio/soundemitter.cpp engine/core/audio/soundemitter.h engine/core/audio/soundemitter.i
diffstat 3 files changed, 73 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/engine/core/audio/soundemitter.cpp	Thu Jan 22 15:35:22 2009 +0000
+++ b/engine/core/audio/soundemitter.cpp	Sat Jan 24 22:26:46 2009 +0000
@@ -38,7 +38,7 @@
 
 namespace FIFE {
 	static Logger _log(LM_AUDIO);
-	
+
 	SoundEmitter::SoundEmitter(SoundManager* manager, SoundClipPool* pool, unsigned int uid) : m_manager(manager), m_pool(pool), m_source(0), m_soundclip(NULL), m_soundclipid(0), m_streamid(0),
 															m_emitterid(uid), m_loop(false) {
 		if (!m_manager->isActive()) {
@@ -48,10 +48,9 @@
 		TimeManager::instance()->registerEvent(this);
 		setPeriod(-1);
 		alGenSources(1, &m_source);
-
 		CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error creating source")
 	}
-		
+
 	SoundEmitter::~SoundEmitter() {
 		if (!m_manager->isActive()) {
 			return;
@@ -62,25 +61,25 @@
 		reset();
 		alDeleteSources(1, &m_source);
 	}
-	
+
 	void SoundEmitter::reset(bool defaultall) {
 		if (m_soundclip != NULL) {
-			
+
 			setPeriod(-1);
 			alSourceStop(m_source);
-			
+
 			// Release all buffers
 			alSourcei(m_source, AL_BUFFER, AL_NONE);
 			alGetError();
-			
+
 			if (m_soundclip->isStream()) {
 				m_soundclip->quitStreaming(m_streamid);
 			}
-			
+
 			// release the soundclip
 			m_pool->release(m_soundclipid, true);
 			m_soundclip = NULL;
-			
+
 			// default source properties
 			if (defaultall) {
 				setPosition(0.0f, 0.0f, 0.0f);
@@ -95,12 +94,12 @@
 	void SoundEmitter::release() {
 		m_manager->releaseEmitter(m_emitterid);
 	}
-	
+
 	void SoundEmitter::setSoundClip(unsigned int sound_id) {
 		m_soundclipid = sound_id;
 		m_soundclip = &(m_pool->getSoundClip(m_soundclipid));
 		m_soundclip->addRef();
-		
+
 		attachSoundClip();
 	}
 
@@ -109,12 +108,12 @@
 			// non-streaming
 			alSourceQueueBuffers(m_source, m_soundclip->countBuffers(), m_soundclip->getBuffers());
 			alSourcei(m_source, AL_LOOPING, m_loop ? AL_TRUE : AL_FALSE);
-			
+
 		} else {
 			// streaming
 			m_streamid = m_soundclip->beginStreaming();
 			m_soundclip->acquireStream(m_streamid);
-			
+
 			// queue initial buffers
 			alSourceQueueBuffers(m_source, BUFFER_NUM, m_soundclip->getBuffers(m_streamid));
 			alSourcei(m_source, AL_LOOPING, AL_FALSE);
@@ -122,17 +121,17 @@
 
 		CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error attaching sound clip")
 	}
-	
+
 	void SoundEmitter::updateEvent(unsigned long time) {
 		ALint procs;
 		ALint bufs;
 		ALuint buffer;
-		
+
 		alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &procs);
-		
+
 		while (procs--) {
 			alSourceUnqueueBuffers(m_source, 1, &buffer);
-			
+
 			if (m_soundclip->getStream(m_streamid, buffer)) {
 				// EOF!
 				if (m_loop) {
@@ -140,7 +139,7 @@
 					m_soundclip->setStreamPos(m_streamid, SD_BYTE_POS, 0);
 					m_soundclip->getStream(m_streamid, buffer);
 				} else {
-					
+
 					// check if the playback has been finished
 					alGetSourcei(m_source, AL_BUFFERS_QUEUED, &bufs);
 					if (bufs == 0) {
@@ -155,7 +154,7 @@
 
 		CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error while streaming")
 	}
-	
+
 	void SoundEmitter::setLooping(bool loop) {
 		if (m_soundclip) {
 			if (!m_soundclip->isStream()) {
@@ -166,7 +165,7 @@
 		}
 		m_loop = loop;
 	}
-	
+
 	void SoundEmitter::play() {
 		if (m_soundclip) {
 			alSourcePlay(m_source);
@@ -175,11 +174,11 @@
 			}
 		}
 	}
-	
+
 	void SoundEmitter::stop() {
 		if (m_soundclip) {
 			alSourceStop(m_source);
-			
+
 			if (m_soundclip->isStream()) {
 				setPeriod(-1);
 				setCursor(SD_BYTE_POS, 0);
@@ -193,9 +192,9 @@
 		if (!m_soundclip) {
 			return;
 		}
-		
+
 		ALint state = 0;
-		
+
 		if (!m_soundclip->isStream()) {
 			switch(type) {
 			case SD_BYTE_POS:
@@ -213,21 +212,21 @@
 		}
 		else {
 			alGetSourcei(m_source, AL_SOURCE_STATE, &state);
-			
+
 			if (state == AL_PLAYING || AL_PAUSED) {
 				setPeriod(-1);
 				alSourceStop(m_source);
 			}
-			
+
 			m_soundclip->setStreamPos(m_streamid, type, value);
-			
+
 			// detach all buffers
 			alSourcei(m_source, AL_BUFFER, 0);
 
 			// queue the buffers with new data
 			m_soundclip->acquireStream(m_streamid);
 			alSourceQueueBuffers(m_source, BUFFER_NUM, m_soundclip->getBuffers(m_streamid));
-			
+
 			if (state == AL_PLAYING) {
 				setPeriod(5000);
 				alSourcePlay(m_source);
@@ -236,14 +235,14 @@
 			CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error setting stream cursor position")
 		}
 	}
-	
+
 	float SoundEmitter::getCursor(SoundPositionType type) {
 		if (!m_soundclip) {
 			return 0.0f;
 		}
-		
+
 		ALfloat pos = 0.0f;
-		
+
 		switch(type) {
 			case SD_BYTE_POS:
 				alGetSourcef(m_source, AL_BYTE_OFFSET, &pos);
@@ -255,13 +254,13 @@
 				alGetSourcef(m_source, AL_SEC_OFFSET, &pos);
 				break;
 		}
-		
+
 		if (m_soundclip->isStream()) {
 			pos += m_soundclip->getStreamPos(m_streamid, type);
 		}
 
 		CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error getting cursor")
-		
+
 		return pos;
 	}
 }
--- a/engine/core/audio/soundemitter.h	Thu Jan 22 15:35:22 2009 +0000
+++ b/engine/core/audio/soundemitter.h	Sat Jan 24 22:26:46 2009 +0000
@@ -18,7 +18,7 @@
  *   Free Software Foundation, Inc.,                                       *
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
  ***************************************************************************/
- 
+
 #ifndef FIFE_SOUNDEMITTER_H_
 #define FIFE_SOUNDEMITTER_H_
 
@@ -41,38 +41,46 @@
 
 	class SoundManager;
 	class SoundClipPool;
-	
+
 	/** The class for playing audio files
 	 */
 	class SoundEmitter : private TimeEvent {
 	public:
 		SoundEmitter(SoundManager* manager, SoundClipPool* pool, unsigned int uid);
-		
+
 		~SoundEmitter();
-		
+
 		/** Returns the emitter-id
 		 */
 		unsigned int getID() {
 			return m_emitterid;
 		}
-		
+
 		/** Sets Positioning-Type
 		 * Default is false
-		 * 
+		 *
 		 * @param relative If set to true, the emitters position will be interpreted relative to the listener object
-		 * 
+		 *
 		 */
 		void setPositioning(bool relative) {
 			alSourcei(m_source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
 		}
 
+		/** Sets the AL_ROLEOFF_FACTOR. Rolloff factor judges the strength of attenuation over distance.
+		 *
+		 * @param roleoff Roleoff factor. You'll need to do a lot of testing to find a value which suits your needs.
+		 */
+		void setRoleoff(float roleoff) {
+			alSourcef (m_source, AL_ROLLOFF_FACTOR,  roleoff);
+		}
+
 		/** Sets the sound clip to be used by this emitter.
 		 * @param sound_id SoundClipPool id of the sound to be used.
 		 */
 		void setSoundClip(unsigned int sound_id);
-		
+
 		/** Reset the emitter, free all internal buffers
-		 *  
+		 *
 		 * @param defaultall If set to true, emitter position, velocity, gain and type will be set to the default values
 		 */
 		void reset(bool defaultall = false);
@@ -80,19 +88,19 @@
 		/** Releases the emitter
 		 */
 		void release();
-		
+
 		/** Sets the playing mode
 		 */
 		void setLooping(bool loop);
-		
+
 		/** Plays the associated audio file.
 		 */
 		void play();
-		
+
 		/** Stops playing the audio file and rewinds to the beginning
 		 */
 		void stop();
-		
+
 		/** Pauses playing the audio file
 		 */
 		void pause() {
@@ -100,7 +108,7 @@
 				alSourcePause(m_source);
 			}
 		}
-		
+
 		/** Sets the gain of the emitter
 		 *
 		 * @param gain The gain value. 0=silence ... 1.0=normal loudness.
@@ -108,7 +116,7 @@
 		void setGain(float gain) {
 			alSourcef(m_source, AL_GAIN, gain);
 		}
-		
+
 		/** Returns the gain of the emitter
 		 *
 		 * @return The gain value. 0=silence ... 1.0=normal loudness.
@@ -118,18 +126,18 @@
 			alGetSourcef(m_source, AL_GAIN, &tmp);
 			return tmp;
 		}
-		
+
 		/** Tests if the audio data is stereo data or mono.
-		 * 
+		 *
 		 * @return Returns true if the audio data is stereo, false if mono.
-		 */		
+		 */
 		bool isStereo() {
 			if (m_soundclip) {
 				return m_soundclip->getDecoder()->isStereo();
 			}
 			return false;
 		}
-		
+
 		/** Returns the bit resolution
 		 */
 		short getBitResolution() {
@@ -138,7 +146,7 @@
 			}
 			return 0;
 		}
-		
+
 		/** Returns the sample rate
 		 */
 		unsigned long getSampleRate() {
@@ -147,27 +155,27 @@
 			}
 			return 0;
 		}
-		
+
 		/** Sets the cursor position in the audio file
 		 */
 		void setCursor(SoundPositionType type, float value);
-		
+
 		/** Returns the cursor position in the audio file
 		 */
 		float getCursor(SoundPositionType type);
-		
+
 		/** Sets the position of the SoundEmitter in the virtual audio space.
 		 */
 		void setPosition(float x, float y, float z) {
 			alSource3f(m_source, AL_POSITION, x, y, z);
 		}
-		
+
 		/** Sets the velocity of the SoundEmitter in the virtual audio space.
 		 */
 		void setVelocity(float x, float y, float z) {
 			alSource3f(m_source, AL_VELOCITY, x, y, z);
 		}
-		
+
 	private:
 		/** Implementation of the pure virtual function from TimeEvent to update streaming
 		 */
--- a/engine/core/audio/soundemitter.i	Thu Jan 22 15:35:22 2009 +0000
+++ b/engine/core/audio/soundemitter.i	Sat Jan 24 22:26:46 2009 +0000
@@ -36,33 +36,34 @@
 	class SoundManager;
 	class SoundClipPool;
 
-	
+
 	class SoundEmitter {
 	public:
 		SoundEmitter(SoundManager* manager, SoundClipPool* pool, unsigned int uid);
 		~SoundEmitter();
-		
+
 		unsigned int getID();
-		
+
 		void setSoundClip(unsigned int sound_id);
 		void reset(bool defaultall = false);
 		void release();
-		
+
 		void play();
 		void pause();
 		void stop();
-		
+
 		void setLooping(bool loop);
+		void setRoleoff(float roleoff);
 		void setPositioning(bool relative);
 		void setPosition(float x, float y, float z);
 		void setVelocity(float x, float y, float z);
 		void setGain(float gain);
 		float getGain();
-		
+
 		bool isStereo();
 		short getBitResolution();
 		unsigned long getSampleRate();
-		
+
 		void setCursor(SoundPositionType type, float value);
 		float getCursor(SoundPositionType type);
 	};