yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·7.2 MiB·4b67e80·Tahinli·59 minutes ago·400 commits
| 1 | use std::{ |
| 2 | rc::Rc, |
| 3 | sync::{Arc, Mutex}, |
| 4 | time::{Duration, Instant}, |
| 5 | }; |
| 6 | |
| 7 | use crate::lang::w; |
| 8 | use common::call::CreateMessage; |
| 9 | use common::constant::VOICE_SAMPLE_RATE; |
| 10 | use gpui_kit::{ |
| 11 | App, Context, Div, Entity, InteractiveElement, IntoElement, MouseButton, ParentElement, Styled, |
| 12 | Task, Window, |
| 13 | assets::IconName, |
| 14 | component::{Icon, WindowExt, notification::Notification}, |
| 15 | div, px, |
| 16 | }; |
| 17 | use rtrb::{Consumer, RingBuffer}; |
| 18 | |
| 19 | use super::{ |
| 20 | Palette, Tooltip as _, Yumush, |
| 21 | chat::VoiceState, |
| 22 | clickable, |
| 23 | media_view::{time_text, upload_file}, |
| 24 | notify_error, parts, |
| 25 | style::{self, radius, size, space}, |
| 26 | text, |
| 27 | widget::ok_or_notify, |
| 28 | }; |
| 29 | use crate::{ |
| 30 | media::voice_note::{self, BARS}, |
| 31 | voice::{ |
| 32 | Playback, |
| 33 | audio::{self, Audio}, |
| 34 | }, |
| 35 | }; |
| 36 | |
| 37 | const TICK: Duration = Duration::from_millis(100); |
| 38 | const RING_SAMPLES: usize = VOICE_SAMPLE_RATE as usize; |
| 39 | const MAX_SECONDS: usize = 300; |
| 40 | const MAX_SAMPLES: usize = VOICE_SAMPLE_RATE as usize * MAX_SECONDS; |
| 41 | const SLOT_WIDTH: f32 = 5.; |
| 42 | const BAR_WIDTH: f32 = 3.; |
| 43 | const BAR_MIN: f32 = 2.; |
| 44 | const BAR_MAX: f32 = 24.; |
| 45 | |
| 46 | #[derive(Default)] |
| 47 | pub enum VoiceNote { |
| 48 | #[default] |
| 49 | Idle, |
| 50 | Recording { |
| 51 | started: Instant, |
| 52 | _audio: Audio, |
| 53 | consumer: Consumer<f32>, |
| 54 | samples: Vec<f32>, |
| 55 | levels: Vec<f32>, |
| 56 | _tick: Task<()>, |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | fn drain(consumer: &mut Consumer<f32>, samples: &mut Vec<f32>) { |
| 61 | while samples.len() < MAX_SAMPLES |
| 62 | && let Ok(sample) = consumer.pop() |
| 63 | { |
| 64 | samples.push(sample); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | pub fn bars( |
| 69 | levels: &[f32], |
| 70 | progress: f32, |
| 71 | palette: Palette, |
| 72 | on_pick: impl Fn(usize, &mut Window, &mut App) + 'static, |
| 73 | ) -> Div { |
| 74 | let on_pick = Rc::new(on_pick); |
| 75 | let offset = BARS - levels.len().min(BARS); |
| 76 | div() |
| 77 | .flex_1() |
| 78 | .h(px(BAR_MAX)) |
| 79 | .flex() |
| 80 | .items_center() |
| 81 | .children((0..BARS).map(|index| { |
| 82 | let level = index |
| 83 | .checked_sub(offset) |
| 84 | .and_then(|slot| levels.get(slot)) |
| 85 | .copied() |
| 86 | .unwrap_or(0.); |
| 87 | let color = if (index as f32) < progress * BARS as f32 { |
| 88 | palette.accent |
| 89 | } else { |
| 90 | palette.muted |
| 91 | }; |
| 92 | let (down, drag) = (on_pick.clone(), on_pick.clone()); |
| 93 | div() |
| 94 | .w(px(SLOT_WIDTH)) |
| 95 | .h_full() |
| 96 | .flex() |
| 97 | .items_center() |
| 98 | .justify_center() |
| 99 | .on_mouse_down(MouseButton::Left, move |_, window, cx| { |
| 100 | down(index, window, cx) |
| 101 | }) |
| 102 | .on_mouse_move(move |event, window, cx| { |
| 103 | if event.pressed_button == Some(MouseButton::Left) { |
| 104 | drag(index, window, cx); |
| 105 | } |
| 106 | }) |
| 107 | .child( |
| 108 | div() |
| 109 | .w(px(BAR_WIDTH)) |
| 110 | .h(px((BAR_MAX * level).max(BAR_MIN))) |
| 111 | .rounded_full() |
| 112 | .bg(color), |
| 113 | ) |
| 114 | })) |
| 115 | } |
| 116 | |
| 117 | pub fn recording_row( |
| 118 | started: Instant, |
| 119 | levels: &[f32], |
| 120 | entity: &Entity<Yumush>, |
| 121 | palette: Palette, |
| 122 | ) -> impl IntoElement { |
| 123 | let cancel = entity.clone(); |
| 124 | let send = entity.clone(); |
| 125 | |
| 126 | div() |
| 127 | .min_h(px(size::ROW)) |
| 128 | .pl(px(space::XS)) |
| 129 | .pr(px(space::XS)) |
| 130 | .py(px(space::XS)) |
| 131 | .flex() |
| 132 | .items_center() |
| 133 | .gap(px(space::SM)) |
| 134 | .rounded(px(radius::MD)) |
| 135 | .bg(palette.field) |
| 136 | .child( |
| 137 | clickable("cancel_voice_note", palette, move |_, cx| { |
| 138 | cancel.update(cx, |yumush, cx| yumush.cancel_voice_note(cx)) |
| 139 | }) |
| 140 | .tip(w().voice_note.cancel) |
| 141 | .size(px(size::CONTROL_SM)) |
| 142 | .flex() |
| 143 | .items_center() |
| 144 | .justify_center() |
| 145 | .rounded(px(radius::MD)) |
| 146 | .text_color(palette.muted) |
| 147 | .child(Icon::new(IconName::X).size(px(size::ICON))), |
| 148 | ) |
| 149 | .child( |
| 150 | div() |
| 151 | .flex_1() |
| 152 | .flex() |
| 153 | .items_center() |
| 154 | .gap(px(space::SM)) |
| 155 | .text_size(text(style::text::LABEL)) |
| 156 | .text_color(palette.text) |
| 157 | .child(super::parts::small_icon(IconName::Mic)) |
| 158 | .child(time_text( |
| 159 | started |
| 160 | .elapsed() |
| 161 | .min(Duration::from_secs(MAX_SECONDS as u64)), |
| 162 | )) |
| 163 | .child(bars(levels, 1., palette, |_, _, _| {})), |
| 164 | ) |
| 165 | .child( |
| 166 | parts::pill("send_voice_note", palette.tokens, move |window, cx| { |
| 167 | send.update(cx, |yumush, cx| yumush.send_voice_note(window, cx)) |
| 168 | }) |
| 169 | .tip(w().voice_note.send) |
| 170 | .size(px(size::CONTROL_SM)) |
| 171 | .justify_center() |
| 172 | .child(parts::small_icon(IconName::ArrowUp)), |
| 173 | ) |
| 174 | } |
| 175 | |
| 176 | pub fn mic_button(entity: &Entity<Yumush>, palette: Palette) -> impl IntoElement { |
| 177 | let entity = entity.clone(); |
| 178 | |
| 179 | clickable("voice_note_button", palette, move |window, cx| { |
| 180 | entity.update(cx, |yumush, cx| yumush.start_voice_note(window, cx)) |
| 181 | }) |
| 182 | .tip(w().voice_note.voice_message) |
| 183 | .size(px(size::CONTROL_SM)) |
| 184 | .flex() |
| 185 | .items_center() |
| 186 | .justify_center() |
| 187 | .rounded(px(radius::MD)) |
| 188 | .text_color(palette.muted) |
| 189 | .child(Icon::new(IconName::Mic).size(px(size::ICON))) |
| 190 | } |
| 191 | |
| 192 | impl Yumush { |
| 193 | fn start_voice_note(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 194 | if !matches!(self.chat.voice_state, VoiceState::Off) { |
| 195 | window.push_notification(Notification::error(w().voice_note.leave_call_first), cx); |
| 196 | return; |
| 197 | } |
| 198 | let (producer, consumer) = RingBuffer::new(RING_SAMPLES); |
| 199 | let capture = Arc::new(Mutex::new(producer)); |
| 200 | let (_, silent) = RingBuffer::new(1); |
| 201 | let playback: Playback = Arc::new(Mutex::new(silent)); |
| 202 | let audio = audio::start(Some(capture), playback, self.settings.voice.choice.clone()); |
| 203 | let tick = cx.spawn(async move |this, cx| { |
| 204 | loop { |
| 205 | cx.background_executor().timer(TICK).await; |
| 206 | if this |
| 207 | .update(cx, |yumush, cx| yumush.tick_voice_note(cx)) |
| 208 | .is_err() |
| 209 | { |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | }); |
| 214 | self.chat.media.voice_note = VoiceNote::Recording { |
| 215 | started: Instant::now(), |
| 216 | _audio: audio, |
| 217 | consumer, |
| 218 | samples: Vec::new(), |
| 219 | levels: Vec::new(), |
| 220 | _tick: tick, |
| 221 | }; |
| 222 | cx.notify(); |
| 223 | } |
| 224 | |
| 225 | fn tick_voice_note(&mut self, cx: &mut Context<Self>) { |
| 226 | if let VoiceNote::Recording { |
| 227 | consumer, |
| 228 | samples, |
| 229 | levels, |
| 230 | .. |
| 231 | } = &mut self.chat.media.voice_note |
| 232 | { |
| 233 | let start = samples.len(); |
| 234 | drain(consumer, samples); |
| 235 | levels.push(voice_note::level(&samples[start..])); |
| 236 | if levels.len() > BARS { |
| 237 | levels.remove(0); |
| 238 | } |
| 239 | } |
| 240 | cx.notify(); |
| 241 | } |
| 242 | |
| 243 | fn cancel_voice_note(&mut self, cx: &mut Context<Self>) { |
| 244 | self.chat.media.voice_note = VoiceNote::Idle; |
| 245 | cx.notify(); |
| 246 | } |
| 247 | |
| 248 | fn send_voice_note(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 249 | let VoiceNote::Recording { |
| 250 | mut consumer, |
| 251 | mut samples, |
| 252 | .. |
| 253 | } = std::mem::take(&mut self.chat.media.voice_note) |
| 254 | else { |
| 255 | return; |
| 256 | }; |
| 257 | drain(&mut consumer, &mut samples); |
| 258 | let Some(community_id) = self.chat.selected_community_id else { |
| 259 | return; |
| 260 | }; |
| 261 | let network = self.network.clone(); |
| 262 | let maximum = self |
| 263 | .server |
| 264 | .as_ref() |
| 265 | .map(|settings| settings.max_attachment_bytes); |
| 266 | cx.notify(); |
| 267 | |
| 268 | let order = self.send_order.clone(); |
| 269 | cx.spawn_in(window, async move |this, cx| { |
| 270 | let Some(path) = cx |
| 271 | .background_executor() |
| 272 | .spawn(async move { voice_note::save(&samples) }) |
| 273 | .await |
| 274 | else { |
| 275 | return; |
| 276 | }; |
| 277 | let uploaded = |
| 278 | upload_file(&this, &network, community_id, path.clone(), maximum, cx).await; |
| 279 | let _ = std::fs::remove_file(&path); |
| 280 | let attachment = match uploaded { |
| 281 | Ok(attachment) => attachment, |
| 282 | Err(error) => return notify_error(&this, cx, error), |
| 283 | }; |
| 284 | let _turn = order.lock().await; |
| 285 | ok_or_notify( |
| 286 | network |
| 287 | .call(CreateMessage { |
| 288 | community_id, |
| 289 | body: String::new(), |
| 290 | attachments: vec![attachment.id], |
| 291 | reply_to: None, |
| 292 | }) |
| 293 | .await, |
| 294 | &this, |
| 295 | cx, |
| 296 | ); |
| 297 | }) |
| 298 | .detach(); |
| 299 | } |
| 300 | } |