Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
474fd80a95 | ||
|
|
bf5ee88610 | ||
|
|
db76289660 | ||
|
|
702780c61b | ||
|
|
eceb33052d | ||
|
|
32e89cca73 | ||
|
|
878fc3f8ca | ||
|
|
954e066859 | ||
|
|
ea7e349fc2 | ||
|
|
557fc77a52 | ||
|
|
d788ca39ec | ||
|
|
00f7334580 | ||
|
|
c5c0fb6b03 | ||
|
|
cc17b501cc | ||
|
|
1c8d429f81 |
@@ -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**; while playing, **Läs senaste repliken** in the voice settings is the recovery action if Safari blocks a reply. 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.
|
||||
|
||||
|
||||
@@ -2,5 +2,5 @@ apiVersion: v2
|
||||
name: ai-spel-frontend
|
||||
description: Static web frontend for the Swedish life simulator
|
||||
type: application
|
||||
version: 0.1.36
|
||||
appVersion: "0.1.36"
|
||||
version: 0.1.43
|
||||
appVersion: "0.1.43"
|
||||
+47
-7
@@ -72,7 +72,7 @@
|
||||
</section>
|
||||
</main>
|
||||
<footer id="version-footer">Frontend <span id="frontend-version">__FRONTEND_VERSION__</span> · API <span id="api-version">hämtar version…</span></footer>
|
||||
<script src="/voice.js"></script>
|
||||
<script src="/voice.js?v=__FRONTEND_VERSION__"></script>
|
||||
<script>
|
||||
const auth = document.querySelector('#auth');
|
||||
const play = document.querySelector('#play');
|
||||
@@ -89,10 +89,10 @@
|
||||
let pendingDialogue = null;
|
||||
let latestAssistantText = '';
|
||||
let latestAssistantVoice = {role: 'narrator', voiceIdentity: 'neutral'};
|
||||
let speechRecoveryElement = null;
|
||||
const voiceSettingsKey = 'ai-spel.voice-settings.v2';
|
||||
let voiceSettings = window.GameVoice.loadSettings(localStorage, voiceSettingsKey, 'ai-spel.voice-settings.v1');
|
||||
let voiceController;
|
||||
let speechActivatedByGesture = false;
|
||||
let audioContext;
|
||||
let voiceDisclosureAcknowledged = false;
|
||||
let adminOffset = 0;
|
||||
@@ -117,7 +117,7 @@
|
||||
latestAssistantText = content;
|
||||
latestAssistantVoice = options.voice || {role: 'narrator', voiceIdentity: 'neutral'};
|
||||
if (options.speak !== false && location.pathname !== '/admin') {
|
||||
voiceController?.speak(content, latestAssistantVoice);
|
||||
voiceController?.speak(content, {...latestAssistantVoice, onStart: clearSpeechRecovery});
|
||||
}
|
||||
}
|
||||
return line;
|
||||
@@ -156,6 +156,33 @@
|
||||
const authVoiceStatus = document.querySelector('#auth-voice-status');
|
||||
if (authVoiceStatus && !auth.hidden && message) authVoiceStatus.textContent = message;
|
||||
}
|
||||
function clearSpeechRecovery() {
|
||||
speechRecoveryElement?.remove();
|
||||
speechRecoveryElement = null;
|
||||
const status = document.querySelector('#voice-status');
|
||||
if (status?.textContent.startsWith('Uppläsningen startade inte')) setVoiceStatus('Läser upp dialog.');
|
||||
}
|
||||
function showSpeechRecovery(message) {
|
||||
clearSpeechRecovery();
|
||||
const item = document.createElement('li');
|
||||
item.dataset.role = 'voice-controls';
|
||||
item.setAttribute('aria-label', 'Uppläsning kunde inte starta');
|
||||
const explanation = document.createElement('p');
|
||||
explanation.textContent = message;
|
||||
const retry = document.createElement('button');
|
||||
retry.type = 'button';
|
||||
retry.textContent = 'Läs senaste repliken';
|
||||
retry.addEventListener('click', () => {
|
||||
voiceController.activateFromUserGesture();
|
||||
if (voiceController.speak(latestAssistantText, {...latestAssistantVoice, onStart: clearSpeechRecovery})) {
|
||||
clearSpeechRecovery();
|
||||
}
|
||||
});
|
||||
item.append(explanation, retry);
|
||||
transcript.append(item);
|
||||
item.scrollIntoView({block: 'end'});
|
||||
speechRecoveryElement = item;
|
||||
}
|
||||
function refreshMicrophone() {
|
||||
const button = document.querySelector('#microphone');
|
||||
if (button) button.disabled = play.hidden || messageField.disabled || submitBusy;
|
||||
@@ -191,6 +218,8 @@
|
||||
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);
|
||||
},
|
||||
onTranscript(text) {
|
||||
@@ -198,8 +227,8 @@
|
||||
},
|
||||
});
|
||||
function primeSpeechFromGesture(event) {
|
||||
if (speechActivatedByGesture || !event.isTrusted) return;
|
||||
speechActivatedByGesture = voiceController.activateFromUserGesture();
|
||||
if (!event.isTrusted) return;
|
||||
voiceController.activateFromUserGesture();
|
||||
}
|
||||
document.addEventListener('pointerdown', primeSpeechFromGesture, true);
|
||||
document.addEventListener('keydown', primeSpeechFromGesture, true);
|
||||
@@ -238,8 +267,16 @@
|
||||
signalLabel.append(signals, document.createTextNode(' Ljudsignal vid röstinmatning')); fieldset.append(signalLabel);
|
||||
item.append(fieldset);
|
||||
const speakLatest = () => {
|
||||
if (!voiceController.speak(latestAssistantText, latestAssistantVoice)) setVoiceStatus('Talsyntes stöds inte i den här webbläsaren. Dialogen finns kvar som text.');
|
||||
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());
|
||||
@@ -318,7 +355,6 @@
|
||||
auth.hidden = true;
|
||||
adminView.hidden = true;
|
||||
play.hidden = false;
|
||||
document.querySelector('#version-footer').hidden = true;
|
||||
refreshMicrophone();
|
||||
messageField.focus();
|
||||
}
|
||||
@@ -702,6 +738,10 @@
|
||||
refreshMicrophone();
|
||||
voiceController.stopListening();
|
||||
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 = '';
|
||||
try {
|
||||
if (stage === 'biography') await handleBiography(message);
|
||||
|
||||
+3
-1
@@ -10,7 +10,9 @@ server {
|
||||
}
|
||||
|
||||
location / {
|
||||
add_header Cache-Control "no-cache" always;
|
||||
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0" always;
|
||||
add_header Pragma "no-cache" always;
|
||||
add_header Expires "0" always;
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ set -euo pipefail
|
||||
test -s index.html
|
||||
grep -q '<html lang="sv">' index.html
|
||||
grep -q 'id="frontend-version">__FRONTEND_VERSION__' index.html
|
||||
grep -q '<script src="/voice.js?v=__FRONTEND_VERSION__"></script>' index.html
|
||||
grep -q 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0' nginx/default.conf
|
||||
grep -q "request('/version')" index.html
|
||||
grep -Fq "credentials: 'same-origin'" index.html
|
||||
grep -q 'id="dialogue" aria-label="Dialoghistorik" role="log" aria-live="polite"' index.html
|
||||
@@ -14,14 +16,19 @@ grep -q 'id="auth-repeat-intro"' index.html
|
||||
grep -q 'function playFirstVisitIntroduction' index.html
|
||||
grep -q 'id="auth-voice-status" role="status" aria-live="polite"' index.html
|
||||
grep -q 'activateFromUserGesture' index.html
|
||||
grep -q 'function showSpeechRecovery' index.html
|
||||
grep -q "retry.textContent = 'Läs senaste repliken'" index.html
|
||||
grep -q "location.pathname !== '/admin'" index.html
|
||||
grep -q "if (options.speak !== false && location.pathname !== '/admin')" 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 "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 'Webbläsaren har startat talsyntesen' 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
|
||||
|
||||
+47
-2
@@ -117,17 +117,44 @@ test('recognition watchdog closes a session that never ends and permits retry',
|
||||
assert.equal(h.recognitions.length, 2);
|
||||
});
|
||||
|
||||
test('TTS selects Swedish fallback, applies rate, and cancellation invalidates callbacks', () => {
|
||||
test('TTS uses the system default voice unless a named Swedish voice is selected', () => {
|
||||
const h = makeHarness();
|
||||
h.controller.update({mode: 'game-voice', rate: 1.4});
|
||||
assert.equal(h.controller.speak('Hej.'), true);
|
||||
const first = h.utterances[0];
|
||||
assert.equal(first.lang, 'sv-SE');
|
||||
assert.equal(first.rate, 1.4);
|
||||
assert.equal(first.voice.name, 'Svensk');
|
||||
assert.equal(first.voice, null);
|
||||
h.controller.cancelSpeech();
|
||||
first.onend();
|
||||
assert.equal(h.controller.getState(), 'IDLE');
|
||||
assert.equal(h.controller.speak('Namngiven röst.', {voiceName: 'Svensk'}), true);
|
||||
assert.equal(h.utterances.at(-1).voice.name, 'Svensk');
|
||||
});
|
||||
|
||||
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', () => {
|
||||
@@ -155,8 +182,24 @@ test('replacing active speech waits one event-loop turn after WebKit cancel', ()
|
||||
test('speech start timeout tells the player how to recover when mobile playback is blocked', () => {
|
||||
const h = makeHarness();
|
||||
h.controller.speak('En replik');
|
||||
const cancelledBeforeTimeout = h.synthesis.cancelled;
|
||||
h.runTimer(4000);
|
||||
assert.match(h.states.at(-1).message, /Tryck på Läs senaste repliken/);
|
||||
assert.equal(h.synthesis.cancelled, cancelledBeforeTimeout + 1);
|
||||
const cancelledBeforeRetry = h.synthesis.cancelled;
|
||||
h.controller.speak('Manuell återläsning');
|
||||
assert.equal(h.synthesis.spoken.at(-1).text, 'Manuell återläsning');
|
||||
assert.equal(h.synthesis.cancelled, cancelledBeforeRetry);
|
||||
});
|
||||
|
||||
test('iOS not-allowed speech error offers an immediate gesture-based retry', () => {
|
||||
const h = makeHarness();
|
||||
h.controller.speak('En replik');
|
||||
h.utterances[0].onerror({error: 'not-allowed'});
|
||||
assert.match(h.states.at(-1).message, /Safari blockerade uppläsningen/);
|
||||
assert.match(h.states.at(-1).message, /Läs senaste repliken/);
|
||||
assert.equal(h.controller.speak('Försök igen'), true);
|
||||
assert.equal(h.synthesis.spoken.at(-1).text, 'Försök igen');
|
||||
});
|
||||
|
||||
test('character voice identity gives stable distinct masculine, feminine, and neutral timbres', () => {
|
||||
@@ -186,6 +229,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.at(-1).onstart();
|
||||
assert.equal(h.controller.getState(), 'SPEAKING');
|
||||
});
|
||||
|
||||
|
||||
@@ -100,6 +100,9 @@
|
||||
// same turn as cancel(). Do not clear an already-empty native queue.
|
||||
if (wasQueued) {
|
||||
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');
|
||||
}
|
||||
@@ -149,6 +152,10 @@
|
||||
return false;
|
||||
}
|
||||
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;
|
||||
let instance;
|
||||
try { instance = new Recognition(); }
|
||||
@@ -205,7 +212,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 +224,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';
|
||||
@@ -222,14 +233,22 @@
|
||||
utterance.pitch = isNarrator ? 1 : Math.min(2, Math.max(0, Number(voiceOptions.pitch ?? profile.pitch ?? identityProfile.pitch) || 1));
|
||||
const requestedName = voiceOptions.voiceName || profile.voiceName || settings.voiceName;
|
||||
const voices = availableVoices();
|
||||
utterance.voice = voices.find(voice => voice.name === requestedName)
|
||||
|| voices.find(voice => /^sv(-|$)/i.test(voice.lang))
|
||||
|| null;
|
||||
// An empty selection means the operating system should choose its
|
||||
// default voice for the requested language. Forcing the first voice
|
||||
// returned by WebKit can select a voice that is listed but cannot speak.
|
||||
utterance.voice = requestedName
|
||||
? voices.find(voice => voice.name === requestedName)
|
||||
|| voices.find(voice => /^sv(-|$)/i.test(voice.lang))
|
||||
|| null
|
||||
: null;
|
||||
utterance.onstart = () => {
|
||||
if (token !== generation) return;
|
||||
if (speechStartTimer !== null) win.clearTimeout(speechStartTimer);
|
||||
speechStartTimer = null;
|
||||
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 = () => {
|
||||
if (token !== generation) return;
|
||||
@@ -240,7 +259,7 @@
|
||||
currentUtterance = null;
|
||||
setState('IDLE');
|
||||
};
|
||||
utterance.onerror = () => {
|
||||
utterance.onerror = event => {
|
||||
if (token !== generation) return;
|
||||
if (speechTimer !== null) win.clearTimeout(speechTimer);
|
||||
if (speechStartTimer !== null) win.clearTimeout(speechStartTimer);
|
||||
@@ -248,13 +267,25 @@
|
||||
speechStartTimer = null;
|
||||
currentUtterance = null;
|
||||
generation += 1;
|
||||
setState('IDLE', 'Uppläsningen avbröts. Dialogen finns kvar som text.');
|
||||
const blocked = event?.error === 'not-allowed' || event?.error === 'service-not-allowed';
|
||||
const message = blocked
|
||||
? 'Safari blockerade uppläsningen. Tryck på Läs senaste repliken för att starta ljudet.'
|
||||
: '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;
|
||||
speechStartTimer = null;
|
||||
generation += 1;
|
||||
currentUtterance = null;
|
||||
if (speechTimer !== null) win.clearTimeout(speechTimer);
|
||||
speechTimer = null;
|
||||
// Drop a blocked native utterance now. A player retry can then call
|
||||
// speak() directly from its button gesture instead of following a
|
||||
// same-turn cancel() that older WebKit versions may discard.
|
||||
try { win.speechSynthesis.cancel(); } catch (_) { /* Best effort. */ }
|
||||
setState('IDLE', 'Uppläsningen startade inte. Tryck på Läs senaste repliken för att starta ljudet.');
|
||||
}, 4000);
|
||||
speechTimer = win.setTimeout(() => {
|
||||
|
||||
Reference in New Issue
Block a user