Skip to content

Commit

Permalink
Re-read sample rate and MTU AFTER stream activation to recompute Cubi…
Browse files Browse the repository at this point in the history
…cSDR buffers
  • Loading branch information
vsonnier committed Mar 10, 2019
1 parent 910f7b1 commit 485edba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
64 changes: 22 additions & 42 deletions src/sdr/SoapySDRThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,7 @@ bool SDRThread::init() {
//2. Set sample rate:
device->setSampleRate(SOAPY_SDR_RX, 0, sampleRate.load());

//2.2 Device-specific workarounds:
// TODO: explore bandwidth setting option to see if this is necessary for others
if (device->getDriverKey() == "bladeRF") {
device->setBandwidth(SOAPY_SDR_RX, 0, sampleRate.load());
}

//3. Re-read MTU and build Cubic buffers:
int streamMTU = device->getStreamMTU(stream);
mtuElems.store(streamMTU);

numChannels.store(getOptimalChannelCount(sampleRate.load()));
numElems.store(getOptimalElementCount(sampleRate.load(), TARGET_DISPLAY_FPS));

//fallback if mtuElems was wrong
if (!mtuElems.load()) {
mtuElems.store(numElems.load());
std::cout << "SDRThread::init(): Device Stream MTU is broken, use " << mtuElems.load() << "instead..." << std::endl << std::flush;
} else {
std::cout << "SDRThread::init(): Device Stream set to MTU: " << mtuElems.load() << std::endl << std::flush;
}

overflowBuffer.data.resize(mtuElems.load());

buffs[0] = ::malloc(mtuElems.load() * 4 * sizeof(float));
numOverflow = 0;

//3.2 Store Stream-specific parameters to current Device
//3. Store Stream-specific parameters to current Device
deviceInfo.load()->setStreamArgs(currentStreamArgs);
deviceConfig.load()->setStreamOpts(currentStreamArgs);

Expand Down Expand Up @@ -177,14 +151,14 @@ bool SDRThread::init() {
setting_value_changed.store(false);

} //leave lock guard scope

updateSettings();


wxGetApp().sdrThreadNotify(SDRThread::SDR_THREAD_INITIALIZED, std::string("Device Initialized."));

//5. Activate stream:
//5. Activate stream: (through update settings)
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, std::string("Activating stream."));
device->activateStream(stream);

rate_changed.store(true);
updateSettings();

//rebuild menu now that settings are really been applied.
wxGetApp().notifyMainUIOfDeviceChange(true);
Expand All @@ -195,8 +169,13 @@ bool SDRThread::init() {
void SDRThread::deinit() {
device->deactivateStream(stream);
device->closeStream(stream);

if (buffs[0] != nullptr) {
::free(buffs[0]);
buffs[0] = nullptr;
}

stream = nullptr;
::free(buffs[0]);
}

void SDRThread::assureBufferMinSize(SDRThreadIQData * dataOut, size_t minSize) {
Expand Down Expand Up @@ -470,34 +449,35 @@ void SDRThread::updateSettings() {
device->setBandwidth(SOAPY_SDR_RX, 0, sampleRate.load());
}

//3. Re-do Cubic buffers :
//re-read current sample rate:
//3. Re-activate stream:
device->activateStream(stream);

//4. Re-do Cubic buffers :
//re-read current sample rate and MTU:
sampleRate.store(device->getSampleRate(SOAPY_SDR_RX, 0));

numChannels.store(getOptimalChannelCount(sampleRate.load()));
numElems.store(getOptimalElementCount(sampleRate.load(), TARGET_DISPLAY_FPS));
//read (new) MTU size:
int streamMTU = device->getStreamMTU(stream);

mtuElems.store(streamMTU);

//fallback if mtuElems was wrong
if (!mtuElems.load()) {
mtuElems.store(numElems.load());
std::cout << "SDRThread::updateSettings(): Device Stream MTU is broken, use " << mtuElems.load() << "instead..." << std::endl << std::flush;
std::cout << "SDRThread: Device Stream MTU is broken, use " << mtuElems.load() << " instead..." << std::endl << std::flush;
} else {
std::cout << "SDRThread::updateSettings(): Device Stream changing to MTU: " << mtuElems.load() << std::endl << std::flush;
std::cout << "SDRThread : Device Stream set to MTU: " << mtuElems.load() << std::endl << std::flush;
}

overflowBuffer.data.resize(mtuElems.load());
::free(buffs[0]);
if (buffs[0] != nullptr) {
::free(buffs[0]);
}
buffs[0] = ::malloc(mtuElems.load() * 4 * sizeof(float));
//clear overflow buffer
numOverflow = 0;

//4. Re-activate stream:
device->activateStream(stream);

//
rate_changed.store(false);
doUpdate = true;
Expand Down
4 changes: 2 additions & 2 deletions src/sdr/SoapySDRThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ class SDRThread : public IOThread {
void updateSettings();
SoapySDR::Kwargs combineArgs(SoapySDR::Kwargs a, SoapySDR::Kwargs b);

SoapySDR::Stream *stream;
SoapySDR::Stream *stream = nullptr;
SoapySDR::Device *device;
void *buffs[1];
void *buffs[1] = { nullptr };
ReBuffer<SDRThreadIQData> buffers;
SDRThreadIQData overflowBuffer;
int numOverflow;
Expand Down

0 comments on commit 485edba

Please sign in to comment.