Speak dialogue with persistent character voices
Build and publish frontend / publish (push) Successful in 50s

This commit is contained in:
Ein Anderssono
2026-10-15 10:06:04 +02:00
parent c9e31d23ba
commit f2a2a3b30a
5 changed files with 69 additions and 20 deletions
+2
View File
@@ -12,6 +12,8 @@ The game is text- and dialogue-based and uses no images. Place descriptions incl
Voice input and output are optional browser features. The microphone button beside the text field uses Web Speech API when available and sends only its final Swedish transcript through the same submission path as typed text. Before its first use, the client explains that the browser may process audio through a provider-selected service; the game itself does not receive or store recordings, while the final transcript is saved as ordinary dialogue. Writing `röstinställningar` or saying `röstkommando alternativ` opens the temporary controls in the dialogue. The player can choose `Spelets röst`, `Skärmläsare`, or `Endast text`, select available Swedish speech voices and rate, and enable auto-read and short audio cues. The default mode is `Skärmläsare`; auto-read is off. These preferences are stored in browser `localStorage`. If speech APIs are unavailable or permission is denied, text play remains available.
When a character or NPC is created, its saved voice identity is masculine, feminine, or neutral. The biography prompt only assigns masculine/feminine when the character is explicitly described that way; otherwise it uses neutral. Speech synthesis applies a stable, subtly varied pitch/rate profile per character, so a character keeps the same sound across visits. Narration always uses neutral pitch. The browser's selected Swedish voice still determines the underlying voice, so the exact sound depends on installed system voices.
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.
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.
+13 -5
View File
@@ -84,6 +84,7 @@
let submitBusy = false;
let pendingDialogue = null;
let latestAssistantText = '';
let latestAssistantVoice = {role: 'narrator', voiceIdentity: 'neutral'};
const voiceSettingsKey = 'ai-spel.voice-settings.v1';
let voiceSettings = window.GameVoice.loadSettings(localStorage, voiceSettingsKey);
let voiceController;
@@ -106,9 +107,10 @@
line.scrollIntoView({block: 'end'});
if (role === 'assistant') {
latestAssistantText = content;
latestAssistantVoice = options.voice || {role: 'narrator', voiceIdentity: 'neutral'};
if (options.speak !== false && !content.trim().endsWith('…') && !content.trim().endsWith('...')
&& voiceSettings.mode === 'game-voice' && voiceSettings.autoSpeak && !play.hidden) {
voiceController?.speak(content, options.voice || {});
voiceController?.speak(content, latestAssistantVoice);
}
}
return line;
@@ -199,7 +201,7 @@
signalLabel.append(signals, document.createTextNode(' Ljudsignal vid röstinmatning')); fieldset.append(signalLabel);
item.append(fieldset);
const speakLatest = () => {
if (!voiceController.speak(latestAssistantText)) setVoiceStatus('Talsyntes stöds inte här eller är avstängd. Dialogen finns kvar som text.');
if (!voiceController.speak(latestAssistantText, latestAssistantVoice)) setVoiceStatus('Talsyntes stöds inte här eller är avstängd. Dialogen finns kvar som text.');
};
addVoiceButton(item, 'Läs senaste repliken', speakLatest);
addVoiceButton(item, 'Upprepa', speakLatest);
@@ -231,7 +233,7 @@
if (!parsed) return false;
if (parsed.command === 'settings') appendVoiceSettings();
else if (parsed.command === 'repeat') {
if (!voiceController.speak(latestAssistantText)) setVoiceStatus('Talsyntes stöds inte här eller är avstängd. Dialogen finns kvar som text.');
if (!voiceController.speak(latestAssistantText, latestAssistantVoice)) setVoiceStatus('Talsyntes stöds inte här eller är avstängd. Dialogen finns kvar som text.');
} else if (parsed.command === 'stop') {
voiceController.cancelSpeech(); voiceController.stopListening(); setVoiceStatus('Röstinteraktionen är avbruten.');
} else if (parsed.command === 'help') {
@@ -532,10 +534,16 @@
gameState.world.version = result.world_version;
gameState.world.world_time = result.world_time;
pendingDialogue = null;
if (result.intent === 'move') {
try {
const refreshed = await request('/api/world');
if (refreshed.world) gameState = refreshed;
} catch (_) { /* The committed dialogue stays successful if this optional view refresh fails. */ }
}
const speakingNpc = result.intent === 'speak' ? gameState.world.npc : null;
appendLine('assistant', result.reply, speakingNpc
? {speaker: speakingNpc.name, voice: {characterId: speakingNpc.id}}
: {speaker: 'Berättelsen'});
? {speaker: speakingNpc.name, voice: {characterId: speakingNpc.id, voiceIdentity: speakingNpc.voice_identity}}
: {speaker: 'Berättelsen', voice: {role: 'narrator', voiceIdentity: 'neutral'}});
} catch (error) {
messageField.value = message;
appendLine('assistant', `Meddelandet kunde inte behandlas: ${error.message}. Texten ligger kvar i inmatningen; skicka igen för att försöka på nytt.`);
+3
View File
@@ -13,6 +13,9 @@ grep -q 'id="voice-status" role="status" aria-live="polite"' index.html
grep -q "window.GameVoice.createVoiceController" index.html
grep -q 'Jag förstår – starta mikrofonen' index.html
grep -q 'supportsRecognition' voice.js
grep -q 'characterVoiceProfile' voice.js
grep -q "voiceIdentity: speakingNpc.voice_identity" index.html
grep -q "role: 'narrator', voiceIdentity: 'neutral'" index.html
grep -q 'COPY voice.js /usr/share/nginx/html/voice.js' Dockerfile
grep -q "SpeechRecognition || win.webkitSpeechRecognition" voice.js
grep -q "submitMessage(text, 'voice')" index.html
+35 -12
View File
@@ -5,6 +5,9 @@ const test = require('node:test');
const vm = require('node:vm');
const source = fs.readFileSync(path.join(__dirname, '..', 'voice.js'), 'utf8');
const apiWindow = {};
vm.runInNewContext(source, {window: apiWindow, globalThis: apiWindow, Promise, String, Math, Object});
const GameVoice = apiWindow.GameVoice;
function makeHarness({supported = true, tts = true, onTranscript = () => Promise.resolve()} = {}) {
const recognitions = [];
@@ -119,6 +122,26 @@ test('TTS selects Swedish fallback, applies rate, and cancellation invalidates c
assert.equal(h.controller.getState(), 'IDLE');
});
test('character voice identity gives stable distinct masculine, feminine, and neutral timbres', () => {
const profile = GameVoice.characterVoiceProfile;
const masculine = profile('masculine', 'character-1');
const feminine = profile('feminine', 'character-1');
const neutral = profile('neutral', 'character-1');
assert.equal(profile('masculine', 'character-1').pitch, masculine.pitch);
assert.ok(masculine.pitch < neutral.pitch);
assert.ok(neutral.pitch < feminine.pitch);
assert.notEqual(profile('masculine', 'character-2').pitch, masculine.pitch);
});
test('narrator uses neutral pitch while character replies use their saved voice identity', () => {
const h = makeHarness();
h.controller.update({mode: 'game-voice'});
h.controller.speak('Berättarröst', {role: 'narrator', voiceIdentity: 'feminine', characterId: 'narrator'});
h.controller.speak('NPC-replik', {voiceIdentity: 'masculine', characterId: 'npc-1'});
assert.equal(h.utterances[0].pitch, 1);
assert.ok(h.utterances[1].pitch < 1);
});
test('TTS and microphone are mutually exclusive', () => {
const h = makeHarness();
h.controller.update({mode: 'game-voice'});
@@ -139,13 +162,13 @@ test('speech is disabled in screen-reader mode and gracefully degrades without s
test('system commands require an explicit prefix; ordinary dialogue words are untouched', () => {
const global = {};
vm.runInNewContext(source, {window: global, globalThis: global, Promise, String, Math, Object});
assert.equal(global.GameVoice.parseVoiceCommand('upprepa'), null);
assert.equal(global.GameVoice.parseVoiceCommand('hjälp'), null);
assert.equal(global.GameVoice.parseVoiceCommand('röstkommando upprepa.' ).command, 'repeat');
assert.equal(global.GameVoice.parseVoiceCommand('röstkommando alternativ').command, 'settings');
assert.equal(global.GameVoice.parseVoiceCommand('röstinställningar').command, 'settings');
assert.equal(global.GameVoice.parseVoiceCommand('röstinställningar', {requirePrefix: true}), null);
assert.equal(global.GameVoice.parseVoiceCommand('röstkommando alternativ', {requirePrefix: true}).command, 'settings');
assert.equal(GameVoice.parseVoiceCommand('upprepa'), null);
assert.equal(GameVoice.parseVoiceCommand('hjälp'), null);
assert.equal(GameVoice.parseVoiceCommand('röstkommando upprepa.' ).command, 'repeat');
assert.equal(GameVoice.parseVoiceCommand('röstkommando alternativ').command, 'settings');
assert.equal(GameVoice.parseVoiceCommand('röstinställningar').command, 'settings');
assert.equal(GameVoice.parseVoiceCommand('röstinställningar', {requirePrefix: true}), null);
assert.equal(GameVoice.parseVoiceCommand('röstkommando alternativ', {requirePrefix: true}).command, 'settings');
});
test('settings survive reload and invalid storage falls back to accessible defaults', () => {
@@ -153,14 +176,14 @@ test('settings survive reload and invalid storage falls back to accessible defau
vm.runInNewContext(source, {window: global, globalThis: global, Promise, String, Math, Object});
const values = new Map();
const storage = {getItem: key => values.get(key) || null, setItem: (key, value) => values.set(key, value)};
const requested = {...global.GameVoice.DEFAULT_SETTINGS, mode: 'game-voice', autoSpeak: true, rate: 1.5, voiceName: 'Svensk'};
assert.equal(global.GameVoice.saveSettings(storage, 'voice', requested), true);
const loaded = global.GameVoice.loadSettings(storage, 'voice');
const requested = {...GameVoice.DEFAULT_SETTINGS, mode: 'game-voice', autoSpeak: true, rate: 1.5, voiceName: 'Svensk'};
assert.equal(GameVoice.saveSettings(storage, 'voice', requested), true);
const loaded = GameVoice.loadSettings(storage, 'voice');
assert.equal(loaded.mode, 'game-voice');
assert.equal(loaded.autoSpeak, true);
assert.equal(loaded.rate, 1.5);
assert.equal(loaded.voiceName, 'Svensk');
values.set('voice', '{trasig');
assert.equal(global.GameVoice.loadSettings(storage, 'voice').mode, 'screen-reader');
assert.equal(global.GameVoice.saveSettings({setItem() { throw new Error('blocked'); }}, 'voice', requested), false);
assert.equal(GameVoice.loadSettings(storage, 'voice').mode, 'screen-reader');
assert.equal(GameVoice.saveSettings({setItem() { throw new Error('blocked'); }}, 'voice', requested), false);
});
+16 -3
View File
@@ -44,6 +44,16 @@
catch (_) { return false; }
}
function characterVoiceProfile(voiceIdentity, characterId = '') {
const identity = ['masculine', 'feminine', 'neutral'].includes(voiceIdentity) ? voiceIdentity : 'neutral';
let hash = 2166136261;
for (const char of String(characterId)) hash = Math.imul(hash ^ char.charCodeAt(0), 16777619) >>> 0;
const variation = (hash % 1000) / 1000;
if (identity === 'masculine') return {pitch: 0.88 + variation * 0.08, rate: 0.96 + variation * 0.04};
if (identity === 'feminine') return {pitch: 1.06 + variation * 0.08, rate: 0.98 + variation * 0.04};
return {pitch: 0.98 + variation * 0.04, rate: 1};
}
function createVoiceController(options = {}) {
const win = options.window || root;
const onState = options.onState || (() => {});
@@ -163,8 +173,11 @@
catch (_) { setState('IDLE', 'Talsyntesen kunde inte startas. Dialogen finns kvar som text.'); return false; }
utterance.lang = settings.language || 'sv-SE';
const profile = voiceOptions.characterId ? settings.speakerProfiles?.[voiceOptions.characterId] || {} : {};
utterance.rate = Math.min(2, Math.max(0.5, Number(voiceOptions.rate ?? profile.rate ?? settings.rate) || 1));
utterance.pitch = Math.min(2, Math.max(0, Number(voiceOptions.pitch ?? profile.pitch ?? 1) || 1));
const identityProfile = characterVoiceProfile(voiceOptions.voiceIdentity, voiceOptions.characterId);
const isNarrator = voiceOptions.role === 'narrator';
const configuredRate = Number(voiceOptions.rate ?? profile.rate ?? settings.rate) || 1;
utterance.rate = Math.min(2, Math.max(0.5, configuredRate * (isNarrator || voiceOptions.rate != null || profile.rate != null ? 1 : identityProfile.rate)));
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)
@@ -205,5 +218,5 @@
return {startListening, stopListening, speak, cancelSpeech, stopAll, update, getState, getSettings, getVoices, supportsRecognition};
}
root.GameVoice = {createVoiceController, DEFAULT_SETTINGS, parseVoiceCommand, loadSettings, saveSettings};
root.GameVoice = {createVoiceController, DEFAULT_SETTINGS, parseVoiceCommand, loadSettings, saveSettings, characterVoiceProfile};
})(typeof window !== 'undefined' ? window : globalThis);