ITARMSDetector.cpp 3.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#include "ITARMSDetector.h"

#include <ITAException.h>
#include <ITANumericUtils.h>

#include <cmath>

ITARMSDetector::ITARMSDetector( ITADatasource* pDataSource )
	: m_pDataSource( pDataSource )
{
11
	m_dSampleRate = pDataSource->GetSampleRate();
12 13 14 15
	m_uiChannels = pDataSource->GetNumberOfChannels();
	m_uiBlocklength = pDataSource->GetBlocklength();
	m_pfRMSs = 0;

16
	if( ( m_uiBlocklength == 0 ) || ( m_uiChannels == 0 ) || ( m_dSampleRate == 0 ) )
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		ITA_EXCEPT0( INVALID_PARAMETER );

	m_pfRMSs = new float[ m_uiChannels ];

	Reset();
}

ITARMSDetector::~ITARMSDetector()
{
	delete[] m_pfRMSs;
};

void ITARMSDetector::Reset()
{
	m_cs.enter();
	for( unsigned int c = 0; c < m_uiChannels; c++ )
		m_pfRMSs[ c ] = 0.00001f;

	m_fOverallRMS = 0;
	m_uiOverallRMSChannel = 0;
	m_cs.leave();
}

void ITARMSDetector::GetOverallRMS( float* pfPeak, unsigned int* puiChannel, bool bReset )
{
	m_cs.enter();

	if( pfPeak )
		*pfPeak = m_fOverallRMS;

	if( puiChannel )
		*puiChannel = m_uiOverallRMSChannel;

	if( bReset )
	{
		m_fOverallRMS = 0;
		m_uiOverallRMSChannel = 0;
	}

	m_cs.leave();
}

void ITARMSDetector::GetOverallRMSDecibel( double* pdPeakDecibel, unsigned int* puiChannel, bool bReset )
{
	m_cs.enter();

	if( pdPeakDecibel )
		*pdPeakDecibel = ratio_to_db20( m_fOverallRMS );

	if( puiChannel )
		*puiChannel = m_uiOverallRMSChannel;

	if( bReset )
	{
		m_fOverallRMS = 0;
		m_uiOverallRMSChannel = 0;
	}

	m_cs.leave();
}

float ITARMSDetector::GetRMS( unsigned int uiChannel, bool bReset )
{
	if( uiChannel >= m_uiChannels )
		ITA_EXCEPT0( INVALID_PARAMETER );

	m_cs.enter();

	float fResult = m_pfRMSs[ uiChannel ];
	if( bReset )
		m_pfRMSs[ uiChannel ] = 0;

	m_cs.leave();

	return fResult;
}

double ITARMSDetector::GetRMSDecibel( unsigned int uiChannel, bool bReset )
{
	return ratio_to_db20( GetRMS( uiChannel, bReset ) );
}

99
void ITARMSDetector::GetRMSs( std::vector< float >& vfDest, bool bReset )
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
	if( ( ( unsigned int ) vfDest.size() ) < m_uiChannels )
		vfDest.resize( m_uiChannels );

	m_cs.enter();
	for( unsigned int c = 0; c < m_uiChannels; c++ )
	{
		vfDest[ c ] = m_pfRMSs[ c ];
		if( bReset ) 
			m_pfRMSs[ c ] = 0;
	}
	m_cs.leave();
}

void ITARMSDetector::GetRMSsDecibel( std::vector<double>& vdDestDecibel, bool bReset )
{
	if( ( ( unsigned int ) vdDestDecibel.size() ) < m_uiChannels )
		vdDestDecibel.resize( m_uiChannels );

	m_cs.enter();
	for( unsigned int c = 0; c < m_uiChannels; c++ )
	{
		vdDestDecibel[ c ] = ratio_to_db20( m_pfRMSs[ c ] );
		if( bReset )
			m_pfRMSs[ c ] = 0;
	}
	m_cs.leave();
}

const float* ITARMSDetector::GetBlockPointer( unsigned int uiChannel, const ITAStreamInfo* pStreamInfo )
{
	const float* pfData = m_pDataSource->GetBlockPointer( uiChannel, pStreamInfo );

	if( pfData )
	{
		// TODO: Ist es wirklich ntig bei jedem GBP die CS zu betreten? :-(
		m_cs.enter();

		// Daten analysieren
		for( unsigned int i = 0; i < m_uiBlocklength; i++ )
		{
			float fAbs = std::abs( pfData[ i ] );

			if( fAbs > m_pfRMSs[ uiChannel ] )
				m_pfRMSs[ uiChannel ] = fAbs;

			if( fAbs > m_fOverallRMS )
			{
				m_fOverallRMS = fAbs;
				m_uiOverallRMSChannel = uiChannel;
			}
		}

		m_cs.leave();
	}

	return pfData;
}

void ITARMSDetector::IncrementBlockPointer()
{
	m_pDataSource->IncrementBlockPointer();
}