Merge branch 'main' into worktree-agent-a768f9cc6a31bfa54
Browse files at this commit6 changed files +822 -47
- crates/app/src/keymap.rs +27 -3
- crates/app/src/main.rs +629 -42
- crates/engine-audio/src/lib.rs +87 -1
- crates/engine/src/ao.rs +15 -1
- crates/engine/src/playback.rs +19 -0
- crates/engine/tests/ao_output.rs +45 -0
crates/app/src/keymap.rsmodified+27 -3
| @@ -46,13 +46,16 @@ pub enum ActionId { | |||
| 46 | Cut, | 46 | Cut, |
| 47 | Delete, | 47 | Delete, |
| 48 | Undo, | 48 | Undo, |
| 49 | + ToggleMute, | ||
| 50 | + VolumeUp, | ||
| 51 | + VolumeDown, | ||
| 49 | CancelExport, | 52 | CancelExport, |
| 50 | } | 53 | } |
| 51 | 54 | ||
| 52 | impl ActionId { | 55 | impl ActionId { |
| 53 | /// Display order everywhere -- the editor lists them in it and | 56 | /// Display order everywhere -- the editor lists them in it and |
| 54 | /// [`Keymap::defaults`] binds them in it. | 57 | /// [`Keymap::defaults`] binds them in it. |
| 55 | - pub const ALL: [ActionId; 9] = [ | 58 | + pub const ALL: [ActionId; 12] = [ |
| 56 | ActionId::Play, | 59 | ActionId::Play, |
| 57 | ActionId::Export, | 60 | ActionId::Export, |
| 58 | ActionId::Save, | 61 | ActionId::Save, |
| @@ -61,6 +64,9 @@ impl ActionId { | |||
| 61 | ActionId::Cut, | 64 | ActionId::Cut, |
| 62 | ActionId::Delete, | 65 | ActionId::Delete, |
| 63 | ActionId::Undo, | 66 | ActionId::Undo, |
| 67 | + ActionId::ToggleMute, | ||
| 68 | + ActionId::VolumeUp, | ||
| 69 | + ActionId::VolumeDown, | ||
| 64 | ActionId::CancelExport, | 70 | ActionId::CancelExport, |
| 65 | ]; | 71 | ]; |
| 66 | 72 | ||
| @@ -75,6 +81,9 @@ impl ActionId { | |||
| 75 | ActionId::Cut => "Cut", | 81 | ActionId::Cut => "Cut", |
| 76 | ActionId::Delete => "Delete", | 82 | ActionId::Delete => "Delete", |
| 77 | ActionId::Undo => "Undo", | 83 | ActionId::Undo => "Undo", |
| 84 | + ActionId::ToggleMute => "Mute / Unmute", | ||
| 85 | + ActionId::VolumeUp => "Volume up", | ||
| 86 | + ActionId::VolumeDown => "Volume down", | ||
| 78 | ActionId::CancelExport => "Cancel export", | 87 | ActionId::CancelExport => "Cancel export", |
| 79 | } | 88 | } |
| 80 | } | 89 | } |
| @@ -91,6 +100,9 @@ impl ActionId { | |||
| 91 | ActionId::Cut => "cut", | 100 | ActionId::Cut => "cut", |
| 92 | ActionId::Delete => "delete", | 101 | ActionId::Delete => "delete", |
| 93 | ActionId::Undo => "undo", | 102 | ActionId::Undo => "undo", |
| 103 | + ActionId::ToggleMute => "toggle-mute", | ||
| 104 | + ActionId::VolumeUp => "volume-up", | ||
| 105 | + ActionId::VolumeDown => "volume-down", | ||
| 94 | ActionId::CancelExport => "cancel-export", | 106 | ActionId::CancelExport => "cancel-export", |
| 95 | } | 107 | } |
| 96 | } | 108 | } |
| @@ -199,6 +211,12 @@ impl Keymap { | |||
| 199 | b(ActionId::Delete, "delete", false), | 211 | b(ActionId::Delete, "delete", false), |
| 200 | b(ActionId::Undo, "z", false), | 212 | b(ActionId::Undo, "z", false), |
| 201 | b(ActionId::Undo, "z", true), | 213 | b(ActionId::Undo, "z", true), |
| 214 | + b(ActionId::ToggleMute, "m", false), | ||
| 215 | + // The unshifted pair, which is what gpui reports for those two | ||
| 216 | + // keys ("=" and "-", platform.rs:862): the volume keys every | ||
| 217 | + // player has, without asking for shift to make the "+". | ||
| 218 | + b(ActionId::VolumeUp, "=", false), | ||
| 219 | + b(ActionId::VolumeDown, "-", false), | ||
| 202 | b(ActionId::CancelExport, "escape", false), | 220 | b(ActionId::CancelExport, "escape", false), |
| 203 | ], | 221 | ], |
| 204 | } | 222 | } |
| @@ -425,7 +443,7 @@ mod tests { | |||
| 425 | #[test] | 443 | #[test] |
| 426 | fn every_default_stroke_reaches_its_action() { | 444 | fn every_default_stroke_reaches_its_action() { |
| 427 | let k = Keymap::defaults(); | 445 | let k = Keymap::defaults(); |
| 428 | - assert_eq!(k.entries().len(), 11); | 446 | + assert_eq!(k.entries().len(), 14); |
| 429 | assert_eq!(k.lookup("space", false), Some(ActionId::Play)); | 447 | assert_eq!(k.lookup("space", false), Some(ActionId::Play)); |
| 430 | assert_eq!(k.lookup("e", false), Some(ActionId::Export)); | 448 | assert_eq!(k.lookup("e", false), Some(ActionId::Export)); |
| 431 | assert_eq!(k.lookup("s", true), Some(ActionId::Save)); | 449 | assert_eq!(k.lookup("s", true), Some(ActionId::Save)); |
| @@ -436,6 +454,12 @@ mod tests { | |||
| 436 | assert_eq!(k.lookup("delete", false), Some(ActionId::Delete)); | 454 | assert_eq!(k.lookup("delete", false), Some(ActionId::Delete)); |
| 437 | assert_eq!(k.lookup("z", false), Some(ActionId::Undo)); | 455 | assert_eq!(k.lookup("z", false), Some(ActionId::Undo)); |
| 438 | assert_eq!(k.lookup("z", true), Some(ActionId::Undo)); | 456 | assert_eq!(k.lookup("z", true), Some(ActionId::Undo)); |
| 457 | + assert_eq!(k.lookup("m", false), Some(ActionId::ToggleMute)); | ||
| 458 | + // The volume pair is the unshifted one, so neither needs a modifier | ||
| 459 | + // and neither is the "+" gpui reports for shift+=. | ||
| 460 | + assert_eq!(k.lookup("=", false), Some(ActionId::VolumeUp)); | ||
| 461 | + assert_eq!(k.lookup("-", false), Some(ActionId::VolumeDown)); | ||
| 462 | + assert_eq!(k.lookup("+", false), None); | ||
| 439 | assert_eq!(k.lookup("escape", false), Some(ActionId::CancelExport)); | 463 | assert_eq!(k.lookup("escape", false), Some(ActionId::CancelExport)); |
| 440 | // The modifier is half the chord: ctrl+e is not e. | 464 | // The modifier is half the chord: ctrl+e is not e. |
| 441 | assert_eq!(k.lookup("e", true), None); | 465 | assert_eq!(k.lookup("e", true), None); |
| @@ -564,7 +588,7 @@ mod tests { | |||
| 564 | ); | 588 | ); |
| 565 | // The label is the editor's column, and never the file's word for it. | 589 | // The label is the editor's column, and never the file's word for it. |
| 566 | assert_eq!(ActionId::CancelExport.label(), "Cancel export"); | 590 | assert_eq!(ActionId::CancelExport.label(), "Cancel export"); |
| 567 | - assert_eq!(ActionId::ALL.len(), 9); | 591 | + assert_eq!(ActionId::ALL.len(), 12); |
| 568 | } | 592 | } |
| 569 | 593 | ||
| 570 | #[test] | 594 | #[test] |
crates/app/src/main.rsmodified+629 -42
| @@ -8,6 +8,7 @@ use std::rc::Rc; | |||
| 8 | use std::sync::Arc; | 8 | use std::sync::Arc; |
| 9 | use std::time::{Duration, Instant}; | 9 | use std::time::{Duration, Instant}; |
| 10 | 10 | ||
| 11 | +use engine::export::ExportSettings; | ||
| 11 | use engine::{Clip, ExportHandle, Frame, PlaybackSession}; | 12 | use engine::{Clip, ExportHandle, Frame, PlaybackSession}; |
| 12 | use gpui::{ | 13 | use gpui::{ |
| 13 | AnyElement, App, Application, Bounds, ClickEvent, Context, FocusHandle, KeyDownEvent, | 14 | AnyElement, App, Application, Bounds, ClickEvent, Context, FocusHandle, KeyDownEvent, |
| @@ -59,6 +60,10 @@ const TIME_W: f32 = 200.; | |||
| 59 | /// 360 px tall window. The rows are click targets, so `HIT_MIN` binds them too. | 60 | /// 360 px tall window. The rows are click targets, so `HIT_MIN` binds them too. |
| 60 | const KEYS_W: f32 = 320.; | 61 | const KEYS_W: f32 = 320.; |
| 61 | const KEYS_ROW_H: f32 = HIT_MIN; | 62 | const KEYS_ROW_H: f32 = HIT_MIN; |
| 63 | +/// How much of the row list is on screen at once; past this it scrolls. What | ||
| 64 | +/// keeps the card inside the smallest window no matter how many actions the | ||
| 65 | +/// editor grows -- ten rows fit here, and the eleventh is a scroll away. | ||
| 66 | +const KEYS_ROWS_H: f32 = 10. * KEYS_ROW_H; | ||
| 62 | 67 | ||
| 63 | /// The one key name this file still spells out, and gpui's spelling of it: it | 68 | /// The one key name this file still spells out, and gpui's spelling of it: it |
| 64 | /// is the way out of a capture and out of the overlay, and both have to work | 69 | /// is the way out of a capture and out of the overlay, and both have to work |
| @@ -66,6 +71,68 @@ const KEYS_ROW_H: f32 = HIT_MIN; | |||
| 66 | /// through the keymap to find it. | 71 | /// through the keymap to find it. |
| 67 | const ESCAPE: &str = "escape"; | 72 | const ESCAPE: &str = "escape"; |
| 68 | 73 | ||
| 74 | +/// What the monitoring output is set to. Two things, not one: the level the | ||
| 75 | +/// user picked, and whether it is being held silent -- so unmuting comes back | ||
| 76 | +/// to the level rather than to a guess. | ||
| 77 | +/// | ||
| 78 | +/// The level counts steps rather than carrying an `f32`, because 5% at a time | ||
| 79 | +/// down and back up again through a float would not land on the number it | ||
| 80 | +/// started from, and the label would eventually read `79%`. | ||
| 81 | +/// | ||
| 82 | +/// Volume and mute stay independent on purpose: turning the level down while | ||
| 83 | +/// muted must not be what makes sound come out. Only the mute key unmutes, and | ||
| 84 | +/// the button says both things at once ("Muted 80%") so neither is a surprise. | ||
| 85 | +#[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
| 86 | +struct Volume { | ||
| 87 | + steps: u8, | ||
| 88 | + muted: bool, | ||
| 89 | +} | ||
| 90 | + | ||
| 91 | +impl Volume { | ||
| 92 | + /// 5% a press: twenty of them across the range. | ||
| 93 | + const MAX_STEPS: u8 = 20; | ||
| 94 | + | ||
| 95 | + /// What the device is set to: mute wins, and the level is what it returns | ||
| 96 | + /// to. `0.0..=1.0`, which is the range the plugin's ABI accepts. | ||
| 97 | + fn gain(self) -> f32 { | ||
| 98 | + if self.muted { | ||
| 99 | + 0. | ||
| 100 | + } else { | ||
| 101 | + f32::from(self.steps) / f32::from(Self::MAX_STEPS) | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + /// One press up or down, clamped at both ends -- saturating, so the count | ||
| 106 | + /// cannot wrap past silence into full volume. | ||
| 107 | + fn step(&mut self, up: bool) { | ||
| 108 | + self.steps = if up { | ||
| 109 | + self.steps.saturating_add(1).min(Self::MAX_STEPS) | ||
| 110 | + } else { | ||
| 111 | + self.steps.saturating_sub(1) | ||
| 112 | + }; | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + /// What the button reads. The level shows while muted too: it is what the | ||
| 116 | + /// next press of the mute key brings back. | ||
| 117 | + fn label(self) -> String { | ||
| 118 | + let percent = u32::from(self.steps) * 100 / u32::from(Self::MAX_STEPS); | ||
| 119 | + if self.muted { | ||
| 120 | + format!("Muted {percent}%") | ||
| 121 | + } else { | ||
| 122 | + format!("Vol {percent}%") | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +impl Default for Volume { | ||
| 128 | + fn default() -> Self { | ||
| 129 | + Self { | ||
| 130 | + steps: Self::MAX_STEPS, | ||
| 131 | + muted: false, | ||
| 132 | + } | ||
| 133 | + } | ||
| 134 | +} | ||
| 135 | + | ||
| 69 | struct Player { | 136 | struct Player { |
| 70 | /// The timeline, once there is one. A run with no file opens without it and | 137 | /// The timeline, once there is one. A run with no file opens without it and |
| 71 | /// waits: the first media import or project load is what fills it, and | 138 | /// waits: the first media import or project load is what fills it, and |
| @@ -116,10 +183,26 @@ struct Player { | |||
| 116 | /// Which stroke means what, and what every shortcut on screen is called. | 183 | /// Which stroke means what, and what every shortcut on screen is called. |
| 117 | /// The one place either question is answered. | 184 | /// The one place either question is answered. |
| 118 | keymap: Keymap, | 185 | keymap: Keymap, |
| 186 | + /// How loud the monitoring is, and whether it is muted. Lives here rather | ||
| 187 | + /// than in the session so it survives closing one file and opening the | ||
| 188 | + /// next -- it is a setting of the player, not of the timeline, which is | ||
| 189 | + /// also why it is not written to the project file and cannot reach an | ||
| 190 | + /// export. [`Player::apply_volume`] is what pushes it at a session. | ||
| 191 | + volume: Volume, | ||
| 119 | /// The keybindings overlay is up. While it is, it owns the keyboard and the | 192 | /// The keybindings overlay is up. While it is, it owns the keyboard and the |
| 120 | /// pointer: a stroke or a click meant for a row must not also cut the | 193 | /// pointer: a stroke or a click meant for a row must not also cut the |
| 121 | /// timeline. | 194 | /// timeline. |
| 122 | keys_open: bool, | 195 | keys_open: bool, |
| 196 | + /// The export options card is up: what the export action opens now, so | ||
| 197 | + /// nothing is written until the card's own button says so. One card at a | ||
| 198 | + /// time -- opening either closes the other, since both are the whole window | ||
| 199 | + /// and two stacked scrims say nothing about which one is listening. | ||
| 200 | + export_open: bool, | ||
| 201 | + /// Which quality row the card has picked, and the megabits typed against | ||
| 202 | + /// the custom one. Kept across closes, so a second export offers what the | ||
| 203 | + /// first one chose. | ||
| 204 | + quality: Quality, | ||
| 205 | + custom_mbps: u32, | ||
| 123 | /// The action whose row is waiting for a stroke. The next key that is | 206 | /// The action whose row is waiting for a stroke. The next key that is |
| 124 | /// neither escape nor a lone modifier becomes the whole of what reaches it. | 207 | /// neither escape nor a lone modifier becomes the whole of what reaches it. |
| 125 | rebinding: Option<ActionId>, | 208 | rebinding: Option<ActionId>, |
| @@ -311,6 +394,9 @@ impl Player { | |||
| 311 | Ok(session) => { | 394 | Ok(session) => { |
| 312 | self.fps = session.meta().frame_rate; | 395 | self.fps = session.meta().frame_rate; |
| 313 | self.session = Some(session); | 396 | self.session = Some(session); |
| 397 | + // A fresh session comes up at full volume; the player's own | ||
| 398 | + // setting outlives the file, so it is pushed at every new one. | ||
| 399 | + self.apply_volume(); | ||
| 314 | self.export_path = export_path(path); | 400 | self.export_path = export_path(path); |
| 315 | self.project_path = project_path(path); | 401 | self.project_path = project_path(path); |
| 316 | self.name = file_name(path).into(); | 402 | self.name = file_name(path).into(); |
| @@ -365,6 +451,7 @@ impl Player { | |||
| 365 | // media: that is the only place an export has ever landed. | 451 | // media: that is the only place an export has ever landed. |
| 366 | self.export_path = export_path(&session.sources()[0]); | 452 | self.export_path = export_path(&session.sources()[0]); |
| 367 | self.session = Some(session); | 453 | self.session = Some(session); |
| 454 | + self.apply_volume(); | ||
| 368 | self.project_path = path.to_path_buf(); | 455 | self.project_path = path.to_path_buf(); |
| 369 | self.name = file_name(path).into(); | 456 | self.name = file_name(path).into(); |
| 370 | // A copied clip names its source by index, which means a | 457 | // A copied clip names its source by index, which means a |
| @@ -461,6 +548,26 @@ impl Player { | |||
| 461 | 548 | ||
| 462 | /// The play binding and the transport button share it: once the timeline is finished | 549 | /// The play binding and the transport button share it: once the timeline is finished |
| 463 | /// the only sensible "play" is from the top. | 550 | /// the only sensible "play" is from the top. |
| 551 | + /// Pushes the current volume at the session, which is the only place it is | ||
| 552 | + /// ever pushed: after a change here, and after a session arrives. A session | ||
| 553 | + /// starts at full volume, so a file opened while muted has to be told -- | ||
| 554 | + /// that is the whole reason this is not just called from the key handler. | ||
| 555 | + /// Silent no-op with no timeline, or with a run that has no audio device. | ||
| 556 | + fn apply_volume(&self) { | ||
| 557 | + if let Some(session) = &self.session { | ||
| 558 | + session.set_gain(self.volume.gain()); | ||
| 559 | + } | ||
| 560 | + } | ||
| 561 | + | ||
| 562 | + /// The mute key and the two volume keys, and the click on the button. The | ||
| 563 | + /// picture is not touched: silencing the output is not pausing it, so the | ||
| 564 | + /// clock -- which the device still drives -- runs straight through. | ||
| 565 | + fn set_volume(&mut self, change: impl FnOnce(&mut Volume), cx: &mut Context<Self>) { | ||
| 566 | + change(&mut self.volume); | ||
| 567 | + self.apply_volume(); | ||
| 568 | + cx.notify(); | ||
| 569 | + } | ||
| 570 | + | ||
| 464 | fn toggle_or_restart(&mut self, cx: &mut Context<Self>) { | 571 | fn toggle_or_restart(&mut self, cx: &mut Context<Self>) { |
| 465 | if self.exporting().is_some() { | 572 | if self.exporting().is_some() { |
| 466 | return; | 573 | return; |
| @@ -483,24 +590,82 @@ impl Player { | |||
| 483 | self.export.as_ref().filter(|_| !self.cancelling) | 590 | self.export.as_ref().filter(|_| !self.cancelling) |
| 484 | } | 591 | } |
| 485 | 592 | ||
| 486 | - /// Writes the edit list out beside the source. Playback stops first: the | 593 | + /// What the export action does now: opens the card, which is where the |
| 487 | - /// exporter opens its own decoder -- and, on the hardware path, an encoder -- | 594 | + /// quality, the destination and the decision to write at all are. Nothing |
| 488 | - /// so a running player would only compete with it for the GPU. A cancelled | 595 | + /// is encoded until the button in it is pressed. |
| 489 | - /// export still winding down holds this off for the frame it takes to | 596 | + fn open_export(&mut self, cx: &mut Context<Self>) { |
| 490 | - /// notice, which is what keeps its `remove_file` off the new output. | ||
| 491 | - fn start_export(&mut self, cx: &mut Context<Self>) { | ||
| 492 | if self.export.is_some() { | 597 | if self.export.is_some() { |
| 493 | return; | 598 | return; |
| 494 | } | 599 | } |
| 495 | - // Nothing to write out, and a refusal rather than a silent button: the | 600 | + // Nothing to write out, and a refusal rather than a card about it: the |
| 496 | // window is empty and the export path is not even chosen yet. | 601 | // window is empty and the export path is not even chosen yet. |
| 602 | + if self.session.is_none() { | ||
| 603 | + self.notice = Some("NOTHING TO EXPORT — open a file first".into()); | ||
| 604 | + cx.notify(); | ||
| 605 | + return; | ||
| 606 | + } | ||
| 607 | + self.export_open = true; | ||
| 608 | + // One card at a time, and a waiting row must not outlive the card it | ||
| 609 | + // was waiting in. | ||
| 610 | + self.keys_open = false; | ||
| 611 | + self.rebinding = None; | ||
| 612 | + cx.notify(); | ||
| 613 | + } | ||
| 614 | + | ||
| 615 | + /// The card's Destination row: the desktop's save dialog, on a background | ||
| 616 | + /// thread like the import chooser -- the user may sit in it and the window | ||
| 617 | + /// behind must not freeze. No chooser at all leaves the default path, which | ||
| 618 | + /// is what the refusal says. | ||
| 619 | + fn pick_destination(&mut self, cx: &mut Context<Self>) { | ||
| 620 | + let default = self.export_path.clone(); | ||
| 621 | + let picked = cx | ||
| 622 | + .background_executor() | ||
| 623 | + .spawn(async move { pick_save(&default) }); | ||
| 624 | + cx.spawn(async move |this, cx| { | ||
| 625 | + let picked = picked.await; | ||
| 626 | + this.update(cx, |this, cx| { | ||
| 627 | + // The dialog outlives the card: an export started meanwhile | ||
| 628 | + // took the old path and its notice must name what it wrote. | ||
| 629 | + if this.export.is_some() { | ||
| 630 | + return; | ||
| 631 | + } | ||
| 632 | + match picked { | ||
| 633 | + Ok(Some(path)) => this.export_path = path, | ||
| 634 | + // Cancelled: the default stands, as it did before. | ||
| 635 | + Ok(None) => {} | ||
| 636 | + Err(text) => { | ||
| 637 | + eprintln!("{text}"); | ||
| 638 | + this.notice = Some(text.into()); | ||
| 639 | + } | ||
| 640 | + } | ||
| 641 | + cx.notify(); | ||
| 642 | + }) | ||
| 643 | + .ok(); | ||
| 644 | + }) | ||
| 645 | + .detach(); | ||
| 646 | + } | ||
| 647 | + | ||
| 648 | + /// Writes the edit list out, at the settings the card was left at. Playback | ||
| 649 | + /// stops first: the exporter opens its own decoder -- and, on the hardware | ||
| 650 | + /// path, an encoder -- so a running player would only compete with it for | ||
| 651 | + /// the GPU. A cancelled export still winding down holds this off for the | ||
| 652 | + /// frame it takes to notice, which is what keeps its `remove_file` off the | ||
| 653 | + /// new output. | ||
| 654 | + fn start_export(&mut self, cx: &mut Context<Self>) { | ||
| 655 | + if self.export.is_some() { | ||
| 656 | + return; | ||
| 657 | + } | ||
| 658 | + let settings = export_settings(self.quality, self.custom_mbps); | ||
| 497 | let Some(session) = &mut self.session else { | 659 | let Some(session) = &mut self.session else { |
| 498 | self.notice = Some("NOTHING TO EXPORT — open a file first".into()); | 660 | self.notice = Some("NOTHING TO EXPORT — open a file first".into()); |
| 499 | cx.notify(); | 661 | cx.notify(); |
| 500 | return; | 662 | return; |
| 501 | }; | 663 | }; |
| 502 | session.pause(); | 664 | session.pause(); |
| 503 | - self.export = Some(session.export_to(&self.export_path)); | 665 | + self.export = Some(session.export_to_with(&self.export_path, &settings)); |
| 666 | + // The card has been answered; the progress line takes the panel from | ||
| 667 | + // here, and it is the running export's escape that matters now. | ||
| 668 | + self.export_open = false; | ||
| 504 | cx.notify(); | 669 | cx.notify(); |
| 505 | } | 670 | } |
| 506 | 671 | ||
| @@ -632,15 +797,39 @@ impl Render for Player { | |||
| 632 | } | 797 | } |
| 633 | return; | 798 | return; |
| 634 | } | 799 | } |
| 800 | + // The export card owns it the same way, and for the same | ||
| 801 | + // reason. Escape closes it -- nothing has been written yet, so | ||
| 802 | + // there is nothing here to cancel -- and a digit is the custom | ||
| 803 | + // bitrate being typed: the card has no text field (nothing in | ||
| 804 | + // it takes focus, ledger:182) so this listener is its input, | ||
| 805 | + // exactly as it is a waiting row's. | ||
| 806 | + if this.export_open { | ||
| 807 | + if key == ESCAPE { | ||
| 808 | + this.export_open = false; | ||
| 809 | + } else if let Ok(digit) = key.parse::<u32>() { | ||
| 810 | + this.custom_mbps = push_digit(this.custom_mbps, digit); | ||
| 811 | + this.quality = Quality::Custom; | ||
| 812 | + } else if key == "backspace" { | ||
| 813 | + this.custom_mbps /= 10; | ||
| 814 | + this.quality = Quality::Custom; | ||
| 815 | + } | ||
| 816 | + cx.notify(); | ||
| 817 | + return; | ||
| 818 | + } | ||
| 635 | match action { | 819 | match action { |
| 636 | Some(ActionId::Play) => this.toggle_or_restart(cx), | 820 | Some(ActionId::Play) => this.toggle_or_restart(cx), |
| 637 | - Some(ActionId::Export) => this.start_export(cx), | 821 | + Some(ActionId::Export) => this.open_export(cx), |
| 638 | Some(ActionId::Save) => this.save_project(cx), | 822 | Some(ActionId::Save) => this.save_project(cx), |
| 639 | Some(ActionId::Copy) => this.copy_selected(), | 823 | Some(ActionId::Copy) => this.copy_selected(), |
| 640 | Some(ActionId::Paste) => this.paste(cx), | 824 | Some(ActionId::Paste) => this.paste(cx), |
| 641 | Some(ActionId::Cut) => this.cut(cx), | 825 | Some(ActionId::Cut) => this.cut(cx), |
| 642 | Some(ActionId::Delete) => this.delete_selected(cx), | 826 | Some(ActionId::Delete) => this.delete_selected(cx), |
| 643 | Some(ActionId::Undo) => this.undo(cx), | 827 | Some(ActionId::Undo) => this.undo(cx), |
| 828 | + Some(ActionId::ToggleMute) => { | ||
| 829 | + this.set_volume(|volume| volume.muted = !volume.muted, cx) | ||
| 830 | + } | ||
| 831 | + Some(ActionId::VolumeUp) => this.set_volume(|volume| volume.step(true), cx), | ||
| 832 | + Some(ActionId::VolumeDown) => this.set_volume(|volume| volume.step(false), cx), | ||
| 644 | // Nothing to cancel while nothing is exporting. | 833 | // Nothing to cancel while nothing is exporting. |
| 645 | Some(ActionId::CancelExport) | None => {} | 834 | Some(ActionId::CancelExport) | None => {} |
| 646 | } | 835 | } |
| @@ -653,8 +842,10 @@ impl Render for Player { | |||
| 653 | // The overlay owns the pointer as well as the keyboard, and a | 842 | // The overlay owns the pointer as well as the keyboard, and a |
| 654 | // drop is a click the scrim cannot swallow: gpui delivers it to | 843 | // drop is a click the scrim cannot swallow: gpui delivers it to |
| 655 | // the root's hitbox, which is under the scrim but is not a | 844 | // the root's hitbox, which is under the scrim but is not a |
| 656 | - // sibling it can stop. | 845 | + // sibling it can stop. The export card is over the timeline for |
| 657 | - if this.keys_open { | 846 | + // the same reason: importing under it would change the very |
| 847 | + // edit list the card is about to write out. | ||
| 848 | + if this.keys_open || this.export_open { | ||
| 658 | return; | 849 | return; |
| 659 | } | 850 | } |
| 660 | for path in paths.paths() { | 851 | for path in paths.paths() { |
| @@ -738,8 +929,10 @@ impl Render for Player { | |||
| 738 | // the picture nothing the rest of the time. | 929 | // the picture nothing the rest of the time. |
| 739 | .children(self.notice_bar(cx)) | 930 | .children(self.notice_bar(cx)) |
| 740 | .child(self.panel(position, duration, playing, cx)) | 931 | .child(self.panel(position, duration, playing, cx)) |
| 741 | - // Last, so it is over everything -- it takes no room in the column. | 932 | + // Last, so they are over everything -- they take no room in the |
| 933 | + // column, and only one of the two is ever up. | ||
| 742 | .children(self.keys_overlay(cx)) | 934 | .children(self.keys_overlay(cx)) |
| 935 | + .children(self.export_card(cx)) | ||
| 743 | } | 936 | } |
| 744 | } | 937 | } |
| 745 | 938 | ||
| @@ -839,6 +1032,24 @@ impl Player { | |||
| 839 | live && self.selected.is_some(), | 1032 | live && self.selected.is_some(), |
| 840 | cx.listener(|this, _: &ClickEvent, _, cx| this.delete_selected(cx)), | 1033 | cx.listener(|this, _: &ClickEvent, _, cx| this.delete_selected(cx)), |
| 841 | )) | 1034 | )) |
| 1035 | + // With the transport, not the edit group: it changes what | ||
| 1036 | + // the file sounds like, never what it is. Reads as its own | ||
| 1037 | + // state, so muted is legible without a tooltip. | ||
| 1038 | + .child(control( | ||
| 1039 | + "volume", | ||
| 1040 | + None, | ||
| 1041 | + self.volume.label(), | ||
| 1042 | + format!( | ||
| 1043 | + "{} mutes · {} louder · {} quieter", | ||
| 1044 | + key(ActionId::ToggleMute), | ||
| 1045 | + key(ActionId::VolumeUp), | ||
| 1046 | + key(ActionId::VolumeDown) | ||
| 1047 | + ), | ||
| 1048 | + live, | ||
| 1049 | + cx.listener(|this, _: &ClickEvent, _, cx| { | ||
| 1050 | + this.set_volume(|volume| volume.muted = !volume.muted, cx) | ||
| 1051 | + }), | ||
| 1052 | + )) | ||
| 842 | .child(separator()) | 1053 | .child(separator()) |
| 843 | .child(control( | 1054 | .child(control( |
| 844 | "import", | 1055 | "import", |
| @@ -853,11 +1064,11 @@ impl Player { | |||
| 853 | None, | 1064 | None, |
| 854 | "Export", | 1065 | "Export", |
| 855 | format!( | 1066 | format!( |
| 856 | - "{} — writes the timeline out beside the source", | 1067 | + "{} — quality and destination, then writes the timeline out", |
| 857 | key(ActionId::Export) | 1068 | key(ActionId::Export) |
| 858 | ), | 1069 | ), |
| 859 | live && self.export.is_none(), | 1070 | live && self.export.is_none(), |
| 860 | - cx.listener(|this, _: &ClickEvent, _, cx| this.start_export(cx)), | 1071 | + cx.listener(|this, _: &ClickEvent, _, cx| this.open_export(cx)), |
| 861 | )) | 1072 | )) |
| 862 | .child(control( | 1073 | .child(control( |
| 863 | "save", | 1074 | "save", |
| @@ -879,6 +1090,8 @@ impl Player { | |||
| 879 | cx.listener(|this, _: &ClickEvent, _, cx| { | 1090 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 880 | this.keys_open = !this.keys_open; | 1091 | this.keys_open = !this.keys_open; |
| 881 | this.rebinding = None; | 1092 | this.rebinding = None; |
| 1093 | + // One card at a time, both ways round. | ||
| 1094 | + this.export_open = false; | ||
| 882 | cx.notify(); | 1095 | cx.notify(); |
| 883 | }), | 1096 | }), |
| 884 | )), | 1097 | )), |
| @@ -1070,9 +1283,9 @@ impl Player { | |||
| 1070 | // two, and the notice bar it would otherwise appear in is | 1283 | // two, and the notice bar it would otherwise appear in is |
| 1071 | // under the scrim. | 1284 | // under the scrim. |
| 1072 | // | 1285 | // |
| 1073 | - // ponytail: a refusal long enough to wrap to three lines | 1286 | + // The row list below is capped and scrolls, so neither a |
| 1074 | - // pushes the card past a 360 px tall window. The upgrade | 1287 | + // wrapped refusal here nor another action added to `ALL` |
| 1075 | - // path is a scrolling row list, not a shorter message. | 1288 | + // can push the card past a 360 px window any more. |
| 1076 | .child( | 1289 | .child( |
| 1077 | div() | 1290 | div() |
| 1078 | .flex_none() | 1291 | .flex_none() |
| @@ -1085,7 +1298,146 @@ impl Player { | |||
| 1085 | .unwrap_or_else(|| "click a row, then press a key".into()), | 1298 | .unwrap_or_else(|| "click a row, then press a key".into()), |
| 1086 | ), | 1299 | ), |
| 1087 | ) | 1300 | ) |
| 1088 | - .children(rows), | 1301 | + // Capped and scrolling rather than as tall as the action |
| 1302 | + // list happens to be: the list grows with the editor, | ||
| 1303 | + // the smallest window does not. | ||
| 1304 | + .child( | ||
| 1305 | + div() | ||
| 1306 | + .id("keys-rows") | ||
| 1307 | + .flex() | ||
| 1308 | + .flex_col() | ||
| 1309 | + .gap(px(2.)) | ||
| 1310 | + .max_h(px(KEYS_ROWS_H)) | ||
| 1311 | + .overflow_y_scroll() | ||
| 1312 | + .children(rows), | ||
| 1313 | + ), | ||
| 1314 | + ), | ||
| 1315 | + ) | ||
| 1316 | + } | ||
| 1317 | + | ||
| 1318 | + /// What an export is going to be, before there is one: the destination, the | ||
| 1319 | + /// quality rows, and the two things that are not a choice at all. The same | ||
| 1320 | + /// scrim, width and row shape as the keybindings overlay -- two cards of | ||
| 1321 | + /// different builds over one window read as two different programs -- and | ||
| 1322 | + /// the same plain divs, so the root keeps the keyboard and a typed digit | ||
| 1323 | + /// reaches the custom row. | ||
| 1324 | + fn export_card(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { | ||
| 1325 | + if !self.export_open { | ||
| 1326 | + return None; | ||
| 1327 | + } | ||
| 1328 | + let row = |id: (&'static str, usize)| { | ||
| 1329 | + div() | ||
| 1330 | + .id(id) | ||
| 1331 | + .flex() | ||
| 1332 | + // The floor, not the height: the destination's path wraps on a | ||
| 1333 | + // long name and must not paint over the row under it. | ||
| 1334 | + .min_h(px(KEYS_ROW_H)) | ||
| 1335 | + .items_center() | ||
| 1336 | + .justify_between() | ||
| 1337 | + .gap(px(12.)) | ||
| 1338 | + .px(px(6.)) | ||
| 1339 | + .rounded(px(3.)) | ||
| 1340 | + .cursor_pointer() | ||
| 1341 | + .hover(|s| s.bg(rgb(HOVER))) | ||
| 1342 | + }; | ||
| 1343 | + let rows: Vec<_> = Quality::ALL | ||
| 1344 | + .into_iter() | ||
| 1345 | + .enumerate() | ||
| 1346 | + .map(|(i, quality)| { | ||
| 1347 | + row(("quality", i)) | ||
| 1348 | + .when(self.quality == quality, |d| d.bg(rgb(SELECTED))) | ||
| 1349 | + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { | ||
| 1350 | + this.quality = quality; | ||
| 1351 | + cx.notify(); | ||
| 1352 | + })) | ||
| 1353 | + .child(quality.label()) | ||
| 1354 | + .child( | ||
| 1355 | + div() | ||
| 1356 | + .text_size(px(11.)) | ||
| 1357 | + .text_color(rgb(INK_DIM)) | ||
| 1358 | + .child(quality.detail(self.custom_mbps)), | ||
| 1359 | + ) | ||
| 1360 | + }) | ||
| 1361 | + .collect(); | ||
| 1362 | + Some( | ||
| 1363 | + div() | ||
| 1364 | + .absolute() | ||
| 1365 | + .inset_0() | ||
| 1366 | + .flex() | ||
| 1367 | + .justify_center() | ||
| 1368 | + .items_center() | ||
| 1369 | + .bg(rgba(0x101010cc)) | ||
| 1370 | + .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { | ||
| 1371 | + cx.stop_propagation() | ||
| 1372 | + }) | ||
| 1373 | + .child( | ||
| 1374 | + div() | ||
| 1375 | + .w(px(KEYS_W)) | ||
| 1376 | + .flex() | ||
| 1377 | + .flex_col() | ||
| 1378 | + .gap(px(2.)) | ||
| 1379 | + .p(px(12.)) | ||
| 1380 | + .rounded(px(6.)) | ||
| 1381 | + .bg(rgb(SURFACE)) | ||
| 1382 | + .child(div().flex_none().px(px(6.)).child("Export")) | ||
| 1383 | + // The status line, where a refusal from the save dialog | ||
| 1384 | + // lands: the notice bar it would otherwise take is under | ||
| 1385 | + // the scrim. | ||
| 1386 | + .child( | ||
| 1387 | + div() | ||
| 1388 | + .flex_none() | ||
| 1389 | + .px(px(6.)) | ||
| 1390 | + .text_size(px(11.)) | ||
| 1391 | + .text_color(rgb(INK_DIM)) | ||
| 1392 | + .child(self.notice.clone().unwrap_or_else(|| { | ||
| 1393 | + "pick a quality, then Export — esc closes".into() | ||
| 1394 | + })), | ||
| 1395 | + ) | ||
| 1396 | + .child( | ||
| 1397 | + row(("destination", 0)) | ||
| 1398 | + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { | ||
| 1399 | + this.pick_destination(cx) | ||
| 1400 | + })) | ||
| 1401 | + .child("Destination") | ||
| 1402 | + .child( | ||
| 1403 | + div() | ||
| 1404 | + .text_size(px(11.)) | ||
| 1405 | + .text_color(rgb(INK_DIM)) | ||
| 1406 | + .child(file_name(&self.export_path)), | ||
| 1407 | + ), | ||
| 1408 | + ) | ||
| 1409 | + .children(rows) | ||
| 1410 | + // Not offered, so said: these are what this program can | ||
| 1411 | + // write, and a menu of one is a lie about the other | ||
| 1412 | + // entries. `moov` last is the muxer's own shape -- the | ||
| 1413 | + // file is playable only once it is finished. | ||
| 1414 | + .child( | ||
| 1415 | + div() | ||
| 1416 | + .flex_none() | ||
| 1417 | + .px(px(6.)) | ||
| 1418 | + .text_size(px(11.)) | ||
| 1419 | + .text_color(rgb(INK_DIM)) | ||
| 1420 | + .child("H.264 · MP4 · moov at end"), | ||
| 1421 | + ) | ||
| 1422 | + .child( | ||
| 1423 | + div() | ||
| 1424 | + .id("export-confirm") | ||
| 1425 | + .mt(px(4.)) | ||
| 1426 | + .flex() | ||
| 1427 | + .h(px(CONTROL_H)) | ||
| 1428 | + .items_center() | ||
| 1429 | + .justify_center() | ||
| 1430 | + .rounded(px(3.)) | ||
| 1431 | + .bg(rgb(SELECTED)) | ||
| 1432 | + .cursor_pointer() | ||
| 1433 | + .hover(|s| s.bg(rgb(HOVER))) | ||
| 1434 | + .on_click( | ||
| 1435 | + cx.listener(|this, _: &ClickEvent, _, cx| { | ||
| 1436 | + this.start_export(cx) | ||
| 1437 | + }), | ||
| 1438 | + ) | ||
| 1439 | + .child("Export"), | ||
| 1440 | + ), | ||
| 1089 | ), | 1441 | ), |
| 1090 | ) | 1442 | ) |
| 1091 | } | 1443 | } |
| @@ -1147,31 +1499,62 @@ fn scrub_due(target: u32, last_target: u32, since: Duration) -> bool { | |||
| 1147 | target != last_target && since >= SCRUB_GAP | 1499 | target != last_target && since >= SCRUB_GAP |
| 1148 | } | 1500 | } |
| 1149 | 1501 | ||
| 1150 | -/// The desktop's own file choosers, best first. Asked for by name because gpui | 1502 | +/// Runs the first chooser that is installed. `Some(None)` is a cancelled |
| 1151 | -/// 0.2 has no file dialog of its own and none of these is worth a dependency. | 1503 | +/// dialog; `None` is a machine with no chooser at all, and what still works |
| 1152 | -const PICKERS: [(&str, &[&str]); 2] = [ | 1504 | +/// without one differs per dialog, so the caller words that refusal. |
| 1153 | - ("zenity", &["--file-selection", "--title=edith — import"]), | 1505 | +/// |
| 1154 | - ("kdialog", &["--getopenfilename"]), | 1506 | +/// The desktop's own choosers, asked for by name because gpui 0.2 has no file |
| 1155 | -]; | 1507 | +/// dialog of its own and none of these is worth a dependency. |
| 1156 | - | 1508 | +fn run_picker(pickers: [(&str, Vec<String>); 2]) -> Option<Option<PathBuf>> { |
| 1157 | -/// Runs the first chooser that is installed. `Ok(None)` is a cancelled dialog; | 1509 | + for (bin, args) in pickers { |
| 1158 | -/// the error is for a machine with no chooser at all, which still has the drop | ||
| 1159 | -/// target -- the import path that never depended on another program. | ||
| 1160 | -fn pick_file() -> Result<Option<PathBuf>, &'static str> { | ||
| 1161 | - for (bin, args) in PICKERS { | ||
| 1162 | // Not installed: try the next one. Anything else (a cancel, a refusal) | 1510 | // Not installed: try the next one. Anything else (a cancel, a refusal) |
| 1163 | // is that chooser's answer and is taken as final. | 1511 | // is that chooser's answer and is taken as final. |
| 1164 | let Ok(out) = std::process::Command::new(bin).args(args).output() else { | 1512 | let Ok(out) = std::process::Command::new(bin).args(args).output() else { |
| 1165 | continue; | 1513 | continue; |
| 1166 | }; | 1514 | }; |
| 1167 | let path = String::from_utf8_lossy(&out.stdout).trim().to_string(); | 1515 | let path = String::from_utf8_lossy(&out.stdout).trim().to_string(); |
| 1168 | - return Ok((!path.is_empty()).then(|| PathBuf::from(path))); | 1516 | + return Some((!path.is_empty()).then(|| PathBuf::from(path))); |
| 1169 | } | 1517 | } |
| 1170 | - Err( | 1518 | + None |
| 1519 | +} | ||
| 1520 | + | ||
| 1521 | +fn pick_file() -> Result<Option<PathBuf>, &'static str> { | ||
| 1522 | + run_picker([ | ||
| 1523 | + ( | ||
| 1524 | + "zenity", | ||
| 1525 | + vec!["--file-selection".into(), "--title=edith — import".into()], | ||
| 1526 | + ), | ||
| 1527 | + ("kdialog", vec!["--getopenfilename".into()]), | ||
| 1528 | + ]) | ||
| 1529 | + .ok_or( | ||
| 1171 | "NO FILE CHOOSER — install zenity or kdialog, or drag the file onto this window to import it", | 1530 | "NO FILE CHOOSER — install zenity or kdialog, or drag the file onto this window to import it", |
| 1172 | ) | 1531 | ) |
| 1173 | } | 1532 | } |
| 1174 | 1533 | ||
| 1534 | +/// The save-side dialog, opened on where the export would land anyway: with no | ||
| 1535 | +/// chooser installed that default is still what gets written, so this refusal | ||
| 1536 | +/// costs the export nothing and says so. | ||
| 1537 | +fn pick_save(default: &std::path::Path) -> Result<Option<PathBuf>, &'static str> { | ||
| 1538 | + let default = default.to_string_lossy().into_owned(); | ||
| 1539 | + run_picker([ | ||
| 1540 | + ( | ||
| 1541 | + "zenity", | ||
| 1542 | + vec![ | ||
| 1543 | + "--file-selection".into(), | ||
| 1544 | + // No `--confirm-overwrite`: zenity 4.2 lists it as deprecated | ||
| 1545 | + // and does the confirming itself. | ||
| 1546 | + "--save".into(), | ||
| 1547 | + "--title=edith — export to".into(), | ||
| 1548 | + format!("--filename={default}"), | ||
| 1549 | + ], | ||
| 1550 | + ), | ||
| 1551 | + ("kdialog", vec!["--getsavefilename".into(), default]), | ||
| 1552 | + ]) | ||
| 1553 | + .ok_or( | ||
| 1554 | + "NO FILE CHOOSER — install zenity or kdialog to choose where; exporting beside the source", | ||
| 1555 | + ) | ||
| 1556 | +} | ||
| 1557 | + | ||
| 1175 | /// Where an export goes: the source path with `.export.mp4` for an extension, | 1558 | /// Where an export goes: the source path with `.export.mp4` for an extension, |
| 1176 | /// so it lands beside the original and can never be the original. | 1559 | /// so it lands beside the original and can never be the original. |
| 1177 | fn export_path(source: impl Into<PathBuf>) -> PathBuf { | 1560 | fn export_path(source: impl Into<PathBuf>) -> PathBuf { |
| @@ -1213,6 +1596,81 @@ fn source_tint(source: usize) -> u32 { | |||
| 1213 | SOURCE_TINTS[source % SOURCE_TINTS.len()] | 1596 | SOURCE_TINTS[source % SOURCE_TINTS.len()] |
| 1214 | } | 1597 | } |
| 1215 | 1598 | ||
| 1599 | +/// What the export card offers, top to bottom. Bitrate is the only thing the | ||
| 1600 | +/// encoder actually takes: the codec and the container are what this program | ||
| 1601 | +/// can write and nothing else, so the card states them rather than offering | ||
| 1602 | +/// them. | ||
| 1603 | +#[derive(Clone, Copy, PartialEq, Debug)] | ||
| 1604 | +enum Quality { | ||
| 1605 | + Auto, | ||
| 1606 | + Low, | ||
| 1607 | + Medium, | ||
| 1608 | + High, | ||
| 1609 | + Custom, | ||
| 1610 | +} | ||
| 1611 | + | ||
| 1612 | +impl Quality { | ||
| 1613 | + const ALL: [Quality; 5] = [ | ||
| 1614 | + Quality::Auto, | ||
| 1615 | + Quality::Low, | ||
| 1616 | + Quality::Medium, | ||
| 1617 | + Quality::High, | ||
| 1618 | + Quality::Custom, | ||
| 1619 | + ]; | ||
| 1620 | + | ||
| 1621 | + fn label(self) -> &'static str { | ||
| 1622 | + match self { | ||
| 1623 | + Quality::Auto => "Auto", | ||
| 1624 | + Quality::Low => "Low", | ||
| 1625 | + Quality::Medium => "Medium", | ||
| 1626 | + Quality::High => "High", | ||
| 1627 | + Quality::Custom => "Custom", | ||
| 1628 | + } | ||
| 1629 | + } | ||
| 1630 | + | ||
| 1631 | + /// The figure the row stands for, said in the units the row is chosen by. | ||
| 1632 | + fn detail(self, custom_mbps: u32) -> String { | ||
| 1633 | + match self { | ||
| 1634 | + Quality::Auto => "from the picture size and frame rate".to_string(), | ||
| 1635 | + Quality::Custom => format!("{custom_mbps} Mbps — type a number, 1–20"), | ||
| 1636 | + other => format!( | ||
| 1637 | + "{} Mbps", | ||
| 1638 | + export_settings(other, 0).bitrate.unwrap_or_default() / 1_000_000 | ||
| 1639 | + ), | ||
| 1640 | + } | ||
| 1641 | + } | ||
| 1642 | +} | ||
| 1643 | + | ||
| 1644 | +/// The card's rows as the engine takes them. `Auto` leaves the bitrate to the | ||
| 1645 | +/// exporter, which derives it from the picture; the fixed rows are figures that | ||
| 1646 | +/// hold from 720p to 1080p, and a typed one is passed exactly as typed -- the | ||
| 1647 | +/// engine clamps every explicit bitrate to 1..20 Mbps (export.rs:290), so this | ||
| 1648 | +/// must not clamp it a second time and disagree about where the edge is. | ||
| 1649 | +fn export_settings(quality: Quality, custom_mbps: u32) -> ExportSettings { | ||
| 1650 | + ExportSettings { | ||
| 1651 | + bitrate: match quality { | ||
| 1652 | + Quality::Auto => None, | ||
| 1653 | + Quality::Low => Some(2_000_000), | ||
| 1654 | + Quality::Medium => Some(6_000_000), | ||
| 1655 | + Quality::High => Some(12_000_000), | ||
| 1656 | + Quality::Custom => Some(u64::from(custom_mbps) * 1_000_000), | ||
| 1657 | + }, | ||
| 1658 | + // No row of its own: the software pin is for a driver that encodes | ||
| 1659 | + // badly, which is a thing about the machine and not about the output -- | ||
| 1660 | + // `VE_SW_ENC` already says it, and to the whole run rather than once. | ||
| 1661 | + force_sw: false, | ||
| 1662 | + } | ||
| 1663 | +} | ||
| 1664 | + | ||
| 1665 | +/// A digit typed against the custom row, appended to what is already there. | ||
| 1666 | +/// Refused rather than truncated past two digits: the engine's ceiling is | ||
| 1667 | +/// 20 Mbps, so a third digit can only be a mistake, and a silently dropped one | ||
| 1668 | +/// would leave the card showing a number nobody typed. | ||
| 1669 | +fn push_digit(mbps: u32, digit: u32) -> u32 { | ||
| 1670 | + let next = mbps * 10 + digit; | ||
| 1671 | + if next <= 99 { next } else { mbps } | ||
| 1672 | +} | ||
| 1673 | + | ||
| 1216 | /// Whether a stroke gets out of a running export. Escape does, whatever | 1674 | /// Whether a stroke gets out of a running export. Escape does, whatever |
| 1217 | /// modifiers are held with it, for the same reason it gets out of a capture and | 1675 | /// modifiers are held with it, for the same reason it gets out of a capture and |
| 1218 | /// out of the overlay: it is this window's way out, and a way out that only | 1676 | /// out of the overlay: it is this window's way out, and a way out that only |
| @@ -1249,11 +1707,13 @@ fn width_frac(len: f64, total: f64) -> f32 { | |||
| 1249 | fn control( | 1707 | fn control( |
| 1250 | id: &'static str, | 1708 | id: &'static str, |
| 1251 | glyph: Option<AnyElement>, | 1709 | glyph: Option<AnyElement>, |
| 1252 | - label: &'static str, | 1710 | + // Not `&'static str`: the volume button's label is its state. |
| 1711 | + label: impl Into<SharedString>, | ||
| 1253 | shortcut: String, | 1712 | shortcut: String, |
| 1254 | enabled: bool, | 1713 | enabled: bool, |
| 1255 | on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, | 1714 | on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 1256 | ) -> impl IntoElement { | 1715 | ) -> impl IntoElement { |
| 1716 | + let label = label.into(); | ||
| 1257 | let tip: SharedString = format!("{label} — {shortcut}").into(); | 1717 | let tip: SharedString = format!("{label} — {shortcut}").into(); |
| 1258 | div() | 1718 | div() |
| 1259 | .id(id) | 1719 | .id(id) |
| @@ -1435,9 +1895,10 @@ fn timecode(t: f64, fps: f64) -> String { | |||
| 1435 | #[cfg(test)] | 1895 | #[cfg(test)] |
| 1436 | mod tests { | 1896 | mod tests { |
| 1437 | use super::{ | 1897 | use super::{ |
| 1438 | - CONTROL_H, HIT_MIN, KEYS_ROW_H, KEYS_W, LANE_H, RULER_HIT_H, SOURCE_TINTS, SURFACE, | 1898 | + CONTROL_H, HIT_MIN, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LANE_H, Quality, RULER_HIT_H, |
| 1439 | - cancels_export, export_path, frac_along, is_bare_modifier, is_project, keymap, | 1899 | + SOURCE_TINTS, SURFACE, Volume, cancels_export, export_path, export_settings, frac_along, |
| 1440 | - project_path, scrub_due, source_tint, timecode, width_frac, | 1900 | + is_bare_modifier, is_project, keymap, project_path, push_digit, scrub_due, source_tint, |
| 1901 | + timecode, width_frac, | ||
| 1441 | }; | 1902 | }; |
| 1442 | use gpui::{Bounds, Pixels, point, px, size}; | 1903 | use gpui::{Bounds, Pixels, point, px, size}; |
| 1443 | use std::time::Duration; | 1904 | use std::time::Duration; |
| @@ -1581,24 +2042,142 @@ mod tests { | |||
| 1581 | 2042 | ||
| 1582 | #[test] | 2043 | #[test] |
| 1583 | fn the_keybindings_card_fits_the_smallest_window() { | 2044 | fn the_keybindings_card_fits_the_smallest_window() { |
| 1584 | - // A row per action -- not per binding, which is why the count is | 2045 | + // The row list is capped and scrolls, so the card's height no longer |
| 1585 | - // `ALL` -- under a title and a status line, inside the 640x360 the rest | 2046 | + // depends on how many actions there are: a title, a status line and the |
| 1586 | - // of the layout is already sized for. | 2047 | + // viewport, inside the 640x360 the rest of the layout is sized for. |
| 1587 | - let rows = keymap::ActionId::ALL.len() as f32; | ||
| 1588 | let title = 17.; // 13 px text on its own line | 2048 | let title = 17.; // 13 px text on its own line |
| 1589 | let status = 28.; // 11 px text, two lines: a refusal wraps | 2049 | let status = 28.; // 11 px text, two lines: a refusal wraps |
| 1590 | - let gaps = (rows + 1.) * 2.; | 2050 | + let gaps = 3. * 2.; |
| 1591 | let padding = 24.; | 2051 | let padding = 24.; |
| 1592 | assert!( | 2052 | assert!( |
| 1593 | - title + status + rows * KEYS_ROW_H + gaps + padding <= 360., | 2053 | + title + status + KEYS_ROWS_H + gaps + padding <= 360., |
| 1594 | "card too tall" | 2054 | "card too tall" |
| 1595 | ); | 2055 | ); |
| 2056 | + // The cap is only honest if it is the taller list that scrolls, not the | ||
| 2057 | + // card that grows: every action must be reachable by scrolling, and | ||
| 2058 | + // enough of them visible that the list reads as a list. | ||
| 2059 | + assert!( | ||
| 2060 | + KEYS_ROWS_H / KEYS_ROW_H >= 8., | ||
| 2061 | + "too few rows visible to scan" | ||
| 2062 | + ); | ||
| 1596 | assert!(KEYS_W <= 640., "card too wide"); | 2063 | assert!(KEYS_W <= 640., "card too wide"); |
| 1597 | // The rows are clickable, so WCAG 2.5.8 binds them like every other | 2064 | // The rows are clickable, so WCAG 2.5.8 binds them like every other |
| 1598 | // target in this window. | 2065 | // target in this window. |
| 1599 | assert!(KEYS_ROW_H >= HIT_MIN); | 2066 | assert!(KEYS_ROW_H >= HIT_MIN); |
| 1600 | } | 2067 | } |
| 1601 | 2068 | ||
| 2069 | + /// Mute and level are one control with two states, and the whole point is | ||
| 2070 | + /// that mute keeps the level: the user gets back what they had, not 100%. | ||
| 2071 | + #[test] | ||
| 2072 | + fn muting_keeps_the_level_it_comes_back_to() { | ||
| 2073 | + let mut volume = Volume::default(); | ||
| 2074 | + assert_eq!(volume.gain(), 1.0); | ||
| 2075 | + assert_eq!(volume.label(), "Vol 100%"); | ||
| 2076 | + | ||
| 2077 | + // Four presses down, then muted: the gain is silence but the level is | ||
| 2078 | + // still what it was, and the button keeps saying so. | ||
| 2079 | + for _ in 0..4 { | ||
| 2080 | + volume.step(false); | ||
| 2081 | + } | ||
| 2082 | + assert_eq!(volume.gain(), 0.8); | ||
| 2083 | + volume.muted = true; | ||
| 2084 | + assert_eq!(volume.gain(), 0.0); | ||
| 2085 | + assert_eq!(volume.label(), "Muted 80%"); | ||
| 2086 | + | ||
| 2087 | + // Turning it down while muted stays muted -- the one thing a mute | ||
| 2088 | + // button must never do is get louder because you asked for quieter. | ||
| 2089 | + volume.step(false); | ||
| 2090 | + assert_eq!(volume.gain(), 0.0); | ||
| 2091 | + assert!(volume.muted); | ||
| 2092 | + | ||
| 2093 | + // Unmute returns to the level, including the step taken while silent. | ||
| 2094 | + volume.muted = false; | ||
| 2095 | + assert_eq!(volume.gain(), 0.75); | ||
| 2096 | + } | ||
| 2097 | + | ||
| 2098 | + /// Both ends hold under a key held down: the ABI only accepts `0.0..=1.0`, | ||
| 2099 | + /// and a wrapped step count would hand it something else. | ||
| 2100 | + #[test] | ||
| 2101 | + fn the_volume_stops_at_both_ends() { | ||
| 2102 | + let mut volume = Volume::default(); | ||
| 2103 | + for _ in 0..40 { | ||
| 2104 | + volume.step(true); | ||
| 2105 | + } | ||
| 2106 | + assert_eq!(volume.gain(), 1.0); | ||
| 2107 | + assert_eq!(volume.steps, Volume::MAX_STEPS); | ||
| 2108 | + | ||
| 2109 | + for _ in 0..40 { | ||
| 2110 | + volume.step(false); | ||
| 2111 | + } | ||
| 2112 | + assert_eq!(volume.gain(), 0.0); | ||
| 2113 | + assert_eq!(volume.label(), "Vol 0%"); | ||
| 2114 | + | ||
| 2115 | + // Silent by the level rather than by the flag is still not muted: the | ||
| 2116 | + // button says which, because only one of them survives a step up. | ||
| 2117 | + assert!(!volume.muted); | ||
| 2118 | + volume.step(true); | ||
| 2119 | + assert_eq!(volume.gain(), 0.05); | ||
| 2120 | + } | ||
| 2121 | + | ||
| 2122 | + #[test] | ||
| 2123 | + fn a_quality_row_is_the_bitrate_it_promises() { | ||
| 2124 | + // Auto is the one row that says nothing: the exporter derives it, and | ||
| 2125 | + // a number typed against the custom row must not leak into it. | ||
| 2126 | + assert_eq!(export_settings(Quality::Auto, 7).bitrate, None); | ||
| 2127 | + assert_eq!(export_settings(Quality::Low, 0).bitrate, Some(2_000_000)); | ||
| 2128 | + assert_eq!(export_settings(Quality::Medium, 0).bitrate, Some(6_000_000)); | ||
| 2129 | + assert_eq!(export_settings(Quality::High, 0).bitrate, Some(12_000_000)); | ||
| 2130 | + // Megabits as typed, and as the row says it back. | ||
| 2131 | + assert_eq!(export_settings(Quality::Custom, 7).bitrate, Some(7_000_000)); | ||
| 2132 | + assert_eq!(Quality::Low.detail(0), "2 Mbps"); | ||
| 2133 | + // Every fixed row sits inside the engine's clamp (export.rs:290), so no | ||
| 2134 | + // row can promise a bitrate the exporter silently changes. | ||
| 2135 | + for quality in Quality::ALL { | ||
| 2136 | + let settings = export_settings(quality, 7); | ||
| 2137 | + if let Some(bitrate) = settings.bitrate { | ||
| 2138 | + assert!( | ||
| 2139 | + (1_000_000..=20_000_000).contains(&bitrate), | ||
| 2140 | + "{quality:?} outside the engine clamp" | ||
| 2141 | + ); | ||
| 2142 | + } | ||
| 2143 | + // The software pin is the environment's to set, never a row's. | ||
| 2144 | + assert!(!settings.force_sw); | ||
| 2145 | + } | ||
| 2146 | + } | ||
| 2147 | + | ||
| 2148 | + #[test] | ||
| 2149 | + fn a_typed_bitrate_stops_at_two_digits() { | ||
| 2150 | + assert_eq!(push_digit(0, 8), 8); | ||
| 2151 | + assert_eq!(push_digit(1, 2), 12); | ||
| 2152 | + // A third digit is refused whole: the ceiling is 20 Mbps, so it can | ||
| 2153 | + // only be a mistake, and dropping it silently would leave the card | ||
| 2154 | + // showing a number nobody typed. | ||
| 2155 | + assert_eq!(push_digit(12, 3), 12); | ||
| 2156 | + assert_eq!(push_digit(99, 9), 99); | ||
| 2157 | + // Never past what the clamp can take back to a real bitrate. | ||
| 2158 | + assert!(u64::from(push_digit(99, 9)) * 1_000_000 < u64::from(u32::MAX)); | ||
| 2159 | + } | ||
| 2160 | + | ||
| 2161 | + #[test] | ||
| 2162 | + fn the_export_card_fits_the_smallest_window() { | ||
| 2163 | + // Same 640x360 floor the keybindings card is measured against: a | ||
| 2164 | + // destination row, one row per quality, the fixed-format line and the | ||
| 2165 | + // confirm button, under a title and a status line. | ||
| 2166 | + let rows = Quality::ALL.len() as f32 + 1.; | ||
| 2167 | + let title = 17.; | ||
| 2168 | + let status = 28.; | ||
| 2169 | + let fixed = 17.; | ||
| 2170 | + let gaps = (rows + 3.) * 2.; | ||
| 2171 | + let padding = 24.; | ||
| 2172 | + assert!( | ||
| 2173 | + title + status + rows * KEYS_ROW_H + fixed + CONTROL_H + 4. + gaps + padding <= 360., | ||
| 2174 | + "card too tall" | ||
| 2175 | + ); | ||
| 2176 | + // Clickable rows, so WCAG 2.5.8 binds them as it binds the panel's. | ||
| 2177 | + assert!(KEYS_ROW_H >= HIT_MIN); | ||
| 2178 | + assert!(CONTROL_H >= HIT_MIN); | ||
| 2179 | + } | ||
| 2180 | + | ||
| 1602 | #[test] | 2181 | #[test] |
| 1603 | fn nothing_clickable_is_smaller_than_the_wcag_minimum() { | 2182 | fn nothing_clickable_is_smaller_than_the_wcag_minimum() { |
| 1604 | // Every hit target in the panel, including the scrub strip -- whose bar | 2183 | // Every hit target in the panel, including the scrub strip -- whose bar |
| @@ -1720,6 +2299,9 @@ fn main() { | |||
| 1720 | // screen. An empty window has none to wait for. | 2299 | // screen. An empty window has none to wait for. |
| 1721 | pending_seek: session.is_some(), | 2300 | pending_seek: session.is_some(), |
| 1722 | session, | 2301 | session, |
| 2302 | + // Full and unmuted, which is what the session it was just | ||
| 2303 | + // handed is already set to: nothing to push at startup. | ||
| 2304 | + volume: Volume::default(), | ||
| 1723 | // Only ever used with a timeline; 30 keeps the empty | 2305 | // Only ever used with a timeline; 30 keeps the empty |
| 1724 | // timecode reading in frames rather than in NaN. | 2306 | // timecode reading in frames rather than in NaN. |
| 1725 | fps: meta.map_or(30., |meta| meta.frame_rate), | 2307 | fps: meta.map_or(30., |meta| meta.frame_rate), |
| @@ -1740,6 +2322,11 @@ fn main() { | |||
| 1740 | project_path: project.clone(), | 2322 | project_path: project.clone(), |
| 1741 | keymap: keymap.clone(), | 2323 | keymap: keymap.clone(), |
| 1742 | keys_open: false, | 2324 | keys_open: false, |
| 2325 | + export_open: false, | ||
| 2326 | + // What an export is until someone says otherwise: the | ||
| 2327 | + // bitrate the picture asks for. | ||
| 2328 | + quality: Quality::Auto, | ||
| 2329 | + custom_mbps: 0, | ||
| 1743 | rebinding: None, | 2330 | rebinding: None, |
| 1744 | notice: notice.clone().map(SharedString::from), | 2331 | notice: notice.clone().map(SharedString::from), |
| 1745 | displayed: 0, | 2332 | displayed: 0, |
crates/engine-audio/src/lib.rsmodified+87 -1
| @@ -91,6 +91,27 @@ impl Ring { | |||
| 91 | } | 91 | } |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | +/// Multiplies the little-endian f32 samples in `dst` by `gain`, in place. | ||
| 95 | +/// | ||
| 96 | +/// This is how the editor's volume and mute are applied, rather than through | ||
| 97 | +/// the stream's PipeWire volume control: the session manager persists every | ||
| 98 | +/// change to a node's Props into its restore state (measured: WirePlumber | ||
| 99 | +/// writes the bucket ~2 s after the change), so a mute keypress here would | ||
| 100 | +/// become "edith is muted" in the system mixer tomorrow, and a quit while | ||
| 101 | +/// muted would come back muted with nothing in the app to explain it. Scaling | ||
| 102 | +/// the samples is invisible to the daemon, costs one multiply each, and -- | ||
| 103 | +/// because the frames are still consumed and still handed over -- leaves the | ||
| 104 | +/// device clock, and so the whole A/V sync, exactly where it was. | ||
| 105 | +fn scale(dst: &mut [u8], gain: f32) { | ||
| 106 | + if gain == 1.0 { | ||
| 107 | + return; | ||
| 108 | + } | ||
| 109 | + for sample in dst.chunks_exact_mut(4) { | ||
| 110 | + let scaled = f32::from_le_bytes(sample.try_into().unwrap_or([0; 4])) * gain; | ||
| 111 | + sample.copy_from_slice(&scaled.to_le_bytes()); | ||
| 112 | + } | ||
| 113 | +} | ||
| 114 | + | ||
| 94 | /// The consumer half of a flush: drops everything queued, then pops as usual. | 115 | /// The consumer half of a flush: drops everything queued, then pops as usual. |
| 95 | /// Doing it here rather than in `ao_flush` keeps the ring single-consumer -- | 116 | /// Doing it here rather than in `ao_flush` keeps the ring single-consumer -- |
| 96 | /// only the RT thread ever moves `read` -- and means a flush asked for while | 117 | /// only the RT thread ever moves `read` -- and means a flush asked for while |
| @@ -132,6 +153,9 @@ struct Shared { | |||
| 132 | /// graph clock keeps ticking while the stream is inactive, and that time | 153 | /// graph clock keeps ticking while the stream is inactive, and that time |
| 133 | /// was never played. | 154 | /// was never played. |
| 134 | resumed: AtomicBool, | 155 | resumed: AtomicBool, |
| 156 | + /// Output gain as f32 bits, 1.0 until the caller says otherwise. Read once | ||
| 157 | + /// per callback on the RT thread, so it is an atomic rather than a lock. | ||
| 158 | + gain: AtomicU32, | ||
| 135 | underruns: AtomicU64, | 159 | underruns: AtomicU64, |
| 136 | } | 160 | } |
| 137 | 161 | ||
| @@ -228,6 +252,13 @@ fn run( | |||
| 228 | slice[got * 4..want * 4].fill(0); | 252 | slice[got * 4..want * 4].fill(0); |
| 229 | shared.underruns.fetch_add(1, Ordering::Relaxed); | 253 | shared.underruns.fetch_add(1, Ordering::Relaxed); |
| 230 | } | 254 | } |
| 255 | + // After the fill, so the silence stays silent whatever | ||
| 256 | + // the gain is, and the underrun count keeps meaning | ||
| 257 | + // "the decoder was late" rather than "the user muted". | ||
| 258 | + scale( | ||
| 259 | + &mut slice[..want * 4], | ||
| 260 | + f32::from_bits(shared.gain.load(Ordering::Relaxed)), | ||
| 261 | + ); | ||
| 231 | frames | 262 | frames |
| 232 | } | 263 | } |
| 233 | None => 0, | 264 | None => 0, |
| @@ -377,6 +408,7 @@ pub extern "C" fn ao_open(sample_rate: u32, channels: u32) -> *mut c_void { | |||
| 377 | dead: AtomicBool::new(false), | 408 | dead: AtomicBool::new(false), |
| 378 | flush: AtomicBool::new(false), | 409 | flush: AtomicBool::new(false), |
| 379 | resumed: AtomicBool::new(false), | 410 | resumed: AtomicBool::new(false), |
| 411 | + gain: AtomicU32::new(1.0f32.to_bits()), | ||
| 380 | underruns: AtomicU64::new(0), | 412 | underruns: AtomicU64::new(0), |
| 381 | }); | 413 | }); |
| 382 | let (ready_tx, ready_rx) = mpsc::channel(); | 414 | let (ready_tx, ready_rx) = mpsc::channel(); |
| @@ -471,6 +503,32 @@ pub unsafe extern "C" fn ao_set_active(session: *mut c_void, active: u32) -> i32 | |||
| 471 | .unwrap_or(-1) | 503 | .unwrap_or(-1) |
| 472 | } | 504 | } |
| 473 | 505 | ||
| 506 | +/// Sets the output gain: 0.0 is silence, 1.0 is the samples as written. One | ||
| 507 | +/// knob for both the editor's volume and its mute -- mute is a gain of 0.0 | ||
| 508 | +/// pushed down, and which of the two the user is looking at is the caller's | ||
| 509 | +/// business. Takes effect on the next callback (~23 ms), and the position keeps | ||
| 510 | +/// advancing throughout: this silences the output, it does not pause it. | ||
| 511 | +/// 0 on success, negative on failure or a gain that is not in `0.0..=1.0`. | ||
| 512 | +/// | ||
| 513 | +/// # Safety | ||
| 514 | +/// `session` must come from [`ao_open`]. | ||
| 515 | +#[unsafe(no_mangle)] | ||
| 516 | +pub unsafe extern "C" fn ao_set_volume(session: *mut c_void, gain: f32) -> i32 { | ||
| 517 | + catch_unwind(AssertUnwindSafe(|| { | ||
| 518 | + // A NaN would compare false against every bound and then multiply every | ||
| 519 | + // sample into a NaN, which is a loud fault in a speaker: refuse it here, | ||
| 520 | + // at the ABI, rather than let it reach the RT thread. | ||
| 521 | + if session.is_null() || !(0.0..=1.0).contains(&gain) { | ||
| 522 | + return -1; | ||
| 523 | + } | ||
| 524 | + // SAFETY: caller-guaranteed live session. | ||
| 525 | + let session = unsafe { &*(session as *const Session) }; | ||
| 526 | + session.shared.gain.store(gain.to_bits(), Ordering::Relaxed); | ||
| 527 | + 0 | ||
| 528 | + })) | ||
| 529 | + .unwrap_or(-1) | ||
| 530 | +} | ||
| 531 | + | ||
| 474 | /// Drops every queued sample, so playback resumes from whatever is written | 532 | /// Drops every queued sample, so playback resumes from whatever is written |
| 475 | /// next: what a seek needs. Takes effect on the next process callback (or the | 533 | /// next: what a seek needs. Takes effect on the next process callback (or the |
| 476 | /// first one after a resume), and leaves the played position alone -- the | 534 | /// first one after a resume), and leaves the played position alone -- the |
| @@ -509,7 +567,35 @@ pub unsafe extern "C" fn ao_close(session: *mut c_void) { | |||
| 509 | 567 | ||
| 510 | #[cfg(test)] | 568 | #[cfg(test)] |
| 511 | mod tests { | 569 | mod tests { |
| 512 | - use super::{AtomicBool, Ordering, Ring, accept, consume}; | 570 | + use super::{AtomicBool, Ordering, Ring, accept, consume, scale}; |
| 571 | + | ||
| 572 | + /// The gain is applied to the bytes the device is about to play, so silence | ||
| 573 | + /// really is silence and a partial gain really is quieter -- and 1.0 must | ||
| 574 | + /// leave the samples bit-identical, since that is every callback's path. | ||
| 575 | + #[test] | ||
| 576 | + fn gain_scales_the_samples_the_device_sees() { | ||
| 577 | + let sample = | ||
| 578 | + |bytes: &[u8], i: usize| f32::from_le_bytes(bytes[i..i + 4].try_into().unwrap()); | ||
| 579 | + | ||
| 580 | + let mut buf = Vec::new(); | ||
| 581 | + for v in [1.0f32, -0.5, 0.25] { | ||
| 582 | + buf.extend_from_slice(&v.to_le_bytes()); | ||
| 583 | + } | ||
| 584 | + | ||
| 585 | + // Unity is the no-op the common case walks through. | ||
| 586 | + let untouched = buf.clone(); | ||
| 587 | + scale(&mut buf, 1.0); | ||
| 588 | + assert_eq!(buf, untouched); | ||
| 589 | + | ||
| 590 | + // Half volume, sign kept. | ||
| 591 | + scale(&mut buf, 0.5); | ||
| 592 | + assert_eq!(sample(&buf, 0), 0.5); | ||
| 593 | + assert_eq!(sample(&buf, 4), -0.25); | ||
| 594 | + | ||
| 595 | + // Mute is a gain of zero: every sample gone, none left ringing. | ||
| 596 | + scale(&mut buf, 0.0); | ||
| 597 | + assert!(buf.chunks_exact(4).all(|s| sample(s, 0) == 0.0)); | ||
| 598 | + } | ||
| 513 | 599 | ||
| 514 | /// The paused-seek ordering: a flush requested with no RT callbacks running | 600 | /// The paused-seek ordering: a flush requested with no RT callbacks running |
| 515 | /// must not let the writer queue new audio behind the pending discard. | 601 | /// must not let the writer queue new audio behind the pending discard. |
crates/engine/src/ao.rsmodified+15 -1
| @@ -18,6 +18,7 @@ struct Plugin { | |||
| 18 | write: unsafe extern "C" fn(*mut c_void, *const f32, usize) -> isize, | 18 | write: unsafe extern "C" fn(*mut c_void, *const f32, usize) -> isize, |
| 19 | position: unsafe extern "C" fn(*mut c_void) -> i64, | 19 | position: unsafe extern "C" fn(*mut c_void) -> i64, |
| 20 | set_active: unsafe extern "C" fn(*mut c_void, u32) -> i32, | 20 | set_active: unsafe extern "C" fn(*mut c_void, u32) -> i32, |
| 21 | + set_volume: unsafe extern "C" fn(*mut c_void, f32) -> i32, | ||
| 21 | flush: unsafe extern "C" fn(*mut c_void) -> i32, | 22 | flush: unsafe extern "C" fn(*mut c_void) -> i32, |
| 22 | close: unsafe extern "C" fn(*mut c_void), | 23 | close: unsafe extern "C" fn(*mut c_void), |
| 23 | // Never dropped (lives in a static), so the fn pointers above stay valid. | 24 | // Never dropped (lives in a static), so the fn pointers above stay valid. |
| @@ -56,7 +57,11 @@ fn load() -> Option<Plugin> { | |||
| 56 | position: *lib.get(b"ao_position\0").ok()?, | 57 | position: *lib.get(b"ao_position\0").ok()?, |
| 57 | set_active: *lib.get(b"ao_set_active\0").ok()?, | 58 | set_active: *lib.get(b"ao_set_active\0").ok()?, |
| 58 | // Required, so a plugin predating seek is rejected whole | 59 | // Required, so a plugin predating seek is rejected whole |
| 59 | - // and we play muted rather than half-working. | 60 | + // and we play muted rather than half-working. Volume is |
| 61 | + // required on the same terms: the two ship from one build, | ||
| 62 | + // and a mute button that does nothing is worse than a run | ||
| 63 | + // with no sound at all, which at least says so. | ||
| 64 | + set_volume: *lib.get(b"ao_set_volume\0").ok()?, | ||
| 60 | flush: *lib.get(b"ao_flush\0").ok()?, | 65 | flush: *lib.get(b"ao_flush\0").ok()?, |
| 61 | close: *lib.get(b"ao_close\0").ok()?, | 66 | close: *lib.get(b"ao_close\0").ok()?, |
| 62 | _lib: lib, | 67 | _lib: lib, |
| @@ -123,6 +128,15 @@ impl AoSession { | |||
| 123 | unsafe { (self.plugin.set_active)(self.handle, active as u32) == 0 } | 128 | unsafe { (self.plugin.set_active)(self.handle, active as u32) == 0 } |
| 124 | } | 129 | } |
| 125 | 130 | ||
| 131 | + /// Sets the output gain, 0.0 (silent) to 1.0 (as written): the editor's | ||
| 132 | + /// volume and its mute are the same knob by the time they get here. The | ||
| 133 | + /// clock is unaffected -- a silenced stream still plays, so the picture | ||
| 134 | + /// keeps running against it. `false` for a gain outside the range. | ||
| 135 | + pub fn set_volume(&self, gain: f32) -> bool { | ||
| 136 | + // SAFETY: `handle` came from `ao_open` and is still open. | ||
| 137 | + unsafe { (self.plugin.set_volume)(self.handle, gain) == 0 } | ||
| 138 | + } | ||
| 139 | + | ||
| 126 | /// Drops everything still queued, so the next samples written play right | 140 | /// Drops everything still queued, so the next samples written play right |
| 127 | /// away -- what a seek needs. The played position keeps counting. | 141 | /// away -- what a seek needs. The played position keeps counting. |
| 128 | pub fn flush(&self) -> bool { | 142 | pub fn flush(&self) -> bool { |
crates/engine/src/playback.rsmodified+19 -0
| @@ -507,6 +507,25 @@ impl PlaybackSession { | |||
| 507 | } | 507 | } |
| 508 | } | 508 | } |
| 509 | 509 | ||
| 510 | + /// Sets the monitoring gain, 0.0 (silent) to 1.0. Mute is a gain of 0.0: | ||
| 511 | + /// one knob, because muting and turning it down are the same thing to the | ||
| 512 | + /// device, and *which* the user meant is a question only the UI can answer | ||
| 513 | + /// -- it keeps the level to come back to. | ||
| 514 | + /// | ||
| 515 | + /// Deliberately not session state: nothing here remembers it, so a caller | ||
| 516 | + /// pushes it once per session and an export -- which renders the file | ||
| 517 | + /// rather than what the speakers are doing -- can never pick it up. Silent | ||
| 518 | + /// with no audio device, which is also the only honest answer there. | ||
| 519 | + /// | ||
| 520 | + /// Mute is not pause: the samples are still consumed and the device clock | ||
| 521 | + /// still runs, so the picture keeps playing against it. | ||
| 522 | + pub fn set_gain(&self, gain: f32) -> bool { | ||
| 523 | + match &self.audio { | ||
| 524 | + Some(audio) => audio.ao.lock().unwrap().set_volume(gain), | ||
| 525 | + None => false, | ||
| 526 | + } | ||
| 527 | + } | ||
| 528 | + | ||
| 510 | /// Repositions the timeline to `secs`, clamped to the *timeline*. Both decode | 529 | /// Repositions the timeline to `secs`, clamped to the *timeline*. Both decode |
| 511 | /// workers are replaced rather than steered: a fresh channel cannot hold a | 530 | /// workers are replaced rather than steered: a fresh channel cannot hold a |
| 512 | /// stale frame, and it works just as well after EOF, where the old workers | 531 | /// stale frame, and it works just as well after EOF, where the old workers |
crates/engine/tests/ao_output.rsmodified+45 -0
| @@ -88,6 +88,51 @@ fn plays_and_tracks_position() { | |||
| 88 | ); | 88 | ); |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | +/// Muting is not pausing: the device keeps consuming and the master clock keeps | ||
| 92 | +/// running, which is the whole reason the picture can play on through a mute. | ||
| 93 | +/// That the samples really go quiet is the plugin's own `scale` unit test -- | ||
| 94 | +/// nothing out here can hear the device. | ||
| 95 | +#[test] | ||
| 96 | +#[ignore = "needs libengine_audio.so and a PipeWire daemon"] | ||
| 97 | +fn muting_silences_without_stopping_the_clock() { | ||
| 98 | + assert!(AoSession::probe(), "plugin not loadable"); | ||
| 99 | + let mut ao = AoSession::open(RATE, CHANNELS).expect("no PipeWire playback available"); | ||
| 100 | + | ||
| 101 | + let samples = tone(); | ||
| 102 | + assert_eq!(ao.write(&samples), Some(samples.len()), "short write"); | ||
| 103 | + let first = wait_for_playback(&ao, Duration::from_secs(2)); | ||
| 104 | + | ||
| 105 | + // Mute, then keep it fed: the position must move exactly as it would have. | ||
| 106 | + assert!(ao.set_volume(0.0), "mute rejected"); | ||
| 107 | + assert_eq!(ao.write(&samples), Some(samples.len()), "short write"); | ||
| 108 | + std::thread::sleep(Duration::from_millis(300)); | ||
| 109 | + let muted = ao.position().expect("position went unknown"); | ||
| 110 | + let advanced = muted - first; | ||
| 111 | + let expected = RATE as i64 * 300 / 1000; | ||
| 112 | + eprintln!("clock advanced {advanced} samples in 300 ms while muted (expected ~{expected})"); | ||
| 113 | + assert!( | ||
| 114 | + (advanced - expected).abs() < expected / 5, | ||
| 115 | + "the clock stalled while muted: {advanced} samples in 300 ms" | ||
| 116 | + ); | ||
| 117 | + | ||
| 118 | + // Every level in range is taken, and the two ends are in range. | ||
| 119 | + for gain in [0.0, 0.5, 1.0] { | ||
| 120 | + assert!(ao.set_volume(gain), "gain {gain} rejected"); | ||
| 121 | + } | ||
| 122 | + // Out of range is refused at the ABI rather than multiplied into the | ||
| 123 | + // speakers -- a NaN would otherwise reach the RT thread and stay there. | ||
| 124 | + for gain in [-0.1, 1.5, f32::NAN, f32::INFINITY] { | ||
| 125 | + assert!(!ao.set_volume(gain), "gain {gain} accepted"); | ||
| 126 | + } | ||
| 127 | + // ...and a refusal changes nothing: the last accepted gain still stands. | ||
| 128 | + assert_eq!(ao.write(&samples), Some(samples.len()), "short write"); | ||
| 129 | + std::thread::sleep(Duration::from_millis(100)); | ||
| 130 | + assert!( | ||
| 131 | + ao.position().expect("position went unknown") > muted, | ||
| 132 | + "clock stopped after a refused gain" | ||
| 133 | + ); | ||
| 134 | +} | ||
| 135 | + | ||
| 91 | /// No reachable daemon must fail the open cleanly and quickly, never hang. | 136 | /// No reachable daemon must fail the open cleanly and quickly, never hang. |
| 92 | #[test] | 137 | #[test] |
| 93 | #[ignore = "needs libengine_audio.so and a PipeWire daemon"] | 138 | #[ignore = "needs libengine_audio.so and a PipeWire daemon"] |