diff options
| author | Christoph Berg <myon@debian.org> | 2026-09-26 23:24:28 +0200 |
|---|---|---|
| committer | Christoph Berg <myon@debian.org> | 2026-09-26 23:24:28 +0200 |
| commit | 69344560ea394c06ca94acac46a2e4c3e8b7b229 (patch) | |
| tree | ebc98370a697a9bbd6b973a14fa5113c475c6db7 /Transceiver/TCITransceiver.cpp | |
| parent | 3e9d10a67671d0bd5c7b8c3ca8088f1818322eff (diff) | |
| parent | 059831534c7e884f0a607ed86b582fd00d821731 (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 'Transceiver/TCITransceiver.cpp')
| -rw-r--r-- | Transceiver/TCITransceiver.cpp | 736 |
1 files changed, 488 insertions, 248 deletions
diff --git a/Transceiver/TCITransceiver.cpp b/Transceiver/TCITransceiver.cpp index 86d8e76e..5ee4bc0b 100644 --- a/Transceiver/TCITransceiver.cpp +++ b/Transceiver/TCITransceiver.cpp @@ -1,10 +1,16 @@ #include "Transceiver/TCITransceiver.hpp" +#include "Transceiver/TCIProtocol.hpp" #include <QRegularExpression> -#include <QLocale> #include <QThread> #include <qmath.h> +#include <algorithm> +#include <cmath> +#include <cstddef> +#include <cstring> +#include <limits> + #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) #include <QRandomGenerator> #endif @@ -18,12 +24,39 @@ #include <QDir> #include <QDebug> #include <QDateTime> +#include <QMutexLocker> #include <QTimer> +#include <QVector> + +#include "DecDataMutex.hpp" namespace { char const * const TCI_transceiver_1_name {"TCI Client RX1"}; char const * const TCI_transceiver_2_name {"TCI Client RX2"}; + // Exactly 1 MHz remains within the current band. + quint64 constexpr band_change_threshold_hz {1000000u}; + + bool has_required_args (QStringList const& args, int required) + { + return args.size () >= required; + } + + bool decimal_tenths (QString const& value, int * result) + { + auto const parts = value.split ('.'); + bool whole_ok {false}; + bool fractional_ok {false}; + int const whole = parts.value (0).toInt (&whole_ok); + int const fractional = parts.value (1).toInt (&fractional_ok); + if (!result || parts.size () < 2 || !whole_ok || !fractional_ok) + { + return false; + } + + *result = 10 * whole + fractional; + return true; + } QString map_mode (Transceiver::MODE mode) { @@ -31,11 +64,8 @@ namespace { case Transceiver::AM: return "am"; case Transceiver::CW: return "cw"; -// case Transceiver::CW_R: return "CW-R"; case Transceiver::USB: return "usb"; case Transceiver::LSB: return "lsb"; -// case Transceiver::FSK: return "RTTY"; -// case Transceiver::FSK_R: return "RTTY-R"; case Transceiver::DIG_L: return "digl"; case Transceiver::DIG_U: return "digu"; case Transceiver::FM: return "wfm"; @@ -51,7 +81,7 @@ namespace static const QString SmTrue("true"); static const QString SmFalse("false"); - // Command maps + // TCI command names static const QString CmdDevice("device"); static const QString CmdReceiveOnly("receive_only"); static const QString CmdTrxCount("trx_count"); @@ -112,9 +142,9 @@ namespace } extern "C" { - void fil4_(qint16*, qint32*, qint16*, qint32*, short int*); + void fil4_state_(qint16*, qint32*, qint16*, qint32*, float*); } -extern dec_data dec_data; +#include "ReceiveAudio.hpp" extern float gran(); // Noise generator (for tests only) @@ -134,11 +164,9 @@ void TCITransceiver::register_transceivers (logger_type *, TransceiverFactory::T (*registry)[TCI_transceiver_2_name] = TransceiverFactory::Capabilities {id2, TransceiverFactory::Capabilities::tci, true}; } -static constexpr quint32 AudioHeaderSize = 16u*sizeof(quint32); - TCITransceiver::TCITransceiver (logger_type * logger, std::unique_ptr<TransceiverBase> wrapped,QString const& rignr, QString const& address, bool use_for_ptt, - int poll_interval, QObject * parent) + int poll_interval, QObject * parent, ReceiveClock receive_clock) : PollingTransceiver {logger, poll_interval, parent} , wrapped_ {std::move (wrapped)} , rx_ {rignr} @@ -191,7 +219,6 @@ TCITransceiver::TCITransceiver (logger_type * logger, std::unique_ptr<Transceive , tci_loop7_ {nullptr} , tci_timer8_ {nullptr} , tci_loop8_ {nullptr} - , wavptr_ {nullptr} , m_downSampleFactor {4} , m_buffer ((m_downSampleFactor > 1) ? new short [max_buffer_size * m_downSampleFactor] : nullptr) @@ -200,12 +227,20 @@ TCITransceiver::TCITransceiver (logger_type * logger, std::unique_ptr<Transceive , m_toneSpacing {0.0} , m_fSpread {0.0} , m_state {Idle} + , m_jttyDrainTimer {nullptr} + , m_jttyDrainGuard {24000} + , m_tciBackendStartSequence {0} + , m_tciSuccessfullyWrittenFrames {0} + , m_tciStartMsecs {0} + , m_tciLastReportMsecs {0} + , m_tciFinalSnapshotEmitted {false} , m_tuning {false} , m_cwLevel {false} , m_j0 {-1} , m_toneFrequency0 {1500.0} , wav_file_ {QDir(QStandardPaths::writableLocation (QStandardPaths::DataLocation)).absoluteFilePath ("tx.wav").toStdString()} { + receive_clock_ = std::move (receive_clock); m_samplesPerFFT = 6912 / 2; tci_Ready = false; trxA = 0; @@ -269,6 +304,10 @@ TCITransceiver::TCITransceiver (logger_type * logger, std::unique_ptr<Transceive mapCmd_[CmdAgcMode] = Cmd_AgcMode; mapCmd_[CmdAgcGain] = Cmd_AgcGain; mapCmd_[CmdLock] = Cmd_Lock; + + m_jttyDrainTimer = new QTimer {this}; + m_jttyDrainTimer->setInterval (25); + connect (m_jttyDrainTimer, &QTimer::timeout, this, &TCITransceiver::poll_jtty_drain); } void TCITransceiver::onConnected() @@ -288,7 +327,11 @@ void TCITransceiver::onError(QAbstractSocket::SocketError err) { qDebug() << "WebInThread::onError"; CAT_TRACE ("TCITransceiver entered TCI onError and ErrorNumber is " + QString::number(err) + '\n'); - error_ = tr ("TCI websocket error: %1").arg (errortable.at (err)); + auto const error_index = static_cast<int> (err); + auto const error_name = error_index >= 0 && error_index < errortable.size () + ? errortable.at (error_index) + : tr ("UnknownSocketError"); + error_ = tr ("TCI websocket error: %1").arg (error_name); } int TCITransceiver::do_start () @@ -297,14 +340,13 @@ int TCITransceiver::do_start () CAT_TRACE ("TCITransceiver entered TCI do_start and tci_Ready is " + QString::number(tci_Ready) + '\n'); qDebug () << "qDebug says do_start tci_Ready is: " << tci_Ready; if (wrapped_) wrapped_->start (0); - url_.setUrl("ws://" + server_); //server_ + url_.setUrl("ws://" + server_); if (url_.host() == "") url_.setHost("localhost"); if (url_.port() == -1) url_.setPort(40001); if (!commander_) { - commander_ = new QWebSocket {}; // QObject takes ownership + commander_ = new QWebSocket {}; CAT_TRACE ("TCITransceiver entered TCI do_start and commander created\n"); - //printf ("commander created\n"); connect(commander_,SIGNAL(connected()),this,SLOT(onConnected())); connect(commander_,SIGNAL(disconnected()),this,SLOT(onDisconnected())); connect(commander_,SIGNAL(binaryMessageReceived(QByteArray)),this,SLOT(onBinaryReceived(QByteArray))); @@ -388,6 +430,7 @@ int TCITransceiver::do_start () tci_Ready = false; ESDR3 = false; HPSDR = false; + stream_channels_supported_ = false; band_change = false; trxA = 0; trxB = 0; @@ -428,19 +471,17 @@ int TCITransceiver::do_start () _power_ = false; CAT_TRACE ("TCITransceiver entered TCI do_start and url " + url_.toString() + " rig_power:" + QString::number(rig_power_) + " rig_power_off:" + QString::number(rig_power_off_) + " tci_audio:" + QString::number(tci_audio_) + " do_snr:" + QString::number(do_snr_) + " do_pwr:" + QString::number(do_pwr_) + '\n'); commander_->open (url_); - tci_done6(); - mysleep6(2000); + mysleep7(2000); busy_split_ = true; const QString cmd = CmdSplitEnable + SmDP + "false" + SmTZ; sendTextMessage(cmd); - //mysleep6(500); busy_split_ = false; if (error_.isEmpty()) { tci_Ready = true; if (!_power_) { if (rig_power_) { rig_power(true); - mysleep6(1000); + mysleep7(1000); if(!_power_) throw error {tr ("TCI SDR could not be switched on")}; } else { tci_Ready = false; @@ -456,7 +497,7 @@ int TCITransceiver::do_start () } if (tci_audio_) { stream_audio (true); - mysleep6(500); + mysleep7(500); if (!stream_audio_) { tci_Ready = false; throw error {tr ("TCI Audio could not be switched on")}; @@ -469,10 +510,6 @@ int TCITransceiver::do_start () const QString cmd = CmdSmeter + SmDP + rx_ + SmCM + "0" + SmTZ; sendTextMessage(cmd); } -// if (!requested_rx_frequency_.isEmpty()) do_frequency(string_to_frequency (requested_rx_frequency_),get_mode(true),false); -// if (!requested_other_frequency_.isEmpty()) do_tx_frequency(string_to_frequency (requested_other_frequency_),get_mode(true),false); -// else if (requested_split_ != split_) {rig_split();} - do_poll (); if (ESDR3) { const QString cmd = CmdTxSensorsEnable + SmDP + (do_pwr_ ? "true" : "false") + SmCM + "500" + SmTZ; @@ -495,20 +532,18 @@ int TCITransceiver::do_start () void TCITransceiver::do_stop () { CAT_TRACE ("TCITransceiver TCI close\n"); - //printf ("TCI close\n"); if (!commander_) return; if (stream_audio_ && tci_Ready && inConnected && _power_) { stream_audio (false); - mysleep1(500); + mysleep7(500); CAT_TRACE ("TCI audio closed\n"); - //printf ("TCI audio closed\n"); } if (tci_Ready && inConnected && _power_) { requested_other_frequency_ = ""; busy_split_ = true; const QString cmd = CmdSplitEnable + SmDP + "false" + SmTZ; sendTextMessage(cmd); - mysleep1(500); + mysleep5(500); busy_split_ = false; requested_split_ = false; rig_split(); @@ -521,7 +556,6 @@ void TCITransceiver::do_stop () rig_power(false); mysleep1(500); CAT_TRACE ("TCI power down\n"); - //printf ("TCI power down\n"); } tci_Ready = false; if (commander_) @@ -530,100 +564,85 @@ void TCITransceiver::do_stop () delete commander_, commander_ = nullptr; CAT_TRACE ("deleted commander & closed websocket & deleted:"); } + // The wait helpers are parent-owned and reused when the adapter restarts. if (tci_timer1_) { if (tci_timer1_->isActive()) tci_timer1_->stop(); - // tci_timer1_->deleteLater(), tci_timer1_ = nullptr; CAT_TRACE ("timer1 "); } if (tci_loop1_) { tci_loop1_->quit(); - // tci_loop1_->deleteLater(), tci_loop1_ = nullptr; CAT_TRACE ("loop1 "); } if (tci_timer2_) { if (tci_timer2_->isActive()) tci_timer2_->stop(); - // tci_timer2_->deleteLater(), tci_timer2_ = nullptr; CAT_TRACE ("timer2 "); } if (tci_loop2_) { tci_loop2_->quit(); - // tci_loop2_->deleteLater(), tci_loop2_ = nullptr; CAT_TRACE ("loop2 "); } if (tci_timer3_) { if (tci_timer3_->isActive()) tci_timer3_->stop(); - // tci_timer3_->deleteLater(), tci_timer3_ = nullptr; CAT_TRACE ("timer3 "); } if (tci_loop3_) { tci_loop3_->quit(); - // tci_loop3_->deleteLater(), tci_loop3_ = nullptr; CAT_TRACE ("loop3 "); } if (tci_timer4_) { if (tci_timer4_->isActive()) tci_timer4_->stop(); - // tci_timer4_->deleteLater(), tci_timer4_ = nullptr; CAT_TRACE ("timer4 "); } if (tci_loop4_) { tci_loop4_->quit(); - // tci_loop4_->deleteLater(), tci_loop4_ = nullptr; CAT_TRACE ("loop4 "); } if (tci_timer5_) { if (tci_timer5_->isActive()) tci_timer5_->stop(); - // tci_timer5_->deleteLater(), tci_timer5_ = nullptr; CAT_TRACE ("timer5 "); } if (tci_loop5_) { tci_loop5_->quit(); - // tci_loop5_->deleteLater(), tci_loop5_ = nullptr; CAT_TRACE ("loop5 "); } if (tci_timer6_) { if (tci_timer6_->isActive()) tci_timer6_->stop(); - // tci_timer6_->deleteLater(), tci_timer6_ = nullptr; CAT_TRACE ("timer6 "); } if (tci_loop6_) { tci_loop6_->quit(); - // tci_loop6_->deleteLater(), tci_loop6_ = nullptr; CAT_TRACE ("loop6 "); } if (tci_timer7_) { if (tci_timer7_->isActive()) tci_timer7_->stop(); - // tci_timer7_->deleteLater(), tci_timer7_ = nullptr; CAT_TRACE ("timer7 "); } if (tci_loop7_) { tci_loop7_->quit(); - // tci_loop7_->deleteLater(), tci_loop7_ = nullptr; CAT_TRACE ("loop7 "); } if (tci_timer8_) { if (tci_timer8_->isActive()) tci_timer8_->stop(); - // tci_timer8_->deleteLater(), tci_timer8_ = nullptr; CAT_TRACE ("timer8 "); } if (tci_loop8_) { tci_loop8_->quit(); - // tci_loop8_->deleteLater(), tci_loop8_ = nullptr; CAT_TRACE ("loop8 "); } @@ -637,6 +656,7 @@ void TCITransceiver::onMessageReceived(const QString &str) QStringList cmd_list = str.split(";", SkipEmptyParts); for (QString cmds : cmd_list){ QStringList cmd = cmds.split(":", SkipEmptyParts); + if (cmd.isEmpty ()) continue; QStringList args = cmd.last().split(",", SkipEmptyParts); Tci_Cmd idCmd = mapCmd_[cmd.first()]; if (idCmd != Cmd_Power && idCmd != Cmd_SWR && idCmd != Cmd_Smeter && idCmd != Cmd_AppFocus && idCmd != Cmd_RxSensors && idCmd != Cmd_TxSensors) { printf("%s TCI message received:|%s| ",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),str.toStdString().c_str()); printf("idCmd : %d args : %s\n",idCmd,args.join("|").toStdString().c_str());} @@ -645,53 +665,87 @@ void TCITransceiver::onMessageReceived(const QString &str) switch (idCmd) { case Cmd_Smeter: + if (!has_required_args (args, 3)) break; if(args.at(0)==rx_ && args.at(1) == "0") level_ = args.at(2).toInt() + 73; break; case Cmd_RxSensors: + if (!has_required_args (args, 2)) break; if(args.at(0)==rx_) level_ = args.at(1).split(".")[0].toInt() + 73; printf("Smeter=%d\n",level_); break; case Cmd_TxSensors: + if (!has_required_args (args, 5)) break; if(args.at(0)==rx_) { - power_ = 10 * args.at(3).split(".")[0].toInt() + args.at(3).split(".")[1].toInt(); - swr_ = 10 * args.at(4).split(".")[0].toInt() + args.at(4).split(".")[1].toInt(); + int power; + int swr; + if (!decimal_tenths (args.at (3), &power) || + !decimal_tenths (args.at (4), &swr)) break; + power_ = power; + swr_ = swr; printf("Power=%d SWR=%d\n",power_,swr_); } break; case Cmd_SWR: + if (!has_required_args (args, 1)) break; printf("%s Cmd_SWR : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); - swr_ = 10 * args.at(0).split(".")[0].toInt() + args.at(0).split(".")[1].toInt(); + { + int swr; + if (!decimal_tenths (args.at (0), &swr)) break; + swr_ = swr; + } break; case Cmd_Power: - power_ = 10 * args.at(0).split(".")[0].toInt() + args.at(0).split(".")[1].toInt(); + if (!has_required_args (args, 1)) break; + { + int power; + if (!decimal_tenths (args.at (0), &power)) break; + power_ = power; + } printf("%s Cmd_Power : %s %d\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str(),power_); break; case Cmd_VFO: + { printf("%s Cmd_VFO : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); - printf("band_change:%d busy_other_frequency_:%d timer1_remaining:%d timer2_remaining:%d",band_change,busy_other_frequency_,tci_timer7_->remainingTime(),tci_timer2_->remainingTime()); //was timer1 and timer2 - if(args.at(0)==rx_ && args.at(1) == "0") { - if (args.at(2).left(1) != "-") rx_frequency_ = args.at(2); + printf("band_change:%d busy_other_frequency_:%d timer1_remaining:%d timer2_remaining:%d",band_change,busy_other_frequency_,tci_timer7_->remainingTime(),tci_timer2_->remainingTime()); + if (!has_required_args (args, 1) || args.at (0) != rx_) break; + if (!has_required_args (args, 3) + || (args.at (1) != "0" && args.at (1) != "1")) + { + error_ = tr ("TCI sent an invalid VFO frequency"); + CAT_TRACE (error_); + break; + } + Frequency received_frequency; + if (!TciProtocol::parse_frequency (args.at (2), &received_frequency)) + { + error_ = tr ("TCI sent an invalid VFO frequency"); + CAT_TRACE (error_ + ": " + args.at (2)); + break; + } + auto const received_frequency_text = QString::number (received_frequency); + if(args.at(1) == "0") { + rx_frequency_ = received_frequency_text; CAT_TRACE("Rx VFO Frequency from SDR is :"); CAT_TRACE(rx_frequency_); if (!tci_Ready && requested_rx_frequency_.isEmpty()) requested_rx_frequency_ = rx_frequency_; if (busy_rx_frequency_ && !band_change) { printf (" cmdvfo0 done1"); - tci_done7(); //was tci_done1 (do_frequency) + tci_done7(); } else if (!tci_timer2_->isActive() && split_) { printf (" cmdvfo0 timer2 start 210"); tci_timer2_->start(210); } } - else if (args.at(0)==rx_ && args.at(1) == "1") { - if (args.at(2).left(1) != "-") other_frequency_ = args.at(2); + else { + other_frequency_ = received_frequency_text; CAT_TRACE("Tx VFO (other) Frequency from SDR is :"); CAT_TRACE(other_frequency_); if (!tci_Ready && requested_other_frequency_.isEmpty()) requested_other_frequency_ = other_frequency_; - if (band_change && tci_timer7_->isActive()) { //was tci_timer1 + if (band_change && tci_timer7_->isActive()) { printf (" cmdvfo1 done1"); band_change = false; tci_timer2_->start(210); - tci_done7(); //was tci_done1 (do_frequency) + tci_done7(); } else if (busy_other_frequency_) { printf (" cmdvfo1 done2"); tci_done2(); @@ -704,11 +758,18 @@ void TCITransceiver::onMessageReceived(const QString &str) } } break; + } case Cmd_Mode: printf("%s Cmd_Mode : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); + if (!has_required_args (args, 2)) break; if(args.at(0)==rx_) { if (ESDR3 || HPSDR) { - if (args.at(1) == "0" ) mode_ = args.at(2).toLower(); else mode_ = args.at(1).toLower(); + if (args.at(1) == "0" ) { + if (!has_required_args (args, 3)) break; + mode_ = args.at(2).toLower(); + } else { + mode_ = args.at(1).toLower(); + } } else mode_ = args.at(1); if (started_mode_.isEmpty()) started_mode_ = mode_; if (busy_mode_) { @@ -722,20 +783,22 @@ void TCITransceiver::onMessageReceived(const QString &str) break; case Cmd_SplitEnable: printf("%s Cmd_SplitEnable : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); + if (!has_required_args (args, 2)) break; if(args.at(0)==rx_) { if (args.at(1) == "false") split_ = false; else if (args.at(1) == "true") split_ = true; if (!tci_Ready) {started_split_ = split_;} - else if (busy_split_) tci_done5(); //was tci_done2 - else if (requested_split_ != split_ && !tci_timer5_->isActive()) { //was tci_timer2 + else if (busy_split_) tci_done5(); + else if (requested_split_ != split_ && !tci_timer5_->isActive()) { CAT_TRACE("tci_timer5 started in onMessageReceived-Cmd_SplitEnable"); - tci_timer5_->start(210); //was tci_timer2 + tci_timer5_->start(210); rig_split(); } } break; case Cmd_Drive: printf("%s Cmd_Drive : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); + if ((!ESDR3 && !HPSDR && !has_required_args (args, 1)) || ((ESDR3 || HPSDR) && !has_required_args (args, 2))) break; if((!ESDR3 && !HPSDR) || args.at(0)==rx_) { if (ESDR3 || HPSDR) drive_ = args.at(1); else drive_ = args.at(0); if (requested_drive_.isEmpty()) requested_drive_ = drive_; @@ -746,6 +809,7 @@ void TCITransceiver::onMessageReceived(const QString &str) break; case Cmd_Trx: printf("%s Cmd_Trx : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); + if (!has_required_args (args, 2)) break; if(args.at(0)==rx_) { if (args.at(1) == "false") PTT_ = false; else if (args.at(1) == "true") PTT_ = true; @@ -760,16 +824,18 @@ void TCITransceiver::onMessageReceived(const QString &str) break; case Cmd_AudioStart: printf("%s Cmd_AudioStart : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); + if (!has_required_args (args, 1)) break; if(args.at(0)==rx_) { stream_audio_ = true; if (tci_Ready) { printf ("cmdaudiostart done1\n"); - tci_done7(); //was tci_done1 (do_frequency) + tci_done7(); } } break; case Cmd_RxEnable: printf("%s Cmd_RxEnable : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); + if (!has_required_args (args, 2)) break; if(args.at(0)=="1") { if (args.at(1) == "false") rx2_ = false; else if (args.at(1) == "true") rx2_ = true; @@ -781,11 +847,12 @@ void TCITransceiver::onMessageReceived(const QString &str) break; case Cmd_AudioStop: printf("%s CmdAudioStop : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); + if (!has_required_args (args, 1)) break; if(args.at(0)==rx_) { stream_audio_ = false; if (tci_Ready) { printf ("cmdaudiostop done1\n"); - tci_done7(); //was tci_done1 (do_frequency) + tci_done7(); } } break; @@ -794,7 +861,7 @@ void TCITransceiver::onMessageReceived(const QString &str) _power_ = true; printf ("cmdstart done1\n"); if (tci_Ready) { - tci_done7(); //was tci_done1 (do_frequency) + tci_done7(); } break; case Cmd_Stop: @@ -806,10 +873,11 @@ void TCITransceiver::onMessageReceived(const QString &str) power_ = 0; if (do_pwr_) update_power (0); swr_ = 0; if (do_pwr_) update_swr (0); m_state = Idle; + if (m_jttyDrainTimer && m_jttyDrainTimer->isActive ()) m_jttyDrainTimer->stop (); Q_EMIT tci_mod_active(m_state != Idle); } _power_ = false; - if (tci_timer1_->isActive()) { //was tci_timer1 + if (tci_timer1_->isActive()) { printf ("cmdstop done1\n"); tci_done1(); } else { @@ -818,17 +886,21 @@ void TCITransceiver::onMessageReceived(const QString &str) break; case Cmd_Version: printf("%s CmdVersion : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); + if (!has_required_args (args, 1)) break; if(args.at(0)=="ExpertSDR3") ESDR3 = true; else if (args.at(0)=="Thetis") HPSDR = true; + stream_channels_supported_ = has_required_args (args, 2) + && TciStream::protocol_has_channels (args.at (1)); break; case Cmd_Device: printf("%s CmdDevice : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); + if (!has_required_args (args, 1)) break; if((args.at(0)=="SunSDR2DX" || args.at(0)=="SunSDR2PRO") && !ESDR3) tx_top_ = false; printf ("tx_top_:%d\n",tx_top_); break; case Cmd_Ready: printf("%s CmdReady : %s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),args.join("|").toStdString().c_str()); - tci_done7(); //was tci_done1 (do_frequency) + tci_done7(); break; default: @@ -847,11 +919,18 @@ void TCITransceiver::sendTextMessage(const QString &message) void TCITransceiver::onBinaryReceived(const QByteArray &data) { - Data_Stream *pStream = (Data_Stream*)(data.data()); + auto const * pStream = TciStream::header (data); + if (!pStream) { + return; + } + if (pStream->type != last_type) { last_type = pStream->type; } - if (pStream->type == Iq_Stream){ + if (pStream->type == TciStream::IqStream){ + if (!TciStream::has_complete_float_payload (data, pStream->length)) { + return; + } bool tx = false; if (pStream->receiver == 0){ tx = trxA == 0; @@ -862,125 +941,220 @@ void TCITransceiver::onBinaryReceived(const QByteArray &data) trxB = 1; } printf("sendIqData\n"); - emit sendIqData(pStream->receiver,pStream->length,pStream->data,tx); + emit sendIqData(pStream->receiver,pStream->length, + const_cast<float *> (TciStream::float_payload (data)),tx); qDebug() << "IQ" << data.size() << pStream->length; - } else if (pStream->type == RxAudioStream && audio_ && pStream->receiver == rx_.toUInt()) { - writeAudioData(pStream->data,pStream->length); - } else if (pStream->type == TxChrono && pStream->receiver == rx_.toUInt()) { - mtx_.lock(); tx_fifo += 1; tx_fifo &= 7; - int ssize = AudioHeaderSize+pStream->length*sizeof(float)*2; - quint16 tehtud; - if (m_tx1[tx_fifo].size() != ssize) m_tx1[tx_fifo].resize(ssize); - Data_Stream * pOStream1 = (Data_Stream*)(m_tx1[tx_fifo].data()); - pOStream1->receiver = pStream->receiver; - pOStream1->sampleRate = pStream->sampleRate; - pOStream1->format = pStream->format; - pOStream1->codec = 0; - pOStream1->crc = 0; - pOStream1->length = pStream->length; - pOStream1->type = TxAudioStream; - tehtud = readAudioData(pOStream1->data,pOStream1->length, txAtten); - if (tehtud && tehtud != pOStream1->length) { - quint16 valmis = tehtud; - tehtud = readAudioData(pOStream1->data + valmis,pOStream1->length - valmis,txAtten); + } else if (pStream->type == TciStream::RxAudioStream && audio_ && pStream->receiver == rx_.toUInt()) { + if (!TciStream::has_complete_float_payload (data, pStream->length)) { + return; + } + writeAudioData(const_cast<float *> (TciStream::float_payload (data)),pStream->length); + } else if (pStream->type == TciStream::TxChrono && pStream->receiver == rx_.toUInt()) { + quint32 channels; + if (!TciStream::valid_tx_chrono (*pStream, stream_channels_supported_, + audioSampleRate, &channels)) { + return; + } + + qint64 generatedScalars {0}; + { + std::lock_guard<std::mutex> lock {mtx_}; + tx_fifo += 1; tx_fifo &= 7; + if (!TciStream::prepare_tx_audio_frame (&m_tx1[tx_fifo], *pStream, channels)) { + return; + } + + auto * response = TciStream::header (&m_tx1[tx_fifo]); + auto * payload = TciStream::float_payload (&m_tx1[tx_fifo]); + auto generated = readAudioData (payload, response->length, channels, txAtten); + generatedScalars = generated; + if (generated < response->length) { + generatedScalars += readAudioData (payload + generated, + response->length - generated, + channels, txAtten); + } + tx_fifo2 = tx_fifo; } - tx_fifo2 = tx_fifo; mtx_.unlock(); + if (!inConnected || commander_->sendBinaryMessage(m_tx1[tx_fifo2]) != m_tx1[tx_fifo2].size()) { + return; } + + // A generated source frame is committed only when its containing WebSocket + // response is sent completely; protocol padding past the source extent is + // not evidence that omitted source audio reached the radio. + qint64 const frames = TxEvidence::interleavedFrameCount (generatedScalars, + int (channels)); + m_tciSuccessfullyWrittenFrames += frames; + emit_tci_playout_snapshot (false, m_state == Idle); } } void TCITransceiver::txAudioData(quint32 len, float * data) { + if (len % TciStream::DefaultChannels || (len && !data)) { + return; + } + + TciStream::Header chrono {}; + chrono.receiver = 0u; + chrono.sampleRate = audioSampleRate; + chrono.length = len; QByteArray tx; - tx.resize(AudioHeaderSize+len*sizeof(float)*2); - Data_Stream * pStream = (Data_Stream*)(tx.data()); - pStream->receiver = 0; - pStream->sampleRate = audioSampleRate; - pStream->format = 3; - pStream->codec = 0; - pStream->crc = 0; - pStream->length = len; - pStream->type = TxAudioStream; - memcpy(pStream->data,data,len*sizeof(float)*2); + if (!TciStream::prepare_tx_audio_frame (&tx, chrono, TciStream::DefaultChannels)) { + return; + } + + if (len) { + std::memcpy (TciStream::float_payload (&tx), data, len * sizeof (float)); + } commander_->sendBinaryMessage(tx); } +void TCITransceiver::enqueue_jtty_pcm (QByteArray const& samples, + TxAudioQueueEpoch epoch, + qint64 enqueueId) noexcept +{ + qint64 const count = samples.size () / int (sizeof (qint16)); + if (count <= 0) return; + + qint16 const * pcm = reinterpret_cast<qint16 const *> (samples.constData ()); + auto const result = m_txAudioQueue.enqueue (pcm, count, epoch); + if (!result.accepted) + { + if (epoch == result.progress.epoch) + { + CAT_WARNING ("JTTY TCI transmit FIFO overflow; rejecting PCM enqueue\n"); + } + else + { + CAT_DEBUG ("JTTY TCI transmit queue epoch changed; rejecting stale PCM enqueue\n"); + } + Q_EMIT jtty_enqueue_failed (epoch, enqueueId); + return; + } + Q_EMIT jtty_enqueue_accepted (enqueueId, count, result.progress); +} + +void TCITransceiver::clear_jtty_pcm (TxAudioQueueEpoch epoch) noexcept +{ + m_txAudioQueue.clear (epoch); +} + +void TCITransceiver::poll_jtty_drain () +{ + // TCI audio is pulled while responding to TxChrono packets. Emitting the + // completion signal here keeps Qt work out of that packet/audio path. + auto const drain = m_txAudioQueue.takeDrainReady (); + if (drain.ready) + { + Q_EMIT jtty_drained (drain); + } +} + +void TCITransceiver::clear () +{ + m_receiveAudioProducer.reset (m_period); + m_bufferPos = 0; +} + quint32 TCITransceiver::writeAudioData (float * data, qint32 maxSize) { - static unsigned mstr0=999999; - qint64 ms0 = QDateTime::currentMSecsSinceEpoch() % 86400000; + if (dec_data_input_blocked ()) return maxSize; + + QVector<qint64> frame_counts; + QVector<ReceiveAudio> audio; + qint64 ms0 = (receive_clock_ ? receive_clock_ () + : QDateTime::currentMSecsSinceEpoch ()) % 86400000; unsigned mstr = ms0 % int(1000.0*m_period); // ms into the nominal Tx start time - if(mstr < mstr0/2) { //When mstr has wrapped around to 0, restart the buffer - dec_data.params.kin = 0; - m_bufferPos = 0; - } - mstr0=mstr; - if(data != NULL) { - // block below adjusts receive audio attenuation - quint64 i = 0; - float * data1 = (float*)malloc(maxSize * sizeof(float)); - for(i=0;i<(quint64)maxSize;i++) { - data1[i] = ((float)(pow(10, 0.05*rxAtten) * data[i])); + if(data == NULL) { + QMutexLocker lock {&dec_data_mutex ()}; + if (dec_data_input_blocked ()) return maxSize; + if(mstr < m_lastPeriodOffsetMs/2) { //When mstr has wrapped around to 0, restart the buffer + clear (); + } + m_lastPeriodOffsetMs=mstr; + return maxSize; // we drop any data past the end of the buffer on + // the floor until the next period starts } - // no torn frames - Q_ASSERT (!(maxSize % static_cast<qint32> (bytesPerFrame))); + double const receiveGain = std::pow (10.0, 0.05 * rxAtten); - // these are in terms of input frames (not down sampled) - size_t framesAcceptable ((sizeof (dec_data.d2) / - sizeof (dec_data.d2[0]) - dec_data.params.kin) * m_downSampleFactor); - size_t framesAccepted (qMin (static_cast<size_t> (maxSize / - bytesPerFrame), framesAcceptable)); + { + QMutexLocker lock {&dec_data_mutex ()}; + auto& producer = m_receiveAudioProducer.data (); + if (dec_data_input_blocked ()) return maxSize; - if (framesAccepted < static_cast<size_t> (maxSize / bytesPerFrame)) { - qDebug () << "dropped " << maxSize / bytesPerFrame - framesAccepted - << " frames of data on the floor!" - << dec_data.params.kin << mstr; - } + if(mstr < m_lastPeriodOffsetMs/2) { //When mstr has wrapped around to 0, restart the buffer + clear (); + } + m_lastPeriodOffsetMs=mstr; - for (unsigned remaining = framesAccepted; remaining; ) { - size_t numFramesProcessed (qMin (m_samplesPerFFT * - m_downSampleFactor - m_bufferPos, remaining)); + // no torn frames + Q_ASSERT (!(maxSize % static_cast<qint32> (rxChannels))); - if(m_downSampleFactor > 1) { - store (&data1[(framesAccepted - remaining) * bytesPerFrame], - numFramesProcessed, &m_buffer[m_bufferPos]); - m_bufferPos += numFramesProcessed; + // these are in terms of input frames (not down sampled) + size_t framesAcceptable ((sizeof producer.d2 / + sizeof producer.d2[0] - m_receiveAudioProducer.frames ()) * m_downSampleFactor); + size_t framesAccepted (qMin (static_cast<size_t> (maxSize / + rxChannels), framesAcceptable)); - if(m_bufferPos==m_samplesPerFFT*m_downSampleFactor) { - qint32 framesToProcess (m_samplesPerFFT * m_downSampleFactor); - qint32 framesAfterDownSample (m_samplesPerFFT); - if(m_downSampleFactor > 1 && dec_data.params.kin>=0 && - dec_data.params.kin < (NTMAX*12000 - framesAfterDownSample)) { - fil4_(&m_buffer[0], &framesToProcess, &dec_data.d2[dec_data.params.kin], - &framesAfterDownSample, &dec_data.d2[dec_data.params.kin]); - dec_data.params.kin += framesAfterDownSample; - } else { - qDebug() << "framesToProcess = " << framesToProcess; - qDebug() << "dec_data.params.kin = " << dec_data.params.kin; - qDebug() << "framesAfterDownSample" << framesAfterDownSample; + if (framesAccepted < static_cast<size_t> (maxSize / rxChannels)) { + qDebug () << "dropped " << maxSize / rxChannels - framesAccepted + << " frames of data on the floor!" + << m_receiveAudioProducer.frames () << mstr; + } + + for (unsigned remaining = framesAccepted; remaining; ) { + size_t numFramesProcessed (qMin (m_samplesPerFFT * + m_downSampleFactor - m_bufferPos, remaining)); + + if(m_downSampleFactor > 1) { + store (&data[(framesAccepted - remaining) * rxChannels], + numFramesProcessed, &m_buffer[m_bufferPos], receiveGain); + m_bufferPos += numFramesProcessed; + + if(m_bufferPos==m_samplesPerFFT*m_downSampleFactor) { + qint32 framesToProcess (m_samplesPerFFT * m_downSampleFactor); + qint32 framesAfterDownSample (m_samplesPerFFT); + if(m_downSampleFactor > 1 && m_receiveAudioProducer.frames ()>=0 && + m_receiveAudioProducer.frames () < (NTMAX*12000 - framesAfterDownSample)) { + fil4_state_(&m_buffer[0], &framesToProcess, + &producer.d2[m_receiveAudioProducer.frames ()], + &framesAfterDownSample, m_downsampleState.data ()); + m_receiveAudioProducer.setFrames (m_receiveAudioProducer.frames () + framesAfterDownSample); + } else { + qDebug() << "framesToProcess = " << framesToProcess; + qDebug() << "receive audio frames = " << m_receiveAudioProducer.frames (); + qDebug() << "framesAfterDownSample" << framesAfterDownSample; + } + frame_counts << m_receiveAudioProducer.frames (); + audio << m_receiveAudioProducer.capture (m_receiveAudioProducer.frames (), m_period); + m_bufferPos = 0; } - Q_EMIT tciframeswritten (dec_data.params.kin); - m_bufferPos = 0; - } - } else { - store (&data1[(framesAccepted - remaining) * bytesPerFrame], - numFramesProcessed, &dec_data.d2[dec_data.params.kin]); - m_bufferPos += numFramesProcessed; - dec_data.params.kin += numFramesProcessed; - if (m_bufferPos == static_cast<unsigned> (m_samplesPerFFT)) { - Q_EMIT tciframeswritten (dec_data.params.kin); - m_bufferPos = 0; + } else { + store (&data[(framesAccepted - remaining) * rxChannels], + numFramesProcessed, &producer.d2[m_receiveAudioProducer.frames ()], + receiveGain); + m_bufferPos += numFramesProcessed; + m_receiveAudioProducer.setFrames (m_receiveAudioProducer.frames () + numFramesProcessed); + if (m_bufferPos == static_cast<unsigned> (m_samplesPerFFT)) { + frame_counts << m_receiveAudioProducer.frames (); + audio << m_receiveAudioProducer.capture (m_receiveAudioProducer.frames (), m_period); + m_bufferPos = 0; + } } + remaining -= numFramesProcessed; } - remaining -= numFramesProcessed; } - free(data1); -} + for (auto frames : frame_counts) { + Q_EMIT tciframeswritten (frames); + } + for (auto const& block : audio) Q_EMIT receiveAudio (block); + return maxSize; // we drop any data past the end of the buffer on // the floor until the next period starts } @@ -1044,8 +1218,9 @@ void TCITransceiver::do_audio (bool on) { TRACE_CAT ("TCITransceiver", on << state ()); if (on) { - dec_data.params.kin = 0; + QMutexLocker lock {&dec_data_mutex ()}; m_bufferPos = 0; + if (!dec_data_input_blocked ()) clear (); } audio_ = on; } @@ -1053,7 +1228,12 @@ void TCITransceiver::do_audio (bool on) void TCITransceiver::do_period (double period) { TRACE_CAT ("TCITransceiver", period << state ()); - m_period = period; + if (m_period != period) + { + QMutexLocker lock {&dec_data_mutex ()}; + m_period = period; + if (!dec_data_input_blocked ()) clear (); + } } void TCITransceiver::do_volume (qreal volume) @@ -1130,14 +1310,14 @@ void TCITransceiver::do_frequency (Frequency f, MODE m, bool no_ignore) if (tci_Ready && _power_) { if ((rx_frequency_ != requested_rx_frequency_)) { busy_rx_frequency_ = true; - band_change = abs(rx_frequency_.toInt()-requested_rx_frequency_.toInt()) > 1000000; + band_change = TciProtocol::frequency_difference_exceeds ( + string_to_frequency (rx_frequency_), f, band_change_threshold_hz); const QString cmd = CmdVFO + SmDP + rx_ + SmCM + "0" + SmCM + requested_rx_frequency_ + SmTZ; if(f > 100000 && f< 250000000000) sendTextMessage(cmd); mysleep7(2000); - // if (band_change) mysleep7(500); - band_change2 = abs(rx_frequency_.toInt()-requested_rx_frequency_.toInt()) > 1000000; + band_change2 = TciProtocol::frequency_difference_exceeds ( + string_to_frequency (rx_frequency_), f, band_change_threshold_hz); if (!band_change2) update_rx_frequency (f); - //if (requested_rx_frequency_ == rx_frequency_) update_rx_frequency (f); else { printf("%s TCI failed set rxfreq:%s->%s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),rx_frequency_.toStdString().c_str(),requested_rx_frequency_.toStdString().c_str()); error_ = tr ("TCI failed set rxfreq"); @@ -1160,8 +1340,6 @@ void TCITransceiver::do_frequency (Frequency f, MODE m, bool no_ignore) else { printf("%s TCI failed set mode %s->%s",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),mode_.toStdString().c_str(),requested_mode_.toStdString().c_str()); error_ = tr ("TCI failed set mode"); - // tci_Ready = false; - // throw error {tr ("TCI failed set mode")}; } busy_mode_ = false; } @@ -1202,15 +1380,12 @@ void TCITransceiver::do_tx_frequency (Frequency tx, MODE mode, bool no_ignore) const QString cmd = CmdVFO + SmDP + rx_ + SmCM + "1" + SmCM + requested_other_frequency_ + SmTZ; if(tx > 100000 && tx < 250000000000) sendTextMessage(cmd); mysleep2(1000); - other_band_change = abs(other_frequency_.toInt()-requested_other_frequency_.toInt()) > 1000000; + other_band_change = TciProtocol::frequency_difference_exceeds ( + string_to_frequency (other_frequency_), tx, band_change_threshold_hz); if (!other_band_change) update_other_frequency (tx); - // if (requested_other_frequency_ == other_frequency_) update_other_frequency (tx); else { printf("%s TCI failed set txfreq:%s->%s\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),other_frequency_.toStdString().c_str(),requested_other_frequency_.toStdString().c_str()); CAT_TRACE("TCI failed set txfreq"); - // error_ = tr ("TCI failed set txfreq"); - // tci_Ready = false; - // throw error {tr ("TCI failed set txfreq")}; } busy_other_frequency_ = false; } else update_other_frequency (string_to_frequency (other_frequency_)); @@ -1239,7 +1414,6 @@ void TCITransceiver::do_tx_frequency (Frequency tx, MODE mode, bool no_ignore) } } -//do_mode is never called. void TCITransceiver::do_mode (MODE m) { TRACE_CAT ("TCITransceiver", m << state ()); @@ -1253,8 +1427,6 @@ void TCITransceiver::do_mode (MODE m) else { printf("%s TCI failed set mode %s->%s",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),mode_.toStdString().c_str(),requested_mode_.toStdString().c_str()); error_ = tr ("TCI failed set mode"); - // tci_Ready = false; - // throw error {tr ("TCI failed set mode")}; } busy_mode_ = false; } @@ -1356,55 +1528,30 @@ auto TCITransceiver::get_mode (bool requested) -> MODE QString TCITransceiver::mode_to_command (QString m_string) const { - // if (ESDR3) { - // const QString cmd = CmdMode + SmDP + rx_ + SmCM + m_string.toUpper() + SmTZ; - // return cmd; - // } else { const QString cmd = CmdMode + SmDP + rx_ + SmCM + m_string + SmTZ; return cmd; - // } } QString TCITransceiver::frequency_to_string (Frequency f) const { - // number is localized and in kHz, avoid floating point translation - // errors by adding a small number (0.1Hz) + // Frequency is an integer number of hertz, so decimal conversion is locale-independent. auto f_string = QString {}.setNum(f); printf ("frequency_to_string3 |%s|\n",f_string.toStdString().c_str()); return f_string; } -auto TCITransceiver::string_to_frequency (QString s) const -> Frequency +auto TCITransceiver::string_to_frequency (QString const& s) const -> Frequency { - // temporary hack because Commander is returning invalid UTF-8 bytes - s.replace (QChar {QChar::ReplacementCharacter}, locale_.groupSeparator ()); - - bool ok; - - auto f = QLocale::c ().toDouble (s, &ok); // temporary fix - - if (!ok) - { - printf("Frequency rejected is ***%s***\n",s.toStdString().c_str()); - // throw error {tr ("TCI sent an unrecognized frequency") + " |" + s + "|"}; - } - return f; + Frequency frequency; + if (!TciProtocol::parse_frequency (s, &frequency)) + { + throw error {tr ("TCI sent an invalid VFO frequency")}; + } + return frequency; } -/* - * mysleep1 is used in do_stop - * mysleep2 is used in do_tx_frequency - * mysleep3 is used in do_ptt - * mysleep4 is used in rx2_enable - * mysleep5 is used in rig_split - * mysleep6 is used in do_start - * mysleep7 is used in do_frequency - * mysleep8 is used in do_mode which is never called - * */ - void TCITransceiver::mysleep1 (int ms) { - //printf("%s TCI sleep1 start %d %d\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),ms,tci_timer1_->isActive()); if (ms) tci_timer1_->start(ms); tci_loop1_->exec(); if (tci_timer1_->isActive() && tci_Ready) tci_timer1_->stop(); @@ -1454,39 +1601,40 @@ void TCITransceiver::mysleep8 (int ms) } // Modulator part -void TCITransceiver::do_modulator_start (QString mode, unsigned symbolsLength, double framesPerSymbol, - double frequency, double toneSpacing, bool synchronize, bool fastMode, double dBSNR, double TRperiod) +void TCITransceiver::do_modulator_start (TxEvidence::TxRequest const& request) { // Time according to this computer which becomes our base time qint64 ms0 = QDateTime::currentMSecsSinceEpoch() % 86400000; qDebug() << "ModStart" << QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.sss"); unsigned mstr = ms0 % int(1000.0*m_period); // ms into the nominal Tx start time if (m_state != Idle) { - // stop (); throw error {tr ("TCI modulator not Idle")}; } m_quickClose = false; - m_symbolsLength = symbolsLength; + m_txMode = request.mode; + 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_trfrequency = frequency; + m_addNoise = request.snr_db < 0.; + m_nsps = request.frames_per_symbol; + m_trfrequency = 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; 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 // 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; } @@ -1501,13 +1649,28 @@ void TCITransceiver::do_modulator_start (QString mode, unsigned symbolsLength, d m_silentFrames = 0; // calculate number of silent frames to send - if (m_ic == 0 && synchronize && !m_tuning) { + if (m_ic == 0 && request.synchronize && !request.tuning) { m_silentFrames = audioSampleRate / (1000 / delay_ms) - (mstr * (audioSampleRate / 1000)); } - m_state = (synchronize && m_silentFrames) ? + m_state = (request.synchronize && m_silentFrames) ? Synchronizing : Active; - printf("%s TCI modulator startdelay_ms=%d ASR=%d mstr=%d mstr2=%d m_ic=%d s_Frames=%lld synchronize=%d m_tuning=%d State=%d\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),delay_ms,audioSampleRate,mstr,mstr2,m_ic,m_silentFrames,synchronize,m_tuning,m_state); + m_txStartSnapshot.session_id = request.session_id; + m_txStartSnapshot.generation = request.generation; + m_txStartSnapshot.mode = m_txMode; + m_txStartSnapshot.sample_rate_hz = int (audioSampleRate); + m_txStartSnapshot.committed_end_sample = bounded_source_frames (request.tuning); + m_txStartSnapshot.target_known = m_txStartSnapshot.committed_end_sample >= 0; + m_txStartSnapshot.diagnostic = tr ("TCI source commitment; playout evidence is protocol-send dead reckoning, not DAC confirmation"); + ++m_tciBackendStartSequence; + m_tciSuccessfullyWrittenFrames = 0; + m_tciStartMsecs = QDateTime::currentMSecsSinceEpoch (); + m_tciLastReportMsecs = m_tciStartMsecs; + m_tciFinalSnapshotEmitted = false; + printf("%s TCI modulator startdelay_ms=%d ASR=%d mstr=%d mstr2=%d m_ic=%d s_Frames=%lld synchronize=%d m_tuning=%d State=%d\n",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),delay_ms,audioSampleRate,mstr,mstr2,m_ic,m_silentFrames,request.synchronize,m_tuning,m_state); + if (m_txMode == "JTTY" && !m_jttyDrainTimer->isActive ()) m_jttyDrainTimer->start (); Q_EMIT tci_mod_active(m_state != Idle); + Q_EMIT txSourceCommitted (m_txStartSnapshot); + emit_tci_playout_snapshot (true, true); } void TCITransceiver::do_tune (bool newState) @@ -1523,11 +1686,57 @@ void TCITransceiver::do_modulator_stop (bool quick) m_state = Idle; Q_EMIT tci_mod_active(m_state != Idle); } + if (m_jttyDrainTimer->isActive ()) m_jttyDrainTimer->stop (); + emit_tci_playout_snapshot (false, true); tx_audio_ = false; } -quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, qreal txAtten) +qint64 TCITransceiver::bounded_source_frames (bool tuning) const +{ + if (tuning || m_txMode == "JTTY" || m_txMode == "CW" || !m_cwId.isEmpty ()) return -1; + + qint64 i1 = qint64 (m_symbolsLength * 4.0 * m_nsps); + if (m_bFastMode) + { + i1 = qint64 (m_TRperiod * 48000.0 - 24000.0); + } + return TxEvidence::boundedCommittedEndSample (m_silentFrames, m_ic, i1); +} + +void TCITransceiver::emit_tci_playout_snapshot (bool start_event, bool force) { + qint64 const now = QDateTime::currentMSecsSinceEpoch (); + bool const final = m_state == Idle; + if (!start_event && !force && now - m_tciLastReportMsecs < 1000) return; + if (final && m_tciFinalSnapshotEmitted) return; + + TxEvidence::TxRawPlayoutSnapshot snapshot; + snapshot.tier = TxEvidence::TxPlayoutTier::DeadReckoning; + snapshot.backend_start_sequence = m_tciBackendStartSequence; + snapshot.start_event = start_event; + snapshot.available = inConnected; + snapshot.state = final ? TxEvidence::TxBackendState::Idle : TxEvidence::TxBackendState::Active; + snapshot.elapsed_usecs = (now - m_tciStartMsecs) * 1000; + snapshot.successfully_written_frames = m_tciSuccessfullyWrittenFrames; + snapshot.sample_rate_hz = int (audioSampleRate); + snapshot.report_interval_ms = int (now - m_tciLastReportMsecs); + snapshot.diagnostic = tr ("TCI WebSocket send progress; no radio DAC or RF playback confirmation"); + if (m_txMode == "JTTY") + { + auto const progress = m_txAudioQueue.progress (); + snapshot.source_served_frames = progress.served_samples; + snapshot.source_total_frames = progress.total_samples; + } + Q_EMIT rawTxPlayoutSnapshot (snapshot); + m_tciLastReportMsecs = now; + if (final) m_tciFinalSnapshotEmitted = true; +} + +quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, quint32 channels, qreal txAtten) +{ + Q_ASSERT (channels == 1u || channels == 2u); + Q_ASSERT (maxSize >= 0 && !(static_cast<quint32> (maxSize) % channels)); + double toneFrequency=1500.0; if(m_nsps==6) { toneFrequency=1000.0; @@ -1537,11 +1746,10 @@ quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, qreal txAtt if(maxSize==0) return 0; qreal newVolume = pow(2.22222 * (45 - txAtten) * 0.01,2); - //printf("txAtten is %f and newVolume is %f\n",txAtten, newVolume); - qint64 numFrames (maxSize/bytesPerFrame); + qint64 numFrames (maxSize/channels); float * samples (reinterpret_cast<float *> (data)); - float * end (samples + numFrames * bytesPerFrame); + float * end (samples + numFrames * channels); qint64 framesGenerated (0); switch (m_state) @@ -1550,10 +1758,10 @@ quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, qreal txAtt { if (m_silentFrames) { // send silence up to first second framesGenerated = qMin (m_silentFrames, numFrames); - for ( ; samples != end; samples = load (0, samples)) { // silence + for ( ; samples != end; samples = load (0, channels, samples)) { // silence } m_silentFrames -= framesGenerated; - return framesGenerated * bytesPerFrame; + return framesGenerated * channels; } m_state = Active; Q_EMIT tci_mod_active(m_state != Idle); @@ -1564,10 +1772,15 @@ quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, qreal txAtt case Active: { + if (m_txMode == "JTTY" && !m_tuning) + { + return readJttyAudioData (data, maxSize, channels, txAtten); + } + unsigned int isym=0; qint16 sample=0; if(!m_tuning) isym=m_ic/(4.0*m_nsps); // Actual fsample=48000 - bool slowCwId=((isym >= m_symbolsLength) && (icw[0] > 0)); + bool slowCwId=((isym >= m_symbolsLength) && !m_cwId.isEmpty ()); m_nspd=2560; // 22.5 WPM if(m_TRperiod > 16.0 && slowCwId) { // Transmit CW ID? @@ -1577,7 +1790,13 @@ quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, qreal txAtt 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 ())) + { + m_state = Idle; + Q_EMIT tci_mod_active(m_state != Idle); + return framesGenerated * channels; + } + bool level {bool (m_cwId.at (j - 1))}; m_phi += m_dphi; if (m_phi > m_twoPi) m_phi -= m_twoPi; sample=0; @@ -1591,15 +1810,9 @@ quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, qreal txAtt } sample=round(newVolume * amp * x); } - if (int (j) <= icw[0] && j < NUM_CW_SYMBOLS) { // stopu condition - samples = load (postProcessSample (sample), samples); - ++framesGenerated; - ++m_ic; - } else { - m_state = Idle; - Q_EMIT tci_mod_active(m_state != Idle); - return framesGenerated * bytesPerFrame; - } + samples = load (postProcessSample (sample), channels, samples); + ++framesGenerated; + ++m_ic; // adjust ramp if ((m_ramp != 0 && m_ramp != std::numeric_limits<qint16>::min ()) || level != m_cwLevel) { @@ -1608,7 +1821,7 @@ quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, qreal txAtt } m_cwLevel = level; } - return framesGenerated * bytesPerFrame; + return framesGenerated * channels; } //End of code for CW ID double const baud (12000.0 / m_nsps); @@ -1674,17 +1887,17 @@ quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, qreal txAtt m_amp=32767.0; sample=qRound(newVolume * m_amp*foxcom_.wave[m_ic]); } - samples = load (postProcessSample (sample), samples); + samples = load (postProcessSample (sample), channels, samples); ++framesGenerated; ++m_ic; } 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 m_state = Idle; Q_EMIT tci_mod_active(m_state != Idle); - return framesGenerated * bytesPerFrame; + return framesGenerated * channels; } m_phi = 0.0; } @@ -1694,21 +1907,48 @@ quint16 TCITransceiver::readAudioData (float * data, qint32 maxSize, qreal txAtt if (samples != end && framesGenerated) { } while (samples != end) { // pad block with silence - samples = load (0, samples); + samples = load (0, channels, samples); ++framesGenerated; } - return framesGenerated * bytesPerFrame; + return framesGenerated * channels; } // fall through case Idle: - while (samples != end) samples = load (0, samples); + while (samples != end) samples = load (0, channels, samples); } Q_ASSERT (Idle == m_state); return 0; } +quint16 TCITransceiver::readJttyAudioData (float * data, qint32 maxSize, quint32 channels, qreal txAtten) +{ + Q_ASSERT (channels == 1u || channels == 2u); + Q_ASSERT (maxSize >= 0 && !(static_cast<quint32> (maxSize) % channels)); + + if(maxSize==0) return 0; + + qreal const newVolume = pow(2.22222 * (45 - txAtten) * 0.01,2); + qint64 const numFrames (maxSize/channels); + float * samples (reinterpret_cast<float *> (data)); + qint64 framesGenerated (0); + + // JTTY is pre-rendered mono PCM shared with the soundcard path. When the FIFO + // runs dry we keep sending silence until the drain guard proves the last real + // sample has cleared the backend. + for (qint64 i = 0; i < numFrames; ++i) + { + qint32 sample = qRound (newVolume * m_txAudioQueue.pullSample (m_jttyDrainGuard)); + if (sample > std::numeric_limits<qint16>::max ()) sample = std::numeric_limits<qint16>::max (); + if (sample < std::numeric_limits<qint16>::min ()) sample = std::numeric_limits<qint16>::min (); + samples = load (postProcessSample (qint16 (sample)), channels, samples); + ++framesGenerated; + } + + return framesGenerated * channels; +} + qint16 TCITransceiver::postProcessSample (qint16 sample) const { if (m_addNoise) { // Test frame, we'll add noise |
