ITADirectConvolutionImpl.cpp 1.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include "ITADirectConvolutionImpl.h"

#include <ITAUPConvolution.h>
#include <ITAUPFilter.h>

#include <ITANumericUtils.h>

ITADirectConvolutionImpl::ITADirectConvolutionImpl( const int src1length, const int src2length, const int )
	: m_pConvolver( NULL )
	, m_pFilter( NULL )
{
	int iSignallength;
	int iFilterlength;

	// Krzere Eingabe = Signal, die andere das Filter
	if( src1length < src2length )
	{
		iSignallength = src2length;
		iFilterlength = src1length;
		m_iFilter = 1;
	}
	else
	{
		iSignallength = src1length;
		iFilterlength = src2length;
		m_iFilter = 2;
	}

	// Blocklnge = Nchste Zweierpotenz grer der Filterlnge
	m_iBlockLength = ( int ) nextPow2( ( unsigned int ) iFilterlength );
	m_pConvolver = new ITAUPConvolution( m_iBlockLength, iFilterlength );
	m_pConvolver->setFilterExchangeMode( ITAUPConvolution::SWITCH );

	// Filter holen
	m_pFilter = m_pConvolver->requestFilter();
}

ITADirectConvolutionImpl::~ITADirectConvolutionImpl()
{
	delete m_pConvolver;
}

void ITADirectConvolutionImpl::Convolve( const float* src1, const int src1length, const float* src2, const int src2length, float* dest, const int destlength )
{
	if( !m_pConvolver )
		return;

	const float* pfInput;
	int iInputLength;

	// Filter laden
	if( m_iFilter == 1 )
	{
		m_pFilter->load( src1, src1length );
		pfInput = src2;
		iInputLength = src2length;
	}
	else
	{
		m_pFilter->load( src2, src2length );
		pfInput = src1;
		iInputLength = src1length;
	}
	m_pConvolver->exchangeFilter( m_pFilter );

	// Iterativ durchfalten
	int k = 0;
	while( k < destlength )
	{
		// Restliche Samples in der Eingabe
		int ri = ( std::min )( ( std::max )( iInputLength - k, 0 ), m_iBlockLength );

		// Restliche Samples in der Ausgabe
		int ro = ( std::min )( ( std::max )( destlength - k, 0 ), m_iBlockLength );

		m_pConvolver->process( pfInput + k, ri, dest + k, ro );
		k += ro;
	}
}