diff options
| author | hibby <d@vehibberd.com> | 2026-10-06 21:24:40 +0100 |
|---|---|---|
| committer | hibby <d@vehibberd.com> | 2026-10-06 21:24:40 +0100 |
| commit | c25cbdf6fb6d946175f4335fab77bfea601b56db (patch) | |
| tree | 6f7a94b7b81f999e434f61b73d47e337dfd26bf2 /Audio/TxAudioQueue.cpp | |
| parent | 71f5689252c9f13e70e1c24856904475b2307809 (diff) | |
| parent | 0685fcada9f95fe90f70b387cd7d757da8209624 (diff) | |
Merge tag 'debian/3.2.0_rc1+dfsg-2' into debian/trixie-backportsdebian/trixie-backports
tagging wsjtx 3.2.0~rc1+dfsg-2
Diffstat (limited to 'Audio/TxAudioQueue.cpp')
| -rw-r--r-- | Audio/TxAudioQueue.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/Audio/TxAudioQueue.cpp b/Audio/TxAudioQueue.cpp new file mode 100644 index 00000000..9217d199 --- /dev/null +++ b/Audio/TxAudioQueue.cpp @@ -0,0 +1,124 @@ +#include "TxAudioQueue.hpp" + +#include <atomic> + +#include "TxAudioQueueEpochSnapshot.hpp" +#include "Modulator/JttyPcmFifo.hpp" + +class TxAudioQueue::Impl +{ +public: + explicit Impl (qint64 capacity_samples) + : fifo {capacity_samples} + { + } + + bool tryEpoch (TxAudioQueueEpoch& result) const noexcept + { + auto const before = epoch_sequence.load (std::memory_order_acquire); + if (before & 1) + { + return false; + } + + auto const current_epoch = epoch.load (std::memory_order_relaxed); + // Keep payload reads ahead of the sequence validation load. + std::atomic_thread_fence (std::memory_order_acquire); + auto const after = epoch_sequence.load (std::memory_order_relaxed); + return TxAudioQueueDetail::tryMakeEpochSnapshot ( + before, current_epoch, after, result); + } + + void clear (TxAudioQueueEpoch value) + { + epoch_sequence.fetch_add (1, std::memory_order_acq_rel); + fifo.clear (value.value ()); + epoch.store (value.value (), std::memory_order_relaxed); + epoch_sequence.fetch_add (1, std::memory_order_release); + } + + JttyPcmFifo fifo; + std::atomic<qint64> epoch {0}; + std::atomic<quint64> epoch_sequence {0}; +}; + +TxAudioQueue::TxAudioQueue () + : TxAudioQueue {defaultCapacity ()} +{ +} + +TxAudioQueue::TxAudioQueue (qint64 capacity_samples) + : impl_ {new Impl {capacity_samples}} +{ +} + +TxAudioQueue::~TxAudioQueue () = default; + +TxAudioQueueEnqueueResult TxAudioQueue::enqueue ( + QVector<qint16> const& samples, TxAudioQueueEpoch epoch) +{ + return enqueue (samples.constData (), samples.size (), epoch); +} + +TxAudioQueueEnqueueResult TxAudioQueue::enqueue ( + qint16 const * samples, qint64 count, TxAudioQueueEpoch epoch) +{ + bool accepted {false}; + TxAudioQueueEpoch current_epoch; + if (impl_->tryEpoch (current_epoch) && epoch == current_epoch) + { + accepted = impl_->fifo.enqueue (samples, count, epoch.value ()); + } + return TxAudioQueueEnqueueResult {accepted, progress ()}; +} + +void TxAudioQueue::clear (TxAudioQueueEpoch epoch) +{ + impl_->clear (epoch); +} + +void TxAudioQueue::applyPendingReset () noexcept +{ + impl_->fifo.applyPendingReset (); +} + +qint16 TxAudioQueue::pullSample (qint64 drain_guard) noexcept +{ + return impl_->fifo.pullSample (drain_guard); +} + +TxAudioQueueDrainState TxAudioQueue::takeDrainReady () noexcept +{ + TxAudioQueueEpoch epoch_before; + if (!impl_->tryEpoch (epoch_before)) + { + return TxAudioQueueDrainState {}; + } + + auto const raw = impl_->fifo.takeDrainReady (); + TxAudioQueueEpoch epoch; + if (!impl_->tryEpoch (epoch) || !raw.ready + || epoch_before != epoch + || raw.epoch != epoch.value ()) + { + return TxAudioQueueDrainState {false, epoch, 0}; + } + return TxAudioQueueDrainState {true, epoch, raw.totalAtDrain}; +} + +TxAudioQueueProgress TxAudioQueue::progress () const noexcept +{ + TxAudioQueueEpoch epoch; + impl_->tryEpoch (epoch); + return TxAudioQueueProgress { + epoch, + impl_->fifo.queuedReal (), + impl_->fifo.servedReal (), + impl_->fifo.totalReal () + }; +} + +qint64 TxAudioQueue::capacity () const noexcept +{ + return impl_->fifo.capacity (); +} |
