From 02a583f0b3c9cdecb920bac12e01ae6ce85ec014 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sat, 22 Feb 2020 06:10:55 +0100 Subject: [PATCH] Reduce processing delay Process one packet all at once --- ambed/cagc.cpp | 47 ++++++++------------ ambed/cagc.h | 6 +-- ambed/cfirfilter.cpp | 15 +------ ambed/cfirfilter.h | 5 +-- ambed/cfixedgain.cpp | 44 ++++++++++++++++++ ambed/cfixedgain.h | 45 +++++++++++++++++++ ambed/csampleprocessor.h | 36 +++++++++++++++ ambed/csignalprocessor.cpp | 89 +++++++++++++++++++++++++++++++++++++ ambed/csignalprocessor.h | 48 ++++++++++++++++++++ ambed/cusb3xxxinterface.cpp | 10 +---- ambed/cvocodecchannel.cpp | 30 +++---------- ambed/cvocodecchannel.h | 21 ++------- ambed/cvoicepacket.cpp | 15 ------- ambed/cvoicepacket.h | 3 -- 14 files changed, 298 insertions(+), 116 deletions(-) create mode 100644 ambed/cfixedgain.cpp create mode 100644 ambed/cfixedgain.h create mode 100644 ambed/csampleprocessor.h create mode 100644 ambed/csignalprocessor.cpp create mode 100644 ambed/csignalprocessor.h diff --git a/ambed/cagc.cpp b/ambed/cagc.cpp index d5b2105..23ef6e4 100644 --- a/ambed/cagc.cpp +++ b/ambed/cagc.cpp @@ -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 #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); - } -} \ No newline at end of file + return output; +} diff --git a/ambed/cagc.h b/ambed/cagc.h index c1547e3..db22b22 100644 --- a/ambed/cagc.h +++ b/ambed/cagc.h @@ -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: diff --git a/ambed/cfirfilter.cpp b/ambed/cfirfilter.cpp index 920ffe3..c7bd0ca 100644 --- a/ambed/cfirfilter.cpp +++ b/ambed/cfirfilter.cpp @@ -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); - } -} diff --git a/ambed/cfirfilter.h b/ambed/cfirfilter.h index 9d1d9c4..43c2c31 100644 --- a/ambed/cfirfilter.h +++ b/ambed/cfirfilter.h @@ -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; diff --git a/ambed/cfixedgain.cpp b/ambed/cfixedgain.cpp new file mode 100644 index 0000000..4e8645c --- /dev/null +++ b/ambed/cfixedgain.cpp @@ -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 . +// ---------------------------------------------------------------------------- +// Geoffrey Merck F4FXL / KC3FRA AGC + +#include "cfixedgain.h" +#include + +//////////////////////////////////////////////////////////////////////////////////////// +// 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; +} \ No newline at end of file diff --git a/ambed/cfixedgain.h b/ambed/cfixedgain.h new file mode 100644 index 0000000..39a2a3f --- /dev/null +++ b/ambed/cfixedgain.h @@ -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 . +// ---------------------------------------------------------------------------- +// 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 */ \ No newline at end of file diff --git a/ambed/csampleprocessor.h b/ambed/csampleprocessor.h new file mode 100644 index 0000000..c9ec932 --- /dev/null +++ b/ambed/csampleprocessor.h @@ -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 . +// ---------------------------------------------------------------------------- +// Geoffrey Merck F4FXL / KC3FRA + +#ifndef csampleprocessor_h +#define csampleprocessor_h + +class CSampleProcessor +{ +public: + //processing + virtual float ProcessSample(float input) = 0; +}; + +#endif /* csampleprocessor_h */ \ No newline at end of file diff --git a/ambed/csignalprocessor.cpp b/ambed/csignalprocessor.cpp new file mode 100644 index 0000000..99ed30a --- /dev/null +++ b/ambed/csignalprocessor.cpp @@ -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 . +// ---------------------------------------------------------------------------- +// 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); + } +} \ No newline at end of file diff --git a/ambed/csignalprocessor.h b/ambed/csignalprocessor.h new file mode 100644 index 0000000..da7cedf --- /dev/null +++ b/ambed/csignalprocessor.h @@ -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 . +// ---------------------------------------------------------------------------- +// Geoffrey Merck F4FXL / KC3FRA + +#ifndef csignalprocessor_h +#define csignalprocessor_h + +#include +#include "csampleprocessor.h" + +class CSignalProcessor +{ +public: + //Constructor + CSignalProcessor(float gaindB); + + //Destructor + ~CSignalProcessor(); + + //Processing + void Process(uint8* voice, int length); + +private: + std::vector m_sampleProcessors; +}; + +#endif /* csignalprocessor_h */ \ No newline at end of file diff --git a/ambed/cusb3xxxinterface.cpp b/ambed/cusb3xxxinterface.cpp index 49c1269..c15801c 100644 --- a/ambed/cusb3xxxinterface.cpp +++ b/ambed/cusb3xxxinterface.cpp @@ -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(); } diff --git a/ambed/cvocodecchannel.cpp b/ambed/cvocodecchannel.cpp index 8ce57f8..7a10d41 100644 --- a/ambed/cvocodecchannel.cpp +++ b/ambed/cvocodecchannel.cpp @@ -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 diff --git a/ambed/cvocodecchannel.h b/ambed/cvocodecchannel.h index 735fa00..9726b9d 100644 --- a/ambed/cvocodecchannel.h +++ b/ambed/cvocodecchannel.h @@ -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; }; //////////////////////////////////////////////////////////////////////////////////////// diff --git a/ambed/cvoicepacket.cpp b/ambed/cvoicepacket.cpp index 0fc05f7..3cfb66e 100644 --- a/ambed/cvoicepacket.cpp +++ b/ambed/cvoicepacket.cpp @@ -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); - } -} diff --git a/ambed/cvoicepacket.h b/ambed/cvoicepacket.h index 7dbefc3..bb8e6ce 100644 --- a/ambed/cvoicepacket.h +++ b/ambed/cvoicepacket.h @@ -58,9 +58,6 @@ public: // set void SetVoice(const uint8 *, int); - // gain - void ApplyGain(int); - protected: // data int m_iSize;