From e78ee1b946ccc5880bcb34f43bf4c23ca789c743 Mon Sep 17 00:00:00 2001 From: f4exb Date: Thu, 13 Sep 2018 02:33:56 +0200 Subject: [PATCH] Make SDRDaemonSink -> DaemonSource work in all 16 / 24 bit samples combination --- .../channeltx/daemonsource/daemonsource.cpp | 3 +- sdrbase/channel/sdrdaemondatareadqueue.cpp | 17 ++++---- sdrbase/channel/sdrdaemondatareadqueue.h | 39 +++++++++++-------- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/plugins/channeltx/daemonsource/daemonsource.cpp b/plugins/channeltx/daemonsource/daemonsource.cpp index 267095de7..d23e1e100 100644 --- a/plugins/channeltx/daemonsource/daemonsource.cpp +++ b/plugins/channeltx/daemonsource/daemonsource.cpp @@ -46,7 +46,6 @@ DaemonSource::DaemonSource(DeviceSinkAPI *deviceAPI) : m_deviceAPI(deviceAPI), m_sourceThread(0), m_running(false), - m_dataReadQueue(SDR_TX_SAMP_SZ <= 16 ? 4 : 8), m_nbCorrectableErrors(0), m_nbUncorrectableErrors(0) { @@ -72,7 +71,7 @@ DaemonSource::~DaemonSource() void DaemonSource::pull(Sample& sample) { - m_dataReadQueue.readSample(sample); + m_dataReadQueue.readSample(sample, true); // true is scale for Tx } void DaemonSource::pullAudio(int nbSamples __attribute__((unused))) diff --git a/sdrbase/channel/sdrdaemondatareadqueue.cpp b/sdrbase/channel/sdrdaemondatareadqueue.cpp index c71158771..601a5f12a 100644 --- a/sdrbase/channel/sdrdaemondatareadqueue.cpp +++ b/sdrbase/channel/sdrdaemondatareadqueue.cpp @@ -25,12 +25,12 @@ const uint32_t SDRDaemonDataReadQueue::MinimumMaxSize = 10; -SDRDaemonDataReadQueue::SDRDaemonDataReadQueue(uint32_t sampleSize) : - m_sampleSize(sampleSize), +SDRDaemonDataReadQueue::SDRDaemonDataReadQueue() : m_dataBlock(0), m_maxSize(MinimumMaxSize), m_blockIndex(1), m_sampleIndex(0), + m_sampleCount(0), m_full(false) {} @@ -86,7 +86,7 @@ void SDRDaemonDataReadQueue::setSize(uint32_t size) } } -void SDRDaemonDataReadQueue::readSample(Sample& s) +void SDRDaemonDataReadQueue::readSample(Sample& s, bool scaleForTx) { // depletion/repletion state if (m_dataBlock == 0) @@ -96,7 +96,7 @@ void SDRDaemonDataReadQueue::readSample(Sample& s) qDebug("SDRDaemonDataReadQueue::readSample: initial pop new block: queue size: %u", length()); m_blockIndex = 1; m_dataBlock = m_dataReadQueue.takeFirst(); - convertDataToSample(s, m_blockIndex, m_sampleIndex); + convertDataToSample(s, m_blockIndex, m_sampleIndex, scaleForTx); m_sampleIndex++; m_sampleCount++; } @@ -108,11 +108,12 @@ void SDRDaemonDataReadQueue::readSample(Sample& s) return; } - uint32_t samplesPerBlock = SDRDaemonNbBytesPerBlock / m_sampleSize; + int sampleSize = m_dataBlock->m_superBlocks[m_blockIndex].m_header.m_sampleBytes * 2; + uint32_t samplesPerBlock = SDRDaemonNbBytesPerBlock / sampleSize; if (m_sampleIndex < samplesPerBlock) { - convertDataToSample(s, m_blockIndex, m_sampleIndex); + convertDataToSample(s, m_blockIndex, m_sampleIndex, scaleForTx); m_sampleIndex++; m_sampleCount++; } @@ -123,7 +124,7 @@ void SDRDaemonDataReadQueue::readSample(Sample& s) if (m_blockIndex < SDRDaemonNbOrginalBlocks) { - convertDataToSample(s, m_blockIndex, m_sampleIndex); + convertDataToSample(s, m_blockIndex, m_sampleIndex, scaleForTx); m_sampleIndex++; m_sampleCount++; } @@ -144,7 +145,7 @@ void SDRDaemonDataReadQueue::readSample(Sample& s) //qDebug("SDRDaemonDataReadQueue::readSample: pop new block: queue size: %u", length()); m_blockIndex = 1; m_dataBlock = m_dataReadQueue.takeFirst(); - convertDataToSample(s, m_blockIndex, m_sampleIndex); + convertDataToSample(s, m_blockIndex, m_sampleIndex, scaleForTx); m_sampleIndex++; m_sampleCount++; } diff --git a/sdrbase/channel/sdrdaemondatareadqueue.h b/sdrbase/channel/sdrdaemondatareadqueue.h index c20a8f9c6..ce120f8d2 100644 --- a/sdrbase/channel/sdrdaemondatareadqueue.h +++ b/sdrbase/channel/sdrdaemondatareadqueue.h @@ -31,12 +31,12 @@ class Sample; class SDRDaemonDataReadQueue { public: - SDRDaemonDataReadQueue(uint32_t sampleSize); + SDRDaemonDataReadQueue(); ~SDRDaemonDataReadQueue(); void push(SDRDaemonDataBlock* dataBlock); //!< push block on the queue SDRDaemonDataBlock* pop(); //!< Pop block from the queue - void readSample(Sample& s); //!< Read sample from queue + void readSample(Sample& s, bool scaleForTx = false); //!< Read sample from queue possibly scaling to Tx size uint32_t length() const { return m_dataReadQueue.size(); } //!< Returns queue length uint32_t size() const { return m_maxSize; } //!< Returns queue size (max length) void setSize(uint32_t size); //!< Sets the queue size (max length) @@ -45,7 +45,6 @@ public: static const uint32_t MinimumMaxSize; private: - uint32_t m_sampleSize; QQueue m_dataReadQueue; SDRDaemonDataBlock *m_dataBlock; uint32_t m_maxSize; @@ -54,25 +53,33 @@ private: uint32_t m_sampleCount; //!< use a counter capped below 2^31 as it is going to be converted to an int in the web interface bool m_full; //!< full condition was hit - inline void convertDataToSample(Sample& s, uint32_t blockIndex, uint32_t sampleIndex) + inline void convertDataToSample(Sample& s, uint32_t blockIndex, uint32_t sampleIndex, bool scaleForTx) { - if (sizeof(Sample) == m_sampleSize) + int sampleSize = m_dataBlock->m_superBlocks[blockIndex].m_header.m_sampleBytes * 2; // I/Q sample size in data block + int samplebits = m_dataBlock->m_superBlocks[blockIndex].m_header.m_sampleBits; // I or Q sample size in bits + int32_t iconv, qconv; + + if (sizeof(Sample) == sampleSize) // generally 16->16 or 24->24 bits { - s = *((Sample*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*m_sampleSize])); + s = *((Sample*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*sampleSize])); } - else if ((sizeof(Sample) == 4) && (m_sampleSize == 8)) + else if ((sizeof(Sample) == 4) && (sampleSize == 8)) // generally 24->16 bits { - int32_t rp = *( (int32_t*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*m_sampleSize]) ); - int32_t ip = *( (int32_t*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*m_sampleSize+4]) ); - s.setReal(rp>>8); - s.setImag(ip>>8); + iconv = ((int32_t*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*sampleSize]))[0]; + qconv = ((int32_t*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*sampleSize+4]))[0]; + iconv >>= scaleForTx ? (SDR_TX_SAMP_SZ-SDR_RX_SAMP_SZ) : (samplebits-SDR_RX_SAMP_SZ); + qconv >>= scaleForTx ? (SDR_TX_SAMP_SZ-SDR_RX_SAMP_SZ) : (samplebits-SDR_RX_SAMP_SZ); + s.setReal(iconv); + s.setImag(qconv); } - else if ((sizeof(Sample) == 8) && (m_sampleSize == 4)) + else if ((sizeof(Sample) == 8) && (sampleSize == 4)) // generally 16->24 bits { - int32_t rp = *( (int16_t*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*m_sampleSize]) ); - int32_t ip = *( (int16_t*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*m_sampleSize+2]) ); - s.setReal(rp<<8); - s.setImag(ip<<8); + iconv = ((int16_t*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*sampleSize]))[0]; + qconv = ((int16_t*) &(m_dataBlock->m_superBlocks[blockIndex].m_protectedBlock.buf[sampleIndex*sampleSize+2]))[0]; + iconv <<= scaleForTx ? (SDR_TX_SAMP_SZ-samplebits) : (SDR_RX_SAMP_SZ-samplebits); + qconv <<= scaleForTx ? (SDR_TX_SAMP_SZ-samplebits) : (SDR_RX_SAMP_SZ-samplebits); + s.setReal(iconv); + s.setImag(qconv); } else {