[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [11690] branches/soc-2007-hcube/intern/ tinySND: Added SND_DataMultiplexer to convert and multiplex float pcm data to more common format like (U8, S8, S16, S24, S32, etc).

Csaba Hruska csaba.hruska at gmail.com
Sun Aug 19 12:42:04 CEST 2007


Revision: 11690
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11690
Author:   hcube
Date:     2007-08-19 12:42:03 +0200 (Sun, 19 Aug 2007)

Log Message:
-----------
Added SND_DataMultiplexer to convert and multiplex float pcm data to more common format like (U8, S8, S16, S24, S32, etc).

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

Added: branches/soc-2007-hcube/intern/tinySND/SND_DataMultiplexer.h
===================================================================
--- branches/soc-2007-hcube/intern/tinySND/SND_DataMultiplexer.h	                        (rev 0)
+++ branches/soc-2007-hcube/intern/tinySND/SND_DataMultiplexer.h	2007-08-19 10:42:03 UTC (rev 11690)
@@ -0,0 +1,35 @@
+#ifndef __SND_DATAMULTIPLEXER_H__
+#define __SND_DATAMULTIPLEXER_H__
+
+#include <vector>
+#include "SND_Defines.h"
+#include "SND_SoundInterface.h"
+
+// Multiplexes and converts sound data to the requested format
+
+class SND_DataMultiplexer
+{
+public:
+    
+			SND_DataMultiplexer();
+			~SND_DataMultiplexer();
+    
+    void	assignSoundToChannel( SND_SoundInterface *sound, int channelIdx );
+
+    int		getNumChannels();
+    void	setNumChannels( int channelsNum );
+
+	int		getSampleFormat();
+	void	setSampleFormat( int sampleFormat );
+	
+	// multiplexes addes sounds, and converts to the given format    
+	void	fillBuffer( char *buffer, int framesNum );
+    
+protected:
+	std::vector<SND_SoundInterface*>	mSounds;
+	std::vector<float*>					mSoundBuffers;
+	int									mSampleFormat;
+
+};
+
+#endif //__SND_DATAMULTIPLEXER_H__

Added: branches/soc-2007-hcube/intern/tinySND/intern/SND_DataMultiplexer.cpp
===================================================================
--- branches/soc-2007-hcube/intern/tinySND/intern/SND_DataMultiplexer.cpp	                        (rev 0)
+++ branches/soc-2007-hcube/intern/tinySND/intern/SND_DataMultiplexer.cpp	2007-08-19 10:42:03 UTC (rev 11690)
@@ -0,0 +1,224 @@
+#include <assert.h>
+#include "SND_DataMultiplexer.h"
+
+SND_DataMultiplexer::SND_DataMultiplexer()
+{
+	mSounds.clear();
+	mSoundBuffers.clear();
+	mSampleFormat = SND_FORMAT_PCM_16;
+}
+
+SND_DataMultiplexer::~SND_DataMultiplexer()
+{
+}
+
+void SND_DataMultiplexer::assignSoundToChannel( SND_SoundInterface *sound, int channelIdx )
+{
+	assert( sound != 0 );
+	assert( channelIdx >= 0 || channelIdx < (int)mSounds.size() );
+
+	mSounds[ channelIdx ] = sound;
+	mSoundBuffers[ channelIdx ] = 0;
+}
+
+int SND_DataMultiplexer::getNumChannels()
+{
+	return mSounds.size();
+}
+
+void SND_DataMultiplexer::setNumChannels( int channelsNum )
+{
+	mSounds.resize( channelsNum, 0 );
+	mSoundBuffers.resize( channelsNum, 0 );
+}
+
+int SND_DataMultiplexer::getSampleFormat()
+{
+	return mSampleFormat;
+}
+
+void SND_DataMultiplexer::setSampleFormat( int sampleFormat )
+{
+	mSampleFormat = sampleFormat;
+}
+
+/*
+enum
+{
+    SND_ENDIAN_LITTLE       = 0x1000,
+    SND_ENDIAN_BIG          = 0x2000
+};
+
+enum
+{
+    SND_FORMAT_PCM_S8       = 0x0001,       // Signed 8 bit data 
+    SND_FORMAT_PCM_16       = 0x0002,       // Signed 16 bit data 
+    SND_FORMAT_PCM_24       = 0x0003,       // Signed 24 bit data 
+    SND_FORMAT_PCM_32       = 0x0004,       // Signed 32 bit data 
+
+    SND_FORMAT_PCM_U8       = 0x0005,       // Unsigned 8 bit data (WAV and RAW only) 
+
+    SND_FORMAT_FLOAT        = 0x0006,       // 32 bit float data 
+    SND_FORMAT_DOUBLE       = 0x0007        // 64 bit float data 
+*/
+
+void SND_DataMultiplexer::fillBuffer( char *buffer, int framesNum )
+{
+	int i, channelsNum;
+
+	channelsNum = mSounds.size();
+	for( i = 0 ; i < channelsNum ; ++i )
+	{
+		if( mSounds[ i ] != 0 && mSounds[ i ]->isDataAvailable() )
+		{
+			mSoundBuffers[ i ] = mSounds[ i ]->getPCMDataPtr( framesNum );
+		}
+		else
+		{
+			mSoundBuffers[ i ] = 0;
+		}
+	}
+	
+	// !!!! endian conversion is UNIMPLEMENTED YET !!!!
+	// !!!! SND_FORMAT_PCM_24 is UNIMPLEMENTED YET !!!!
+	
+	if( mSampleFormat == SND_FORMAT_FLOAT )
+	{
+		// float -> float, only multiplexing
+		int j;
+		float *dstBuffer = static_cast<float *>((void*)buffer);
+		
+	    for( i = 0 ; i < framesNum ; ++i )
+	    {
+			for( j = 0 ; j < channelsNum ; ++j )
+			{
+			    if( mSoundBuffers[j] != 0 )
+			    {
+					dstBuffer[0] = mSoundBuffers[j][0];
+					++mSoundBuffers[j];
+			    }
+				else
+			    {
+					dstBuffer[0] = 0.0;
+			    }
+			    ++dstBuffer;
+			}
+	    }
+	}
+	else if( mSampleFormat == SND_FORMAT_DOUBLE )
+	{
+		// float -> double
+		int j;
+		double *dstBuffer = static_cast<double *>((void*)buffer);
+		
+	    for( i = 0 ; i < framesNum ; ++i )
+	    {
+			for( j = 0 ; j < channelsNum ; ++j )
+			{
+			    if( mSoundBuffers[j] != 0 )
+			    {
+					dstBuffer[0] = (double)mSoundBuffers[j][0];
+					++mSoundBuffers[j];
+			    }
+				else
+			    {
+					dstBuffer[0] = 0.0;
+			    }
+			    ++dstBuffer;
+			}
+	    }
+	}
+	else if( mSampleFormat == SND_FORMAT_PCM_U8 )
+	{
+		// float -> unsigned char
+		int j;
+		unsigned char *dstBuffer = static_cast<unsigned char *>((void*)buffer);
+		
+	    for( i = 0 ; i < framesNum ; ++i )
+	    {
+			for( j = 0 ; j < channelsNum ; ++j )
+			{
+			    if( mSoundBuffers[j] != 0 )
+			    {
+					dstBuffer[0] = (unsigned char)(128.0 + 128.0 * mSoundBuffers[j][0]);
+					++mSoundBuffers[j];
+			    }
+				else
+			    {
+					dstBuffer[0] = 0;
+			    }
+			    ++dstBuffer;
+			}
+	    }
+	}
+	else if( mSampleFormat == SND_FORMAT_PCM_S8 )
+	{
+		// float -> signed char
+		int j;
+		char *dstBuffer = static_cast<char *>((void*)buffer);
+		
+	    for( i = 0 ; i < framesNum ; ++i )
+	    {
+			for( j = 0 ; j < channelsNum ; ++j )
+			{
+			    if( mSoundBuffers[j] != 0 )
+			    {
+					dstBuffer[0] = (char)(128.0 * mSoundBuffers[j][0]);
+					++mSoundBuffers[j];
+			    }
+				else
+			    {
+					dstBuffer[0] = 0;
+			    }
+			    ++dstBuffer;
+			}
+	    }
+	}
+	else if( mSampleFormat == SND_FORMAT_PCM_16 )
+	{
+		// float -> signed short
+		int j;
+		short *dstBuffer = static_cast<short *>((void*)buffer);
+		
+	    for( i = 0 ; i < framesNum ; ++i )
+	    {
+			for( j = 0 ; j < channelsNum ; ++j )
+			{
+			    if( mSoundBuffers[j] != 0 )
+			    {
+					dstBuffer[0] = (short)(32768.0 * mSoundBuffers[j][0]);
+					++mSoundBuffers[j];
+			    }
+				else
+			    {
+					dstBuffer[0] = 0;
+			    }
+			    ++dstBuffer;
+			}
+	    }
+	}
+	else if( mSampleFormat == SND_FORMAT_PCM_32 )
+	{
+		// float -> signed int
+		int j;
+		short *dstBuffer = static_cast<short *>((void*)buffer);
+		
+	    for( i = 0 ; i < framesNum ; ++i )
+	    {
+			for( j = 0 ; j < channelsNum ; ++j )
+			{
+			    if( mSoundBuffers[j] != 0 )
+			    {
+					dstBuffer[0] = (int)(2147483648.0 * mSoundBuffers[j][0]);
+					++mSoundBuffers[j];
+			    }
+				else
+			    {
+					dstBuffer[0] = 0;
+			    }
+			    ++dstBuffer;
+			}
+	    }
+	}
+}
+





More information about the Bf-blender-cvs mailing list