feat: add text to speech #69

Merged
reya merged 4 commits from feat/tts into master 2026-10-30 07:16:48 +00:00
5 changed files with 87 additions and 31 deletions
Showing only changes of commit 348504c33a - Show all commits
+37 -16
View File
@@ -30,7 +30,7 @@ use person::{Directory, Person};
use regex::Regex; use regex::Regex;
use settings::{AppSettings, SignerKind}; use settings::{AppSettings, SignerKind};
use smallvec::{SmallVec, smallvec}; use smallvec::{SmallVec, smallvec};
use speech::speaker::Speaker; use speech::speaker::{END_GRACE, Speaker};
use state::{ use state::{
FileAttachment, LocalFile, NostrRegistry, PickedFile, download_and_decrypt_file, pick_file, FileAttachment, LocalFile, NostrRegistry, PickedFile, download_and_decrypt_file, pick_file,
}; };
@@ -46,8 +46,7 @@ use crate::file::*;
const PRIVATE_NOTICE: &str = const PRIVATE_NOTICE: &str =
"This conversation is private. Only members can see each other's messages."; "This conversation is private. Only members can see each other's messages.";
/// Regex matching strings that consist entirely of emoji characters, /// Regex matching strings that consist entirely of emoji characters.
/// zero-width joiners, variation selectors, and keycap combiners.
static EMOJI_RE: LazyLock<Regex> = static EMOJI_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[\p{Emoji}\u{200D}\u{FE0F}\u{20E3}]+$").unwrap()); LazyLock::new(|| Regex::new(r"^[\p{Emoji}\u{200D}\u{FE0F}\u{20E3}]+$").unwrap());
@@ -90,9 +89,10 @@ pub struct ChatPanel {
speech_started: Option<instant::Instant>, speech_started: Option<instant::Instant>,
/// Reads messages aloud /// Reads messages aloud
speaker: Speaker, speaker: Speaker,
/// The message being revealed in step with speech, with its estimated duration /// The message being revealed in step with speech, with its estimated duration
speech_reveal: Option<(EventId, Duration)>, speech_reveal: Option<(EventId, Duration)>,
/// Fallback reveal end for backends that cannot report utterance end
reveal_timeout: Option<Task<Result<(), Error>>>,
/// Subject input state /// Subject input state
subject_input: Entity<InputState>, subject_input: Entity<InputState>,
/// Subject bar visibility /// Subject bar visibility
@@ -253,6 +253,7 @@ impl ChatPanel {
speech_started: None, speech_started: None,
speaker, speaker,
speech_reveal: None, speech_reveal: None,
reveal_timeout: None,
subject_input, subject_input,
subject_bar, subject_bar,
replies_to, replies_to,
@@ -1473,22 +1474,42 @@ impl ChatPanel {
actions actions
}; };
let speaking = self.speech_reveal.is_some();
let speech_id = *id; let speech_id = *id;
let actions = actions.speech(cx.listener(move |this, _event, window, cx| {
let text = this.message(&speech_id).map(|message| message.preview());
if let Some(text) = text { let actions = actions.speech(
match this.speaker.speak(&text) { speaking,
Ok(duration) => { cx.listener(move |this, _event, window, cx| {
this.speech_reveal = Some((speech_id, duration)); let text = this.message(&speech_id).map(|message| message.preview());
cx.notify();
} if let Some(text) = text {
Err(error) => { match this.speaker.speak(&text) {
window.push_notification(Notification::error(error.to_string()), cx) Ok(duration) => {
this.speech_reveal = Some((speech_id, duration));
// Re-enable speaking when no end event ever arrives
let session = (speech_id, duration);
let timer = cx.background_executor().timer(duration + END_GRACE);
this.reveal_timeout = Some(cx.spawn(async move |this, cx| {
timer.await;
this.update(cx, |this, cx| {
if this.speech_reveal.as_ref() == Some(&session) {
this.speech_reveal = None;
cx.notify();
}
})?;
Ok(())
}));
cx.notify();
}
Err(error) => {
window.push_notification(Notification::error(error.to_string()), cx)
}
} }
} }
} }),
})); );
actions.dropdown(move |menu, _window, _cx| { actions.dropdown(move |menu, _window, _cx| {
menu.menu("Copy author", Box::new(Command::Copy(public_key))) menu.menu("Copy author", Box::new(Command::Copy(public_key)))
+4
View File
@@ -325,6 +325,8 @@ pub struct GroupPanel {
speaker: Speaker, speaker: Speaker,
/// The message being revealed in step with speech, with its estimated duration /// The message being revealed in step with speech, with its estimated duration
speech_reveal: Option<(EventId, Duration)>, speech_reveal: Option<(EventId, Duration)>,
/// Fallback reveal end for backends that cannot report utterance end
reveal_timeout: Option<Task<Result<()>>>,
/// Uploaded, non-encrypted attachments waiting to be sent. /// Uploaded, non-encrypted attachments waiting to be sent.
attachments: Vec<Url>, attachments: Vec<Url>,
/// Whether an upload is in flight. /// Whether an upload is in flight.
@@ -455,6 +457,7 @@ impl GroupPanel {
speech_started: None, speech_started: None,
speaker, speaker,
speech_reveal: None, speech_reveal: None,
reveal_timeout: None,
attachments: Vec::new(), attachments: Vec::new(),
uploading: false, uploading: false,
tasks: smallvec![reveal_end_task], tasks: smallvec![reveal_end_task],
@@ -1083,6 +1086,7 @@ impl GroupPanel {
panel: cx.weak_entity(), panel: cx.weak_entity(),
admin, admin,
pinned, pinned,
speaking: self.speech_reveal.is_some(),
persons: self.directory.clone(), persons: self.directory.clone(),
}, },
cx, cx,
+34 -10
View File
@@ -14,6 +14,7 @@ use nip29::{Activity, Group, Reaction};
use nostr_sdk::prelude::{EventId, Kind}; use nostr_sdk::prelude::{EventId, Kind};
use person::Directory; use person::Directory;
use settings::AppSettings; use settings::AppSettings;
use speech::speaker::END_GRACE;
use state::NostrRegistry; use state::NostrRegistry;
use ui::{ use ui::{
Avatar, MessageActions, MessageMedia, MessageRow, REACTION_EMOJIS, ReactionBar, ReplyPreview, Avatar, MessageActions, MessageMedia, MessageRow, REACTION_EMOJIS, ReactionBar, ReplyPreview,
@@ -27,6 +28,8 @@ pub(crate) struct Actions {
pub panel: WeakEntity<GroupPanel>, pub panel: WeakEntity<GroupPanel>,
pub admin: bool, pub admin: bool,
pub pinned: bool, pub pinned: bool,
/// Whether a message is currently being spoken, disabling the speech buttons
pub speaking: bool,
pub persons: Entity<Directory>, pub persons: Entity<Directory>,
} }
@@ -92,18 +95,39 @@ pub(crate) fn render(
let panel = speech_panel.clone(); let panel = speech_panel.clone();
let speech_text = message.preview(); let speech_text = message.preview();
let speech_id = message.id; let speech_id = message.id;
let speaking = actions.speaking;
let row = row.overlay(MessageActions::new().speech(move |_event, _window, cx| { let row = row.overlay(
_ = speech_panel.update_in(cx, |panel, window, cx| { MessageActions::new().speech(speaking, move |_event, _window, cx| {
match panel.speaker.speak(&speech_text) { _ = speech_panel.update_in(cx, |panel, window, cx| {
Ok(duration) => { match panel.speaker.speak(&speech_text) {
panel.speech_reveal = Some((speech_id, duration)); Ok(duration) => {
cx.notify(); panel.speech_reveal = Some((speech_id, duration));
let session = (speech_id, duration);
let timer = cx.background_executor().timer(duration + END_GRACE);
panel.reveal_timeout = Some(cx.spawn(async move |this, cx| {
timer.await;
this.update(cx, |this, cx| {
if this.speech_reveal.as_ref() == Some(&session) {
this.speech_reveal = None;
cx.notify();
}
})?;
Ok(())
}));
cx.notify();
}
Err(error) => {
window.push_notification(Notification::error(error.to_string()), cx)
}
} }
Err(error) => window.push_notification(Notification::error(error.to_string()), cx), });
} }),
}); );
}));
let custom = nostr let custom = nostr
.read(cx) .read(cx)
+4
View File
@@ -5,6 +5,10 @@ use tts::Tts;
/// Words per minute of an engine at its normal rate, for estimating speech duration. /// Words per minute of an engine at its normal rate, for estimating speech duration.
const NORMAL_WPM: f32 = 160.0; const NORMAL_WPM: f32 = 160.0;
/// Extra time past the estimate before speech is considered finished, for backends that
/// cannot report utterance end.
pub const END_GRACE: Duration = Duration::from_secs(5);
/// Reads text aloud through the platform's speech backend, created on first use. /// Reads text aloud through the platform's speech backend, created on first use.
pub struct Speaker { pub struct Speaker {
tts: Option<Tts>, tts: Option<Tts>,
+8 -5
View File
@@ -14,7 +14,8 @@ use gpui_kit::component::button::{Button, ButtonVariants as _};
use gpui_kit::component::clipboard::Clipboard; use gpui_kit::component::clipboard::Clipboard;
use gpui_kit::component::menu::{DropdownMenu as _, PopupMenu, PopupMenuItem}; use gpui_kit::component::menu::{DropdownMenu as _, PopupMenu, PopupMenuItem};
use gpui_kit::component::{ use gpui_kit::component::{
ActiveTheme, InteractiveElementExt as _, Sizable as _, StyledExt as _, h_flex, v_flex, ActiveTheme, Disableable, InteractiveElementExt as _, Sizable as _, StyledExt as _, h_flex,
v_flex,
}; };
use smallvec::SmallVec; use smallvec::SmallVec;
@@ -605,7 +606,7 @@ pub struct MessageActions {
reactions: SmallVec<[(&'static str, SharedClickListener); 4]>, reactions: SmallVec<[(&'static str, SharedClickListener); 4]>,
reply: Option<SharedClickListener>, reply: Option<SharedClickListener>,
copy: Option<(ElementId, SharedString)>, copy: Option<(ElementId, SharedString)>,
speech: Option<SharedClickListener>, speech: Option<(bool, SharedClickListener)>,
dropdown: Option<DropdownBuilder>, dropdown: Option<DropdownBuilder>,
} }
@@ -645,12 +646,13 @@ impl MessageActions {
self self
} }
/// Add the read-aloud button. /// Add the read-aloud button, disabled while a message is already being spoken.
pub fn speech( pub fn speech(
mut self, mut self,
disabled: bool,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self { ) -> Self {
self.speech = Some(Rc::new(handler)); self.speech = Some((disabled, Rc::new(handler)));
self self
} }
@@ -727,13 +729,14 @@ impl RenderOnce for MessageActions {
.when_some(copy, |this, (id, value)| { .when_some(copy, |this, (id, value)| {
this.child(Clipboard::new(id).value(value).tooltip("Copy").small()) this.child(Clipboard::new(id).value(value).tooltip("Copy").small())
}) })
.when_some(speech, |this, speech| { .when_some(speech, |this, (disabled, speech)| {
this.child( this.child(
Button::new("speech") Button::new("speech")
.icon(crate::IconName::Speech) .icon(crate::IconName::Speech)
.tooltip("Read aloud") .tooltip("Read aloud")
.small() .small()
.ghost() .ghost()
.disabled(disabled)
.on_click(move |event, window, cx| speech(event, window, cx)), .on_click(move |event, window, cx| speech(event, window, cx)),
) )
}) })