From ea7e349fc22968e2caa6889e8636c74abeb3641a Mon Sep 17 00:00:00 2001 From: Ein Anderssono Date: Fri, 16 Oct 2026 00:55:32 +0200 Subject: [PATCH] Report actual speech playback state --- README.md | 2 +- index.html | 9 +++++++++ scripts/check-frontend.sh | 3 +++ tests/voice.test.cjs | 13 +++++++++++++ voice.js | 9 +++++++-- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f7cb5ee..f1f4d84 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ When a character or NPC is created, its saved voice identity is masculine, femin System voice commands require the prefix `röstkommando`: `upprepa`, `avbryt`, `hjälp`, `alternativ`, or `paus`. Without the prefix, the same words remain ordinary dialogue. Speech recognition and synthesis are browser-dependent; release acceptance must record which real iPhone/Safari/VoiceOver and Android/Chrome/TalkBack combinations were actually checked. -On iPhone and iPad, Safari may require a direct tap or key press before it permits speech output. The frontend primes speech silently on the first player gesture and then reads game replies automatically. If the first-visit introduction did not play, use **Läs introduktionen igen**; if a game reply does not start, a **Läs senaste repliken** button appears in the dialogue, with the same action also available in voice settings. The introduction is only marked as heard after the browser reports that speech actually started. The speech queue avoids an immediate `cancel()` followed by `speak()`, which older WebKit releases can drop. +On iPhone and iPad, Safari may require a direct tap or key press before it permits speech output. The frontend primes speech silently on the first player gesture and then reads game replies automatically. The status reports speech as in progress only after the browser's `onstart` event. Voice settings include **Testa ljudet**; if a game reply does not start, a **Läs senaste repliken** button appears in the dialogue. When Safari reports speech started but the player hears nothing, the status suggests checking device volume and audio output. The introduction is only marked as heard after the browser reports that speech actually started. The speech queue avoids an immediate `cancel()` followed by `speak()`, which older WebKit releases can drop. The browser and API stay on the same public origin, `https://ai-spel.k3s.k0.nu`. The ingress routes `/` to this frontend service and routes `/api`, `/version`, and `/health` to the Rust service. That keeps the current server-side session cookie same-origin and avoids browser CORS configuration. diff --git a/index.html b/index.html index 475e9ab..7873c34 100644 --- a/index.html +++ b/index.html @@ -219,6 +219,7 @@ microphone.textContent = listening ? 'Avsluta mikrofon' : 'Starta mikrofon'; refreshMicrophone(); if (message) setVoiceStatus(message); + else if (state === 'IDLE') setVoiceStatus(''); if (/Läs senaste repliken/.test(message)) showSpeechRecovery(message); playVoiceSignal(state === 'IDLE' && /Mikrofonen är avstängd/.test(message) ? 'STOPPED' : state); }, @@ -267,8 +268,16 @@ signalLabel.append(signals, document.createTextNode(' Ljudsignal vid röstinmatning')); fieldset.append(signalLabel); item.append(fieldset); const speakLatest = () => { + voiceController.activateFromUserGesture(); if (!voiceController.speak(latestAssistantText, {...latestAssistantVoice, onStart: clearSpeechRecovery})) setVoiceStatus('Talsyntes stöds inte i den här webbläsaren. Dialogen finns kvar som text.'); }; + const testSpeech = () => { + voiceController.activateFromUserGesture(); + if (!voiceController.speak('Det här är ett test av uppläsningen.', {role: 'narrator', voiceIdentity: 'neutral', onStart: clearSpeechRecovery})) { + setVoiceStatus('Talsyntes stöds inte i den här webbläsaren. Dialogen finns kvar som text.'); + } + }; + addVoiceButton(item, 'Testa ljudet', testSpeech); addVoiceButton(item, 'Läs senaste repliken', speakLatest); addVoiceButton(item, 'Upprepa', speakLatest); addVoiceButton(item, 'Avbryt uppläsning', () => voiceController.cancelSpeech()); diff --git a/scripts/check-frontend.sh b/scripts/check-frontend.sh index 8f152c2..a121693 100755 --- a/scripts/check-frontend.sh +++ b/scripts/check-frontend.sh @@ -23,9 +23,12 @@ grep -q "if (options.speak !== false && location.pathname !== '/admin')" index.h grep -q 'id="microphone" aria-pressed="false"' index.html grep -q 'id="voice-status" role="status" aria-live="polite"' index.html grep -q "window.GameVoice.createVoiceController" index.html +grep -q "addVoiceButton(item, 'Testa ljudet'" index.html grep -q 'Jag förstår – starta mikrofonen' index.html grep -q 'supportsRecognition' voice.js grep -q 'characterVoiceProfile' voice.js +grep -q 'utterance.volume = 1' voice.js +grep -q 'Uppläsningen har startat' voice.js grep -q 'WebKit versions before Safari 27' voice.js grep -q "voiceIdentity: speakingNpc.voice_identity" index.html grep -q "role: 'narrator', voiceIdentity: 'neutral'" index.html diff --git a/tests/voice.test.cjs b/tests/voice.test.cjs index 7d1134e..537debf 100644 --- a/tests/voice.test.cjs +++ b/tests/voice.test.cjs @@ -130,6 +130,17 @@ test('TTS selects Swedish fallback, applies rate, and cancellation invalidates c assert.equal(h.controller.getState(), 'IDLE'); }); +test('speech is reported as in progress only after the native onstart event', () => { + const h = makeHarness(); + h.controller.speak('Hej.'); + assert.equal(h.controller.getState(), 'QUEUED'); + assert.match(h.states.at(-1).message, /Startar uppläsningen/); + h.utterances[0].onstart(); + assert.equal(h.controller.getState(), 'SPEAKING'); + assert.match(h.states.at(-1).message, /Uppläsningen har startat/); + assert.match(h.states.at(-1).message, /iPadens volym/); +}); + test('iOS user gesture primes speech silently and idle speech is not cancelled before speak', () => { const h = makeHarness(); assert.equal(h.controller.activateFromUserGesture(), true); @@ -202,6 +213,8 @@ test('TTS and microphone are mutually exclusive', () => { h.controller.startListening(); h.controller.speak('Svar'); assert.equal(h.recognitions[0].aborted, true); + assert.equal(h.controller.getState(), 'QUEUED'); + h.utterances[0].onstart(); assert.equal(h.controller.getState(), 'SPEAKING'); }); diff --git a/voice.js b/voice.js index 4edf212..c1c2ad0 100644 --- a/voice.js +++ b/voice.js @@ -205,7 +205,10 @@ } function speak(text, voiceOptions = {}) { - if (!text || !win.speechSynthesis || !win.SpeechSynthesisUtterance) return false; + if (!text || !win.speechSynthesis || !win.SpeechSynthesisUtterance) { + setState('IDLE', 'Talsyntes stöds inte i den här webbläsaren. Dialogen finns kvar som text.'); + return false; + } stopListening(); const hadQueuedUtterance = currentUtterance !== null; if (hadQueuedUtterance) cancelSpeech(); @@ -214,6 +217,7 @@ try { utterance = new win.SpeechSynthesisUtterance(text); } catch (_) { setState('IDLE', 'Talsyntesen kunde inte startas. Dialogen finns kvar som text.'); return false; } utterance.lang = settings.language || 'sv-SE'; + utterance.volume = 1; const profile = voiceOptions.characterId ? settings.speakerProfiles?.[voiceOptions.characterId] || {} : {}; const identityProfile = characterVoiceProfile(voiceOptions.voiceIdentity, voiceOptions.characterId); const isNarrator = voiceOptions.role === 'narrator'; @@ -229,6 +233,7 @@ if (token !== generation) return; if (speechStartTimer !== null) win.clearTimeout(speechStartTimer); speechStartTimer = null; + setState('SPEAKING', 'Uppläsningen har startat. Om du inte hör ljud, kontrollera iPadens volym och ljudutgång.'); if (typeof voiceOptions.onStart === 'function') voiceOptions.onStart(); }; utterance.onend = () => { @@ -254,7 +259,7 @@ : 'Uppläsningen avbröts. Tryck på Läs senaste repliken för att försöka igen. Dialogen finns kvar som text.'; setState('IDLE', message); }; - setState('SPEAKING', 'Läser upp dialog.'); + setState('QUEUED', 'Startar uppläsningen.'); currentUtterance = utterance; speechStartTimer = win.setTimeout(() => { if (token !== generation || currentUtterance !== utterance) return;