[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [11011] branches/soc-2007-hcube/intern/ tinySND/intern/SND_FXMixer.cpp: Added mix function impementation.

Csaba Hruska csaba.hruska at gmail.com
Fri Jun 22 19:00:16 CEST 2007


Revision: 11011
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11011
Author:   hcube
Date:     2007-06-22 19:00:15 +0200 (Fri, 22 Jun 2007)

Log Message:
-----------
Added mix function impementation.

Added Paths:
-----------
    branches/soc-2007-hcube/intern/tinySND/intern/SND_FXMixer.cpp

Added: branches/soc-2007-hcube/intern/tinySND/intern/SND_FXMixer.cpp
===================================================================
--- branches/soc-2007-hcube/intern/tinySND/intern/SND_FXMixer.cpp	                        (rev 0)
+++ branches/soc-2007-hcube/intern/tinySND/intern/SND_FXMixer.cpp	2007-06-22 17:00:15 UTC (rev 11011)
@@ -0,0 +1,167 @@
+#include <stdio.h>
+#include <assert.h>
+#include "SND_FXMixer.h"
+#include "SND_Defines.h"
+
+SND_FXMixer::SND_FXMixer(void)
+{
+	mSampleRate = 0.0;
+	mGain = 1.0;
+	mIsMuted = false;
+
+	// assign mix buffer
+	mBuffer = new float[ SND_MIX_BUFFER_SIZE ];
+	mBufferSize = SND_MIX_BUFFER_SIZE;
+}
+
+SND_FXMixer::~SND_FXMixer(void)
+{
+	delete mBuffer;
+}
+
+void SND_FXMixer::addSound( SND_SoundInterface* sound )
+{
+	assert( sound != 0 );
+	
+	if( mSounds.empty() )
+	{
+		mSampleRate = sound->getSampleRate();
+	}
+	
+	// all attached sounds must have same sample rates
+	if( sound->getSampleRate() != mSampleRate )
+	{
+		return;
+	}
+	mSounds.push_back( sound );
+	mSoundBuffers.push_back( 0 );
+}
+
+void SND_FXMixer::removeSound( SND_SoundInterface* sound )
+{
+	assert( sound != 0 );
+
+	std::vector<SND_SoundInterface*>::iterator i;
+	
+	for( i = mSounds.begin() ; i != mSounds.end() ; ++i )
+	{
+		if( *i == sound )
+		{
+			mSounds.erase( i );
+			mSoundBuffers.pop_back();
+		}
+	}
+
+	if( mSounds.empty() )
+	{
+		mSampleRate = 0.0;
+	}
+}
+
+SND_SoundInterface* SND_FXMixer::getSoundByIdx( int idx )
+{
+	assert( 0 < idx );
+	assert( idx < (int)mSounds.size() );
+	return mSounds[ idx ];
+}
+
+int SND_FXMixer::getNumSounds(void)
+{
+	return mSounds.size();
+}
+
+void SND_FXMixer::setGain( float gain )
+{
+	mGain = gain;
+}
+
+float SND_FXMixer::getGain(void)
+{
+	return mGain;
+}
+    
+bool SND_FXMixer::isMuted(void)
+{
+	return mIsMuted;
+}
+
+void SND_FXMixer::setMute( bool isMuted )
+{
+	mIsMuted = isMuted;
+}
+
+float SND_FXMixer::getSampleRate(void)
+{
+	return mSampleRate;
+}
+
+bool SND_FXMixer::isDataAvailable(void)
+{
+	if( mIsMuted || mGain==0 || mSounds.empty() )
+	{
+		return false;
+	}
+	
+	bool res = false;
+	std::vector<SND_SoundInterface*>::const_iterator i;
+	
+	for( i = mSounds.begin() ; i != mSounds.end() ; ++i )
+	{
+		res = res || (*i)->isDataAvailable();
+	}
+	return res;
+}
+
+float* SND_FXMixer::getPCMDataPtr( int framesNum )
+{
+	std::vector<SND_SoundInterface*>::const_iterator i;
+	int sndNum, j, n;
+	float mixFactor;
+	float *buffer;
+	
+	// if our buffer is not enough big, then allocate a new one
+	// !!!!!!!! UNIMPLEMENTED YET !!!!!!!!!!!!!!!!!!!!
+	
+	// collect sounds where data is available, count them
+	sndNum = 0;
+	for( i = mSounds.begin() ; i != mSounds.end() ; ++i )
+	{
+		if( (*i)->isDataAvailable() )
+		{
+			mSoundBuffers[ sndNum ] = (*i)->getPCMDataPtr( framesNum );
+			++sndNum;
+		}
+	}
+
+	if( sndNum==1 && mGain==1.0 )
+	{
+		// no mixing is needed, there is only one channel and gain is 1.0
+		return mSoundBuffers[ 0 ];
+	}
+	// mix collected sounds
+	mixFactor = mGain / ((float)sndNum);
+	for( j = 0 ; j < sndNum ; ++j )
+	{
+		buffer = mSoundBuffers[ j ];
+		if( j == 0 )
+		{
+			// first fill the buffer
+			for( n = 0 ; n < framesNum ; ++n )
+			{
+				mBuffer[ n ] = mixFactor * buffer[0];
+				++buffer;
+			}
+		}
+		else
+		{
+			// others adds own pcm data
+			for( n = 0 ; n < framesNum ; ++n )
+			{
+				mBuffer[ n ] += mixFactor * buffer[0];
+				++buffer;
+			}
+		}
+	}
+	return mBuffer;
+}
+





More information about the Bf-blender-cvs mailing list