From 0fc47bc9161c2ca2c5845ae115ea88894c257332 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Fri, 26 Dec 2014 20:58:42 -0500 Subject: [PATCH] FM Stereo experiment, partly working --- src/demod/DemodDefs.h | 3 +- src/demod/DemodulatorInstance.cpp | 10 ++++++ src/demod/DemodulatorInstance.h | 4 +++ src/demod/DemodulatorPreThread.cpp | 1 + src/demod/DemodulatorThread.cpp | 54 ++++++++++++++++++++++++++++-- src/demod/DemodulatorThread.h | 4 +++ src/visual/WaterfallCanvas.cpp | 10 ++++++ 7 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/demod/DemodDefs.h b/src/demod/DemodDefs.h index c8ddf22..a1d2cdd 100644 --- a/src/demod/DemodDefs.h +++ b/src/demod/DemodDefs.h @@ -70,13 +70,14 @@ public: class DemodulatorThreadPostIQData: public ReferenceCounter { public: std::vector data; + int bandwidth; float audio_resample_ratio; msresamp_rrrf audio_resampler; float resample_ratio; msresamp_crcf resampler; DemodulatorThreadPostIQData() : - audio_resample_ratio(0), audio_resampler(NULL), resample_ratio(0), resampler(NULL) { + bandwidth(0), audio_resample_ratio(0), audio_resampler(NULL), resample_ratio(0), resampler(NULL) { } diff --git a/src/demod/DemodulatorInstance.cpp b/src/demod/DemodulatorInstance.cpp index ab26849..746468e 100644 --- a/src/demod/DemodulatorInstance.cpp +++ b/src/demod/DemodulatorInstance.cpp @@ -146,6 +146,16 @@ void DemodulatorInstance::setActive(bool state) { audioThread->setActive(state); } +bool DemodulatorInstance::isStereo() { + return stereo; +} + +void DemodulatorInstance::setStereo(bool state) { + stereo = state; + demodulatorThread->setStereo(state); +} + + void DemodulatorInstance::squelchAuto() { DemodulatorThreadControlCommand command; command.cmd = DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_SQUELCH_AUTO; diff --git a/src/demod/DemodulatorInstance.h b/src/demod/DemodulatorInstance.h index 2853d6c..8d0ee39 100644 --- a/src/demod/DemodulatorInstance.h +++ b/src/demod/DemodulatorInstance.h @@ -49,6 +49,9 @@ public: bool isActive(); void setActive(bool state); + bool isStereo(); + void setStereo(bool state); + void squelchAuto();bool isSquelchEnabled(); void setSquelchEnabled(bool state); @@ -56,5 +59,6 @@ private: std::atomic label;bool terminated;bool demodTerminated;bool audioTerminated;bool preDemodTerminated; std::atomic active; std::atomic squelch; + std::atomic stereo; }; diff --git a/src/demod/DemodulatorPreThread.cpp b/src/demod/DemodulatorPreThread.cpp index 065141f..57bf7eb 100644 --- a/src/demod/DemodulatorPreThread.cpp +++ b/src/demod/DemodulatorPreThread.cpp @@ -219,6 +219,7 @@ void DemodulatorPreThread::threadMain() { resamp->audio_resampler = audio_resampler; resamp->resample_ratio = resample_ratio; resamp->resampler = resampler; + resamp->bandwidth = params.bandwidth; postInputQueue->push(resamp); } diff --git a/src/demod/DemodulatorThread.cpp b/src/demod/DemodulatorThread.cpp index 8b4f7ea..0222efb 100644 --- a/src/demod/DemodulatorThread.cpp +++ b/src/demod/DemodulatorThread.cpp @@ -8,7 +8,7 @@ DemodulatorThread::DemodulatorThread(DemodulatorThreadPostInputQueue* pQueue, DemodulatorThreadControlCommandQueue *threadQueueControl, DemodulatorThreadCommandQueue* threadQueueNotify) : - postInputQueue(pQueue), visOutQueue(NULL), audioInputQueue(NULL), agc(NULL), terminated(false), threadQueueNotify(threadQueueNotify), threadQueueControl( + postInputQueue(pQueue), visOutQueue(NULL), audioInputQueue(NULL), agc(NULL), stereo(false), terminated(false), threadQueueNotify(threadQueueNotify), threadQueueControl( threadQueueControl), squelch_level(0), squelch_tolerance(0), squelch_enabled(false) { float kf = 0.5; // modulation factor @@ -44,7 +44,11 @@ void DemodulatorThread::threadMain() { std::vector resampled_data; std::vector agc_data; std::vector demod_output; + std::vector demod_output_stereo; std::vector resampled_audio_output; + std::vector resampled_audio_output_stereo; + + double freq_index = 0; while (!terminated) { DemodulatorThreadPostIQData *inp; @@ -89,24 +93,49 @@ void DemodulatorThread::threadMain() { if (demod_output.size() != num_written) { if (demod_output.capacity() < num_written) { demod_output.reserve(num_written); + demod_output_stereo.reserve(num_written); } demod_output.resize(num_written); + demod_output_stereo.resize(num_written); } freqdem_demodulate_block(fdem, &agc_data[0], num_written, &demod_output[0]); + if (stereo) { + int shift_freq = 38000-inp->bandwidth; + double freq = (2.0 * M_PI) * (((double) abs(shift_freq)) / ((double) inp->bandwidth)); + + for (int i = 0; i < num_written; i++) { + freq_index+=freq; + + demod_output_stereo[i] = demod_output[i] * sin(freq_index) + demod_output[i] * cos(freq_index); + while (freq_index > (M_PI*2.0)) { + freq_index -= (M_PI*2.0); + } + while (freq_index < (M_PI*2.0)) { + freq_index += (M_PI*2.0); + } + } + } + int audio_out_size = ceil((float) (num_written) * audio_resample_ratio); if (audio_out_size != resampled_audio_output.size()) { if (resampled_audio_output.capacity() < audio_out_size) { resampled_audio_output.reserve(audio_out_size); + resampled_audio_output_stereo.reserve(audio_out_size); } resampled_audio_output.resize(audio_out_size); + resampled_audio_output_stereo.resize(audio_out_size); } unsigned int num_audio_written; msresamp_rrrf_execute(audio_resampler, &demod_output[0], num_written, &resampled_audio_output[0], &num_audio_written); + if (stereo) { + msresamp_rrrf_execute(audio_resampler, &demod_output_stereo[0], num_written, &resampled_audio_output_stereo[0], &num_audio_written); + } + if (audioInputQueue != NULL) { if (!squelch_enabled || ((agc_crcf_get_signal_level(agc)) >= 0.1)) { AudioThreadInput *ati = NULL; @@ -124,8 +153,18 @@ void DemodulatorThread::threadMain() { } ati->setRefCount(1); - ati->channels = 1; - ati->data.assign(resampled_audio_output.begin(), resampled_audio_output.begin() + num_audio_written); + + if (stereo) { + ati->channels = 2; + ati->data.resize(num_audio_written*2); + for (int i = 0; i < num_audio_written; i++) { + ati->data[i*2] = (resampled_audio_output[i]-(resampled_audio_output_stereo[i]*2.0))/2.0; + ati->data[i*2+1] = (resampled_audio_output[i]+(resampled_audio_output_stereo[i]*2.0))/2.0; + } + } else { + ati->channels = 1; + ati->data.assign(resampled_audio_output.begin(), resampled_audio_output.begin() + num_audio_written); + } audioInputQueue->push(ati); } @@ -204,3 +243,12 @@ void DemodulatorThread::terminate() { DemodulatorThreadPostIQData *inp = new DemodulatorThreadPostIQData; // push dummy to nudge queue postInputQueue->push(inp); } + + +void DemodulatorThread::setStereo(bool state) { + stereo = state; +} + +bool DemodulatorThread::isStereo() { + return stereo; +} diff --git a/src/demod/DemodulatorThread.h b/src/demod/DemodulatorThread.h index 3fb4bf6..a546e55 100644 --- a/src/demod/DemodulatorThread.h +++ b/src/demod/DemodulatorThread.h @@ -35,6 +35,9 @@ public: void terminate(); + void setStereo(bool state); + bool isStereo(); + #ifdef __APPLE__ static void *pthread_helper(void *context) { return ((DemodulatorThread *) context)->threadMain(); @@ -49,6 +52,7 @@ protected: freqdem fdem; agc_crcf agc; + std::atomic stereo; std::atomic terminated; DemodulatorThreadCommandQueue* threadQueueNotify; diff --git a/src/visual/WaterfallCanvas.cpp b/src/visual/WaterfallCanvas.cpp index 2d07282..6523da1 100644 --- a/src/visual/WaterfallCanvas.cpp +++ b/src/visual/WaterfallCanvas.cpp @@ -200,6 +200,16 @@ void WaterfallCanvas::OnKeyDown(wxKeyEvent& event) { activeDemod->squelchAuto(); } break; + case WXK_SPACE: + if (!activeDemod) { + break; + } + if (activeDemod->isStereo()) { + activeDemod->setStereo(false); + } else { + activeDemod->setStereo(true); + } + break; default: event.Skip(); return;