edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! Voice-over recording: the Record voice-over sheet's logic. A take runs the engine's |
| 2 | //! recorder while the timeline plays from the start point, muted unless the sheet says |
| 3 | //! to hear it. Stop places the take on its lane as one undo step. The sheet is drawn by |
| 4 | //! `ui::project_dialogs`; the take's file is kept in the project's media folder. |
| 5 | |
| 6 | use std::time::Duration; |
| 7 | |
| 8 | use engine::pulse; |
| 9 | use engine::recorder::{Recorder, Take}; |
| 10 | |
| 11 | use super::project_dialogs::{Dialog, RecordVoiceover}; |
| 12 | use crate::*; |
| 13 | |
| 14 | /// The meter's reading in silence, in dBFS. |
| 15 | pub(crate) const SILENT_DB: f32 = engine::recorder::SILENCE_DB; |
| 16 | |
| 17 | /// A take in progress: what Stop needs to place it and to put the output back. |
| 18 | pub(crate) struct InProgress { |
| 19 | pub(crate) recorder: Recorder, |
| 20 | /// Timeline seconds the take starts at: the playhead when Record was pressed. |
| 21 | pub(crate) start: f64, |
| 22 | /// The audio lane it lands on; `None` makes a new one. |
| 23 | pub(crate) lane: Option<Lane>, |
| 24 | /// The mute the output had before the take, which Stop restores. |
| 25 | pub(crate) was_muted: bool, |
| 26 | /// Whether the take started the timeline playing, so Stop pauses it again. |
| 27 | pub(crate) started_playback: bool, |
| 28 | /// The take's own seconds at the moment the timeline first moved past `start`. The |
| 29 | /// recorder opens before the seek and the audio restart, so the take's first seconds |
| 30 | /// are that wait: placement drops them, and the voice lines up with the picture. |
| 31 | pub(crate) play_origin: Option<f64>, |
| 32 | } |
| 33 | |
| 34 | impl Player { |
| 35 | /// Whether a take is running. The sheet stays open for it, and its meter is refreshed. |
| 36 | pub(crate) fn record_running(&self) -> bool { |
| 37 | matches!(&self.proj_dialog, Some(Dialog::Record(d)) if d.take.is_some()) |
| 38 | } |
| 39 | |
| 40 | /// The audio lanes, in the order the timeline lists them. |
| 41 | pub(crate) fn audio_lanes(&self) -> Vec<Lane> { |
| 42 | self.session |
| 43 | .as_ref() |
| 44 | .map(|s| s.lanes().into_iter().filter(|l| l.kind == LaneKind::Audio).collect()) |
| 45 | .unwrap_or_default() |
| 46 | } |
| 47 | |
| 48 | /// Timeline > Record voice-over: the sheet opens with the inputs the audio server lists. |
| 49 | pub(crate) fn open_record_voiceover(&mut self, cx: &mut Context<Self>) { |
| 50 | if self.exporting().is_some() { |
| 51 | return; |
| 52 | } |
| 53 | if self.session.is_none() { |
| 54 | self.notify_user("Open a project to record into".into()); |
| 55 | cx.notify(); |
| 56 | return; |
| 57 | } |
| 58 | self.close_card(); |
| 59 | let inputs = pulse::list_sources().unwrap_or_default(); |
| 60 | self.proj_dialog = Some(Dialog::Record(RecordVoiceover { |
| 61 | inputs, |
| 62 | input: None, |
| 63 | lane: None, |
| 64 | hear: false, |
| 65 | take: None, |
| 66 | error: None, |
| 67 | })); |
| 68 | cx.notify(); |
| 69 | } |
| 70 | |
| 71 | /// The input row: segment 0 is the server's default, then each input listed. |
| 72 | pub(crate) fn record_pick_input(&mut self, at: usize, cx: &mut Context<Self>) { |
| 73 | if let Some(Dialog::Record(d)) = &mut self.proj_dialog |
| 74 | && d.take.is_none() |
| 75 | { |
| 76 | d.input = at.checked_sub(1).filter(|i| *i < d.inputs.len()); |
| 77 | d.error = None; |
| 78 | cx.notify(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// The lane row: the audio lanes in order, then a new one (which is `None`). |
| 83 | pub(crate) fn record_pick_lane(&mut self, at: usize, cx: &mut Context<Self>) { |
| 84 | let lane = self.audio_lanes().get(at).copied(); |
| 85 | if let Some(Dialog::Record(d)) = &mut self.proj_dialog |
| 86 | && d.take.is_none() |
| 87 | { |
| 88 | d.lane = lane; |
| 89 | cx.notify(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// The hear switch. During a take it changes the output at once. |
| 94 | pub(crate) fn record_toggle_hear(&mut self, cx: &mut Context<Self>) { |
| 95 | let (hear, live) = match &mut self.proj_dialog { |
| 96 | Some(Dialog::Record(d)) => { |
| 97 | d.hear = !d.hear; |
| 98 | (d.hear, d.take.as_ref().map(|t| t.was_muted)) |
| 99 | } |
| 100 | _ => return, |
| 101 | }; |
| 102 | if let Some(was_muted) = live { |
| 103 | self.volume.muted = was_muted || !hear; |
| 104 | self.apply_volume(); |
| 105 | } |
| 106 | cx.notify(); |
| 107 | } |
| 108 | |
| 109 | /// Record: starts the take at the playhead, mutes the output unless heard, and plays |
| 110 | /// the timeline from there, so the picture runs along with the voice. |
| 111 | pub(crate) fn record_start(&mut self, cx: &mut Context<Self>) { |
| 112 | let (input, lane, hear) = match &self.proj_dialog { |
| 113 | Some(Dialog::Record(d)) if d.take.is_none() => { |
| 114 | (d.input.and_then(|i| d.inputs.get(i)).map(|s| s.name.clone()), d.lane, d.hear) |
| 115 | } |
| 116 | _ => return, |
| 117 | }; |
| 118 | if self.project_path.as_os_str().is_empty() { |
| 119 | self.record_error("Save the project first: the take is kept in a folder beside it".into(), cx); |
| 120 | return; |
| 121 | } |
| 122 | let Some(start) = self.session.as_ref().map(|s| s.now()) else { |
| 123 | self.record_error("Open a project to record into".into(), cx); |
| 124 | return; |
| 125 | }; |
| 126 | let folder = engine::collect::default_folder(&self.project_path); |
| 127 | let recorder = match Recorder::start(&folder, input.as_deref()) { |
| 128 | Ok(recorder) => recorder, |
| 129 | Err(e) => { |
| 130 | self.record_error(e, cx); |
| 131 | return; |
| 132 | } |
| 133 | }; |
| 134 | let was_muted = self.volume.muted; |
| 135 | self.volume.muted = was_muted || !hear; |
| 136 | self.apply_volume(); |
| 137 | self.seek(start, cx); |
| 138 | let started_playback = !self.active_session().is_some_and(|s| s.is_playing()); |
| 139 | if started_playback { |
| 140 | self.toggle_or_restart(cx); |
| 141 | } |
| 142 | if let Some(Dialog::Record(d)) = &mut self.proj_dialog { |
| 143 | d.error = None; |
| 144 | d.take = Some(InProgress { |
| 145 | recorder, |
| 146 | start, |
| 147 | lane, |
| 148 | was_muted, |
| 149 | started_playback, |
| 150 | play_origin: None, |
| 151 | }); |
| 152 | } |
| 153 | self.record_refresh(cx); |
| 154 | } |
| 155 | |
| 156 | /// Stop: ends the take, pauses the timeline if the take started it, restores the |
| 157 | /// output, and places the take at its start point as one undo step. |
| 158 | pub(crate) fn record_stop(&mut self, cx: &mut Context<Self>) { |
| 159 | let take = match &mut self.proj_dialog { |
| 160 | Some(Dialog::Record(d)) => d.take.take(), |
| 161 | _ => None, |
| 162 | }; |
| 163 | let Some(take) = take else { |
| 164 | return; |
| 165 | }; |
| 166 | // Past the end of the timeline the transport reads as ended, so a toggle here would |
| 167 | // restart it from the top: pause the clock directly. |
| 168 | if take.started_playback |
| 169 | && let Some(session) = self.active_session_mut() |
| 170 | && session.is_playing() |
| 171 | { |
| 172 | session.pause(); |
| 173 | } |
| 174 | self.volume.muted = take.was_muted; |
| 175 | self.apply_volume(); |
| 176 | match take.recorder.stop() { |
| 177 | Ok(done) => match self.place_take(&done, take.lane, take.start, take.play_origin) { |
| 178 | Ok(lane) => { |
| 179 | self.proj_dialog = None; |
| 180 | self.reset_after_reseek(); |
| 181 | let undo = self.keymap.display(ActionId::Undo); |
| 182 | self.notify_user(format!("Voice-over placed on {} · undo {undo}", lane.label()).into()); |
| 183 | } |
| 184 | Err(e) => self.record_error(e, cx), |
| 185 | }, |
| 186 | Err(e) => self.record_error(format!("The take failed: {e}"), cx), |
| 187 | } |
| 188 | cx.notify(); |
| 189 | } |
| 190 | |
| 191 | /// Imports the take and places it, in one batch so that it is one undo step. |
| 192 | /// Returns the lane it landed on. |
| 193 | fn place_take(&mut self, take: &Take, lane: Option<Lane>, start: f64, origin: Option<f64>) -> Result<Lane, String> { |
| 194 | let fps = self.fps; |
| 195 | let session = self.session.as_mut().ok_or("Open a project to record into")?; |
| 196 | let mark = session.step_mark(); |
| 197 | session.begin_batch(); |
| 198 | let source = match session.import(&take.path) { |
| 199 | Ok(n) => n, |
| 200 | Err(e) => { |
| 201 | session.end_batch(); |
| 202 | return Err(format!("{}: refused: {e}", take.path.display())); |
| 203 | } |
| 204 | }; |
| 205 | let lane = lane.unwrap_or_else(|| session.add_lane(LaneKind::Audio)); |
| 206 | let frames = session.file_frames(&take.path); |
| 207 | // Clip frames are timeline frames, so the origin is converted at the project's rate. |
| 208 | let skip = origin.map_or(0, |t| (t * fps).round() as u32).min(frames); |
| 209 | if skip >= frames { |
| 210 | session.end_batch(); |
| 211 | session.undo(); |
| 212 | return Err("The timeline had not moved on from the start point, so there is no take to place".into()); |
| 213 | } |
| 214 | let placed = session.place_at(lane, start, Clip::source_range(source, skip, frames)); |
| 215 | session.end_batch(); |
| 216 | if !placed { |
| 217 | session.undo(); |
| 218 | return Err("The take would not go there: the lane is locked or the spot is taken".into()); |
| 219 | } |
| 220 | session.name_step(mark, "Record voice-over"); |
| 221 | Ok(lane) |
| 222 | } |
| 223 | |
| 224 | /// Shows a reason under the sheet's fields. |
| 225 | pub(crate) fn record_error(&mut self, message: String, cx: &mut Context<Self>) { |
| 226 | if let Some(Dialog::Record(d)) = &mut self.proj_dialog { |
| 227 | d.error = Some(message); |
| 228 | } |
| 229 | cx.notify(); |
| 230 | } |
| 231 | |
| 232 | /// Notes, once, the take's own time at which the timeline moved past the start point. |
| 233 | pub(crate) fn record_track(&mut self) { |
| 234 | let running = self.record_running(); |
| 235 | let now = self.active_session().map(|s| s.now()); |
| 236 | // Past the last frame the picture is done, but the take is not: the clock goes on. |
| 237 | if let Some(session) = self.active_session_mut() |
| 238 | && running |
| 239 | && session.is_eos() |
| 240 | { |
| 241 | session.run_past_end(); |
| 242 | } |
| 243 | if let Some(Dialog::Record(d)) = &mut self.proj_dialog |
| 244 | && let Some(take) = &mut d.take |
| 245 | && take.play_origin.is_none() |
| 246 | && now.is_some_and(|n| n > take.start) |
| 247 | { |
| 248 | take.play_origin = Some(take.recorder.status().elapsed_secs); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /// Repaints the sheet while a take runs, so the meter, the clock and the take's |
| 253 | /// start follow the timeline. |
| 254 | pub(crate) fn record_refresh(&self, cx: &mut Context<Self>) { |
| 255 | cx.notify(); |
| 256 | cx.spawn(async move |this, cx| { |
| 257 | loop { |
| 258 | cx.background_executor().timer(Duration::from_millis(40)).await; |
| 259 | let live = this |
| 260 | .update(cx, |this, cx| { |
| 261 | this.record_track(); |
| 262 | cx.notify(); |
| 263 | this.record_running() |
| 264 | }) |
| 265 | .unwrap_or(false); |
| 266 | if !live { |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | }) |
| 271 | .detach(); |
| 272 | } |
| 273 | } |