-
Notifications
You must be signed in to change notification settings - Fork 117
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
frankplow
wants to merge
4
commits into
IDI-Systems:master
Choose a base branch
from
frankplow:cvsd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1b8d125
Pass modulation type to TS plugin; also clean up surrounding plugin code
frankplow f5e9348
implement cvsd codec
frankplow f2adeb7
fix anti-aliasing filter and tweak constants
frankplow 04b508d
Fix a potential TS crash resulting from improperly configured radios
frankplow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "RadioEffectAnalogue.h" | ||
|
||
|
||
CRadioEffectAnalogue::CRadioEffectAnalogue() { | ||
radioFilter = new CFilterRadio(); | ||
this->setParam("signalQuality", 0.0f); | ||
}; | ||
CRadioEffectAnalogue::~CRadioEffectAnalogue() { | ||
delete radioFilter; | ||
} | ||
void CRadioEffectAnalogue::process(short *samples, int sampleCount) { | ||
bool noise = true; | ||
if (this->getParam<bool>("disableNoise")) { | ||
noise = false; | ||
} | ||
|
||
this->radioFilter->process( | ||
samples, | ||
sampleCount, | ||
1, | ||
this->getParam<acre::volume_t>("signalQuality"), | ||
noise); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include "RadioEffectCVSD.h" | ||
|
||
CRadioEffectCVSD::CVSDDecoder::CVSDDecoder() { | ||
lookback_register.reserve(lookback); | ||
} | ||
|
||
short CRadioEffectCVSD::CVSDDecoder::decode(bool sample) { | ||
// shift the new sample into the lookback shift register | ||
if (lookback_register.size() < lookback) { | ||
lookback_register.emplace_back(); | ||
} | ||
std::copy( | ||
std::next(lookback_register.rbegin()), | ||
lookback_register.rend(), | ||
lookback_register.rbegin()); | ||
if (!lookback_register.empty()) { | ||
lookback_register[0] = sample; | ||
} | ||
|
||
if (lookback_register.size() >= lookback) { | ||
// adjust delta | ||
bool positive_run = true; | ||
bool negative_run = true; | ||
for (bool val: lookback_register) { | ||
positive_run &= val; | ||
negative_run &= !val; | ||
} | ||
if (positive_run || negative_run) { | ||
delta = std::min(delta += delta_step, delta_max); | ||
} else { | ||
delta = std::max(delta *= delta_coef, delta_min); | ||
} | ||
} | ||
|
||
// adjust reference | ||
const short previous_reference = reference; | ||
if (sample) { | ||
reference += delta; | ||
if (reference < previous_reference) { | ||
reference = SHRT_MAX; | ||
} | ||
} else { | ||
reference -= delta; | ||
if (reference > previous_reference) { | ||
reference = SHRT_MIN; | ||
} | ||
} | ||
reference *= decay; | ||
|
||
return reference; | ||
} | ||
|
||
bool CRadioEffectCVSD::CVSDEncoder::encode(short sample) { | ||
const bool ret = sample > reference; | ||
reference = decoder.decode(ret); | ||
return ret; | ||
} | ||
|
||
CRadioEffectCVSD::CRadioEffectCVSD() { | ||
input_filter.setup(8, TS_SAMPLE_RATE, CVSD_RATE/2, 1); | ||
output_filter.setup(8, TS_SAMPLE_RATE, CVSD_RATE/4, 1); | ||
} | ||
|
||
// 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 0.05 * std::pow((signalQuality - 1), 2); | ||
} | ||
|
||
void CRadioEffectCVSD::process(short *samples, int sampleCount) { | ||
// anti-aliasing LPF prior to downsampling | ||
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) std::rand() / RAND_MAX < bit_error_rate) { | ||
coded ^= 1; | ||
} | ||
|
||
samples[i] = decoder.decode(coded); | ||
|
||
// boost the volume | ||
if (samples[i] < SHRT_MAX / VOL_BOOST) { | ||
samples[i] *= VOL_BOOST; | ||
} else { | ||
samples[i] = SHRT_MAX; | ||
} | ||
|
||
// upsample back to 48kHz | ||
for (std::size_t j = 1; j < TS_SAMPLE_RATE / CVSD_RATE; j++) { | ||
samples[i + j] = samples[i]; | ||
} | ||
} | ||
|
||
output_filter.process(sampleCount, &samples); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <climits> | ||
|
||
#include "compat.h" | ||
|
||
#include "SoundMonoEffect.h" | ||
#include "AcreDsp.h" | ||
|
||
// the CVSD rate must be a factor of the sample rate | ||
#define TS_SAMPLE_RATE 48000 | ||
#define CVSD_RATE 16000 | ||
|
||
// this is chosen to match the volume of other radio effects | ||
#define VOL_BOOST 3 | ||
|
||
class CRadioEffectCVSD : public CSoundMonoEffect { | ||
private: | ||
class CVSDDecoder { | ||
private: | ||
std::vector<bool> lookback_register; | ||
const std::size_t lookback = 3; | ||
const short delta_min = 256; | ||
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; | ||
|
||
public: | ||
CVSDDecoder(); | ||
|
||
short decode(bool sample); | ||
}; | ||
|
||
class CVSDEncoder { | ||
private: | ||
CVSDDecoder decoder; | ||
|
||
short reference = 0; | ||
|
||
public: | ||
bool encode(short sample); | ||
}; | ||
|
||
CVSDEncoder encoder; | ||
CVSDDecoder decoder; | ||
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.