| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use serde_json::{Value, json}; |
| 7 | |
| 8 | use engine::project::LaneKind; |
| 9 | use engine::recorder::Recorder; |
| 10 | |
| 11 | use super::project::dirty; |
| 12 | use super::speak::{check_place, imported_words}; |
| 13 | use crate::server::{Reply, Server, Tool, opt_str}; |
| 14 | |
| 15 | const RECORD_DOC: &str = "Record a voice-over from the audio input (the default input, or source: a source name such as alsa_input.usb-mic, \ |
| 16 | as status lists them). start begins a take at, by default, the playhead: at is a time such as 5s or 150f. lane is the audio lane it will land on; \ |
| 17 | without lane, stop adds a new audio lane for it. stop ends the take, imports the WAV as a source and places it at the start point, as one undo step. \ |
| 18 | status reports the take's clock and input level in dBFS while recording, and the input list when idle. \ |
| 19 | The take is a 16-bit stereo WAV at 48 kHz in <project stem>_media/voiceover-<n>.wav beside the project (the folder project_collect uses), so it needs a project path (project_new or project_open). \ |
| 20 | This is not voiceover, which speaks text with Piper. Only one take runs at a time."; |
| 21 | |
| 22 | fn schema() -> Value { |
| 23 | json!({ |
| 24 | "type": "object", |
| 25 | "properties": { |
| 26 | "action": {"type": "string", "enum": ["start", "stop", "status"], "description": "start a take, stop it and place it, or report the take in progress (and the inputs when idle)."}, |
| 27 | "lane": {"type": "string", "examples": ["A2"], "description": "An existing audio lane to place the take on, such as A2. Default: a new audio lane. Used by start."}, |
| 28 | "at": {"type": "string", "examples": ["5s"], "description": "Where the take starts on the timeline, such as 5s or 150f. Default: the playhead. Used by start."}, |
| 29 | "source": {"type": "string", "examples": ["edtest.monitor"], "description": "The input to record from, by name (see status). Default: the audio server's default input. Used by start."} |
| 30 | }, |
| 31 | "required": ["action"], |
| 32 | "additionalProperties": false |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | |
| 37 | pub struct Active { |
| 38 | pub recorder: Recorder, |
| 39 | pub lane: Option<String>, |
| 40 | pub at: String, |
| 41 | } |
| 42 | |
| 43 | pub fn tools() -> Vec<Tool> { |
| 44 | vec![Tool { name: "record", description: RECORD_DOC, schema, run: run_record }] |
| 45 | } |
| 46 | |
| 47 | fn run_record(server: &mut Server, args: &Value) -> Reply { |
| 48 | match opt_str(args, "action") { |
| 49 | Some("start") => run_start(server, args), |
| 50 | Some("stop") => run_stop(server), |
| 51 | Some("status") | None => run_status(server), |
| 52 | Some(other) => Reply::err(format!("unknown action `{other}`: use start, stop or status")), |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn run_start(server: &mut Server, args: &Value) -> Reply { |
| 57 | if server.recording.is_some() { |
| 58 | return Reply::err("already recording: record {action:\"stop\"} ends this take first"); |
| 59 | } |
| 60 | let Some(project) = server.path.clone() else { |
| 61 | return Reply::err("record needs a project path to keep the take beside: open or start one with project_open or project_new {path:\"…\"} first"); |
| 62 | }; |
| 63 | let at = { |
| 64 | let s = match server.session_ref() { |
| 65 | Ok(s) => s, |
| 66 | Err(r) => return r, |
| 67 | }; |
| 68 | match opt_str(args, "at") { |
| 69 | Some(at) => at.to_string(), |
| 70 | None => format!("{:.3}s", s.now()), |
| 71 | } |
| 72 | }; |
| 73 | let lane = opt_str(args, "lane").map(str::to_string); |
| 74 | if let Some(lane) = &lane |
| 75 | && let Err(r) = check_place(server, lane, &at) |
| 76 | { |
| 77 | return r; |
| 78 | } |
| 79 | let folder = engine::collect::default_folder(&project); |
| 80 | let recorder = match Recorder::start(&folder, opt_str(args, "source")) { |
| 81 | Ok(r) => r, |
| 82 | Err(e) => return Reply::err(e), |
| 83 | }; |
| 84 | let path = recorder.status().path; |
| 85 | server.recording = Some(Active { recorder, lane, at: at.clone() }); |
| 86 | Reply::ok(format!("recording to {} from {at}; record {{action:\"stop\"}} places it", path.display())) |
| 87 | } |
| 88 | |
| 89 | fn run_status(server: &mut Server) -> Reply { |
| 90 | let Some(active) = &server.recording else { |
| 91 | let inputs = match engine::pulse::list_sources() { |
| 92 | Some(list) if !list.is_empty() => list |
| 93 | .iter() |
| 94 | .map(|s| format!("{} ({}{})", s.name, s.description, if s.monitor { ", monitor" } else { "" })) |
| 95 | .collect::<Vec<_>>() |
| 96 | .join("; "), |
| 97 | _ => "none (no audio server answers)".to_string(), |
| 98 | }; |
| 99 | return Reply::ok(format!("not recording. inputs: {inputs}")); |
| 100 | }; |
| 101 | let status = active.recorder.status(); |
| 102 | Reply::ok(format!( |
| 103 | "recording {:.2}s to {} at {}, level {:.1} dBFS", |
| 104 | status.elapsed_secs, |
| 105 | status.path.display(), |
| 106 | active.at, |
| 107 | status.level_db |
| 108 | )) |
| 109 | } |
| 110 | |
| 111 | fn run_stop(server: &mut Server) -> Reply { |
| 112 | let Some(active) = server.recording.take() else { |
| 113 | return Reply::err("not recording: record {action:\"start\"} begins a take"); |
| 114 | }; |
| 115 | let take = match active.recorder.stop() { |
| 116 | Ok(t) => t, |
| 117 | Err(e) => return Reply::err(format!("the take failed: {e}")), |
| 118 | }; |
| 119 | if take.frames == 0 { |
| 120 | return Reply::err(format!("nothing was captured: {} is empty, so nothing was placed", take.path.display())); |
| 121 | } |
| 122 | |
| 123 | let start = match server.session_ref() { |
| 124 | Ok(s) => s.history_pushes(), |
| 125 | Err(r) => return r, |
| 126 | }; |
| 127 | let source = { |
| 128 | let s = match server.session() { |
| 129 | Ok(s) => s, |
| 130 | Err(r) => return r, |
| 131 | }; |
| 132 | match s.import(&take.path) { |
| 133 | Ok(n) => n, |
| 134 | Err(e) => return Reply::err(format!("{}: refused: {e}", take.path.display())), |
| 135 | } |
| 136 | }; |
| 137 | let import_steps = match server.session_ref() { |
| 138 | Ok(s) => s.history_pushes().saturating_sub(start) as usize, |
| 139 | Err(r) => return r, |
| 140 | }; |
| 141 | |
| 142 | |
| 143 | let (lane, add_lane) = match active.lane { |
| 144 | Some(lane) => (lane, false), |
| 145 | None => { |
| 146 | let audio = match server.session_ref() { |
| 147 | Ok(s) => s.lanes().into_iter().filter(|l| l.kind == LaneKind::Audio).count(), |
| 148 | Err(r) => return r, |
| 149 | }; |
| 150 | (format!("A{}", audio + 1), true) |
| 151 | } |
| 152 | }; |
| 153 | let mut ops = Vec::new(); |
| 154 | if add_lane { |
| 155 | ops.push(json!({"op": "lane_add", "kind": "audio"})); |
| 156 | } |
| 157 | ops.push(json!({"op": "place", "source": source, "lane": lane, "at": active.at})); |
| 158 | let file_name = take.path.file_name().map_or_else(String::new, |n| n.to_string_lossy().into_owned()); |
| 159 | let label = format!("record voice-over {file_name}"); |
| 160 | let placed = super::edit::run_edit(server, &json!({"ops": ops, "label": label})); |
| 161 | if placed.is_error { |
| 162 | return Reply::err(format!("{} was imported as src:{source} but not placed: {}", take.path.display(), placed.text)); |
| 163 | } |
| 164 | |
| 165 | if let Some(entry) = server.ledger.last_mut() { |
| 166 | entry.steps += import_steps; |
| 167 | } |
| 168 | server.redo_ledger.clear(); |
| 169 | server.history_lost_on_open = false; |
| 170 | server.trim_ledger(); |
| 171 | |
| 172 | let unsaved = dirty(server); |
| 173 | Reply::ok(format!( |
| 174 | "{file_name} ({:.2}s) {}, placed on {lane} at {}, one undo step\n{}", |
| 175 | take.duration_secs, |
| 176 | imported_words(source, unsaved), |
| 177 | active.at, |
| 178 | placed.text |
| 179 | )) |
| 180 | .with(json!({ |
| 181 | "source": source, |
| 182 | "path": take.path.display().to_string(), |
| 183 | "duration": take.duration_secs, |
| 184 | "lane": lane, |
| 185 | })) |
| 186 | } |