1
0
mirror of https://github.com/ShaYmez/xlxd.git synced 2024-12-22 01:20:56 -05:00

Merge pull request #153 from F4FXL/TranscodingTweaks

Transcoding tweaks
This commit is contained in:
LX3JL 2020-04-06 11:21:54 +02:00 committed by GitHub
commit c8e28e77d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 612 additions and 21 deletions

94
ambed/cagc.cpp Normal file
View File

@ -0,0 +1,94 @@
//
// 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 code borrowed from Liquid DSP
// 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 <math.h>
#include "cagc.h"
#include "main.h"
////////////////////////////////////////////////////////////////////////////////////////
// constructor
CAGC::CAGC(float initialLeveldB)
{
// set internal gain appropriately
m_Gain = pow(10.0f, initialLeveldB/20.0f);
//+- 10dB Margin, TODO Move margin to constant
m_GainMax = pow(10.0f, (initialLeveldB + AGC_CLAMPING)/20.0f);
m_GainMin = pow(10.0f, (initialLeveldB - AGC_CLAMPING)/20.0f);
m_EnergyPrime = 1.0f;
// We do not target full scale to avoid stauration
m_targetEnergy = 32767.0f * pow(10.0f, (initialLeveldB - 25.0)/20.0f);//25 dB below saturation as stated in docs
//we also substract our target gain
//this is the time constant of our AGC...
m_Bandwidth = 1e-2f;//TODO : Move to parameter ?
m_Alpha = m_Bandwidth;
}
////////////////////////////////////////////////////////////////////////////////////////
// get
float CAGC::GetGain()
{
return 20.0f*log10(m_Gain);
}
////////////////////////////////////////////////////////////////////////////////////////
// process
inline void CAGC::ProcessSampleBlock(uint8* voice, int length)
{
for(int i = 0; i < length; i += 2)
{
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
//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;
// 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) );
// 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);
}
}

56
ambed/cagc.h Normal file
View File

@ -0,0 +1,56 @@
//
// cagc.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 AGC code largely inspired by Liquid DSP
// 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
#ifndef cagc_h
#define cagc_h
#include "csampleblockprocessor.h"
class CAGC : CSampleBlockProcessor
{
public:
//Constructor
CAGC(float initialLeveldB);
//methods
void ProcessSampleBlock(uint8* voice, int length) ;
float GetGain();//gets current gain
private:
float m_Gain; // current gain value
float m_GainMax, m_GainMin; //gain clamping
float m_targetEnergy; // scale value for target energy
// gain control loop filter parameters
float m_Bandwidth; // bandwidth-time constant
float m_Alpha; // feed-back gain
// signal level estimate
float m_EnergyPrime; // filtered output signal energy estimate
};
#endif /* cgc_h */

75
ambed/cfirfilter.cpp Normal file
View File

@ -0,0 +1,75 @@
//
// cfirfilter.cpp
// 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/>.
// ----------------------------------------------------------------------------
// FIRFilter by Geoffrey Merck F4FXL / KC3FRA
#include "cfirfilter.h"
#include <string.h>
CFIRFilter::CFIRFilter(const float* taps, int tapsLength)
{
m_taps = new float[tapsLength];
m_buffer = new float[tapsLength];
m_tapsLength = tapsLength;
::memcpy(m_taps, taps, tapsLength * sizeof(float));
::memset(m_buffer, 0, tapsLength * sizeof(float));
m_currentBufferPosition = 0;
}
CFIRFilter::~CFIRFilter()
{
delete[] m_taps;
delete[] m_buffer;
}
inline void CFIRFilter::ProcessSampleBlock(uint8* voice, int length)
{
for(int i = 0; i < length; i += 2)
{
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
float output = 0.0f;
int iTaps = 0;
// Buffer latest sample into delay line
m_buffer[m_currentBufferPosition] = input;
for(int i = m_currentBufferPosition; i >= 0; i--)
{
output += m_taps[iTaps++] * m_buffer[i];
}
for(int i = m_tapsLength - 1; i > m_currentBufferPosition; i--)
{
output += m_taps[iTaps++] * m_buffer[i];
}
m_currentBufferPosition = (m_currentBufferPosition + 1) % m_tapsLength;
//write processed sample back
voice[i] = HIBYTE((short)output);
voice[i+1] = LOBYTE((short)output);
}
}

