Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Extensions - Add CVSD codec effect for AN/PRC-343 #1135

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions extensions/src/ACRE2Core/RadioEffectCVSD.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <climits>

#include "RadioEffectCVSD.h"

CRadioEffectCVSD::CVSDDecoder::CVSDDecoder() {
Expand Down Expand Up @@ -28,9 +26,9 @@ short CRadioEffectCVSD::CVSDDecoder::decode(bool sample) {
negative_run &= !val;
}
if (positive_run || negative_run) {
delta = std::min(delta *= delta_coef, delta_max);
delta = std::min(delta += delta_step, delta_max);
} else {
delta = std::max(delta /= delta_coef, delta_min);
delta = std::max(delta *= delta_coef, delta_min);
}
}

Expand Down Expand Up @@ -59,25 +57,26 @@ bool CRadioEffectCVSD::CVSDEncoder::encode(short sample) {
}

CRadioEffectCVSD::CRadioEffectCVSD() {
filter.setup(4, TS_SAMPLE_RATE, CVSD_RATE / 2, 1);
input_filter.setup(8, TS_SAMPLE_RATE, CVSD_RATE/2, 1);
output_filter.setup(8, TS_SAMPLE_RATE, CVSD_RATE/4, 1);
}

// this is a somewhat arbitrary function derived empirically to match the intelligibility
// this is an arbitrary function derived empirically to match the intelligibility
// of the existing signalQuality metric at a variety of distances.
float CRadioEffectCVSD::quality_to_ber(float signalQuality) {
return pow(0.4 * (signalQuality - 1), 2);
return 0.05 * std::pow((signalQuality - 1), 2);
}

void CRadioEffectCVSD::process(short *samples, int sampleCount) {
// anti-aliasing LPF prior to downsampling
filter.process(sampleCount, &samples);
input_filter.process(sampleCount, &samples);

for (std::size_t i = 0; i < sampleCount; i += TS_SAMPLE_RATE / CVSD_RATE) {
bool coded = encoder.encode(samples[i]);

// introduce bit errors
const float bit_error_rate = quality_to_ber(getParam<float>("signalQuality"));
if ((float) rand() / RAND_MAX < bit_error_rate) {
if ((float) std::rand() / RAND_MAX < bit_error_rate) {
coded ^= 1;
}

Expand All @@ -96,5 +95,5 @@ void CRadioEffectCVSD::process(short *samples, int sampleCount) {
}
}

filter.process(sampleCount, &samples);
output_filter.process(sampleCount, &samples);
}
14 changes: 8 additions & 6 deletions extensions/src/ACRE2Core/RadioEffectCVSD.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <vector>
#include <climits>

#include "compat.h"

Expand All @@ -20,10 +21,11 @@ class CRadioEffectCVSD : public CSoundMonoEffect {
private:
std::vector<bool> lookback_register;
const std::size_t lookback = 3;
const short delta_min = 32;
const short delta_max = delta_min * 16;
const short delta_coef = 2;
const float decay = 0.95;
const short delta_min = 256;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first comment on this. Can you use fixed size integers (for example std::int16_t)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done this using shorts as this is what the TS API uses for audio samples (which is a little odd). It's what all the existing effects use also.

const short delta_max = 16 * delta_min;
const short delta_step = delta_min;
const float delta_coef = std::exp(-1.0f / (5e-3 * CVSD_RATE));
const float decay = std::exp(-1.0f / (1e-3 * CVSD_RATE));

short reference = 0;
short delta = delta_min;
Expand All @@ -46,12 +48,12 @@ class CRadioEffectCVSD : public CSoundMonoEffect {

CVSDEncoder encoder;
CVSDDecoder decoder;
Dsp::SimpleFilter<Dsp::ChebyshevII::LowPass<4>, 1> filter;
Dsp::SimpleFilter<Dsp::ChebyshevI::LowPass<8>, 1> input_filter;
Dsp::SimpleFilter<Dsp::ChebyshevI::LowPass<8>, 1> output_filter;

public:
CRadioEffectCVSD();

static float quality_to_ber(float signalQuality);

void process(short* samples, int sampleCount);
};