Debounce link-mode external-audio underrun before dropping PTT

Under [FM] LinkMode=1, linkStateMachine() set m_extSignal false the
instant a single ext-audio sample went missing, with no debounce --
unlike the non-link duplex/simplex paths, which already tolerate a
brief gap via the RELAYING_EXT -> RELAYING_WAIT_EXT / m_ackDelayTimer
pattern before declaring a real loss. Since IO::process() drops PTT
the moment the downstream TX buffer runs dry, that zero-debounce cutoff
meant even a very brief, late-but-recoverable gap from the gateway
(scheduling jitter, a late UDP frame, etc.) could cascade into a full
PTT drop-and-rekey cycle -- far more audible than the underlying gap
itself.

Adds m_extGapTimer, mirroring the existing pattern: on the first missed
sample, start the timer instead of immediately clearing m_extSignal; a
gap that recovers before FM_LINK_EXT_GAP_MS (default 60ms) expires
never affects m_extSignal at all, so the transmitter never notices.
Only a gap that outlasts the timer is treated as a genuine end of
transmission, exactly as before.

linkSamples()'s currentExtSample is now zero-initialized, since
m_extSignal can now stay true for a few samples past an underrun --
silence, not stack garbage, must be what gets played out to the modem
during that window.
This commit is contained in:
Cort Buffington 2026-08-09 11:42:20 -05:00
parent 715d3f5839
commit 8bd467598c
2 changed files with 44 additions and 9 deletions

50
FM.cpp
View File

