summaryrefslogtreecommitdiff
path: root/Modulator
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2026-09-26 23:24:28 +0200
committerChristoph Berg <myon@debian.org>2026-09-26 23:24:28 +0200
commit69344560ea394c06ca94acac46a2e4c3e8b7b229 (patch)
treeebc98370a697a9bbd6b973a14fa5113c475c6db7 /Modulator
parent3e9d10a67671d0bd5c7b8c3ca8088f1818322eff (diff)
parent059831534c7e884f0a607ed86b582fd00d821731 (diff)
Update upstream source from tag 'upstream/3.2.0_rc1+dfsg'
Update to upstream version '3.2.0~rc1+dfsg' with Debian dir dbff457fd2c3616d5c77b863c7bfb2186ed86047
Diffstat (limited to 'Modulator')
-rw-r--r--Modulator/JttyPcmFifo.cpp245
-rw-r--r--Modulator/JttyPcmFifo.hpp81
-rw-r--r--Modulator/JttyTxStream.cpp120
-rw-r--r--Modulator/JttyTxStream.hpp60
-rw-r--r--Modulator/Modulator.cpp180
-rw-r--r--Modulator/Modulator.hpp14
-rw-r--r--Modulator/Modulator.pri3
7 files changed, 654 insertions, 49 deletions
diff --git a/Modulator/JttyPcmFifo.cpp b/Modulator/JttyPcmFifo.cpp
new file mode 100644
index 00000000..fd6d82d7
--- /dev/null
+++ b/Modulator/JttyPcmFifo.cpp
@@ -0,0 +1,245 @@
+#include "JttyPcmFifo.hpp"
+
+#include <algorithm>
+
+namespace
+{
+ qint64 advanceIndex (qint64 index, qint64 capacity) noexcept
+ {
+ ++index;
+ return index == capacity ? 0 : index;
+ }
+
+ qint64 copyIntoRing (QVector<qint16>& buffer, qint64 capacity, qint64 tail,
+ qint16 const * samples, qint64 count)
+ {
+ qint64 const first = std::min (count, capacity - tail);
+ std::copy_n (samples, first, buffer.data () + tail);
+
+ qint64 const remaining = count - first;
+ if (remaining > 0)
+ {
+ std::copy_n (samples + first, remaining, buffer.data ());
+ return remaining;
+ }
+
+ tail += first;
+ return tail == capacity ? 0 : tail;
+ }
+}
+
+JttyPcmFifo::JttyPcmFifo (qint64 capacitySamples)
+ : m_buffer (int (capacitySamples))
+ , m_capacity {capacitySamples}
+ , m_head {0}
+ , m_tail {0}
+ , m_used {0}
+ , m_totalReal {0}
+ , m_servedReal {0}
+ , m_streamPos {0}
+ , m_realEndStreamPos {0}
+ , m_epoch {0}
+ , m_resetRequestedGeneration {0}
+ , m_resetAppliedGeneration {0}
+ , m_resetEpoch {0}
+ , m_resetTail {0}
+ , m_resetTotalBaseline {0}
+ , m_drainSignaled {false}
+ , m_drainReady {false}
+ , m_drainReadyEpoch {0}
+ , m_drainReadyTotal {0}
+{
+}
+
+bool JttyPcmFifo::enqueue (QVector<qint16> const& samples, qint64 epoch)
+{
+ if (samples.isEmpty ()) return true;
+ return enqueue (samples.constData (), samples.size (), epoch);
+}
+
+bool JttyPcmFifo::enqueue (qint16 const * samples, qint64 count, qint64 epoch)
+{
+ if (!samples || count <= 0) return true;
+
+ qint64 const used = m_used.load (std::memory_order_acquire);
+ if (!jttyPcmEnqueueFits (m_capacity, used, count)) return false;
+
+ qint64 const tail = copyIntoRing (m_buffer, m_capacity,
+ m_tail.load (std::memory_order_relaxed),
+ samples, count);
+
+ m_epoch.store (epoch, std::memory_order_release);
+ m_tail.store (tail, std::memory_order_release);
+ m_totalReal.fetch_add (count, std::memory_order_acq_rel);
+ m_drainSignaled.store (false, std::memory_order_release);
+ m_used.fetch_add (count, std::memory_order_release);
+ return true;
+}
+
+void JttyPcmFifo::clear (qint64 epoch)
+{
+ qint64 const tail = m_tail.load (std::memory_order_acquire);
+ qint64 const total = m_totalReal.load (std::memory_order_acquire);
+ qint64 const generation = m_resetRequestedGeneration.load (std::memory_order_relaxed) + 1;
+
+ m_resetEpoch.store (epoch, std::memory_order_release);
+ m_resetTail.store (tail, std::memory_order_release);
+ m_resetTotalBaseline.store (total, std::memory_order_release);
+ m_resetRequestedGeneration.store (generation, std::memory_order_release);
+}
+
+void JttyPcmFifo::applyPendingReset () noexcept
+{
+ qint64 const requested = m_resetRequestedGeneration.load (std::memory_order_acquire);
+ qint64 const applied = m_resetAppliedGeneration.load (std::memory_order_acquire);
+ if (requested <= applied) return;
+
+ qint64 const resetTail = m_resetTail.load (std::memory_order_acquire);
+ qint64 const resetTotal = m_resetTotalBaseline.load (std::memory_order_acquire);
+ qint64 const resetEpoch = m_resetEpoch.load (std::memory_order_acquire);
+ qint64 const served = m_servedReal.load (std::memory_order_acquire);
+ qint64 const used = m_used.load (std::memory_order_acquire);
+ qint64 skipped = resetTotal > served ? resetTotal - served : 0;
+ if (skipped > used) skipped = used;
+
+ m_head.store (resetTail, std::memory_order_release);
+ if (skipped > 0)
+ {
+ m_used.fetch_sub (skipped, std::memory_order_acq_rel);
+ }
+ if (served < resetTotal)
+ {
+ m_servedReal.store (resetTotal, std::memory_order_release);
+ }
+
+ m_streamPos.store (0, std::memory_order_release);
+ m_realEndStreamPos.store (0, std::memory_order_release);
+ m_epoch.store (resetEpoch, std::memory_order_release);
+ m_drainSignaled.store (false, std::memory_order_release);
+ m_drainReady.store (false, std::memory_order_release);
+ m_drainReadyEpoch.store (resetEpoch, std::memory_order_release);
+ m_drainReadyTotal.store (0, std::memory_order_release);
+ m_resetAppliedGeneration.store (requested, std::memory_order_release);
+}
+
+qint16 JttyPcmFifo::pullSample (qint64 drainGuard) noexcept
+{
+ applyPendingReset ();
+
+ qint16 sample {0};
+ if (m_used.load (std::memory_order_acquire) > 0)
+ {
+ qint64 head = m_head.load (std::memory_order_relaxed);
+ sample = m_buffer[int (head)];
+ head = advanceIndex (head, m_capacity);
+ m_head.store (head, std::memory_order_release);
+ m_used.fetch_sub (1, std::memory_order_release);
+ qint64 const streamPos = m_streamPos.fetch_add (1, std::memory_order_acq_rel) + 1;
+ m_servedReal.fetch_add (1, std::memory_order_acq_rel);
+ m_realEndStreamPos.store (streamPos, std::memory_order_release);
+ }
+ else
+ {
+ m_streamPos.fetch_add (1, std::memory_order_acq_rel);
+ }
+
+ updateDrainState (drainGuard);
+ return sample;
+}
+
+JttyPcmFifo::DrainState JttyPcmFifo::takeDrainReady () noexcept
+{
+ if (m_resetRequestedGeneration.load (std::memory_order_acquire)
+ > m_resetAppliedGeneration.load (std::memory_order_acquire))
+ {
+ m_drainReady.exchange (false, std::memory_order_acq_rel);
+ return DrainState {false, 0, 0};
+ }
+
+ if (!m_drainReady.exchange (false, std::memory_order_acq_rel))
+ {
+ return DrainState {false, 0, 0};
+ }
+
+ return DrainState {
+ true,
+ m_drainReadyEpoch.load (std::memory_order_acquire),
+ m_drainReadyTotal.load (std::memory_order_acquire)
+ };
+}
+
+qint64 JttyPcmFifo::queuedReal () const noexcept
+{
+ qint64 const used = m_used.load (std::memory_order_acquire);
+ qint64 const resetTotal = m_resetTotalBaseline.load (std::memory_order_acquire);
+ qint64 const served = m_servedReal.load (std::memory_order_acquire);
+ qint64 const abortedQueued = resetTotal > served ? resetTotal - served : 0;
+ return used > abortedQueued ? used - abortedQueued : 0;
+}
+
+qint64 JttyPcmFifo::servedReal () const noexcept
+{
+ return logicalServedReal ();
+}
+
+qint64 JttyPcmFifo::totalReal () const noexcept
+{
+ return logicalTotalReal ();
+}
+
+void JttyPcmFifo::updateDrainState (qint64 drainGuard) noexcept
+{
+ if (m_drainSignaled.load (std::memory_order_acquire)) return;
+
+ // Drain is based on consumed real samples plus backend tail coverage. This
+ // avoids trusting FIFO-empty alone, which would release PTT before queued
+ // audio has actually left the hardware or TCI transmit buffer.
+ qint64 const servedReal = logicalServedReal ();
+ qint64 const totalReal = logicalTotalReal ();
+ qint64 const streamPos = m_streamPos.load (std::memory_order_acquire);
+ qint64 const realEndStreamPos = m_realEndStreamPos.load (std::memory_order_acquire);
+ if (!jttyTxDrained (servedReal, totalReal, streamPos, realEndStreamPos, drainGuard))
+ {
+ return;
+ }
+
+ bool expected {false};
+ if (m_drainSignaled.compare_exchange_strong (expected, true, std::memory_order_acq_rel))
+ {
+ m_drainReadyEpoch.store (m_epoch.load (std::memory_order_acquire), std::memory_order_release);
+ m_drainReadyTotal.store (totalReal, std::memory_order_release);
+ m_drainReady.store (true, std::memory_order_release);
+ }
+}
+
+qint64 JttyPcmFifo::logicalServedReal () const noexcept
+{
+ qint64 const served = m_servedReal.load (std::memory_order_acquire);
+ qint64 const resetTotal = m_resetTotalBaseline.load (std::memory_order_acquire);
+ return served > resetTotal ? served - resetTotal : 0;
+}
+
+qint64 JttyPcmFifo::logicalTotalReal () const noexcept
+{
+ qint64 const total = m_totalReal.load (std::memory_order_acquire);
+ qint64 const resetTotal = m_resetTotalBaseline.load (std::memory_order_acquire);
+ return total > resetTotal ? total - resetTotal : 0;
+}
+
+bool jttyTxDrained (qint64 servedReal, qint64 totalReal,
+ qint64 streamPos, qint64 realEndStreamPos,
+ qint64 drainGuard)
+{
+ if (totalReal <= 0) return false;
+ if (servedReal < totalReal) return false;
+ return (streamPos - realEndStreamPos) >= drainGuard;
+}
+
+bool jttyPcmEnqueueFits (qint64 capacitySamples, qint64 usedSamples,
+ qint64 count) noexcept
+{
+ if (count <= 0) return true;
+ if (capacitySamples <= 0 || usedSamples < 0) return false;
+ if (usedSamples > capacitySamples) return false;
+ return count <= capacitySamples - usedSamples;
+}
diff --git a/Modulator/JttyPcmFifo.hpp b/Modulator/JttyPcmFifo.hpp
new file mode 100644
index 00000000..f084a6ef
--- /dev/null
+++ b/Modulator/JttyPcmFifo.hpp
@@ -0,0 +1,81 @@
+#ifndef JTTY_PCM_FIFO_HPP__
+#define JTTY_PCM_FIFO_HPP__
+
+#include <atomic>
+
+#include <QVector>
+
+class JttyPcmFifo
+{
+public:
+ // Lock-free SPSC PCM queue: one producer appends rendered messages, one
+ // backend thread pulls samples. clear() only publishes a reset generation;
+ // the consumer applies it so FIFO indices are not concurrently rewritten.
+ // The audio backend reports this edge later, outside the pull callback. The
+ // epoch/total pair lets MainWindow ignore a tail event that belonged to an
+ // older queue state.
+ struct DrainState
+ {
+ bool ready;
+ qint64 epoch;
+ qint64 totalAtDrain;
+ };
+
+ explicit JttyPcmFifo (qint64 capacitySamples);
+
+ bool enqueue (QVector<qint16> const& samples, qint64 epoch);
+ bool enqueue (qint16 const * samples, qint64 count, qint64 epoch);
+ void clear (qint64 epoch = 0);
+ void applyPendingReset () noexcept;
+
+ qint16 pullSample (qint64 drainGuard) noexcept;
+ DrainState takeDrainReady () noexcept;
+
+ qint64 capacity () const {return m_capacity;}
+ qint64 queuedReal () const noexcept;
+ qint64 servedReal () const noexcept;
+ qint64 totalReal () const noexcept;
+
+private:
+ // Single producer, single consumer: MainWindow appends complete rendered
+ // messages, and one audio pull path consumes frames. The pull side is kept to
+ // atomics and array access so it can run in a real-time-ish audio callback.
+ void updateDrainState (qint64 drainGuard) noexcept;
+ qint64 logicalServedReal () const noexcept;
+ qint64 logicalTotalReal () const noexcept;
+
+ QVector<qint16> m_buffer;
+ qint64 const m_capacity;
+
+ std::atomic<qint64> m_head;
+ std::atomic<qint64> m_tail;
+ std::atomic<qint64> m_used;
+
+ std::atomic<qint64> m_totalReal;
+ std::atomic<qint64> m_servedReal;
+ std::atomic<qint64> m_streamPos;
+ std::atomic<qint64> m_realEndStreamPos;
+ std::atomic<qint64> m_epoch;
+
+ // clear() publishes a reset generation boundary; pullSample() and other
+ // consumer-owned entry points apply it. Enqueue, progress getters, and drain
+ // signaling must interpret this same generation baseline.
+ std::atomic<qint64> m_resetRequestedGeneration;
+ std::atomic<qint64> m_resetAppliedGeneration;
+ std::atomic<qint64> m_resetEpoch;
+ std::atomic<qint64> m_resetTail;
+ std::atomic<qint64> m_resetTotalBaseline;
+
+ std::atomic<bool> m_drainSignaled;
+ std::atomic<bool> m_drainReady;
+ std::atomic<qint64> m_drainReadyEpoch;
+ std::atomic<qint64> m_drainReadyTotal;
+};
+
+bool jttyTxDrained (qint64 servedReal, qint64 totalReal,
+ qint64 streamPos, qint64 realEndStreamPos,
+ qint64 drainGuard);
+bool jttyPcmEnqueueFits (qint64 capacitySamples, qint64 usedSamples,
+ qint64 count) noexcept;
+
+#endif
diff --git a/Modulator/JttyTxStream.cpp b/Modulator/JttyTxStream.cpp
new file mode 100644
index 00000000..5e65c1cd
--- /dev/null
+++ b/Modulator/JttyTxStream.cpp
@@ -0,0 +1,120 @@
+#include "JttyTxStream.hpp"
+
+#include "Audio/soundout.h"
+#include "Logger.hpp"
+
+#include "moc_JttyTxStream.cpp"
+
+namespace
+{
+ // Trailing silence (samples) served past the last real sample before we trust
+ // the queue has physically played out, when the device buffer size is unknown.
+ // Generous by design: it only lengthens the PTT tail after the final message,
+ // never the gap between chained messages.
+ constexpr qint64 DEFAULT_DRAIN_GUARD = 9600; // 200 ms at 48 kHz
+ constexpr qint64 DRAIN_GUARD_MARGIN = 4800; // 100 ms safety margin
+}
+
+TxEvidence::TxStartSnapshot makeJttyTxStartSnapshot (TxEvidence::TxRequest const& request,
+ qint64 committedEndSample)
+{
+ TxEvidence::TxStartSnapshot snapshot;
+ snapshot.session_id = request.session_id;
+ snapshot.generation = request.generation;
+ snapshot.mode = request.mode;
+ snapshot.sample_rate_hz = 48000;
+ snapshot.committed_end_sample = committedEndSample;
+ snapshot.target_known = false;
+ snapshot.diagnostic = "JTTY PCM extent may grow while messages are queued";
+ return snapshot;
+}
+
+JttyTxStream::JttyTxStream (TxAudioQueue& queue, QObject * parent)
+ : AudioDevice {parent}
+ , m_queue {queue}
+ , m_drainGuard {DEFAULT_DRAIN_GUARD}
+ , m_drainTimer {new QTimer {this}}
+ , m_active {false}
+{
+ m_drainTimer->setInterval (25);
+ connect (m_drainTimer, &QTimer::timeout, this, &JttyTxStream::pollDrain);
+}
+
+void JttyTxStream::start (TxEvidence::TxRequest request, SoundOutput * stream)
+{
+ if (m_active) return;
+ auto progress = m_queue.progress ();
+ if (progress.epoch != request.queue_epoch)
+ {
+ LOG_DEBUG ("JTTY transmit stream start rejected before pending reset; "
+ "requested queue epoch=" << request.queue_epoch.value ()
+ << " current epoch=" << progress.epoch.value ());
+ return;
+ }
+ m_queue.applyPendingReset ();
+ progress = m_queue.progress ();
+ if (progress.epoch != request.queue_epoch)
+ {
+ LOG_DEBUG ("JTTY transmit stream start rejected after pending reset; "
+ "requested queue epoch=" << request.queue_epoch.value ()
+ << " current epoch=" << progress.epoch.value ());
+ return;
+ }
+ initialize (QIODevice::ReadOnly, request.channel);
+ m_active = true;
+ m_stream = stream;
+ if (m_stream)
+ {
+ m_stream->restart (this);
+ // Size the drain guard to the actual device buffer (now valid) plus a
+ // margin; same audio thread as SoundOutput, so this is not a cross-thread
+ // QAudioOutput access.
+ qint64 const bufFrames = bytesPerFrame ()
+ ? qint64 (m_stream->bufferSize ()) / qint64 (bytesPerFrame ())
+ : 0;
+ m_drainGuard.store (qMax (bufFrames, DEFAULT_DRAIN_GUARD) + DRAIN_GUARD_MARGIN,
+ std::memory_order_release);
+ }
+ if (!m_drainTimer->isActive ()) m_drainTimer->start ();
+ Q_EMIT txSourceCommitted (makeJttyTxStartSnapshot (
+ request, progress.total_samples > 0 ? progress.total_samples - 1 : -1));
+}
+
+void JttyTxStream::stop ()
+{
+ if (m_stream)
+ {
+ m_stream->stop ();
+ }
+ m_active = false;
+ m_drainTimer->stop ();
+ AudioDevice::close ();
+ // Do not drop queued PCM here. A GUI stop for the previous session can cross
+ // with the next enqueue; clear() is the explicit abort path.
+}
+
+qint64 JttyTxStream::readData (char * data, qint64 maxSize)
+{
+ if (maxSize == 0) return 0;
+ Q_ASSERT (!(maxSize % qint64 (bytesPerFrame ()))); // no torn frames
+
+ qint64 const numFrames {maxSize / qint64 (bytesPerFrame ())};
+ qint16 * samples {reinterpret_cast<qint16 *> (data)};
+
+ qint64 const drainGuard = m_drainGuard.load (std::memory_order_acquire);
+ for (qint64 frame = 0; frame < numFrames; ++frame)
+ {
+ samples = load (m_queue.pullSample (drainGuard), samples);
+ }
+
+ return numFrames * qint64 (bytesPerFrame ());
+}
+
+void JttyTxStream::pollDrain ()
+{
+ auto const drain = m_queue.takeDrainReady ();
+ if (drain.ready)
+ {
+ Q_EMIT drained (drain);
+ }
+}
diff --git a/Modulator/JttyTxStream.hpp b/Modulator/JttyTxStream.hpp
new file mode 100644
index 00000000..1a1d8784
--- /dev/null
+++ b/Modulator/JttyTxStream.hpp
@@ -0,0 +1,60 @@
+#ifndef JTTY_TX_STREAM_HPP__
+#define JTTY_TX_STREAM_HPP__
+
+#include <atomic>
+#include <QPointer>
+#include <QTimer>
+#include <QVector>
+
+#include "Audio/AudioDevice.hpp"
+#include "Audio/TxAudioQueue.hpp"
+#include "Audio/TxPlaybackEvidence.hpp"
+#include "Audio/TxRequest.hpp"
+
+class SoundOutput;
+
+TxEvidence::TxStartSnapshot makeJttyTxStartSnapshot (
+ TxEvidence::TxRequest const& request, qint64 committedEndSample);
+
+//
+// JTTY-only asynchronous transmit source.
+//
+// A QIODevice adapter around the shared JTTY PCM FIFO. readData() only serves
+// samples and records drain readiness; drained() is emitted by a timer outside
+// the audio pull path.
+//
+class JttyTxStream
+ : public AudioDevice
+{
+ Q_OBJECT;
+
+public:
+ explicit JttyTxStream (TxAudioQueue& queue, QObject * parent = nullptr);
+
+ bool isActive () const {return m_active;}
+
+ Q_SLOT void start (TxEvidence::TxRequest request, SoundOutput * stream);
+ Q_SLOT void stop ();
+
+ Q_SIGNAL void drained (TxAudioQueueDrainState drain);
+ Q_SIGNAL void txSourceCommitted (TxEvidence::TxStartSnapshot snapshot);
+
+protected:
+ qint64 readData (char * data, qint64 maxSize) override;
+ qint64 writeData (char const * /* data */, qint64 /* maxSize */) override
+ {
+ return -1; // we don't consume data
+ }
+
+private:
+ Q_SLOT void pollDrain ();
+
+ TxAudioQueue& m_queue;
+ std::atomic<qint64> m_drainGuard;
+
+ QPointer<SoundOutput> m_stream;
+ QTimer * m_drainTimer;
+ std::atomic<bool> m_active;
+};
+
+#endif
diff --git a/Modulator/Modulator.cpp b/Modulator/Modulator.cpp
index 0022b08c..0467a483 100644
--- a/Modulator/Modulator.cpp
+++ b/Modulator/Modulator.cpp
@@ -1,4 +1,5 @@
#include "Modulator.hpp"
+#include <cstdint>
#include <limits>
#include <qmath.h>
#include <QDateTime>
@@ -25,6 +26,66 @@ extern float gran(); // Noise generator (for tests only)
double constexpr Modulator::m_twoPi;
+namespace
+{
+ TxEvidence::TxStartSnapshot txStartSnapshot (TxEvidence::TxSessionId sessionId,
+ TxEvidence::TxGeneration generation,
+ QString const& mode, int sampleRateHz,
+ qint64 silentFrames, unsigned initialSample,
+ bool tuning, bool fastMode,
+ bool hasCwId,
+ unsigned symbolsLength,
+ double framesPerSymbol,
+ double trPeriod)
+ {
+ TxEvidence::TxStartSnapshot snapshot;
+ snapshot.session_id = sessionId;
+ snapshot.generation = generation;
+ snapshot.mode = mode;
+ snapshot.sample_rate_hz = sampleRateHz;
+ if (mode == QStringLiteral ("JTTY"))
+ {
+ snapshot.diagnostic = QStringLiteral ("JTTY source commits its own target.");
+ return snapshot;
+ }
+ if (tuning)
+ {
+ snapshot.diagnostic = QStringLiteral ("Tune transmission has no bounded target.");
+ return snapshot;
+ }
+ if (hasCwId)
+ {
+ snapshot.diagnostic = QStringLiteral ("CW ID can extend the generated target.");
+ return snapshot;
+ }
+ if (symbolsLength == 0 || framesPerSymbol <= 0.0)
+ {
+ snapshot.diagnostic = QStringLiteral ("Generated target inputs are invalid.");
+ return snapshot;
+ }
+
+ double const end = fastMode ? trPeriod * 48000.0 - 24000.0
+ : symbolsLength * 4.0 * framesPerSymbol;
+ if (end < 0.0 || end > std::numeric_limits<unsigned>::max ())
+ {
+ snapshot.diagnostic = QStringLiteral ("Generated target end is invalid.");
+ return snapshot;
+ }
+ unsigned const i1 {static_cast<unsigned> (end)};
+ if (initialSample > i1)
+ {
+ snapshot.diagnostic = QStringLiteral ("Late start skipped the generated target.");
+ return snapshot;
+ }
+
+ snapshot.target_known = true;
+ snapshot.committed_end_sample = TxEvidence::boundedCommittedEndSample (
+ silentFrames, initialSample, i1);
+ snapshot.diagnostic = QStringLiteral ("Generated target committed.");
+ return snapshot;
+ }
+}
+
// float wpm=20.0;
// unsigned m_nspd=1.2*48000.0/wpm;
// m_nspd=3072; //18.75 WPM
@@ -46,41 +107,59 @@ Modulator::Modulator (unsigned frameRate, double periodLengthInSeconds,
{
}
-void Modulator::start (QString mode, unsigned symbolsLength, double framesPerSymbol,
- double frequency, double toneSpacing,
- SoundOutput * stream, Channel channel,
- bool synchronize, bool fastMode, double dBSNR, double TRperiod)
+void Modulator::start (TxEvidence::TxRequest request, SoundOutput * stream)
{
- // qDebug () << "mode:" << mode << "symbolsLength:" << symbolsLength << "framesPerSymbol:" << framesPerSymbol << "frequency:" << frequency << "toneSpacing:" << toneSpacing << "channel:" << channel << "synchronize:" << synchronize << "fastMode:" << fastMode << "dBSNR:" << dBSNR << "TRperiod:" << TRperiod;
+// qDebug () << request.mode << request.symbols_length << request.frames_per_symbol
+// << request.frequency_hz << request.tone_spacing << request.channel
+// << request.synchronize << request.fast_mode << request.snr_db
+// << request.tr_period_s;
Q_ASSERT (stream);
// Time according to this computer which becomes our base time
- qint64 ms0 = QDateTime::currentMSecsSinceEpoch() % 86400000;
+ auto const actualStartMs = QDateTime::currentMSecsSinceEpoch ();
+ qint64 ms0 = actualStartMs % 86400000;
unsigned mstr = ms0 % int(1000.0*m_period); // ms into the nominal Tx start time
+ bool const constrained = request.start_window_open_ms >= 0
+ && request.start_window_close_ms >= request.start_window_open_ms;
+ if (constrained
+ && (actualStartMs < request.start_window_open_ms
+ || actualStartMs > request.start_window_close_ms))
+ {
+ Q_EMIT constrainedStartDecided (request.session_id.value (),
+ request.generation.value (),
+ request.start_window_open_ms, false,
+ actualStartMs);
+ return;
+ }
+
if(m_state != Idle) stop();
+ m_mode = request.mode;
m_quickClose = false;
- m_symbolsLength = symbolsLength;
+ m_symbolsLength = request.symbols_length;
m_isym0 = std::numeric_limits<unsigned>::max (); // big number
m_frequency0 = 0.;
m_phi = 0.;
- m_addNoise = dBSNR < 0.;
- m_nsps = framesPerSymbol;
- m_frequency = frequency;
+ m_addNoise = request.snr_db < 0.;
+ m_nsps = request.frames_per_symbol;
+ m_frequency = request.frequency_hz;
m_amp = std::numeric_limits<qint16>::max ();
- m_toneSpacing = toneSpacing;
- m_bFastMode=fastMode;
- m_TRperiod=TRperiod;
+ m_toneSpacing = request.tone_spacing;
+ m_bFastMode=request.fast_mode;
+ m_TRperiod=request.tr_period_s;
+ m_tuning=request.tuning;
+ m_cwId=request.cw_id;
m_icmin=4294967295;
m_icmax=0;
unsigned delay_ms=1000;
- if((mode=="FT8" and m_nsps==1920) or (mode=="FST4" and m_nsps==720)) delay_ms=500; //FT8, FST4-15
- if((mode=="FT8" and m_nsps==1024)) delay_ms=400; //SuperFox Qary Polar Code transmission
- if(mode=="Q65" and m_nsps<=3600) delay_ms=500; //Q65-15 and Q65-30
- if(mode=="FT4") delay_ms=300; //FT4
+ if((request.mode=="FT8" and m_nsps==1920) or (request.mode=="FST4" and m_nsps==720)) delay_ms=500; //FT8, FST4-15
+ if((request.mode=="FT8" and m_nsps==1024)) delay_ms=400; //SuperFox Qary Polar Code transmission
+ if(request.mode=="Q65" and m_nsps<=3600) delay_ms=500; //Q65-15 and Q65-30
+ if(request.mode=="FT4") delay_ms=300; //FT4
+ if(request.mode=="JTTY")delay_ms=0;
// noise generator parameters
if (m_addNoise) {
- m_snr = qPow (10.0, 0.05 * (dBSNR - 6.0));
+ m_snr = qPow (10.0, 0.05 * (request.snr_db - 6.0));
m_fac = 3000.0;
if (m_snr > 1.0) m_fac = 3000.0 / m_snr;
}
@@ -91,35 +170,52 @@ void Modulator::start (QString mode, unsigned symbolsLength, double framesPerSym
{
// calculate number of silent frames to send, so that audio will
// start at the nominal time "delay_ms" into the Tx sequence.
- if (synchronize)
+ if (request.synchronize)
{
- if(delay_ms > mstr) m_silentFrames = (delay_ms - mstr) * m_frameRate / 1000;
+ if(delay_ms > mstr) {
+ m_silentFrames =
+ std::uint64_t {delay_ms - mstr} * m_frameRate / 1000;
+ }
}
// adjust for late starts
if(!m_silentFrames && mstr >= delay_ms)
{
- m_ic = (mstr - delay_ms) * m_frameRate / 1000;
+ auto const initialFrame =
+ std::uint64_t {mstr - delay_ms} * m_frameRate / 1000;
+ Q_ASSERT (initialFrame <= std::numeric_limits<unsigned>::max ());
+ m_ic = static_cast<unsigned> (initialFrame);
}
}
- if(mode=="Echo") m_ic=0;
+ if(request.mode=="Echo" or request.mode=="JTTY") m_ic=0;
- initialize (QIODevice::ReadOnly, channel);
- Q_EMIT stateChanged ((m_state = (synchronize && m_silentFrames) ?
+ initialize (QIODevice::ReadOnly, request.channel);
+ Q_EMIT stateChanged ((m_state = (request.synchronize && m_silentFrames) ?
Synchronizing : Active));
-
// qDebug() << "delay_ms:" << delay_ms << "mstr:" << mstr << "m_silentFrames:"
// << m_silentFrames << "m_ic:" << m_ic << "m_state:" << m_state << synchronize;
m_stream = stream;
if (m_stream)
{
- m_stream->restart (this);
+ m_stream->restart (this, mstr);
}
else
{
qDebug () << "Modulator::start: no audio output stream assigned";
}
+ Q_EMIT txSourceCommitted (txStartSnapshot (request.session_id, request.generation,
+ m_mode, m_frameRate,
+ m_silentFrames, m_ic, m_tuning, m_bFastMode,
+ !m_cwId.isEmpty (),
+ m_symbolsLength, m_nsps, m_TRperiod));
+ if (constrained)
+ {
+ Q_EMIT constrainedStartDecided (request.session_id.value (),
+ request.generation.value (),
+ request.start_window_open_ms, true,
+ actualStartMs);
+ }
}
void Modulator::tune (bool newState)
@@ -201,13 +297,13 @@ qint64 Modulator::readData (char * data, qint64 maxSize)
unsigned int isym=0;
if(!m_tuning) isym=m_ic/(4.0*m_nsps); // Actual fsample=48000
- bool slowCwId=((isym >= m_symbolsLength) && (icw[0] > 0)) && (!m_bFastMode);
+ bool slowCwId=((isym >= m_symbolsLength) && !m_cwId.isEmpty ()) && (!m_bFastMode);
if(m_TRperiod==3.0) slowCwId=false;
bool fastCwId=false;
static bool bCwId=false;
qint64 ms = QDateTime::currentMSecsSinceEpoch();
float tsec=0.001*(ms % int(1000*m_TRperiod));
- if(m_bFastMode and (icw[0]>0) and (tsec > (m_TRperiod-5.0))) fastCwId=true;
+ if(m_bFastMode and !m_cwId.isEmpty () and (tsec > (m_TRperiod-5.0))) fastCwId=true;
if(!m_bFastMode) m_nspd=2560; // 22.5 WPM
@@ -220,7 +316,7 @@ qint64 Modulator::readData (char * data, qint64 maxSize)
m_nsps=4096.0*12000.0/11025.0;
m_ic=2246949;
m_nspd=2560; // 22.5 WPM
- if(icw[0]*m_nspd/48000.0 > 4.0) m_nspd=4.0*48000.0/icw[0]; //Faster CW for long calls
+ if(m_cwId.size ()*m_nspd/48000.0 > 4.0) m_nspd=4.0*48000.0/m_cwId.size (); //Faster CW for long calls
}
bCwId=true;
unsigned ic0 = m_symbolsLength * 4 * m_nsps;
@@ -228,7 +324,12 @@ qint64 Modulator::readData (char * data, qint64 maxSize)
while (samples != end) {
j = (m_ic - ic0)/m_nspd + 1; // symbol of this sample
- bool level {bool (icw[j])};
+ if (j == 0 || j > static_cast<unsigned> (m_cwId.size ()))
+ {
+ Q_EMIT stateChanged ((m_state = Idle));
+ return framesGenerated * bytesPerFrame ();
+ }
+ bool level {bool (m_cwId.at (j - 1))};
m_phi += m_dphi;
if (m_phi > m_twoPi) m_phi -= m_twoPi;
qint16 sample=0;
@@ -246,14 +347,9 @@ qint64 Modulator::readData (char * data, qint64 maxSize)
sample=0;
if(level) sample=32767.0*x;
}
- if (int (j) <= icw[0] && j < NUM_CW_SYMBOLS) { // stop condition
- samples = load (postProcessSample (sample), samples);
- ++framesGenerated;
- ++m_ic;
- } else {
- Q_EMIT stateChanged ((m_state = Idle));
- return framesGenerated * bytesPerFrame ();
- }
+ samples = load (postProcessSample (sample), samples);
+ ++framesGenerated;
+ ++m_ic;
// adjust ramp
if ((m_ramp != 0 && m_ramp != std::numeric_limits<qint16>::min ()) || level != m_cwLevel) {
@@ -340,16 +436,16 @@ qint64 Modulator::readData (char * data, qint64 maxSize)
++m_ic;
}
- if(m_TRperiod==3 and m_ic >i1) {
+ if((m_TRperiod==3 or m_mode=="JTTY") and m_ic >i1) {
Q_EMIT stateChanged ((m_state = Idle));
return framesGenerated * bytesPerFrame ();
}
-// qDebug() << "dd" << QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz")
-// << tsec << m_ic << i1;
+// qDebug() << "dd" << m_mode << QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz")
+// << tsec << m_TRperiod << m_ic << i1;
if (m_amp == 0.0) { // TODO G4WJS: compare double with zero might not be wise
- if (icw[0] == 0) {
+ if (m_cwId.isEmpty ()) {
// no CW ID to send
Q_EMIT stateChanged ((m_state = Idle));
return framesGenerated * bytesPerFrame ();
diff --git a/Modulator/Modulator.hpp b/Modulator/Modulator.hpp
index 272e0a33..024c4d5c 100644
--- a/Modulator/Modulator.hpp
+++ b/Modulator/Modulator.hpp
@@ -3,8 +3,11 @@
#include <QAudio>
#include <QPointer>
+#include <QVector>
#include "Audio/AudioDevice.hpp"
+#include "Audio/TxRequest.hpp"
+#include "Audio/TxPlaybackEvidence.hpp"
class SoundOutput;
@@ -35,14 +38,15 @@ public:
void set_nsym(int n) {m_symbolsLength=n;}
void set_ms0(qint64 ms) {m_ms0=ms;}
- Q_SLOT void start (QString mode, unsigned symbolsLength, double framesPerSymbol, double frequency,
- double toneSpacing, SoundOutput *, Channel = Mono,
- bool synchronize = true, bool fastMode = false,
- double dBSNR = 99., double TRperiod=60.0);
+ Q_SLOT void start (TxEvidence::TxRequest request, SoundOutput *);
Q_SLOT void stop (bool quick = false);
Q_SLOT void tune (bool newState = true);
Q_SLOT void setFrequency (double newFrequency) {m_frequency = newFrequency;}
Q_SIGNAL void stateChanged (ModulatorState) const;
+ Q_SIGNAL void txSourceCommitted (TxEvidence::TxStartSnapshot snapshot) const;
+ Q_SIGNAL void constrainedStartDecided (qint64 sessionId, qint64 generation,
+ qint64 windowOpenMs, bool accepted,
+ qint64 actualStartMs) const;
protected:
qint64 readData (char * data, qint64 maxSize) override;
@@ -82,12 +86,14 @@ private:
unsigned m_frameRate;
ModulatorState m_state;
+ QString m_mode;
bool m_tuning;
bool m_addNoise;
bool m_bFastMode;
bool m_cwLevel;
+ QVector<int> m_cwId;
unsigned m_ic;
unsigned m_icmin;
unsigned m_icmax;
diff --git a/Modulator/Modulator.pri b/Modulator/Modulator.pri
deleted file mode 100644
index f353dece..00000000
--- a/Modulator/Modulator.pri
+++ /dev/null
@@ -1,3 +0,0 @@
-SOURCES += Modulator/Modulator.cpp
-
-HEADERS += Modulator/Modulator.hpp