feat(tts): change the speech speed without a restart and keep fast speech clear

MeloTTS's own speed shortens every sound and slurred fast speech (Whisper
large-v3 on five Korean sentences, three renders each: 5.5% character
errors at 1.5 and 20% at 2.0, against 1.4% at 1.0). The MeloTTS child now
speaks at most at its natural pace and makes faster speech with a WSOLA
time stretch that keeps the pitch (1.9% at 1.5, 2.2% at 2.0, about 15 ms
per 4 s of speech).

A voice.tts.speed saved while the services run goes to the worker at once
(new tts_speed message) and applies from the next sentence; the client
sends it again whenever the worker connects again. Version 0.1.4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-15 23:39:04 +09:00
parent d17a3bbb61
commit dbe5c617bc
17 changed files with 268 additions and 16 deletions

View File

@@ -660,7 +660,7 @@ back as Korean messages for the screen. The Tauri shell passes the screens' `inv
| `token_check` `{token}` | the bot's tag and IDs, whether the Message Content intent is on, the invite link (REST only, nothing stored) |
| `token_save` `{token}` / `token_status` | store it (masked from then on in logs) / `set` and the masked form, never the token |
| `discord_guilds` / `discord_channels` `{guildId}` | the bot's servers / a server's voice and text channels, for the pickers |
| `settings_fields` / `settings_view` / `settings_apply` `{changes}` | the settings editor above; `restartNeeded` when the services run; `sync` with the chat channels sent to Discord (below) |
| `settings_fields` / `settings_view` / `settings_apply` `{changes}` | the settings editor above; `restartNeeded` when the services run and something other than `voice.tts.speed` changed (the speed reaches the running worker at once, `discord_core.spec.md`); `sync` with the chat channels sent to Discord (below) |
| `service_start` / `service_stop` / `service_restart` / `dashboard` | the controller and its snapshot; start and restart run in the background, stop also cancels a start |
| `discord_channel_info` `{id}` / `channel_sync_status` | a channel's Discord name and topic / each synced chat channel's last result (above) |
| `notes_list` / `note_update` `{id, text}` / `note_delete` `{id}` | everyone's personal notes (above) / change one's text / delete one |

View File

