Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0876538e58 | ||
|
|
f2a2a3b30a | ||
|
|
c9e31d23ba | ||
|
|
1f9fc464d4 |
@@ -10,7 +10,9 @@ The separate `/admin` route provides an authenticated, read-only operations view
|
||||
|
||||
The game is text- and dialogue-based and uses no images. Place descriptions include sound, smell, air/temperature, touch/surface, and spatial-orientation cues. Visible details may also be described in words, but are never required to understand the scene or play. The transcript is screen-reader announced, and Enter submits the input while Shift+Enter inserts a newline; no separate send button is required.
|
||||
|
||||
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. 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. No audio is uploaded or stored by this feature.
|
||||
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.
|
||||
|
||||
|
||||
@@ -2,5 +2,5 @@ apiVersion: v2
|
||||
name: ai-spel-frontend
|
||||
description: Static web frontend for the Swedish life simulator
|
||||
type: application
|
||||
version: 0.1.26
|
||||
appVersion: "0.1.26"
|
||||
version: 0.1.28
|
||||
appVersion: "0.1.28"
|
||||
+29
-5
@@ -84,10 +84,12 @@
|
||||
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;
|
||||
let audioContext;
|
||||
let voiceDisclosureAcknowledged = false;
|
||||
let adminOffset = 0;
|
||||
const adminPageSize = 50;
|
||||
|
||||
@@ -105,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;
|
||||
@@ -198,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);
|
||||
@@ -211,12 +214,26 @@
|
||||
addVoiceButton(item, 'Stäng röstinställningar', () => item.remove());
|
||||
transcript.append(item); item.scrollIntoView({block: 'end'}); mode.focus();
|
||||
}
|
||||
function requestVoiceDisclosure() {
|
||||
const item = document.createElement('li'); item.dataset.role = 'voice-controls';
|
||||
item.setAttribute('aria-label', 'Information om mikrofonen');
|
||||
const message = document.createElement('p');
|
||||
message.textContent = 'Innan du startar: webbläsarens taligenkänning kan behandla ljud genom en tjänst som webbläsaren väljer. Spelet tar inte emot eller sparar ljudinspelningen. Den färdiga transkriberingen skickas och sparas som din vanliga dialog. Fortsätt bara om du godkänner webbläsarens hantering av tal.';
|
||||
item.append(message);
|
||||
addVoiceButton(item, 'Jag förstår – starta mikrofonen', () => {
|
||||
voiceDisclosureAcknowledged = true;
|
||||
item.remove();
|
||||
voiceController.startListening();
|
||||
});
|
||||
transcript.append(item); item.scrollIntoView({block: 'end'});
|
||||
item.querySelector('button').focus();
|
||||
}
|
||||
function handleVoiceCommand(message, source) {
|
||||
const parsed = window.GameVoice.parseVoiceCommand(message, {requirePrefix: source === 'voice'});
|
||||
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') {
|
||||
@@ -226,6 +243,7 @@
|
||||
}
|
||||
document.querySelector('#microphone').addEventListener('click', () => {
|
||||
if (voiceController.getState() === 'LISTENING') voiceController.stopListening();
|
||||
else if (!voiceDisclosureAcknowledged && voiceController.supportsRecognition()) requestVoiceDisclosure();
|
||||
else voiceController.startListening();
|
||||
});
|
||||
document.addEventListener('visibilitychange', () => { if (document.hidden) voiceController.stopAll(); });
|
||||
@@ -516,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.`);
|
||||
|
||||
@@ -11,6 +11,11 @@ grep -q 'id="message" name="message"' 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 '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
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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)
|
||||
@@ -201,8 +214,9 @@
|
||||
function getState() { return state; }
|
||||
function getSettings() { return {...settings}; }
|
||||
function getVoices() { return availableVoices(); }
|
||||
return {startListening, stopListening, speak, cancelSpeech, stopAll, update, getState, getSettings, getVoices};
|
||||
function supportsRecognition() { return Boolean(win.SpeechRecognition || win.webkitSpeechRecognition); }
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user