@ -28,6 +28,13 @@ const uint16_t FM_SERIAL_BLOCK_SIZE = 80U;//this is the number of sample pairs t
//three times this value shall never exceed 252 //three times this value shall never exceed 252
const uint16_t FM_SERIAL_BLOCK_SIZE_BYTES = FM_SERIAL_BLOCK_SIZE * 3U; const uint16_t FM_SERIAL_BLOCK_SIZE_BYTES = FM_SERIAL_BLOCK_SIZE * 3U;
// How long a link-mode external-audio underrun is tolerated before it is
// treated as a real end of transmission (see linkStateMachine()). A gap
// shorter than this is inaudible on its own; the PTT drop-and-rekey it
// would otherwise trigger is not. Tune here if needed -- no other code
// depends on this value.
const uint16_t FM_LINK_EXT_GAP_MS = 60U;
const uint8_t FS_LISTENING = 0U; const uint8_t FS_LISTENING = 0U;
const uint8_t FS_KERCHUNK_RF = 1U; const uint8_t FS_KERCHUNK_RF = 1U;
const uint8_t FS_RELAYING_RF = 2U; const uint8_t FS_RELAYING_RF = 2U;
@ -62,6 +69,7 @@ m_ackMinTimer(),
m_ackDelayTimer(), m_ackDelayTimer(),
m_hangTimer(), m_hangTimer(),
m_reverseTimer(), m_reverseTimer(),
m_extGapTimer(),
m_needReverse(false), m_needReverse(false),
m_filterStage1( 724, 1448, 724, 32768, -37895, 21352),//3rd order Cheby Filter 300 to 2700Hz, 0.2dB passband ripple, sampling rate 24kHz m_filterStage1( 724, 1448, 724, 32768, -37895, 21352),//3rd order Cheby Filter 300 to 2700Hz, 0.2dB passband ripple, sampling rate 24kHz
m_filterStage2(32768, 0,-32768, 32768, -50339, 19052), m_filterStage2(32768, 0,-32768, 32768, -50339, 19052),
@ -85,6 +93,7 @@ m_rssiAccum(0U),
m_rssiCount(0U) m_rssiCount(0U)
{ {
m_reverseTimer.setTimeout(0U, 150U); m_reverseTimer.setTimeout(0U, 150U);
m_extGapTimer.setTimeout(0U, FM_LINK_EXT_GAP_MS);
insertDelay(100U); insertDelay(100U);
} }
@ -256,7 +265,11 @@ void CFM::linkSamples(bool cos, q15_t* samples, uint8_t length)
if (m_noiseSquelch) if (m_noiseSquelch)
cos = m_squelch.process(currentRFSample); cos = m_squelch.process(currentRFSample);
q15_t currentExtSample; // Zero-initialized: getSample() leaves this untouched on underrun, and
// with the ext-gap debounce below, m_extSignal can now stay true for a
// few samples past that point -- silence, not stack garbage, must be
// what gets played out to the modem in that window.
q15_t currentExtSample = 0;
bool inputExt = m_inputExtRB.getSample(currentExtSample);//always consume the external input data so it does not overflow bool inputExt = m_inputExtRB.getSample(currentExtSample);//always consume the external input data so it does not overflow
inputExt = inputExt && m_extEnabled; inputExt = inputExt && m_extEnabled;
@ -601,6 +614,7 @@ void CFM::clock(uint8_t length)
m_ackDelayTimer.clock(length); m_ackDelayTimer.clock(length);
m_hangTimer.clock(length); m_hangTimer.clock(length);
m_reverseTimer.clock(length); m_reverseTimer.clock(length);
m_extGapTimer.clock(length);
} }
void CFM::listeningStateDuplex(bool validRFSignal, bool validExtSignal) void CFM::listeningStateDuplex(bool validRFSignal, bool validExtSignal)
@ -1231,6 +1245,12 @@ void CFM::linkStateMachine(bool validRFSignal, bool validExtSignal)
m_extSignal = true; m_extSignal = true;
} }
if (validExtSignal && m_extGapTimer.isRunning()) {
// Recovered before the gap timer committed to a real loss below --
// m_extSignal was never flipped, so there is nothing else to undo.
m_extGapTimer.stop();
}
if (!validRFSignal && m_rfSignal) { if (!validRFSignal && m_rfSignal) {
io.setDecode(false); io.setDecode(false);
io.setADCDetection(false); io.setADCDetection(false);
@ -1248,14 +1268,28 @@ void CFM::linkStateMachine(bool validRFSignal, bool validExtSignal)
} }
if (!validExtSignal && m_extSignal) { if (!validExtSignal && m_extSignal) {
if (!m_rfSignal) { // A missed/late sample used to flip m_extSignal false right here, on
DEBUG1("State to LISTENING"); // the very first bad sample, with zero debounce -- that is what
m_state = FS_LISTENING; // actually keys the transmitter off (IO::process() drops PTT the
serial.writeFMStatus(m_state); // instant the downstream TX buffer runs dry, see IO.cpp). A gap well
} // under FM_LINK_EXT_GAP_MS is inaudible on its own; the PTT
// drop-and-rekey it triggers is not. Give it a short grace period
// instead, mirroring the RELAYING_EXT/RELAYING_WAIT_EXT pattern the
// non-link duplex/simplex paths already use for exactly this
// situation (see relayingExtStateDuplex()/relayingExtWaitStateDuplex()).
if (!m_extGapTimer.isRunning()) {
m_extGapTimer.start();
} else if (m_extGapTimer.hasExpired()) {
if (!m_rfSignal) {
DEBUG1("State to LISTENING");
m_state = FS_LISTENING;
serial.writeFMStatus(m_state);
}
m_needReverse = true; m_needReverse = true;
m_extSignal = false; m_extSignal = false;
m_extGapTimer.stop();
}
} }
} }

1
FM.h
View File

@ -75,6 +75,7 @@ private:
CFMTimer m_ackDelayTimer; CFMTimer m_ackDelayTimer;
CFMTimer m_hangTimer; CFMTimer m_hangTimer;
CFMTimer m_reverseTimer; CFMTimer m_reverseTimer;
CFMTimer m_extGapTimer; // debounce for link-mode ext-audio underrun, see linkStateMachine()
bool m_needReverse; bool m_needReverse;
CFMDirectFormI m_filterStage1; CFMDirectFormI m_filterStage1;
CFMDirectFormI m_filterStage2; CFMDirectFormI m_filterStage2;