2015-11-16 23:49:54 -05:00
|
|
|
#include "ModemFMStereo.h"
|
|
|
|
|
|
|
|
ModemFMStereo::ModemFMStereo() {
|
2015-11-18 00:23:04 -05:00
|
|
|
demodFM = freqdem_create(0.5);
|
2016-07-26 19:25:39 -04:00
|
|
|
_demph = 75;
|
2015-11-16 23:49:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ModemFMStereo::~ModemFMStereo() {
|
2016-03-01 19:47:18 -05:00
|
|
|
freqdem_destroy(demodFM);
|
2015-11-16 23:49:54 -05:00
|
|
|
}
|
|
|
|
|
2015-11-18 23:40:30 -05:00
|
|
|
std::string ModemFMStereo::getType() {
|
|
|
|
return "analog";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ModemFMStereo::getName() {
|
|
|
|
return "FMS";
|
|
|
|
}
|
|
|
|
|
2016-07-24 15:25:17 -04:00
|
|
|
ModemBase *ModemFMStereo::factory() {
|
2015-11-16 23:49:54 -05:00
|
|
|
return new ModemFMStereo;
|
|
|
|
}
|
|
|
|
|
2016-01-28 15:49:40 -05:00
|
|
|
int ModemFMStereo::checkSampleRate(long long sampleRate, int /* audioSampleRate */) {
|
2015-11-22 19:56:25 -05:00
|
|
|
if (sampleRate < 100000) {
|
|
|
|
return 100000;
|
2015-11-22 23:38:26 -05:00
|
|
|
} else if (sampleRate < 1500) {
|
|
|
|
return 1500;
|
2015-11-22 19:56:25 -05:00
|
|
|
} else {
|
|
|
|
return sampleRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 21:58:54 -05:00
|
|
|
int ModemFMStereo::getDefaultSampleRate() {
|
|
|
|
return 200000;
|
|
|
|
}
|
|
|
|
|
2016-07-26 19:25:39 -04:00
|
|
|
ModemArgInfoList ModemFMStereo::getSettings() {
|
|
|
|
ModemArgInfoList args;
|
|
|
|
|
|
|
|
ModemArgInfo demphArg;
|
|
|
|
demphArg.key = "demph";
|
|
|
|
demphArg.name = "De-emphasis";
|
|
|
|
demphArg.value = std::to_string(_demph);
|
|
|
|
demphArg.description = "FM Stereo De-Emphasis, typically 75us in US/Canada, 50us elsewhere.";
|
|
|
|
|
|
|
|
demphArg.type = ModemArgInfo::STRING;
|
|
|
|
|
|
|
|
std::vector<std::string> demphOptNames;
|
|
|
|
demphOptNames.push_back("None");
|
2016-07-26 23:47:31 -04:00
|
|
|
demphOptNames.push_back("10us");
|
|
|
|
demphOptNames.push_back("25us");
|
|
|
|
demphOptNames.push_back("32us");
|
2016-07-26 23:34:49 -04:00
|
|
|
demphOptNames.push_back("50us");
|
|
|
|
demphOptNames.push_back("75us");
|
2016-07-26 19:25:39 -04:00
|
|
|
demphArg.optionNames = demphOptNames;
|
|
|
|
|
|
|
|
std::vector<std::string> demphOpts;
|
|
|
|
demphOpts.push_back("0");
|
2016-07-26 23:47:31 -04:00
|
|
|
demphOpts.push_back("10");
|
|
|
|
demphOpts.push_back("25");
|
|
|
|
demphOpts.push_back("32");
|
2016-07-26 19:25:39 -04:00
|
|
|
demphOpts.push_back("50");
|
|
|
|
demphOpts.push_back("75");
|
|
|
|
demphArg.options = demphOpts;
|
|
|
|
|
|
|
|
args.push_back(demphArg);
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModemFMStereo::writeSetting(std::string setting, std::string value) {
|
|
|
|
if (setting == "demph") {
|
|
|
|
_demph = std::stoi(value);
|
|
|
|
rebuildKit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ModemFMStereo::readSetting(std::string setting) {
|
|
|
|
if (setting == "demph") {
|
|
|
|
return std::to_string(_demph);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-11-16 23:49:54 -05:00
|
|
|
ModemKit *ModemFMStereo::buildKit(long long sampleRate, int audioSampleRate) {
|
|
|
|
ModemKitFMStereo *kit = new ModemKitFMStereo;
|
|
|
|
|
|
|
|
kit->audioResampleRatio = double(audioSampleRate) / double(sampleRate);
|
2015-11-22 23:25:45 -05:00
|
|
|
kit->sampleRate = sampleRate;
|
|
|
|
kit->audioSampleRate = audioSampleRate;
|
|
|
|
|
2015-11-16 23:49:54 -05:00
|
|
|
float As = 60.0f; // stop-band attenuation [dB]
|
|
|
|
|
|
|
|
kit->audioResampler = msresamp_rrrf_create(kit->audioResampleRatio, As);
|
|
|
|
kit->stereoResampler = msresamp_rrrf_create(kit->audioResampleRatio, As);
|
|
|
|
|
|
|
|
// Stereo filters / shifters
|
|
|
|
double firStereoCutoff = 16000.0 / double(audioSampleRate);
|
|
|
|
// filter transition
|
2016-06-01 19:42:34 -04:00
|
|
|
float ft = 1000.0f / double(audioSampleRate);
|
2015-11-16 23:49:54 -05:00
|
|
|
// fractional timing offset
|
|
|
|
float mu = 0.0f;
|
|
|
|
|
|
|
|
if (firStereoCutoff < 0) {
|
|
|
|
firStereoCutoff = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firStereoCutoff > 0.5) {
|
|
|
|
firStereoCutoff = 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int h_len = estimate_req_filter_len(ft, As);
|
|
|
|
float *h = new float[h_len];
|
|
|
|
liquid_firdes_kaiser(h_len, firStereoCutoff, As, mu, h);
|
|
|
|
|
|
|
|
kit->firStereoLeft = firfilt_rrrf_create(h, h_len);
|
|
|
|
kit->firStereoRight = firfilt_rrrf_create(h, h_len);
|
|
|
|
|
|
|
|
// stereo pilot filter
|
2016-06-01 19:42:34 -04:00
|
|
|
float bw = float(sampleRate);
|
2015-11-16 23:49:54 -05:00
|
|
|
if (bw < 100000.0) {
|
|
|
|
bw = 100000.0;
|
|
|
|
}
|
|
|
|
unsigned int order = 5; // filter order
|
2016-06-01 19:42:34 -04:00
|
|
|
float f0 = ((float) 19000 / bw);
|
|
|
|
float fc = ((float) 19500 / bw);
|
2015-11-16 23:49:54 -05:00
|
|
|
float Ap = 1.0f;
|
|
|
|
|
|
|
|
kit->iirStereoPilot = iirfilt_crcf_create_prototype(LIQUID_IIRDES_CHEBY2, LIQUID_IIRDES_BANDPASS, LIQUID_IIRDES_SOS, order, fc, f0, Ap, As);
|
|
|
|
|
2016-03-01 19:47:18 -05:00
|
|
|
kit->firStereoR2C = firhilbf_create(5, 60.0f);
|
|
|
|
kit->firStereoC2R = firhilbf_create(5, 60.0f);
|
|
|
|
|
|
|
|
kit->stereoPilot = nco_crcf_create(LIQUID_VCO);
|
|
|
|
nco_crcf_reset(kit->stereoPilot);
|
|
|
|
nco_crcf_pll_set_bandwidth(kit->stereoPilot, 0.25f);
|
|
|
|
|
2016-07-26 23:34:49 -04:00
|
|
|
kit->demph = _demph;
|
|
|
|
|
|
|
|
if (_demph) {
|
2016-07-27 20:01:37 -04:00
|
|
|
float f = (1.0f / (2.0f * M_PI * double(_demph) * 1e-6));
|
|
|
|
float t = 1.0f / (2.0f * M_PI * f);
|
|
|
|
t = 1.0f / (2.0f * float(audioSampleRate) * tan(1.0f / (2.0f * float(audioSampleRate) * t)));
|
2016-07-26 23:34:49 -04:00
|
|
|
|
|
|
|
float tb = (1.0f + 2.0f * t * float(audioSampleRate));
|
|
|
|
float b_demph[2] = { 1.0f / tb, 1.0f / tb };
|
|
|
|
float a_demph[2] = { 1.0f, (1.0f - 2.0f * t * float(audioSampleRate)) / tb };
|
|
|
|
|
|
|
|
kit->iirDemphL = iirfilt_rrrf_create(b_demph, 2, a_demph, 2);
|
|
|
|
kit->iirDemphR = iirfilt_rrrf_create(b_demph, 2, a_demph, 2);
|
|
|
|
} else {
|
|
|
|
kit->iirDemphL = nullptr;
|
|
|
|
kit->iirDemphR = nullptr;
|
|
|
|
kit->demph = 0;
|
|
|
|
}
|
2015-11-16 23:49:54 -05:00
|
|
|
return kit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModemFMStereo::disposeKit(ModemKit *kit) {
|
|
|
|
ModemKitFMStereo *fmkit = (ModemKitFMStereo *)kit;
|
|
|
|
|
|
|
|
msresamp_rrrf_destroy(fmkit->audioResampler);
|
|
|
|
msresamp_rrrf_destroy(fmkit->stereoResampler);
|
|
|
|
firfilt_rrrf_destroy(fmkit->firStereoLeft);
|
|
|
|
firfilt_rrrf_destroy(fmkit->firStereoRight);
|
2016-03-01 19:47:18 -05:00
|
|
|
firhilbf_destroy(fmkit->firStereoR2C);
|
|
|
|
firhilbf_destroy(fmkit->firStereoC2R);
|
|
|
|
nco_crcf_destroy(fmkit->stereoPilot);
|
2016-07-26 23:34:49 -04:00
|
|
|
if (fmkit->iirDemphR) { iirfilt_rrrf_destroy(fmkit->iirDemphR); }
|
|
|
|
if (fmkit->iirDemphL) { iirfilt_rrrf_destroy(fmkit->iirDemphL); }
|
2015-11-16 23:49:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModemFMStereo::demodulate(ModemKit *kit, ModemIQData *input, AudioThreadInput *audioOut) {
|
|
|
|
ModemKitFMStereo *fmkit = (ModemKitFMStereo *)kit;
|
2016-02-04 18:05:33 -05:00
|
|
|
size_t bufSize = input->data.size();
|
2015-11-16 23:49:54 -05:00
|
|
|
liquid_float_complex u, v, w, x, y;
|
|
|
|
|
|
|
|
double audio_resample_ratio = fmkit->audioResampleRatio;
|
|
|
|
|
|
|
|
if (demodOutputData.size() != bufSize) {
|
|
|
|
if (demodOutputData.capacity() < bufSize) {
|
|
|
|
demodOutputData.reserve(bufSize);
|
|
|
|
}
|
|
|
|
demodOutputData.resize(bufSize);
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:42:34 -04:00
|
|
|
size_t audio_out_size = (size_t)ceil((double) (bufSize) * audio_resample_ratio) + 512;
|
2015-11-16 23:49:54 -05:00
|
|
|
|
|
|
|
freqdem_demodulate_block(demodFM, &input->data[0], bufSize, &demodOutputData[0]);
|
|
|
|
|
|
|
|
if (resampledOutputData.size() != audio_out_size) {
|
|
|
|
if (resampledOutputData.capacity() < audio_out_size) {
|
|
|
|
resampledOutputData.reserve(audio_out_size);
|
|
|
|
}
|
|
|
|
resampledOutputData.resize(audio_out_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int numAudioWritten;
|
|
|
|
|
|
|
|
msresamp_rrrf_execute(fmkit->audioResampler, &demodOutputData[0], bufSize, &resampledOutputData[0], &numAudioWritten);
|
|
|
|
|
|
|
|
if (demodStereoData.size() != bufSize) {
|
|
|
|
if (demodStereoData.capacity() < bufSize) {
|
|
|
|
demodStereoData.reserve(bufSize);
|
|
|
|
}
|
|
|
|
demodStereoData.resize(bufSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
float phase_error = 0;
|
|
|
|
|
2016-02-04 18:05:33 -05:00
|
|
|
for (size_t i = 0; i < bufSize; i++) {
|
2015-11-16 23:49:54 -05:00
|
|
|
// real -> complex
|
2016-03-01 19:47:18 -05:00
|
|
|
firhilbf_r2c_execute(fmkit->firStereoR2C, demodOutputData[i], &x);
|
2015-11-16 23:49:54 -05:00
|
|
|
|
|
|
|
// 19khz pilot band-pass
|
|
|
|
iirfilt_crcf_execute(fmkit->iirStereoPilot, x, &v);
|
2016-03-01 19:47:18 -05:00
|
|
|
nco_crcf_cexpf(fmkit->stereoPilot, &w);
|
2015-11-16 23:49:54 -05:00
|
|
|
|
|
|
|
w.imag = -w.imag; // conjf(w)
|
|
|
|
|
|
|
|
// multiply u = v * conjf(w)
|
|
|
|
u.real = v.real * w.real - v.imag * w.imag;
|
|
|
|
u.imag = v.real * w.imag + v.imag * w.real;
|
|
|
|
|
|
|
|
// cargf(u)
|
|
|
|
phase_error = atan2f(u.imag,u.real);
|
|
|
|
|
|
|
|
// step pll
|
2016-03-01 19:47:18 -05:00
|
|
|
nco_crcf_pll_step(fmkit->stereoPilot, phase_error);
|
|
|
|
nco_crcf_step(fmkit->stereoPilot);
|
2015-11-16 23:49:54 -05:00
|
|
|
|
|
|
|
// 38khz down-mix
|
2016-03-01 19:47:18 -05:00
|
|
|
nco_crcf_mix_down(fmkit->stereoPilot, x, &y);
|
|
|
|
nco_crcf_mix_down(fmkit->stereoPilot, y, &x);
|
2015-11-16 23:49:54 -05:00
|
|
|
|
|
|
|
// complex -> real
|
2016-03-01 19:47:18 -05:00
|
|
|
firhilbf_c2r_execute(fmkit->firStereoC2R, x, &demodStereoData[i]);
|
2015-11-16 23:49:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// std::cout << "[PLL] phase error: " << phase_error;
|
|
|
|
// std::cout << " freq:" << (((nco_crcf_get_frequency(stereoPilot) / (2.0 * M_PI)) * inp->sampleRate)) << std::endl;
|
|
|
|
|
|
|
|
if (audio_out_size != resampledStereoData.size()) {
|
|
|
|
if (resampledStereoData.capacity() < audio_out_size) {
|
|
|
|
resampledStereoData.reserve(audio_out_size);
|
|
|
|
}
|
|
|
|
resampledStereoData.resize(audio_out_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
msresamp_rrrf_execute(fmkit->stereoResampler, &demodStereoData[0], bufSize, &resampledStereoData[0], &numAudioWritten);
|
|
|
|
|
|
|
|
audioOut->channels = 2;
|
|
|
|
if (audioOut->data.capacity() < (numAudioWritten * 2)) {
|
|
|
|
audioOut->data.reserve(numAudioWritten * 2);
|
|
|
|
}
|
|
|
|
audioOut->data.resize(numAudioWritten * 2);
|
2016-02-04 18:05:33 -05:00
|
|
|
for (size_t i = 0; i < numAudioWritten; i++) {
|
2015-11-16 23:49:54 -05:00
|
|
|
float l, r;
|
2016-07-26 23:34:49 -04:00
|
|
|
float ld, rd;
|
|
|
|
|
|
|
|
if (fmkit->demph) {
|
|
|
|
iirfilt_rrrf_execute(fmkit->iirDemphL, 0.568f * (resampledOutputData[i] - (resampledStereoData[i])), &ld);
|
|
|
|
iirfilt_rrrf_execute(fmkit->iirDemphR, 0.568f * (resampledOutputData[i] + (resampledStereoData[i])), &rd);
|
|
|
|
|
|
|
|
firfilt_rrrf_push(fmkit->firStereoLeft, ld);
|
|
|
|
firfilt_rrrf_execute(fmkit->firStereoLeft, &l);
|
|
|
|
|
|
|
|
firfilt_rrrf_push(fmkit->firStereoRight, rd);
|
|
|
|
firfilt_rrrf_execute(fmkit->firStereoRight, &r);
|
|
|
|
} else {
|
|
|
|
firfilt_rrrf_push(fmkit->firStereoLeft, 0.568f * (resampledOutputData[i] - (resampledStereoData[i])));
|
|
|
|
firfilt_rrrf_execute(fmkit->firStereoLeft, &l);
|
|
|
|
|
|
|
|
firfilt_rrrf_push(fmkit->firStereoRight, 0.568f * (resampledOutputData[i] + (resampledStereoData[i])));
|
|
|
|
firfilt_rrrf_execute(fmkit->firStereoRight, &r);
|
|
|
|
}
|
2015-11-16 23:49:54 -05:00
|
|
|
|
|
|
|
audioOut->data[i * 2] = l;
|
|
|
|
audioOut->data[i * 2 + 1] = r;
|
|
|
|
}
|
|
|
|
}
|