1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
#include "Modulator.h"
#include "JS8Submode.h"
#include "JS8_Audio/soundout.h"
#include "JS8_Include/commons.h"
#include "JS8_Main/DriftingDateTime.h"
#include "JS8_UI/mainwindow.h"
#include <QDateTime>
#include <QLoggingCategory>
#include <QtMath>
#include <limits>
#include <numbers>
#include "moc_Modulator.cpp"
Q_DECLARE_LOGGING_CATEGORY(modulator_js8)
namespace {
constexpr double TAU = 2 * std::numbers::pi;
constexpr auto FRAME_RATE = 48000;
constexpr auto MS_PER_SEC = 1000;
} // namespace
void Modulator::start(double const frequency, int const submode,
double const txDelay, SoundOutput *const stream,
Channel const channel) {
Q_ASSERT(stream);
const State current_state = m_state.load();
if (current_state != State::Idle) {
qCDebug(modulator_js8)
<< "Modulator does not find itself in state idle, but"
<< (current_state == State::Active ? "Active"
: current_state == State::Synchronizing ? "Synchronizing"
: current_state == State::Idle ? "Idle"
: "??What??")
<< "so calling stop()";
stop();
}
m_quickClose = false;
m_audioFrequency = frequency;
m_nsps = JS8::Submode::samplesForOneSymbol(submode);
m_toneSpacing = JS8::Submode::toneSpacing(submode);
m_isym0 = std::numeric_limits<unsigned>::max();
m_amp = std::numeric_limits<qint16>::max();
m_audioFrequency0 = 0.0;
m_phi = 0.0;
m_silentFrames = 0;
m_ic = 0;
// If we're not tuning, then we'll need to figure out exactly when we
// should start transmitting; this will depend on the submode in play.
if (!m_tuning) {
// Get the nominal transmit start time for this submode, and determine
// which millisecond of the current transmit period we're currently at.
qint64 const nowMS = DriftingDateTime::currentMSecsSinceEpoch();
unsigned const periodMS = JS8::Submode::period(submode) * MS_PER_SEC;
auto const startDelayMS = JS8::Submode::startDelayMS(submode);
unsigned const periodOffsetMS = nowMS % periodMS;
// If we haven't yet hit the nominal start time for the period, then we
// will need to inject some silence into the transmission; determine the
// number of silent audio samples required to start audio at the correct
// amount of delay into the period.
//
// If we have hit the nominal start time for the period, adjust for late
// start if we're not exactly at the nominal start time.
bool const inTxDelayBeforePeriodStart =
periodMS <= periodOffsetMS + txDelay * MS_PER_SEC;
if (inTxDelayBeforePeriodStart) {
unsigned const additionalMSNeededForTxDelay =
periodMS - periodOffsetMS;
qCDebug(modulator_js8) << "Sending" << additionalMSNeededForTxDelay
<< "ms silence for TX delay.";
m_silentFrames = (startDelayMS + additionalMSNeededForTxDelay) *
FRAME_RATE / MS_PER_SEC;
} else if (startDelayMS > periodOffsetMS) {
qCDebug(modulator_js8)
<< "Starting" << periodOffsetMS
<< "ms late into transmission, skipping some of the"
<< startDelayMS << "ms start delay";
m_silentFrames =
(startDelayMS - periodOffsetMS) * FRAME_RATE / MS_PER_SEC;
} else {
qCWarning(modulator_js8)
<< "Starting" << periodOffsetMS
<< "ms late into transmission, cutting away initial symbol(s).";
m_ic = (periodOffsetMS - startDelayMS) * FRAME_RATE / MS_PER_SEC;
}
} else {
qCDebug(modulator_js8) << "Modulator finds it is tuning.";
}
initialize(QIODevice::ReadOnly, channel);
if (0 < m_silentFrames) {
m_state.store(State::Synchronizing);
qCDebug(modulator_js8)
<< "Symbol transmission to start after"
<< ((float)m_silentFrames) / FRAME_RATE * MS_PER_SEC
<< "ms of silence.";
} else {
m_state.store(State::Active);
qCDebug(modulator_js8) << "Symbol transmission to start immediately.";
}
m_stream = stream;
if (m_stream) {
m_stream->restart(this);
} else {
qCDebug(modulator_js8)
<< "Modulator::start: no audio output stream assigned";
}
}
void Modulator::tune(bool const tuning) {
m_tuning = tuning;
if (!m_tuning)
stop(true);
}
void Modulator::stop(bool const quickClose) {
m_quickClose = quickClose;
close();
}
void Modulator::close() {
if (m_stream) {
if (m_quickClose)
m_stream->reset();
else
m_stream->stop();
}
m_state.store(State::Idle);
AudioDevice::close();
}
qint64 Modulator::readData(char *const data, qint64 const maxSize) {
if (maxSize == 0)
return 0;
Q_ASSERT(!(maxSize % qint64(bytesPerFrame()))); // no torn frames
Q_ASSERT(isOpen());
qint64 framesGenerated = 0;
qint64 const maxFrames = maxSize / bytesPerFrame();
qint16 *samples = reinterpret_cast<qint16 *>(data);
qint16 const *const samplesEnd =
samples + maxFrames * (bytesPerFrame() / sizeof(qint16));
switch (m_state.load()) {
case State::Synchronizing: {
if (m_silentFrames) {
// Send silence up to end of start delay.
framesGenerated = qMin(m_silentFrames, maxFrames);
do {
samples = load(0, samples);
} while (--m_silentFrames && samples != samplesEnd);
if (!m_silentFrames) {
m_state.store(State::Active);
}
}
}
[[fallthrough]];
case State::Active: {
// Fade out parameters; no fade out during tuning.
unsigned int const i0 =
(m_tuning ? 9999 : (JS8_NUM_SYMBOLS - 0.017) * 4.0) * m_nsps;
unsigned int const i1 =
(m_tuning ? 9999 : JS8_NUM_SYMBOLS * 4.0) * m_nsps;
while (samples != samplesEnd && m_ic < i1) {
unsigned int const isym = m_tuning ? 0 : m_ic / (4.0 * m_nsps);
if (isym != m_isym0 || m_audioFrequency != m_audioFrequency0) {
double const toneFrequency =
m_audioFrequency + itone[isym] * m_toneSpacing;
m_dphi = TAU * toneFrequency / FRAME_RATE;
m_isym0 = isym;
m_audioFrequency0 = m_audioFrequency;
}
m_phi += m_dphi;
if (m_phi > TAU)
m_phi -= TAU;
if (m_ic > i0)
m_amp = 0.98 * m_amp;
if (m_ic > i1)
m_amp = 0.0;
samples = load(qRound(m_amp * qSin(m_phi)), samples);
++framesGenerated;
++m_ic;
}
if (m_amp == 0.0) {
m_state.store(State::Idle);
return framesGenerated * bytesPerFrame();
m_phi = 0.0;
}
m_audioFrequency0 = m_audioFrequency;
// Done for this chunk; continue on the next call. Pad the
// block with silence.
while (samples != samplesEnd) {
samples = load(0, samples);
++framesGenerated;
}
return framesGenerated * bytesPerFrame();
}
[[fallthrough]];
case State::Idle:
break;
}
Q_ASSERT(isIdle());
return 0;
}
Q_LOGGING_CATEGORY(modulator_js8, "modulator.js8", QtWarningMsg)
|