Release v2.13.0: Implement zero-Git-audio /quant/ guided tour voice narration (Strategies 1A/2A/3A via hs-quant-voice-build, 250ms onstart watchdog & WebAudio formant fallback) and turnkey Fedora 41 OpenRouter exegesis & git.libre.audio push package (Plan #56 & Plan #57)
This commit is contained in:
1 parent
14c467fa4f
commit
6af5d32b15
40 files changed
+761
-189
No files matched your search
+221
-35
@@ -446,8 +446,11 @@
|
||||
let isPlaying = false;
|
||||
let playTimer = null;
|
||||
let speechEnabled = true; // Enabled by default so tour playback narrates immediately
|
||||
let activeVoiceEngine = "Auto";
|
||||
let cachedVoices = [];
|
||||
let audioCtx = null;
|
||||
let activeAudioEl = null;
|
||||
let formantStopFlag = false;
|
||||
|
||||
function refreshVoices() {
|
||||
if (typeof window === "undefined" || typeof window.speechSynthesis === "undefined") return;
|
||||
@@ -517,7 +520,147 @@
|
||||
}, delayMs);
|
||||
}
|
||||
|
||||
// Strategy 1B & 3A: Synchronous unblocked window.speechSynthesis.speak() + natural-English speechText
|
||||
function stopAllVoiceOutputs() {
|
||||
formantStopFlag = true;
|
||||
if (activeAudioEl) {
|
||||
try {
|
||||
activeAudioEl.pause();
|
||||
activeAudioEl.currentTime = 0;
|
||||
} catch (_) {}
|
||||
}
|
||||
if (typeof window !== "undefined" && typeof window.speechSynthesis !== "undefined") {
|
||||
try {
|
||||
if (window.speechSynthesis.speaking || window.speechSynthesis.pending) {
|
||||
window.speechSynthesis.cancel();
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
rootObj.__hsActiveUtterance = null;
|
||||
}
|
||||
|
||||
// Tier 3 In-Browser Klatt Formant WebAudio Speech Synthesizer (for offline file:// when getVoices() === [])
|
||||
function playInBrowserFormantSpeech(speechText, onFinish) {
|
||||
try {
|
||||
const Ctx = (typeof window !== "undefined") && (window.AudioContext || window.webkitAudioContext);
|
||||
if (!Ctx) {
|
||||
if (onFinish) onFinish();
|
||||
return;
|
||||
}
|
||||
if (!audioCtx) audioCtx = new Ctx();
|
||||
if (audioCtx.state === "suspended") audioCtx.resume();
|
||||
formantStopFlag = false;
|
||||
activeVoiceEngine = "Formant Synth";
|
||||
renderTutorialUI();
|
||||
|
||||
const words = String(speechText || "").toLowerCase().match(/[a-z0-9]+|[.,;:!?]/g) || [];
|
||||
const vowelMap = {
|
||||
a: [660, 1350],
|
||||
e: [510, 1880],
|
||||
i: [370, 2050],
|
||||
o: [520, 950],
|
||||
u: [380, 1050],
|
||||
y: [320, 2100]
|
||||
};
|
||||
let tOffset = audioCtx.currentTime + 0.04;
|
||||
const maxWords = Math.min(words.length, 48);
|
||||
for (let wi = 0; wi < maxWords; wi++) {
|
||||
const w = words[wi];
|
||||
if (w === "." || w === "!" || w === "?") {
|
||||
tOffset += 0.14;
|
||||
continue;
|
||||
}
|
||||
if (w === "," || w === ";" || w === ":") {
|
||||
tOffset += 0.07;
|
||||
continue;
|
||||
}
|
||||
const vowels = w.match(/[aeiouy]/g) || ["a"];
|
||||
const f0 = 116.0 - 14.0 * ((wi % 10) / 10.0);
|
||||
for (let vi = 0; vi < Math.min(vowels.length, 3); vi++) {
|
||||
const vf = vowelMap[vowels[vi]] || [580, 1250];
|
||||
const dur = 0.075;
|
||||
const osc0 = audioCtx.createOscillator();
|
||||
const osc1 = audioCtx.createOscillator();
|
||||
const osc2 = audioCtx.createOscillator();
|
||||
const gain = audioCtx.createGain();
|
||||
osc0.type = "sawtooth";
|
||||
osc1.type = "sine";
|
||||
osc2.type = "sine";
|
||||
osc0.frequency.setValueAtTime(f0, tOffset);
|
||||
osc1.frequency.setValueAtTime(vf[0], tOffset);
|
||||
osc2.frequency.setValueAtTime(vf[1], tOffset);
|
||||
gain.gain.setValueAtTime(0.0001, tOffset);
|
||||
gain.gain.linearRampToValueAtTime(0.045, tOffset + 0.015);
|
||||
gain.gain.exponentialRampToValueAtTime(0.0001, tOffset + dur);
|
||||
osc0.connect(gain);
|
||||
osc1.connect(gain);
|
||||
osc2.connect(gain);
|
||||
gain.connect(audioCtx.destination);
|
||||
osc0.start(tOffset);
|
||||
osc1.start(tOffset);
|
||||
osc2.start(tOffset);
|
||||
osc0.stop(tOffset + dur + 0.01);
|
||||
osc1.stop(tOffset + dur + 0.01);
|
||||
osc2.stop(tOffset + dur + 0.01);
|
||||
tOffset += dur;
|
||||
}
|
||||
tOffset += 0.025;
|
||||
}
|
||||
const totalDurMs = Math.max(2500, Math.round((tOffset - audioCtx.currentTime) * 1000));
|
||||
setTimeout(function () {
|
||||
if (!formantStopFlag && onFinish) onFinish();
|
||||
}, totalDurMs);
|
||||
} catch (_) {
|
||||
if (onFinish) onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
// Tier 2 Deploy-Time Untracked WAV Playback (/quant/audio/ep0X-sY.wav) -> Tier 3 Formant Fallback
|
||||
function playDeployTimeWavOrFormant(clipId, rawSpeech, onFinish) {
|
||||
if (typeof document === "undefined" || typeof Audio === "undefined") {
|
||||
playInBrowserFormantSpeech(rawSpeech, onFinish);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (!activeAudioEl) {
|
||||
activeAudioEl = document.getElementById("tutVoiceAudio");
|
||||
if (!activeAudioEl) {
|
||||
activeAudioEl = document.createElement("audio");
|
||||
activeAudioEl.id = "tutVoiceAudio";
|
||||
activeAudioEl.preload = "auto";
|
||||
activeAudioEl.style.display = "none";
|
||||
document.body.appendChild(activeAudioEl);
|
||||
}
|
||||
}
|
||||
const wavUrl = "./audio/" + clipId + ".wav";
|
||||
let handled = false;
|
||||
const doneOnce = function () {
|
||||
if (handled) return;
|
||||
handled = true;
|
||||
if (onFinish) onFinish();
|
||||
};
|
||||
activeAudioEl.onended = doneOnce;
|
||||
activeAudioEl.onerror = function () {
|
||||
if (handled) return;
|
||||
handled = true;
|
||||
playInBrowserFormantSpeech(rawSpeech, onFinish);
|
||||
};
|
||||
activeAudioEl.src = wavUrl;
|
||||
const playPromise = activeAudioEl.play();
|
||||
activeVoiceEngine = "WAV Audio";
|
||||
renderTutorialUI();
|
||||
if (playPromise && typeof playPromise.catch === "function") {
|
||||
playPromise.catch(function () {
|
||||
if (handled) return;
|
||||
handled = true;
|
||||
playInBrowserFormantSpeech(rawSpeech, onFinish);
|
||||
});
|
||||
}
|
||||
} catch (_) {
|
||||
playInBrowserFormantSpeech(rawSpeech, onFinish);
|
||||
}
|
||||
}
|
||||
|
||||
// Strategies 1A, 2A & 3A: Guarded cancel(), GC-pinned utterance, 250ms onstart watchdog & 3-tier fallback
|
||||
function speakSubtitle(kfOrText, onDoneCallback) {
|
||||
if (!speechEnabled) {
|
||||
if (onDoneCallback) scheduleNextStep(7000);
|
||||
@@ -526,51 +669,94 @@
|
||||
const rawSpeech = (kfOrText && typeof kfOrText === "object")
|
||||
? (kfOrText.speechText || cleanTextForSpeech(kfOrText.subtitle, kfOrText.uiElementLabel))
|
||||
: cleanTextForSpeech(String(kfOrText || ""), "");
|
||||
const clipId = "ep0" + (curEpIdx + 1) + "-s" + (curStepIdx + 1);
|
||||
|
||||
stopAllVoiceOutputs();
|
||||
playWebAudioTelemetryChime(curStepIdx);
|
||||
refreshVoices();
|
||||
|
||||
if (typeof window.speechSynthesis === "undefined" || typeof SpeechSynthesisUtterance === "undefined") {
|
||||
if (onDoneCallback) scheduleNextStep(7500);
|
||||
let settled = false;
|
||||
const finishSceneSpeech = function () {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
rootObj.__hsActiveUtterance = null;
|
||||
if (onDoneCallback && isPlaying) {
|
||||
scheduleNextStep(1600);
|
||||
}
|
||||
};
|
||||
|
||||
const estMs = Math.max(8500, Math.min(22000, rawSpeech.split(/\s+/).length * 410));
|
||||
if (onDoneCallback && isPlaying) {
|
||||
scheduleNextStep(estMs);
|
||||
}
|
||||
|
||||
// If browser has no speechSynthesis or 0 installed OS voices (common on Linux/Fedora desktops),
|
||||
// immediately play the deploy-time WAV track (or Formant Synth fallback) so voice is 100% audible!
|
||||
if (
|
||||
typeof window === "undefined" ||
|
||||
typeof window.speechSynthesis === "undefined" ||
|
||||
typeof SpeechSynthesisUtterance === "undefined" ||
|
||||
cachedVoices.length === 0
|
||||
) {
|
||||
playDeployTimeWavOrFormant(clipId, rawSpeech, finishSceneSpeech);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
window.speechSynthesis.cancel();
|
||||
if (typeof window.speechSynthesis.resume === "function") {
|
||||
window.speechSynthesis.resume();
|
||||
}
|
||||
const u = new SpeechSynthesisUtterance(rawSpeech);
|
||||
if (cachedVoices.length > 0) {
|
||||
const preferred = cachedVoices.find(function (v) {
|
||||
return /en(-|_)(US|GB)/i.test(v.lang) && /natural|neural|google|english/i.test(v.name);
|
||||
}) || cachedVoices.find(function (v) {
|
||||
return /^en/i.test(v.lang);
|
||||
}) || cachedVoices[0];
|
||||
if (preferred) u.voice = preferred;
|
||||
}
|
||||
// Strategy 2A: Pin u on rootObj so Chromium/WebKit GC never collects the utterance mid-speech
|
||||
rootObj.__hsActiveUtterance = u;
|
||||
const preferred = cachedVoices.find(function (v) {
|
||||
return /en(-|_)(US|GB)/i.test(v.lang) && /natural|neural|google|english/i.test(v.name);
|
||||
}) || cachedVoices.find(function (v) {
|
||||
return /^en/i.test(v.lang);
|
||||
}) || cachedVoices[0];
|
||||
if (preferred) u.voice = preferred;
|
||||
u.lang = "en-US";
|
||||
u.rate = 1.03;
|
||||
u.pitch = 1.0;
|
||||
u.volume = 1.0;
|
||||
let settled = false;
|
||||
const finish = function () {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
if (onDoneCallback && isPlaying) {
|
||||
scheduleNextStep(1800);
|
||||
}
|
||||
|
||||
let speechStarted = false;
|
||||
let fallbackTriggered = false;
|
||||
const triggerFallback = function () {
|
||||
if (fallbackTriggered || settled || !speechEnabled) return;
|
||||
fallbackTriggered = true;
|
||||
try {
|
||||
if (window.speechSynthesis.speaking || window.speechSynthesis.pending) {
|
||||
window.speechSynthesis.cancel();
|
||||
}
|
||||
} catch (_) {}
|
||||
playDeployTimeWavOrFormant(clipId, rawSpeech, finishSceneSpeech);
|
||||
};
|
||||
u.onend = finish;
|
||||
u.onerror = finish;
|
||||
// Call speak(u) synchronously without blocking on cachedVoices.length === 0 or setTimeout
|
||||
|
||||
u.onstart = function () {
|
||||
speechStarted = true;
|
||||
activeVoiceEngine = "WebSpeech";
|
||||
renderTutorialUI();
|
||||
};
|
||||
u.onend = function () {
|
||||
if (!fallbackTriggered) finishSceneSpeech();
|
||||
};
|
||||
u.onerror = function () {
|
||||
if (!speechStarted) triggerFallback();
|
||||
else finishSceneSpeech();
|
||||
};
|
||||
|
||||
window.speechSynthesis.speak(u);
|
||||
const estMs = Math.max(8500, Math.min(22000, rawSpeech.split(/\s+/).length * 410));
|
||||
if (onDoneCallback && isPlaying) {
|
||||
scheduleNextStep(estMs);
|
||||
}
|
||||
|
||||
// Strategy 2A: 250ms hardware watchdog — if OS speech-dispatcher socket is hung and onstart never fires,
|
||||
// automatically switch to deploy-time WAV / WebAudio formant synth so the user always hears the narration!
|
||||
setTimeout(function () {
|
||||
if (!speechStarted && !settled && speechEnabled) {
|
||||
triggerFallback();
|
||||
}
|
||||
}, 250);
|
||||
} catch (_) {
|
||||
if (onDoneCallback) scheduleNextStep(7500);
|
||||
playDeployTimeWavOrFormant(clipId, rawSpeech, finishSceneSpeech);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -691,9 +877,7 @@
|
||||
clearTimeout(playTimer);
|
||||
playTimer = null;
|
||||
}
|
||||
if (typeof window.speechSynthesis !== "undefined") {
|
||||
try { window.speechSynthesis.cancel(); } catch (_) {}
|
||||
}
|
||||
stopAllVoiceOutputs();
|
||||
renderTutorialUI();
|
||||
}
|
||||
|
||||
@@ -731,9 +915,13 @@
|
||||
if (speechBtn) {
|
||||
speechBtn.textContent = speechEnabled ? "\uD83D\uDD0A Voice: ON" : "\uD83D\uDD07 Voice: OFF";
|
||||
speechBtn.className = speechEnabled ? "btn primary" : "btn";
|
||||
speechBtn.title = speechEnabled
|
||||
? ("Active Voice Engine: " + activeVoiceEngine + " (Click to mute)")
|
||||
: "Voice Narration Muted (Click to enable spoken narration)";
|
||||
}
|
||||
if (stepBadge) {
|
||||
stepBadge.textContent = ep.code + " \u2022 Scene " + (curStepIdx + 1) + "/" + ep.keyframes.length + (kf.uiElementLabel ? " \u2022 [" + kf.uiElementLabel + "]" : "");
|
||||
const voxTag = speechEnabled ? (" \u2022 \uD83D\uDD0A " + activeVoiceEngine) : "";
|
||||
stepBadge.textContent = ep.code + " \u2022 Scene " + (curStepIdx + 1) + "/" + ep.keyframes.length + voxTag + (kf.uiElementLabel ? " \u2022 [" + kf.uiElementLabel + "]" : "");
|
||||
}
|
||||
if (scrubber) {
|
||||
scrubber.max = String(ep.keyframes.length - 1);
|
||||
@@ -784,9 +972,7 @@
|
||||
speechBtn.onclick = function () {
|
||||
speechEnabled = !speechEnabled;
|
||||
if (speechEnabled) speakSubtitle(getActiveKeyframe(), isPlaying);
|
||||
else if (typeof window.speechSynthesis !== "undefined") {
|
||||
try { window.speechSynthesis.cancel(); } catch (_) {}
|
||||
}
|
||||
else stopAllVoiceOutputs();
|
||||
renderTutorialUI();
|
||||
};
|
||||
}
|
||||
@@ -806,7 +992,7 @@
|
||||
}
|
||||
|
||||
const exportedApi = {
|
||||
version: "2.12.0",
|
||||
version: "2.13.0",
|
||||
episodes: EPISODES,
|
||||
allHighlightableDomIds: ALL_HIGHLIGHTABLE_DOM_IDS,
|
||||
cleanTextForSpeech: cleanTextForSpeech,
|
||||
|
||||
Reference in new issue
Block a user