Reduce processing delay

Process one packet all at once
This commit is contained in:
Geoffrey Merck 2020-02-22 06:10:55 +01:00
parent 5356f37fd8
commit 02a583f0b3
14 changed files with 298 additions and 116 deletions

View File

@ -25,10 +25,9 @@
// Only took the parts we need qnd recoeded it to be close the XLX coding style
// https://github.com/jgaeddert/liquid-dsp/blob/master/src/agc/src/agc.c
#include "main.h"
#include <math.h>
#include "cagc.h"
#include "main.h"
////////////////////////////////////////////////////////////////////////////////////////
// constructor
@ -63,35 +62,27 @@ float CAGC::GetGain()
////////////////////////////////////////////////////////////////////////////////////////
// process
void CAGC::Apply(uint8 * voice, int size)
inline float CAGC::ProcessSample(float input)
{
for (int i = 0; i < size; i+=2)
{
//Get the sample
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
//apply AGC
// apply gain to input sample
float output = input * m_Gain;
//apply AGC
// apply gain to input sample
float output = input * m_Gain;
// compute output signal energy, scaled to 0 to 1
float instantEnergy = abs(output) / m_targetEnergy;
// compute output signal energy, scaled to 0 to 1
float instantEnergy = abs(output) / m_targetEnergy;
// smooth energy estimate using single-pole low-pass filter
m_EnergyPrime = (1.0f - m_Alpha) * m_EnergyPrime + m_Alpha * instantEnergy;
// smooth energy estimate using single-pole low-pass filter
m_EnergyPrime = (1.0f - m_Alpha) * m_EnergyPrime + m_Alpha * instantEnergy;
// update gain according to output energy
if (m_EnergyPrime > 1e-6f)
m_Gain *= exp( -0.5f * m_Alpha * log(m_EnergyPrime) );
// update gain according to output energy
if (m_EnergyPrime > 1e-6f)
m_Gain *= exp( -0.5f * m_Alpha * log(m_EnergyPrime) );
// clamp gain
if (m_Gain > m_GainMax)
m_Gain = m_GainMax;
else if(m_Gain < m_GainMin)
m_Gain = m_GainMin;
// clamp gain
if (m_Gain > m_GainMax)
m_Gain = m_GainMax;
else if(m_Gain < m_GainMin)
m_Gain = m_GainMin;
//write processed sample back
voice[i] = HIBYTE((short)output);
voice[i+1] = LOBYTE((short)output);
}
}
return output;
}

View File

@ -28,16 +28,16 @@
#ifndef cagc_h
#define cagc_h
#include "main.h"
#include "csampleprocessor.h"
class CAGC
class CAGC : CSampleProcessor
{
public:
//Constructor
CAGC(float initialLeveldB);
//methods
void Apply(uint8 * voice, int size);
float ProcessSample(float input);
float GetGain();//gets current gain
private:

View File

@ -43,7 +43,7 @@ CFIRFilter::~CFIRFilter()
delete[] m_buffer;
}
inline float CFIRFilter::Process(float inputSample)
inline float CFIRFilter::ProcessSample(float inputSample)
{
float output = 0.0f;
int iTaps = 0;
@ -66,17 +66,4 @@ inline float CFIRFilter::Process(float inputSample)
return output;
}
void CFIRFilter::Process(uint8* voice, int length)
{
for(int i = 0; i < length; i+=2)
{
//Get the sample
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
float output = Process(input);
//write processed sample back
voice[i] = HIBYTE((short)output);
voice[i+1] = LOBYTE((short)output);
}
}

View File

