Voci dei personaggi con XTTS: una per battuta, castello e leone con voci proprie

tools/voci.mjs (make voci) genera un mp3 per ogni battuta con la voce del personaggio scelta in
tools/voci.json, solo per i testi nuovi o cambiati; tools/voci_xtts.py divide le frasi e toglie
punti finali e caporali, che XTTS in italiano leggeva («Buongiorno, punto»). Nel gioco il fumetto
dura quanto la voce, la musica si abbassa mentre si parla, il muto spegne anche le voci.
Il leone ora parla e i personaggi parlano dalla posizione che la scena dichiara (speakers),
che prima era ignorata. Tutte le voci verificate con Whisper; test: ogni battuta ha la sua
voce, il fumetto non finisce prima della voce, la musica si abbassa (provati con errori iniettati).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UdeKd62opy9PKgpQXtVctF
This commit is contained in:
2026-10-23 15:23:08 +02:00
co-authored by Claude Opus 5.5
parent b467bcc1d9
commit 470d6e7f7b
128 changed files with 753 additions and 7 deletions
+8 -2
View File
@@ -47,8 +47,10 @@ export function restoreState(story, data) {
}
export class Game {
constructor(story, { state, onSave } = {}) {
// voiceTime(who, text): secondi della voce registrata per la battuta, se c'è (il fumetto non finisce prima).
constructor(story, { state, onSave, voiceTime } = {}) {
this.story = story;
this.voiceTime = voiceTime ?? (() => undefined);
this.state = state ? restoreState(story, state) : newState(story);
this.onSave = onSave ?? (() => {});
this.queue = [];
@@ -189,6 +191,7 @@ export class Game {
speakerPoint(who) {
if (!who || who === 'andrea') return [this.state.x, this.state.y - 62 * this.scaleAt(this.state.y)];
if (this.scene.speakers?.[who]) return this.scene.speakers[who];
const h = this.hotspot(who) ?? (this.scene.hotspots ?? []).find(x => x.id === who);
if (!h) return [this.state.x, this.state.y - 62];
return h.talkAt ?? [h.box[0] + h.box[2] / 2, h.box[1] - 4];
@@ -232,7 +235,10 @@ export class Game {
run(e) {
const s = this.state;
if (e.say != null) this.speech = { text: e.say, who: e.who ?? 'andrea', pose: e.pose, left: sayTime(e.say) };
if (e.say != null) {
const who = e.who ?? 'andrea', voice = this.voiceTime(who, e.say);
this.speech = { text: e.say, who, pose: e.pose, left: Math.max(sayTime(e.say), voice ? voice + 0.35 : 0) };
}
else if (e.walk) { this.task = e; this.startWalk(e.walk); }
// `go` prima di `face`: un cambio scena porta con sé il verso d'arrivo.
else if (e.go) this.go(e.go, e.at, e.face);
+2 -1
View File
@@ -20,6 +20,7 @@ export class Music {
this.want = null;
this.current = null; // { src, audio }
this.old = [];
this.duck = 1; // 1 = pieno; meno di 1 mentre parla qualcuno
}
// Chiamata a ogni fotogramma con il brano voluto: avvia, cambia e sfuma.
@@ -30,7 +31,7 @@ export class Music {
this.current = src ? { src, audio: this.start(src) } : null;
}
// Il brano in corso sale (o scende) verso il volume scelto; quelli vecchi sfumano fino a zero.
const step = dt / FADE, target = this.muted ? 0 : this.volume;
const step = dt / FADE, target = this.muted ? 0 : this.volume * this.duck;
if (this.current) {
const a = this.current.audio;
a.volume = a.volume < target ? Math.min(target, a.volume + step) : Math.max(target, a.volume - step);
+49
View File
@@ -0,0 +1,49 @@
// Voci dei personaggi: un mp3 per battuta (generati da tools/voci.mjs), cercati per chi parla e testo.
// Senza assets/voices/index.json, o per una battuta nuova non ancora generata, il gioco resta muto e va avanti.
export const voiceKey = (who, text) => `${who}|${text}`;
// Tutte le battute della storia, ovunque stiano (scene, oggetti, dialoghi, rami `if`), più le righe dell'introduzione
// che dice Andrea. Una battuta è un oggetto con `say`; senza `who` parla Andrea.
export function voiceLines(story) {
const seen = new Map();
const add = (who, text) => { if (text.trim() && !/^[.…!?\s]+$/.test(text)) seen.set(voiceKey(who, text), { who, text }); };
const visit = value => {
if (Array.isArray(value)) return value.forEach(visit);
if (!value || typeof value !== 'object') return;
if (typeof value.say === 'string') add(value.who ?? 'andrea', value.say);
Object.values(value).forEach(visit);
};
for (const line of story.intro ?? []) add('andrea', line);
visit(story.scenes);
visit(story.items);
visit(story.dialogues);
return [...seen.values()];
}
export class Voices {
constructor(index = {}, base = 'assets/voices/') {
this.index = index;
this.base = base;
this.audio = null;
}
seconds(who, text) { return this.index[voiceKey(who, text)]?.sec; }
play(who, text, volume = 1) {
this.stop();
const entry = this.index[voiceKey(who, text)];
if (!entry || volume <= 0) return false;
this.audio = new Audio(this.base + entry.file);
this.audio.volume = Math.min(1, volume);
this.audio.play().catch(() => {});
return true;
}
stop() {
if (this.audio) this.audio.pause();
this.audio = null;
}
get playing() { return !!this.audio && !this.audio.paused && !this.audio.ended; }
}