fix(tts): a stopped utterance no longer finishes the next one

StopSpeakingText purges SAPI's queue, but the purge still queues
START/END events for the streams it cut off, and SAPI numbers streams
from 1 again afterwards. The next SpeakText then reused stream 1, so
the stale events were read as the new utterance's:
VOICE_CHAT_TTS_PLAYBACK_STARTED and _FINISHED fired for it before it
played, and its real end fired a FINISHED with utteranceID 0.

The purge is synchronous and has queued all of those events by the
time it returns, so StopAll now drains and discards them right after
it. That covers stop and speak in the same Lua call too, where the
stale START is still queued and a "require START first" check on the
END handler would not have been enough. An END for a stream we do not
track no longer fires FINISHED with utteranceID 0. Purged utterances
now signal nothing, which is retail's contract.

Verified with a standalone SAPI probe: speak/stop/speak, with and
without a message pump between the calls.
This commit is contained in:
Brues
2026-11-01 00:00:27 -05:00
parent 84171bef2d
commit 9b0aa46ff0
+16 -2
View File
@@ -242,16 +242,19 @@ void __stdcall OnSpeechNotify(WPARAM, LPARAM) {
it->second.startedEmitted = true;
}
} else if (ev.eEventId == SPEI_END_INPUT_STREAM) {
UtteranceMeta meta{0, DEST_LOCAL_PLAYBACK, false};
UtteranceMeta meta{};
bool known = false;
{
std::lock_guard<std::mutex> lock(g_streamMx);
auto it = g_streamMap.find(ev.ulStreamNum);
if (it != g_streamMap.end()) {
meta = it->second;
g_streamMap.erase(it);
known = true;
}
}
FirePlaybackFinished(1, meta.id, meta.destination);
if (known)
FirePlaybackFinished(1, meta.id, meta.destination);
}
// Only START/END stream events are requested; neither carries an
// allocated lParam, so no SpClearEvent (sphelper) is needed.
@@ -398,6 +401,17 @@ void StopAll() {
if (g_pVoice == nullptr)
return;
g_pVoice->Speak(nullptr, SPF_PURGEBEFORESPEAK, nullptr);
// The purge still queues START/END for the streams it cut off, and SAPI
// numbers streams from 1 again after it, so left in the queue those
// events would land on the next utterance: STARTED/FINISHED for it
// before it plays, then a FINISHED with utteranceID 0 when it ends. The
// purge is synchronous and has queued them all by the time it returns
// (probed: speak/stop/speak with and without a pump between), so discard
// them here. Purged utterances signal nothing, as in retail.
SPEVENT ev = {};
ULONG fetched = 0;
while (SUCCEEDED(g_pVoice->GetEvents(1, &ev, &fetched)) && fetched == 1)
ev = SPEVENT{};
std::lock_guard<std::mutex> lock(g_streamMx);
g_streamMap.clear();
}