speed optimzation: avoid some unnecessary conversions from short to double

This commit is contained in:
Volker Fischer 2013-12-15 12:46:09 +00:00
parent 9a2efd25fe
commit 379a3e9f9d
4 changed files with 40 additions and 61 deletions

View file

@ -743,7 +743,6 @@ void CClient::Init()
iStereoBlockSizeSam = 2 * iMonoBlockSizeSam;
vecsAudioSndCrdMono.Init ( iMonoBlockSizeSam );
vecdAudioStereo.Init ( iStereoBlockSizeSam );
// init reverberation
AudioReverbL.Init ( SYSTEM_SAMPLE_RATE_HZ );
@ -833,8 +832,6 @@ void CClient::Init()
vecbyNetwData.Init ( iCeltNumCodedBytes );
if ( bUseStereo )
{
vecsNetwork.Init ( iStereoBlockSizeSam );
// set the channel network properties
Channel.SetAudioStreamProperties ( eAudioCompressionType,
iCeltNumCodedBytes,
@ -843,8 +840,6 @@ void CClient::Init()
}
else
{
vecsNetwork.Init ( iMonoBlockSizeSam );
// set the channel network properties
Channel.SetAudioStreamProperties ( eAudioCompressionType,
iCeltNumCodedBytes,
@ -904,12 +899,6 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
// update stereo signal level meter
SignalLevelMeter.Update ( vecsStereoSndCrd );
// convert data from short to double
for ( i = 0; i < iStereoBlockSizeSam; i++ )
{
vecdAudioStereo[i] = static_cast<double> ( vecsStereoSndCrd[i] );
}
// add reverberation effect if activated
if ( iReverbLevel != 0 )
{
@ -923,12 +912,10 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
for ( i = 0; i < iStereoBlockSizeSam; i += 2 )
{
// left channel
vecdAudioStereo[i] +=
dRevLev * AudioReverbL.ProcessSample ( vecdAudioStereo[i] );
AudioReverbL.ProcessSample ( vecsStereoSndCrd[i], dRevLev );
// right channel
vecdAudioStereo[i + 1] +=
dRevLev * AudioReverbR.ProcessSample ( vecdAudioStereo[i + 1] );
AudioReverbR.ProcessSample ( vecsStereoSndCrd[i + 1], dRevLev );
}
}
else
@ -938,8 +925,7 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
for ( i = 0; i < iStereoBlockSizeSam; i += 2 )
{
// left channel
vecdAudioStereo[i] +=
dRevLev * AudioReverbL.ProcessSample ( vecdAudioStereo[i] );
AudioReverbL.ProcessSample ( vecsStereoSndCrd[i], dRevLev );
}
}
else
@ -947,8 +933,7 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
for ( i = 1; i < iStereoBlockSizeSam; i += 2 )
{
// right channel
vecdAudioStereo[i] +=
dRevLev * AudioReverbR.ProcessSample ( vecdAudioStereo[i] );
AudioReverbR.ProcessSample ( vecsStereoSndCrd[i], dRevLev );
}
}
}
@ -958,22 +943,18 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
// from double to short
if ( iAudioInFader == AUD_FADER_IN_MIDDLE )
{
if ( bUseStereo )
// no action require if fader is in the middle and stereo is used
if ( !bUseStereo )
{
// perform type conversion
for ( i = 0; i < iStereoBlockSizeSam; i++ )
{
vecsNetwork[i] = Double2Short ( vecdAudioStereo[i] );
}
}
else
{
// mix channels together
// mix channels together (store result in first half of the vector)
for ( i = 0, j = 0; i < iMonoBlockSizeSam; i++, j += 2 )
{
vecsNetwork[i] =
Double2Short ( ( vecdAudioStereo[j] +
vecdAudioStereo[j + 1] ) / 2 );
// for the sum make sure we have more bits available (cast to
// int32), after the normalization by 2, the result will fit
// into the old size so that cast to int16 is safe
vecsStereoSndCrd[i] = static_cast<int16_t> (
( static_cast<int32_t> ( vecsStereoSndCrd[j] ) +
vecsStereoSndCrd[j + 1] ) / 2 );
}
}
}
@ -991,11 +972,8 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
for ( i = 0, j = 0; i < iMonoBlockSizeSam; i++, j += 2 )
{
// attenuation on right channel
vecsNetwork[j] = Double2Short (
vecdAudioStereo[j] );
vecsNetwork[j + 1] = Double2Short (
dAttFactStereo * vecdAudioStereo[j + 1] );
vecsStereoSndCrd[j + 1] = Double2Short (
dAttFactStereo * vecsStereoSndCrd[j + 1] );
}
}
else
@ -1003,11 +981,8 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
for ( i = 0, j = 0; i < iMonoBlockSizeSam; i++, j += 2 )
{
// attenuation on left channel
vecsNetwork[j] = Double2Short (
dAttFactStereo * vecdAudioStereo[j] );
vecsNetwork[j + 1] = Double2Short (
vecdAudioStereo[j + 1] );
vecsStereoSndCrd[j] = Double2Short (
dAttFactStereo * vecsStereoSndCrd[j] );
}
}
}
@ -1029,20 +1004,22 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
{
for ( i = 0, j = 0; i < iMonoBlockSizeSam; i++, j += 2 )
{
// attenuation on right channel
vecsNetwork[i] = Double2Short (
dAmplFactMono * vecdAudioStereo[j] +
dAttFactMono * vecdAudioStereo[j + 1] );
// attenuation on right channel (store result in first half
// of the vector)
vecsStereoSndCrd[i] = Double2Short (
dAmplFactMono * vecsStereoSndCrd[j] +
dAttFactMono * vecsStereoSndCrd[j + 1] );
}
}
else
{
for ( i = 0, j = 0; i < iMonoBlockSizeSam; i++, j += 2 )
{
// attenuation on left channel
vecsNetwork[i] = Double2Short (
dAmplFactMono * vecdAudioStereo[j + 1] +
dAttFactMono * vecdAudioStereo[j] );
// attenuation on left channel (store result in first half
// of the vector)
vecsStereoSndCrd[i] = Double2Short (
dAmplFactMono * vecsStereoSndCrd[j + 1] +
dAttFactMono * vecsStereoSndCrd[j] );
}
}
}
@ -1056,7 +1033,7 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
if ( eAudioCompressionType == CT_CELT )
{
cc6_celt_encode ( CeltEncoderStereo,
&vecsNetwork[i * 2 * SYSTEM_FRAME_SIZE_SAMPLES],
&vecsStereoSndCrd[i * 2 * SYSTEM_FRAME_SIZE_SAMPLES],
NULL,
&vecCeltData[0],
iCeltNumCodedBytes );
@ -1064,7 +1041,7 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
else
{
opus_custom_encode ( OpusEncoderStereo,
&vecsNetwork[i * 2 * SYSTEM_FRAME_SIZE_SAMPLES],
&vecsStereoSndCrd[i * 2 * SYSTEM_FRAME_SIZE_SAMPLES],
SYSTEM_FRAME_SIZE_SAMPLES,
&vecCeltData[0],
iCeltNumCodedBytes );
@ -1076,7 +1053,7 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
if ( eAudioCompressionType == CT_CELT )
{
cc6_celt_encode ( CeltEncoderMono,
&vecsNetwork[i * SYSTEM_FRAME_SIZE_SAMPLES],
&vecsStereoSndCrd[i * SYSTEM_FRAME_SIZE_SAMPLES],
NULL,
&vecCeltData[0],
iCeltNumCodedBytes );
@ -1084,7 +1061,7 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd )
else
{
opus_custom_encode ( OpusEncoderMono,
&vecsNetwork[i * SYSTEM_FRAME_SIZE_SAMPLES],
&vecsStereoSndCrd[i * SYSTEM_FRAME_SIZE_SAMPLES],
SYSTEM_FRAME_SIZE_SAMPLES,
&vecCeltData[0],
iCeltNumCodedBytes );

View file

@ -357,8 +357,6 @@ void SetAudoCompressiontype ( const EAudComprType eNAudCompressionType );
bool bUseDefaultCentralServerAddress;
CVector<int16_t> vecsAudioSndCrdMono;
CVector<double> vecdAudioStereo;
CVector<int16_t> vecsNetwork;
// server settings
int iServerSockBufNumFrames;

View file

@ -263,7 +263,8 @@ void CAudioReverb::setT60 ( const double rT60,
}
}
double CAudioReverb::ProcessSample ( const double input )
void CAudioReverb::ProcessSample ( int16_t& input,
const double dAttenuation )
{
// compute one output sample
double temp, temp0, temp1, temp2;
@ -296,7 +297,9 @@ double CAudioReverb::ProcessSample ( const double input )
combDelays_[2].Add ( (int) temp5 );
combDelays_[3].Add ( (int) temp6 );
return ( temp3 + temp4 + temp5 + temp6 ) * (double) 0.5;
// inplace apply the attenuated reverb signal
input = Double2Short ( input +
( temp3 + temp4 + temp5 + temp6 ) * (double) 0.5 * dAttenuation );
}

View file

@ -890,9 +890,10 @@ class CAudioReverb
public:
CAudioReverb() {}
void Init ( const int iSampleRate, const double rT60 = (double) 1.1 );
void Clear();
double ProcessSample ( const double input );
void Init ( const int iSampleRate, const double rT60 = (double) 1.1 );
void Clear();
void ProcessSample ( int16_t& input,
const double dAttenuation );
protected:
void setT60 ( const double rT60, const int iSampleRate );