51
ambed/cfirfilter.h Normal file
View File

@ -0,0 +1,51 @@
//
// cfirfilter.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/>.
// ----------------------------------------------------------------------------
// FIRFilter by Geoffrey Merck F4FXL / KC3FRA
#ifndef cfirfilter_h
#define cfirfilter_h
#include "csampleblockprocessor.h"
class CFIRFilter : CSampleBlockProcessor
{
public :
//Constructor
CFIRFilter(const float* taps, int tapsLength);
// Destructor
~CFIRFilter();
// Processing
void ProcessSampleBlock(uint8* voice, int length);
private:
float* m_taps;
int m_tapsLength;
float* m_buffer;
int m_currentBufferPosition;
};
#endif //cfirfilter_h

53
ambed/cfixedgain.cpp Normal file
View File

@ -0,0 +1,53 @@
//
// 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 void CFixedGain::ProcessSampleBlock(uint8* voice, int length)
{
for(int i = 0; i < length; i += 2)
{
float input = (float)(short)MAKEWORD(voice[i+1], voice[i]);
//apply gain
float output = input * m_gainLinear;
//write processed sample back
voice[i] = HIBYTE((short)output);
voice[i+1] = LOBYTE((short)output);
}
}

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 "csampleblockprocessor.h"
class CFixedGain : CSampleBlockProcessor
{
public:
//Constructor
CFixedGain(float gaindB);
//processing
void ProcessSampleBlock(uint8* voice, int length);
private:
float m_gaindB; //gain in dB
float m_gainLinear; //linearized gain
};
#endif /* cfixedgain_h */

View File

@ -0,0 +1,38 @@
//
// 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 csamplebloclprocessor_h
#define csamplebloclprocessor_h
#include "main.h"
class CSampleBlockProcessor
{
public:
//processing
virtual void ProcessSampleBlock(uint8* voice, int length) = 0;
};
#endif /* csampleprocessor_h */

View File

