From d85c94ead0653f53aa6bdd765077fd16f3c82601 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Tue, 20 Oct 2015 23:57:54 -0400 Subject: [PATCH] Attempt best-match sample rate near "default" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix hard-coded usage of DEFAULT_SAMPLE_RATE, now “suggested” rate --- src/CubicSDR.cpp | 18 ++++++++++++++---- src/demod/DemodDefs.h | 2 +- src/sdr/SDRDeviceInfo.cpp | 15 +++++++++++++++ src/sdr/SDRDeviceInfo.h | 1 + src/sdr/SoapySDRThread.cpp | 2 +- src/sdr/SoapySDRThread.h | 2 +- 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/CubicSDR.cpp b/src/CubicSDR.cpp index 7c30efd..bad8c4c 100644 --- a/src/CubicSDR.cpp +++ b/src/CubicSDR.cpp @@ -112,7 +112,7 @@ long long strToFrequency(std::string freqStr) { } -CubicSDR::CubicSDR() : appframe(NULL), m_glContext(NULL), frequency(0), offset(0), ppm(0), snap(1), sampleRate(DEFAULT_SAMPLE_RATE), directSamplingMode(0), +CubicSDR::CubicSDR() : appframe(NULL), m_glContext(NULL), frequency(0), offset(0), ppm(0), snap(1), sampleRate(0), directSamplingMode(0), sdrThread(NULL), sdrPostThread(NULL), spectrumVisualThread(NULL), demodVisualThread(NULL), pipeSDRIQData(NULL), pipeIQVisualData(NULL), pipeAudioVisualData(NULL), t_SDR(NULL), t_PostSDR(NULL) { } @@ -411,10 +411,20 @@ void CubicSDR::setDevice(SDRDeviceInfo *dev) { // frequency = freqLow; // } - int rateHigh, rateLow; - rateLow = chan->getSampleRates()[0]; - rateHigh = chan->getSampleRates()[chan->getSampleRates().size()-1]; + // Try for a reasonable default sample rate. + if (!sampleRate) { + sampleRate = chan->getSampleRateNear(DEFAULT_SAMPLE_RATE); + } + int rateHigh, rateLow; + + rateHigh = rateLow = sampleRate; + + if (chan->getSampleRates().size()) { + rateLow = chan->getSampleRates()[0]; + rateHigh = chan->getSampleRates()[chan->getSampleRates().size()-1]; + } + if (sampleRate > rateHigh) { sampleRate = rateHigh; } else if (sampleRate < rateLow) { diff --git a/src/demod/DemodDefs.h b/src/demod/DemodDefs.h index 0f49ccd..3bedcce 100644 --- a/src/demod/DemodDefs.h +++ b/src/demod/DemodDefs.h @@ -144,7 +144,7 @@ public: int demodType; DemodulatorThreadParameters() : - frequency(0), sampleRate(DEFAULT_SAMPLE_RATE), bandwidth(200000), audioSampleRate(0), + frequency(0), sampleRate(0), bandwidth(200000), audioSampleRate(0), demodType(DEMOD_TYPE_FM) { } diff --git a/src/sdr/SDRDeviceInfo.cpp b/src/sdr/SDRDeviceInfo.cpp index 4c966bd..8111fc5 100644 --- a/src/sdr/SDRDeviceInfo.cpp +++ b/src/sdr/SDRDeviceInfo.cpp @@ -1,4 +1,5 @@ #include "SDRDeviceInfo.h" +#include SDRDeviceRange::SDRDeviceRange() { low = 0; @@ -84,6 +85,20 @@ std::vector &SDRDeviceChannel::getSampleRates() { return sampleRates; } +long SDRDeviceChannel::getSampleRateNear(long sampleRate_in) { + long returnRate = sampleRates[0]; + long sDelta = (long)sampleRate_in-sampleRates[0]; + long minDelta = abs(sDelta); + for (std::vector::iterator i = sampleRates.begin(); i != sampleRates.end(); i++) { + long thisDelta = abs(sampleRate_in - (*i)); + if (thisDelta < minDelta) { + minDelta = thisDelta; + returnRate = (*i); + } + } + return returnRate; +} + std::vector &SDRDeviceChannel::getFilterBandwidths() { return filterBandwidths; } diff --git a/src/sdr/SDRDeviceInfo.h b/src/sdr/SDRDeviceInfo.h index ffedc27..5a153a8 100644 --- a/src/sdr/SDRDeviceInfo.h +++ b/src/sdr/SDRDeviceInfo.h @@ -69,6 +69,7 @@ public: SDRDeviceRange &getRFRange(); std::vector &getSampleRates(); + long getSampleRateNear(long sampleRate_in); std::vector &getFilterBandwidths(); const bool& hasHardwareDC() const; diff --git a/src/sdr/SoapySDRThread.cpp b/src/sdr/SoapySDRThread.cpp index 3c17b8d..d9b9bc8 100644 --- a/src/sdr/SoapySDRThread.cpp +++ b/src/sdr/SoapySDRThread.cpp @@ -11,7 +11,7 @@ SDRThread::SDRThread() : IOThread() { deviceConfig.store(NULL); deviceInfo.store(NULL); - sampleRate.store(DEFAULT_SAMPLE_RATE); + sampleRate.store(0); frequency.store(0); offset.store(0); ppm.store(0); diff --git a/src/sdr/SoapySDRThread.h b/src/sdr/SoapySDRThread.h index d86044f..1a1b4e8 100644 --- a/src/sdr/SoapySDRThread.h +++ b/src/sdr/SoapySDRThread.h @@ -22,7 +22,7 @@ public: std::vector data; SDRThreadIQData() : - frequency(0), sampleRate(DEFAULT_SAMPLE_RATE), dcCorrected(true), numChannels(0) { + frequency(0), sampleRate(0), dcCorrected(true), numChannels(0) { }