Compare commits

...
7 Commits
Author SHA1 Message Date
Gitea Actions db76289660 Release 0.1.42 [auto-version-bump] 2026-10-16 06:19:32 +02:00
Ein Anderssono 702780c61b Fix speech controller test after microphone priming
Build and publish frontend / publish (push) Successful in 42s
2026-10-16 06:19:09 +02:00
Ein Anderssono eceb33052d Restore iOS speech after cancelling queued audio
Build and publish frontend / publish (push) Failing after 2s
2026-10-16 06:18:26 +02:00
Gitea Actions 32e89cca73 Release 0.1.41 [auto-version-bump] 2026-10-16 00:58:09 +02:00
Ein Anderssono 878fc3f8ca Keep release versions visible during gameplay
Build and publish frontend / publish (push) Successful in 43s
2026-10-16 00:57:45 +02:00
Gitea Actions 954e066859 Release 0.1.40 [auto-version-bump] 2026-10-16 00:55:53 +02:00
Ein Anderssono ea7e349fc2 Report actual speech playback state
Build and publish frontend / publish (push) Successful in 41s
2026-10-16 00:55:32 +02:00
7 changed files with 65 additions and 10 deletions
+1 -1
View File
@@ -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. 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. 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.
+1 -1
View File
@@ -1 +1 @@
0.1.39 0.1.42
+2 -2
View File
@@ -2,5 +2,5 @@ apiVersion: v2
name: ai-spel-frontend name: ai-spel-frontend
description: Static web frontend for the Swedish life simulator description: Static web frontend for the Swedish life simulator
type: application type: application
version: 0.1.39 version: 0.1.42
appVersion: "0.1.39" appVersion: "0.1.42"
+15 -4
View File
@@ -93,7 +93,6 @@
const voiceSettingsKey = 'ai-spel.voice-settings.v2'; const voiceSettingsKey = 'ai-spel.voice-settings.v2';
let voiceSettings = window.GameVoice.loadSettings(localStorage, voiceSettingsKey, 'ai-spel.voice-settings.v1'); let voiceSettings = window.GameVoice.loadSettings(localStorage, voiceSettingsKey, 'ai-spel.voice-settings.v1');
let voiceController; let voiceController;
let speechActivatedByGesture = false;
let audioContext; let audioContext;
let voiceDisclosureAcknowledged = false; let voiceDisclosureAcknowledged = false;
let adminOffset = 0; let adminOffset = 0;
@@ -219,6 +218,7 @@
microphone.textContent = listening ? 'Avsluta mikrofon' : 'Starta mikrofon'; microphone.textContent = listening ? 'Avsluta mikrofon' : 'Starta mikrofon';
refreshMicrophone(); refreshMicrophone();
if (message) setVoiceStatus(message); if (message) setVoiceStatus(message);
else if (state === 'IDLE') setVoiceStatus('');
if (/Läs senaste repliken/.test(message)) showSpeechRecovery(message); if (/Läs senaste repliken/.test(message)) showSpeechRecovery(message);
playVoiceSignal(state === 'IDLE' && /Mikrofonen är avstängd/.test(message) ? 'STOPPED' : state); playVoiceSignal(state === 'IDLE' && /Mikrofonen är avstängd/.test(message) ? 'STOPPED' : state);
}, },
@@ -227,8 +227,8 @@
}, },
}); });
function primeSpeechFromGesture(event) { function primeSpeechFromGesture(event) {
if (speechActivatedByGesture || !event.isTrusted) return; if (!event.isTrusted) return;
speechActivatedByGesture = voiceController.activateFromUserGesture(); voiceController.activateFromUserGesture();
} }
document.addEventListener('pointerdown', primeSpeechFromGesture, true); document.addEventListener('pointerdown', primeSpeechFromGesture, true);
document.addEventListener('keydown', primeSpeechFromGesture, true); document.addEventListener('keydown', primeSpeechFromGesture, true);
@@ -267,8 +267,16 @@
signalLabel.append(signals, document.createTextNode(' Ljudsignal vid röstinmatning')); fieldset.append(signalLabel); signalLabel.append(signals, document.createTextNode(' Ljudsignal vid röstinmatning')); fieldset.append(signalLabel);
item.append(fieldset); item.append(fieldset);
const speakLatest = () => { 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.'); 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, 'Läs senaste repliken', speakLatest);
addVoiceButton(item, 'Upprepa', speakLatest); addVoiceButton(item, 'Upprepa', speakLatest);
addVoiceButton(item, 'Avbryt uppläsning', () => voiceController.cancelSpeech()); addVoiceButton(item, 'Avbryt uppläsning', () => voiceController.cancelSpeech());
@@ -347,7 +355,6 @@
auth.hidden = true; auth.hidden = true;
adminView.hidden = true; adminView.hidden = true;
play.hidden = false; play.hidden = false;
document.querySelector('#version-footer').hidden = true;
refreshMicrophone(); refreshMicrophone();
messageField.focus(); messageField.focus();
} }
@@ -731,6 +738,10 @@
refreshMicrophone(); refreshMicrophone();
voiceController.stopListening(); voiceController.stopListening();
voiceController.cancelSpeech(); voiceController.cancelSpeech();
// cancel() above may also have discarded Safari's silent speech-channel
// primer. Restore it synchronously inside the submit gesture, before
// waiting for the API response that will be read aloud.
voiceController.activateFromUserGesture();
messageField.value = ''; messageField.value = '';
try { try {
if (stage === 'biography') await handleBiography(message); if (stage === 'biography') await handleBiography(message);
+3
View File
@@ -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="microphone" aria-pressed="false"' index.html
grep -q 'id="voice-status" role="status" aria-live="polite"' index.html grep -q 'id="voice-status" role="status" aria-live="polite"' index.html
grep -q "window.GameVoice.createVoiceController" 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 'Jag förstår – starta mikrofonen' index.html
grep -q 'supportsRecognition' voice.js grep -q 'supportsRecognition' voice.js
grep -q 'characterVoiceProfile' voice.js grep -q 'characterVoiceProfile' voice.js
grep -q 'utterance.volume = 1' voice.js
grep -q 'Webbläsaren har startat talsyntesen' voice.js
grep -q 'WebKit versions before Safari 27' voice.js grep -q 'WebKit versions before Safari 27' voice.js
grep -q "voiceIdentity: speakingNpc.voice_identity" index.html grep -q "voiceIdentity: speakingNpc.voice_identity" index.html
grep -q "role: 'narrator', voiceIdentity: 'neutral'" index.html grep -q "role: 'narrator', voiceIdentity: 'neutral'" index.html
+27
View File
@@ -130,6 +130,31 @@ test('TTS selects Swedish fallback, applies rate, and cancellation invalidates c
assert.equal(h.controller.getState(), 'IDLE'); assert.equal(h.controller.getState(), 'IDLE');
}); });
test('a player gesture restores the iOS speech primer after cancelling a queued reply', () => {
const h = makeHarness();
assert.equal(h.controller.activateFromUserGesture(), true);
assert.equal(h.synthesis.spoken[0].text, ' ');
h.controller.speak('Tidigare replik');
h.controller.cancelSpeech();
const countAfterCancel = h.synthesis.spoken.length;
assert.equal(h.controller.activateFromUserGesture(), true);
assert.equal(h.synthesis.spoken.length, countAfterCancel + 1);
assert.equal(h.synthesis.spoken.at(-1).text, ' ');
h.controller.speak('Nytt svar');
assert.equal(h.synthesis.spoken.at(-1).text, 'Nytt svar');
});
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, /Webbläsaren har startat talsyntesen/);
assert.match(h.states.at(-1).message, /Läs senaste repliken/);
});
test('iOS user gesture primes speech silently and idle speech is not cancelled before speak', () => { test('iOS user gesture primes speech silently and idle speech is not cancelled before speak', () => {
const h = makeHarness(); const h = makeHarness();
assert.equal(h.controller.activateFromUserGesture(), true); assert.equal(h.controller.activateFromUserGesture(), true);
@@ -202,6 +227,8 @@ test('TTS and microphone are mutually exclusive', () => {
h.controller.startListening(); h.controller.startListening();
h.controller.speak('Svar'); h.controller.speak('Svar');
assert.equal(h.recognitions[0].aborted, true); assert.equal(h.recognitions[0].aborted, true);
assert.equal(h.controller.getState(), 'QUEUED');
h.utterances.at(-1).onstart();
assert.equal(h.controller.getState(), 'SPEAKING'); assert.equal(h.controller.getState(), 'SPEAKING');
}); });
+16 -2
View File
@@ -100,6 +100,9 @@
// same turn as cancel(). Do not clear an already-empty native queue. // same turn as cancel(). Do not clear an already-empty native queue.
if (wasQueued) { if (wasQueued) {
try { win.speechSynthesis?.cancel(); } catch (_) { /* Best effort. */ } try { win.speechSynthesis?.cancel(); } catch (_) { /* Best effort. */ }
// cancel() also removes the silent user-gesture primer from the native
// queue. A later reply must prime again from the next player gesture.
speechPrimed = false;
} }
if (!recognition) setState('IDLE'); if (!recognition) setState('IDLE');
} }
@@ -149,6 +152,10 @@
return false; return false;
} }
cancelSpeech(); cancelSpeech();
// This runs synchronously from the microphone button gesture. If
// cancelSpeech() removed the earlier primer, restore it before starting
// recognition so a spoken reply can follow the transcript.
activateFromUserGesture();
const token = ++generation; const token = ++generation;
let instance; let instance;
try { instance = new Recognition(); } try { instance = new Recognition(); }
@@ -205,7 +212,10 @@
} }
function speak(text, voiceOptions = {}) { 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(); stopListening();
const hadQueuedUtterance = currentUtterance !== null; const hadQueuedUtterance = currentUtterance !== null;
if (hadQueuedUtterance) cancelSpeech(); if (hadQueuedUtterance) cancelSpeech();
@@ -214,6 +224,7 @@
try { utterance = new win.SpeechSynthesisUtterance(text); } try { utterance = new win.SpeechSynthesisUtterance(text); }
catch (_) { setState('IDLE', 'Talsyntesen kunde inte startas. Dialogen finns kvar som text.'); return false; } catch (_) { setState('IDLE', 'Talsyntesen kunde inte startas. Dialogen finns kvar som text.'); return false; }
utterance.lang = settings.language || 'sv-SE'; utterance.lang = settings.language || 'sv-SE';
utterance.volume = 1;
const profile = voiceOptions.characterId ? settings.speakerProfiles?.[voiceOptions.characterId] || {} : {}; const profile = voiceOptions.characterId ? settings.speakerProfiles?.[voiceOptions.characterId] || {} : {};
const identityProfile = characterVoiceProfile(voiceOptions.voiceIdentity, voiceOptions.characterId); const identityProfile = characterVoiceProfile(voiceOptions.voiceIdentity, voiceOptions.characterId);
const isNarrator = voiceOptions.role === 'narrator'; const isNarrator = voiceOptions.role === 'narrator';
@@ -230,6 +241,9 @@
if (speechStartTimer !== null) win.clearTimeout(speechStartTimer); if (speechStartTimer !== null) win.clearTimeout(speechStartTimer);
speechStartTimer = null; speechStartTimer = null;
if (typeof voiceOptions.onStart === 'function') voiceOptions.onStart(); if (typeof voiceOptions.onStart === 'function') voiceOptions.onStart();
// onstart confirms the browser speech engine accepted the utterance;
// it cannot confirm that sound reached the device's speakers.
setState('SPEAKING', 'Webbläsaren har startat talsyntesen. Om du inte hör ljud, tryck på Läs senaste repliken.');
}; };
utterance.onend = () => { utterance.onend = () => {
if (token !== generation) return; if (token !== generation) return;
@@ -254,7 +268,7 @@
: 'Uppläsningen avbröts. Tryck på Läs senaste repliken för att försöka igen. Dialogen finns kvar som text.'; : '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('IDLE', message);
}; };
setState('SPEAKING', 'Läser upp dialog.'); setState('QUEUED', 'Startar uppläsningen.');
currentUtterance = utterance; currentUtterance = utterance;
speechStartTimer = win.setTimeout(() => { speechStartTimer = win.setTimeout(() => {
if (token !== generation || currentUtterance !== utterance) return; if (token !== generation || currentUtterance !== utterance) return;