@ -26,7 +26,7 @@
#ifndef cfirfilter_h
#define cfirfilter_h
#include "main.h"
#include "csampleprocessor.h"
class CFIRFilter
{
@ -38,8 +38,7 @@ public :
~CFIRFilter();
// Processing
float Process(float inputSample);
void Process(uint8* voice, int length);
float ProcessSample(float inputSample);
private:
float* m_taps;

44
ambed/cfixedgain.cpp Normal file
View File

@ -0,0 +1,44 @@
//
// cfixedgain.cpp
// ambed
//
// Created by Jean-Luc Deltombe (LX3JL) on 28/04/2017.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
//
// ----------------------------------------------------------------------------
// This file is part of ambed.
//
// xlxd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// xlxd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
// Geoffrey Merck F4FXL / KC3FRA AGC
#include "cfixedgain.h"
#include <math.h>
////////////////////////////////////////////////////////////////////////////////////////
// constructor
CFixedGain::CFixedGain(float gaindB)
{
m_gaindB = gaindB;
m_gainLinear = pow(10.0f, m_gaindB/20.0f);
}
////////////////////////////////////////////////////////////////////////////////////////
// processing
inline float CFixedGain::ProcessSample(float input)
{
return input * m_gainLinear;
}

45
ambed/cfixedgain.h Normal file
View File

@ -0,0 +1,45 @@
//
// cfixedgain.h
// ambed
//
// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
//
// ----------------------------------------------------------------------------
// This file is part of ambed.
//
// xlxd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// xlxd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
// Geoffrey Merck F4FXL / KC3FRA
#ifndef cfixedgain_h
#define cfixedgain_h
#include "csampleprocessor.h"
class CFixedGain : CSampleProcessor
{
public:
//Constructor
CFixedGain(float gaindB);
//processing
float ProcessSample(float input);
private:
float m_gaindB; //gain in dB
float m_gainLinear; //linearized gain
};
#endif /* cfixedgain_h */

36
ambed/csampleprocessor.h Normal file
View File

@ -0,0 +1,36 @@
//
// csampleprocessor.h
// ambed
//
// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
//
// ----------------------------------------------------------------------------
// This file is part of ambed.
//
// xlxd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// xlxd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
// Geoffrey Merck F4FXL / KC3FRA
#ifndef csampleprocessor_h
#define csampleprocessor_h
class CSampleProcessor
{
public:
//processing
virtual float ProcessSample(float input) = 0;
};
#endif /* csampleprocessor_h */

View File

@ -0,0 +1,89 @@
//
// cagc.cpp
// ambed
//
// Created by Jean-Luc Deltombe (LX3JL) on 28/04/2017.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
//
// ----------------------------------------------------------------------------
// This file is part of ambed.
//
// xlxd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// xlxd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
// Geoffrey Merck F4FXL / KC3FRA AGC
#include "main.h"
#include "csignalprocessor.h"
#if USE_AGC == 1
#include "cagc.h"
#else
#include "cfixedgain.h"
#endif
#if USE_BANDPASSFILTER == 1
#include "cfirfilter.h"
#endif
////////////////////////////////////////////////////////////////////////////////////////
// constructor
CSignalProcessor::CSignalProcessor(float gaindB)
{
#if USE_BANDPASSFILTER
m_sampleProcessors.push_back((CSampleProcessor*)new CFIRFilter(FILTER_TAPS, FILTER_TAPS_LENGTH));
#endif
#if USE_AGC == 1
m_sampleProcessors.push_back((CSampleProcessor*)new CAGC(gaindB));
#else
m_sampleProcessors.push_back((CSampleProcessor*)new CFixedGain(gaindB));
#endif
}
////////////////////////////////////////////////////////////////////////////////////////
// destructor
CSignalProcessor::~CSignalProcessor()
{
for(int i = 0; i < m_sampleProcessors.size(); i++)
{
delete m_sampleProcessors[i];
}
}
////////////////////////////////////////////////////////////////////////////////////////
// processing
void CSignalProcessor::Process(uint8* voice, int length)
{
float sample;
int i;
auto processorsSize = m_sampleProcessors.size();
for(int i = 0; i < length; i += 2)
{
//Get the sample
sample = (float)(short)MAKEWORD(voice[i+1], voice[i]);
for(i = 0; i < processorsSize; i++)
{
sample = m_sampleProcessors[i]->ProcessSample(sample);
}
//write processed sample back
voice[i] = HIBYTE((short)sample);
voice[i+1] = LOBYTE((short)sample);
}
}

48
ambed/csignalprocessor.h Normal file
View File

@ -0,0 +1,48 @@
//
// csignalprocessor.h
// ambed
//
// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
//
// ----------------------------------------------------------------------------
// This file is part of ambed.
//
// xlxd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// xlxd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
// Geoffrey Merck F4FXL / KC3FRA
#ifndef csignalprocessor_h
#define csignalprocessor_h
#include <vector>
#include "csampleprocessor.h"
class CSignalProcessor
{
public:
//Constructor
CSignalProcessor(float gaindB);
//Destructor
~CSignalProcessor();
//Processing
void Process(uint8* voice, int length);
private:
std::vector<CSampleProcessor *> m_sampleProcessors;
};
#endif /* csignalprocessor_h */

View File

@ -152,15 +152,7 @@ void CUsb3xxxInterface::Task(void)
{
Queue = Channel->GetVoiceQueue();
CVoicePacket *clone = new CVoicePacket(VoicePacket);
#if USE_BANDPASSFILTER
//Aply band pass before AGC to avoidd amplifying signals we do not want
Channel->ApplyFilter(*clone);
#endif
#if USE_AGC == 1
Channel->ApplyAGC(*clone);
#else
clone->ApplyGain(Channel->GetSpeechGain());
#endif
Channel->ProcessSignal(*clone);
Queue->push(clone);
Channel->ReleaseVoiceQueue();
}

View File

@ -38,12 +38,7 @@ CVocodecChannel::CVocodecChannel(CVocodecInterface *InterfaceIn, int iChIn, CVoc
m_InterfaceOut = InterfaceOut;
m_iChannelOut = iChOut;
m_iSpeechGain = iSpeechGain;
#if USE_AGC == 1
m_AGC = new CAGC((float)iSpeechGain);
#endif
#if USE_BANDPASSFILTER == 1
m_filter = new CFIRFilter(FILTER_TAPS, FILTER_TAPS_LENGTH);
#endif
m_signalProcessor = new CSignalProcessor((float)m_iSpeechGain);
}
////////////////////////////////////////////////////////////////////////////////////////
@ -52,12 +47,7 @@ CVocodecChannel::CVocodecChannel(CVocodecInterface *InterfaceIn, int iChIn, CVoc
CVocodecChannel::~CVocodecChannel()
{
PurgeAllQueues();
#if USE_AGC == 1
delete m_AGC;
#endif
#if USE_BANDPASSFILTER == 1
delete m_filter;
#endif
delete m_signalProcessor;
}
////////////////////////////////////////////////////////////////////////////////////////
@ -103,20 +93,14 @@ uint8 CVocodecChannel::GetCodecOut(void) const
return m_InterfaceOut->GetChannelCodec(m_iChannelOut);
}
#if USE_AGC == 1
void CVocodecChannel::ApplyAGC(CVoicePacket& voicePacket)
{
m_AGC->Apply(voicePacket.GetVoice(), voicePacket.GetVoiceSize());
//std::cout << "Gain : " << m_AGC.GetGain() << "\n";
}
#endif
////////////////////////////////////////////////////////////////////////////////////////
// processing
#if USE_BANDPASSFILTER == 1
void CVocodecChannel::ApplyFilter(CVoicePacket& voicePacket)
void CVocodecChannel::ProcessSignal(CVoicePacket& voicePacket)
{
m_filter->Process(voicePacket.GetVoice(), voicePacket.GetVoiceSize());
m_signalProcessor->Process(voicePacket.GetVoice(), voicePacket.GetVoiceSize());
}
#endif
////////////////////////////////////////////////////////////////////////////////////////
// queues helpers

View File

@ -27,12 +27,7 @@
#define cvocodecchannel_h
#include "cpacketqueue.h"
#if USE_AGC == 1
#include "cagc.h"
#endif
#if USE_BANDPASSFILTER == 1
#include "cfirfilter.h"
#endif
#include "csignalprocessor.h"
#include "cvoicepacket.h"
////////////////////////////////////////////////////////////////////////////////////////
@ -62,12 +57,7 @@ public:
int GetSpeechGain(void) const { return m_iSpeechGain; }
//Processing
#if USE_AGC == 1
void ApplyAGC(CVoicePacket& voicePacket);
#endif
#if USE_BANDPASSFILTER
void ApplyFilter(CVoicePacket& voicePacket);
#endif
void ProcessSignal(CVoicePacket& voicePacket);
// interfaces
bool IsInterfaceIn(const CVocodecInterface *interface) { return (interface == m_InterfaceIn); }
@ -108,12 +98,7 @@ protected:
int m_iSpeechGain;
private:
#if USE_AGC == 1
CAGC* m_AGC;
#endif
#if USE_BANDPASSFILTER == 1
CFIRFilter* m_filter;
#endif
CSignalProcessor* m_signalProcessor;
};
////////////////////////////////////////////////////////////////////////////////////////

View File

@ -69,18 +69,3 @@ void CVoicePacket::SetVoice(const uint8 *voice, int size)
::memcpy(m_uiVoice, voice, m_iSize);
}
////////////////////////////////////////////////////////////////////////////////////////
// gain
void CVoicePacket::ApplyGain(int dB)
{
float mult = pow(10, dB/20.0);
for ( int i = 0; i < m_iSize; i += 2 )
{
float smp = (float)(short)MAKEWORD(m_uiVoice[i+1], m_uiVoice[i]);
smp *= mult;
m_uiVoice[i] = HIBYTE((short)smp);
m_uiVoice[i+1] = LOBYTE((short)smp);
}
}

View File

@ -58,9 +58,6 @@ public:
// set
void SetVoice(const uint8 *, int);
// gain
void ApplyGain(int);
protected:
// data
int m_iSize;