@@ -311,7 +311,15 @@ impl AdminApi {
}
}
}
Ok(json!({"saved": changes.len(), "restartNeeded": self.controller()?.state() == crate::control::State::Running,
// The speech speed reaches the running services at once; everything else waits for
// a restart.
if changes.contains_key("voice.tts.speed") {
let speed = crate::settings::load_settings(&file).map_err(|e| e.0)?.voice.tts_speed;
self.controller()?.set_tts_speed(speed);
}
let restart = changes.keys().any(|k| k != "voice.tts.speed");
Ok(json!({"saved": changes.len(),
"restartNeeded": restart && self.controller()?.state() == crate::control::State::Running,
"sync": sync}))
}
"mcp_env_status" => {

View File

@@ -30,6 +30,8 @@ pub trait Running: Send + Sync + 'static {
async fn stop(&self) -> Result<(), String>;
/// Worker, voice channel members and missed requests, for the snapshot.
fn status(&self, now: Instant) -> Value;
/// Speak at this speed (`voice.tts.speed`) from the next sentence on.
fn set_tts_speed(&self, _speed: f64) {}
}
/// What starts the services (the real ones, or a fake in tests).
@@ -167,6 +169,13 @@ impl Controller {
Err("cancelled".into())
}
/// Give the running services a new speech speed. False when nothing runs (the next start reads it
/// from the settings).
pub fn set_tts_speed(&self, speed: f64) -> bool {
let running = self.inner.lock().unwrap().running.clone();
running.map(|r| r.set_tts_speed(speed)).is_some()
}
/// Stop from running, or cancel a start; what was said is forgotten.
pub async fn stop(&self) -> Result<(), String> {
let running = {
@@ -367,6 +376,10 @@ impl Running for crate::services::Services {
"performance": self.llm.stage_view(),
})
}
fn set_tts_speed(&self, speed: f64) {
self.worker.set_tts_speed(speed);
}
}
#[cfg(test)]
@@ -381,6 +394,7 @@ mod tests {
stop_fails: std::sync::atomic::AtomicBool,
cleanups: AtomicU32,
cleanup_fails: std::sync::atomic::AtomicBool,
speeds: Mutex<Vec<f64>>,
}
struct FakeRunning(Arc<Counts>);
@@ -393,6 +407,9 @@ mod tests {
fn status(&self, _: Instant) -> Value {
json!({"worker": {"connected": true}, "voiceMembers": [{"id": "11", "name": "민수"}], "missed": []})
}
fn set_tts_speed(&self, speed: f64) {
self.0.speeds.lock().unwrap().push(speed);
}
}
struct FakeHost {
@@ -471,6 +488,17 @@ mod tests {
}
}
#[tokio::test]
async fn a_new_speech_speed_reaches_the_running_services_without_a_restart() {
let counts = Arc::new(Counts::default());
let c = Controller::new(FakeHost { counts: counts.clone(), fail_first: AtomicU32::new(0), events: Mutex::new(None) });
assert!(!c.set_tts_speed(1.7), "nothing runs: the next start reads the settings");
c.start().await.unwrap();
assert!(c.set_tts_speed(1.8));
assert_eq!(*counts.speeds.lock().unwrap(), [1.8]);
assert_eq!(counts.starts.load(Ordering::SeqCst), 1, "no restart");
}
#[tokio::test]
async fn a_stop_while_starting_cancels_the_start_or_stops_what_came_up() {
for honours_cancel in [true, false] {

View File

@@ -70,7 +70,7 @@ strings (numbers would lose precision).
| `voice.wakeWords` | `["자비스"]` | Sent to the worker as STT `hotwords`, and the voice wake words of call detection. |
| `voice.stt` | `{}` | Other worker STT settings (`partials`, `end_silence_ms`, `final_beam`, `language`, ...); the worker validates them. |
| `chatChannels` | `[]` | `{id, name, description, read (true), send (true), edit (true), react (true), delete (false), pin (false), answer ("wakeWord"), syncDiscord (true)}` per channel; `answer` is in `conversation.spec.md`, `syncDiscord` (the channel's Discord name and topic follow `name` and `description`) in `admin.spec.md`. |
| `voice.tts.speed` | `1.5` | How fast the voice speaks (MeloTTS `speed`, 0.5-2.0), passed to the worker as `--melo-speed` when it speaks (`--melo-python`) and its arguments set no speed. MeloTTS's Korean model has one voice, so there is no voice choice. |
| `voice.tts.speed` | `1.5` | How fast the voice speaks, 0.5-2.0 (1.0 is MeloTTS's natural pace; above it the worker stretches the speech and keeps its pitch, `voice_worker.spec.md`, "TTS"). Passed to the worker as `--melo-speed` when it speaks (`--melo-python`) and its arguments set no speed. A change saved while the services run goes to the worker at once (`tts_speed`) and applies from the next sentence, without a restart; it is sent again whenever the worker connects again. MeloTTS's Korean model has one voice, so there is no voice choice. |
| `voiceWorker.python` | `python3` (`python.exe` on Windows) | Python of the worker venv. |
| `voiceWorker.cwd` | `python-src` next to the app's executable when it is there (the Windows build), else the repo's `src/` | Folder holding the `voice_worker` package. |
| `voiceWorker.args` | `[]` | Extra worker arguments, e.g. `["--melo-python", "<MeloTTS venv python>"]`. |

View File

@@ -120,7 +120,7 @@ pub fn fields() -> Vec<Field> {
f("voice.stt.final_beam", Model, Int { min: 1, max: 5 }, json!(2), "최종 전사 정확도(빔)"),
f("voice.stt.language", Model, LanguageCode, json!("ko"), "받아 적을 언어(비우면 자동)"),
// MeloTTS's Korean model has one voice (speaker "KR"), so only the speed is a choice.
f("voice.tts.speed", Model, Number { min: 0.5, max: 2.0 }, json!(1.5), "말하기 속도(MeloTTS, 기본 1.5)"),
f("voice.tts.speed", Model, Number { min: 0.5, max: 2.0 }, json!(1.5), "말하기 속도(1.0 보통, 기본 1.5, 저장하면 바로 적용)"),
// The LLM (admin.spec.md, "LLM"); `llm.url` (a server the app does not start) is file-only.
f("llm.context", Model, Int { min: 4096, max: 262144 }, json!(32768), "대화 모델이 한 번에 기억하는 양(토큰)"),
f("llm.port", Model, Int { min: 1024, max: 65535 }, json!(8080), "대화 모델 포트"),

View File

@@ -226,6 +226,9 @@ struct Inner {
/// The LLM state the brain should have: sent again on every connection, so a worker that
/// missed it (a dropped connection, a restarted worker) catches up.
llm: Mutex<Option<Value>>,
/// The latest speech speed set while running, sent again on every connection: a restarted worker
/// starts with the speed of its arguments.
tts_speed: Mutex<Option<f64>>,
/// The number of the latest switch.
llm_switches: std::sync::atomic::AtomicU64,
}
@@ -248,6 +251,7 @@ impl WorkerClient {
wake: Notify::new(),
llm_idle: watch::channel(0).0,
llm: Mutex::new(None),
tts_speed: Mutex::new(None),
llm_switches: std::sync::atomic::AtomicU64::new(0),
});
(Self { inner }, rx)
@@ -497,6 +501,14 @@ impl WorkerClient {
self.set_llm(json!({"type": "llm", "state": "ready", "game": game}))
}
/// Speak at `speed` from the next sentence on (`tts_speed`). False when not connected; it still
/// goes out on the next connection.
pub fn set_tts_speed(&self, speed: f64) -> bool {
let mut s = self.inner.tts_speed.lock().unwrap();
*s = Some(speed);
self.send_json(json!({"type": "tts_speed", "speed": speed}))
}
fn set_llm(&self, m: Value) -> bool {
let mut llm = self.inner.llm.lock().unwrap();
*llm = Some(m.clone());
@@ -637,9 +649,11 @@ async fn connect_once(inner: &Arc<Inner>) -> bool {
let (tx, mut rx) = mpsc::unbounded_channel::<Message>();
{
// Locks in the order llm, then state (as `llm_state` takes them): the latest LLM state goes
// first, and the connection is ready for sending before an LLM state set meanwhile is sent.
// Locks in the order llm, tts_speed, then state (as `llm_state` and `set_tts_speed` take
// them): the latest LLM state and speed go first, and the connection is ready for sending
// before one set meanwhile is sent.
let llm = inner.llm.lock().unwrap();
let speed = inner.tts_speed.lock().unwrap();
let mut st = inner.state.lock().unwrap();
if st.closed {
return true;
@@ -647,6 +661,9 @@ async fn connect_once(inner: &Arc<Inner>) -> bool {
if let Some(m) = llm.clone() {
let _ = tx.send(Message::text(m.to_string()));
}
if let Some(s) = *speed {
let _ = tx.send(Message::text(json!({"type": "tts_speed", "speed": s}).to_string()));
}
st.conn = Some(tx);
inner.ready.send_replace(Some(hello_ok.clone()));
}