@ -0,0 +1,94 @@
//
// 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((CSampleBlockProcessor*)new CFIRFilter(FILTER_TAPS, FILTER_TAPS_LENGTH));
#endif
#if USE_AGC == 1
m_sampleProcessors.push_back((CSampleBlockProcessor*)new CAGC(gaindB));
#else
m_sampleProcessors.push_back((CSampleBlockProcessor*)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 j;*/
auto processorsSize = m_sampleProcessors.size();
for(int j = 0; j < processorsSize; j++)
{
m_sampleProcessors[j]->ProcessSampleBlock(voice, length);
}
/*for(int i = 0; i < length; i += 2)
{
//Get the sample
sample = (float)(short)MAKEWORD(voice[i+1], voice[i]);
for(j = 0; j < processorsSize; j++)
{
sample = m_sampleProcessors[j]->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 "csampleblockprocessor.h"
class CSignalProcessor
{
public:
//Constructor
CSignalProcessor(float gaindB);
//Destructor
~CSignalProcessor();
//Processing
void Process(uint8* voice, int length);
private:
std::vector<CSampleBlockProcessor *> m_sampleProcessors;
};
#endif /* csignalprocessor_h */

View File

@ -152,7 +152,7 @@ void CUsb3xxxInterface::Task(void)
{
Queue = Channel->GetVoiceQueue();
CVoicePacket *clone = new CVoicePacket(VoicePacket);
clone->ApplyGain(Channel->GetSpeechGain());
Channel->ProcessSignal(*clone);
Queue->push(clone);
Channel->ReleaseVoiceQueue();
}

View File

@ -27,7 +27,6 @@
#include "cvocodecchannel.h"
#include "cvocodecinterface.h"
////////////////////////////////////////////////////////////////////////////////////////
// constructor
@ -39,6 +38,7 @@ CVocodecChannel::CVocodecChannel(CVocodecInterface *InterfaceIn, int iChIn, CVoc
m_InterfaceOut = InterfaceOut;
m_iChannelOut = iChOut;
m_iSpeechGain = iSpeechGain;
m_signalProcessor = new CSignalProcessor((float)m_iSpeechGain);
}
////////////////////////////////////////////////////////////////////////////////////////
@ -47,6 +47,7 @@ CVocodecChannel::CVocodecChannel(CVocodecInterface *InterfaceIn, int iChIn, CVoc
CVocodecChannel::~CVocodecChannel()
{
PurgeAllQueues();
delete m_signalProcessor;
}
////////////////////////////////////////////////////////////////////////////////////////
@ -92,6 +93,15 @@ uint8 CVocodecChannel::GetCodecOut(void) const
return m_InterfaceOut->GetChannelCodec(m_iChannelOut);
}
////////////////////////////////////////////////////////////////////////////////////////
// processing
void CVocodecChannel::ProcessSignal(CVoicePacket& voicePacket)
{
m_signalProcessor->Process(voicePacket.GetVoice(), voicePacket.GetVoiceSize());
}
////////////////////////////////////////////////////////////////////////////////////////
// queues helpers

View File

@ -27,6 +27,8 @@
#define cvocodecchannel_h
#include "cpacketqueue.h"
#include "csignalprocessor.h"
#include "cvoicepacket.h"
////////////////////////////////////////////////////////////////////////////////////////
// class
@ -54,6 +56,9 @@ public:
int GetChannelOut(void) const { return m_iChannelOut; }
int GetSpeechGain(void) const { return m_iSpeechGain; }
//Processing
void ProcessSignal(CVoicePacket& voicePacket);
// interfaces
bool IsInterfaceIn(const CVocodecInterface *interface) { return (interface == m_InterfaceIn); }
bool IsInterfaceOut(const CVocodecInterface *interface) { return (interface == m_InterfaceOut); }
@ -92,6 +97,8 @@ protected:
// settings
int m_iSpeechGain;
private:
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;

View File

@ -68,9 +68,14 @@
#define CODEC_AMBE2PLUS 2
// Transcoding speech gains
#define CODECGAIN_AMBEPLUS -10 // in dB
#define CODECGAIN_AMBEPLUS -10 // in dB
#define CODECGAIN_AMBE2PLUS +10 // in dB
// Transcoding Tweaks
#define USE_AGC 0
#define AGC_CLAMPING 3 //clamps the AGC gain to +- this value
#define USE_BANDPASSFILTER 1
// Timeouts -----------------------------------------------------
#define STREAM_ACTIVITY_TIMEOUT 3 // in seconds
@ -96,6 +101,39 @@ typedef unsigned int uint;
#define LOWORD(dw) ((uint16)(uint32)(dw & 0x0000FFFF))
#define HIWORD(dw) ((uint16)((((uint32)(dw)) >> 16) & 0xFFFF))
////////////////////////////////////////////////////////////////////////////////////////
// FIR Filter coefficients computed to be the closest to the recommended filter in
// Documentation
//
// Following GNU Octave script was used
/*
pkg load signal;
fsamp = 8000;
fcuts = [300 400 3000 3400];
mags = [0 1 0];
devs = [0.2 1 0.2];
[n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fsamp);
n = n + rem(n,2);
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale');
freqz(hh);
[H,f] = freqz(hh,1,1024,fsamp);
plot(f,abs(H))
disp(hh);
grid
*/
#if USE_BANDPASSFILTER == 1
const float FILTER_TAPS[] {
-0.05063341f, -0.00060337f, -0.08892498f, -0.02026701f, -0.05940750f, -0.10977641f, 0.03244024f, -0.22304499f,
0.11452865f, 0.72500000f, 0.11452865f, -0.22304499f, 0.03244024f, -0.10977641f, -0.05940750f, -0.02026701f,
-0.08892498f, -0.00060337f, -0.05063341f };
#define FILTER_TAPS_LENGTH 19
#endif
////////////////////////////////////////////////////////////////////////////////////////
// global objects