feat(terra): luna speaks with ElevenLabs v4 turbo

eleven_v4_turbo is the new default, same price per character as
v3_conversational. v4 also streams over Text to Dialogue, so the switch is
now uses_dialogue() for both. Audio tags may carry free-text directions
("[whispers, in a German accent]"): commas and apostrophes allowed, up to
80 chars. The voice prompt and luna-soul.md teach her the wider tag set,
including in text_to_speech voice notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-16 00:01:48 +02:00
co-authored by Claude Opus 5.5
parent 1209f165e9
commit efb690206b
6 changed files with 56 additions and 25 deletions
+1 -1
View File
@@ -82,7 +82,7 @@ impl Config {
eleven_key: secret("LUNA_ELEVENLABS_KEY_FILE")?,
voice_id: env("LUNA_VOICE_ID")?,
tts_model: env_or("LUNA_TTS_MODEL", "eleven_v3_conversational"),
tts_model: env_or("LUNA_TTS_MODEL", "eleven_v4_turbo"),
whisper_bin: env_or("LUNA_WHISPER_BIN", "whisper-cli"),
whisper_model: PathBuf::from(env("LUNA_WHISPER_MODEL")?),
+1 -1
View File
@@ -481,7 +481,7 @@ async fn turn(
voice_id: cfg.voice_id.clone(),
model: cfg.tts_model.clone(),
};
let keep_tags = tts_cfg.is_v3();
let keep_tags = tts_cfg.uses_dialogue();
let speech = tokio::spawn({
async move { tts::speak(&tts_cfg, sentence_rx, event_tx).await }
});
+31 -13
View File
@@ -23,10 +23,10 @@ pub struct TtsConfig {
}
impl TtsConfig {
/// v3 is not served on the TTS websocket at all; it streams only over
/// Text to Dialogue, a different protocol.
pub fn is_v3(&self) -> bool {
self.model.starts_with("eleven_v3")
/// v3 and v4 are not served on the TTS websocket at all; they stream only
/// over Text to Dialogue, a different protocol. Only these perform tags.
pub fn uses_dialogue(&self) -> bool {
self.model.starts_with("eleven_v3") || self.model.starts_with("eleven_v4")
}
}
@@ -60,7 +60,7 @@ pub async fn speak(
sentences: mpsc::Receiver<String>,
events: mpsc::Sender<SpeechEvent>,
) -> Result<()> {
if cfg.is_v3() {
if cfg.uses_dialogue() {
speak_dialogue(cfg, sentences, events).await
} else {
speak_tts(cfg, sentences, events).await
@@ -186,7 +186,7 @@ async fn read_audio(
// Alignment times may be per-chunk or cumulative depending on the
// model; `emitted_ms` lets us normalise either into one timeline.
let mut emitted_ms: u32 = 0;
// v3 audio tags come back as characters, and one can straddle chunks.
// Audio tags come back as characters, and one can straddle chunks.
let mut in_tag = false;
let mut result = Ok(());
while let Some(Ok(msg)) = source.next().await {
@@ -266,7 +266,7 @@ fn absolutize(starts: &[i64], emitted_ms: u32) -> Vec<i64> {
pub struct SentenceSplitter {
buf: String,
in_think: bool,
/// Whether v3 audio tags survive to the synthesiser or are dropped.
/// Whether audio tags survive to the synthesiser or are dropped.
keep_tags: bool,
}
@@ -342,17 +342,17 @@ fn drain_sentences(buf: &mut String, force: bool, keep_tags: bool) -> Vec<String
out
}
/// Byte index just past a v3 audio tag starting at `i` ("[laughs]",
/// "[clears throat]"), if there is one. Letters and spaces only, so
/// "[1]" or a markdown link stays ordinary noise.
/// Byte index just past an audio tag starting at `i` ("[laughs]",
/// "[whispers, in a German accent]"), if there is one. No digits, so "[1]"
/// stays ordinary noise; the length cap keeps a bracketed sentence out.
fn tag_end(s: &str, i: usize) -> Option<usize> {
let rest = s[i..].strip_prefix('[')?;
let close = rest.find(']')?;
let body = &rest[..close];
let end = i + 1 + close + 1;
let ok = !body.trim().is_empty()
&& body.len() <= 40
&& body.chars().all(|c| c.is_alphabetic() || c == ' ' || c == '-')
&& body.len() <= 80
&& body.chars().all(|c| c.is_alphabetic() || matches!(c, ' ' | '-' | ',' | '\''))
&& !s[end..].starts_with('(');
ok.then_some(end)
}
@@ -491,20 +491,38 @@ mod tests {
}
#[test]
fn audio_tags_survive_for_v3_only() {
fn audio_tags_survive_for_dialogue_models_only() {
let s = "[laughs]Fine, boss. [clears throat] Na gut.";
assert_eq!(clean_for_speech(s, true), "[laughs] Fine, boss. [clears throat] Na gut.");
assert_eq!(clean_for_speech(s, false), "Fine, boss. Na gut.");
}
#[test]
fn free_text_directions_are_tags() {
let s = "[whispers, in a German accent] Na gut. [like she's half asleep] Okay.";
assert_eq!(clean_for_speech(s, true), s);
assert_eq!(clean_for_speech(s, false), "Na gut. Okay.");
}
#[test]
fn brackets_that_are_not_tags_stay_noise() {
assert_eq!(clean_for_speech("see [1] and [the docs](x)", true), "see 1 and the docs x");
assert_eq!(clean_for_speech("see [1, 2]", true), "see 1, 2");
assert_eq!(clean_for_speech("[see above, then reboot](x)", true), "see above, then reboot x");
}
#[test]
fn captions_hide_tags() {
assert_eq!(strip_tags("[sighs] Okay, boss."), "Okay, boss.");
assert_eq!(strip_tags("[whispers, in a German accent] Na gut."), "Na gut.");
}
#[test]
fn v4_streams_over_dialogue() {
let cfg = |m: &str| TtsConfig { key: String::new(), voice_id: String::new(), model: m.into() };
assert!(cfg("eleven_v4_turbo").uses_dialogue());
assert!(cfg("eleven_v3_conversational").uses_dialogue());
assert!(!cfg("eleven_flash_v2_5").uses_dialogue());
}
#[test]