diff --git a/README.md b/README.md index 5fb33f92..8261c953 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,7 @@ The **strip** holds: | --- | --- | | LIVE | Watch the program while on air, and TAKE or AUTO a cued scene. | | BUILD | Edit the scene on program, or the one open off air, shown in the middle, with its layers and what can be added on the left and the inspector on the right (see [BUILD](#build)). | -| MIX | Add, rename, reorder, and remove audio scenes, pick the audio scene on program and the one to listen to and on which output, set each sound's gain, pan, mute, and delay, and send each sound and audio scene into audio scenes, with a meter on every one. The program shows small in the strip's place for the sound. | +| MIX | Ride each sound's fader and mute it, open its strip for its pan and delay, and send each sound and audio scene into audio scenes, from dots under the strips or as a matrix under **Routes**. Add, rename, reorder, and remove audio scenes, put one on the stream, and listen to one on the output you pick (see [Audio scenes](#audio-scenes)). The program shows small in the strip's place for the sound. | | REVIEW | Pick a stretch of the last 30 seconds of the program or a source from a strip of its pictures, and replay it in a replay source, slowed down if you like, or save it as a clip (see [Replay](#replay)). Beside it, this show's recordings and clips as cards with their length and size, one repaired after a crash marked so, and **Open the folder**. The program's card is the live picture. | The layout follows the program's shape and the window's size, giving the @@ -495,27 +495,46 @@ audio scene holding every microphone, which both a "Stream" audio scene (Mics, without music you may not broadcast) and a "Headphones" one (Mics and the music) hold. A sound an audio scene reaches more than one way plays in it once, and an audio scene never holds itself, even through others. Add, -rename, reorder, and remove audio scenes at the top of the MIX page. - -The MIX page is a mixer. Each sound has a strip, with its gain, pan, mute, -delay, and meter, which apply in every audio scene holding it, and a send to -each audio scene; each audio scene has a strip with its meter and a send to -each other audio scene. A source added from BUILD's Add, or -pasted, is sent to the audio scene on program, so it is heard at once. -**On program** picks the audio scene the broadcast carries, and switching -it, or editing an audio scene, never drops or repeats audio. A recording holds -the audio scene on program unless Settings' Recording tab picks another, such as one -keeping music the stream leaves out, and each published scene carries the one -on program unless BUILD's inspector picks another beside its **Publish** switch; -switching either while it runs never drops or repeats audio either. The audio -scene on program cannot be removed; removing one a recording or published -scene picks returns it to the one on program. - -**Listen** plays an audio scene to an audio output, from the `capture` -feature's build, as the strip's **Listen** plays the audio scene on program: the system default, or the one picked under **Listen on**, -which the studio remembers for you rather than in the scene file. Use -headphones: a microphone that hears the speakers feeds its audio scene back -into itself. +rename, reorder, and remove audio scenes on the MIX page. + +The MIX page is a mixing desk. Each sound has a strip: its name, a meter, a +fader for its gain in decibels (unity is marked, and a double click returns +to it), its gain to read or type, and its mute, which turns amber while it +is on. These apply in every audio scene holding the sound. Click a sound's +name to open its strip to its gain, pan (a double click centers it), and +delay, and the audio scenes it is heard in; the other strips narrow beside it +rather than hide. Each audio scene has a strip after the sounds with its +meter, on the same scale, and **Listen**. A meter's top is full scale, where +its last segment turns red: the sound clips. + +Under each strip is a dot for each audio scene, marked with its initial: +filled where the strip is sent into it, the accent's color for the one the +stream carries; ringed where it is heard there only through another audio +scene; and faint where it is not heard. A click sends it in or takes it out. +An audio scene shows a dash under its own strip and under any it holds, which +it cannot be sent back into. **Routes** shows the same sends as a matrix, a +row for each sound and audio scene and a column for each audio scene, with +what each one holds at its foot. + +A source added from BUILD's Add, or pasted, is sent to the +audio scene on program, so it is heard at once. The audio scene on program is +tagged **Stream hears this**; **Put on stream** on another's strip puts that +one on program instead, and switching it, or editing an audio scene, never +drops or repeats audio. **+** after the audio scenes adds one, holding +nothing. Right-click an audio scene's strip, or its column under **Routes**, +to rename it, move it left or right, or remove it. A recording holds the +audio scene on program unless Settings' Recording tab picks another, such as one keeping music the stream leaves out, which is tagged +**In the recording**, and each published scene carries the one on program +unless BUILD's inspector picks another beside its **Publish** switch; switching +either while it runs never drops or repeats audio either. The audio scene on +program cannot be removed; removing one a recording or published scene picks +returns it to the one on program. + +**Listen** on an audio scene's strip plays it to an audio output, from the +`capture` feature's build: the system default, or the one picked from the tag +on the strip while it plays, which the studio remembers for you rather than +in the scene file. Use headphones: a microphone that hears the speakers feeds +its audio scene back into itself. A source's **delay** holds it back in every audio scene, up to 2 s, so a microphone lines up with a camera that lags it. A broadcast's picture already follows its diff --git a/quest/m0/pin-output-kind.md b/quest/m0/pin-output-kind.md new file mode 100644 index 00000000..aa351b22 --- /dev/null +++ b/quest/m0/pin-output-kind.md @@ -0,0 +1,26 @@ +# [XS] moq-audio's output kind is pinned + +## Goal + +The `moq` submodule includes an output's kind on +`moq_audio::playback::Device`: whether the system reports it as headphones +or a headset, as speakers, or does not say. + +## Plan + +cpal 0.18, which moq-audio already uses, reports it as +`DeviceDescription::device_type` on PipeWire and WASAPI, and as `Unknown` +elsewhere. `playback::list` reads the description for the name already, so +the change upstream is a field beside `name`, mapped from cpal's type so +cpal stays out of moq-audio's API, with a test of the mapping. + +Once the wait clears, advance it with `just upgrade-moq` in its own PR, +fixing whatever the new API breaks here. + +## Waiting + +- moq-dev/moq: `moq_audio::playback::Device` on moq's `main` reports an + output's kind. Check `rs/moq-audio/src/playback/device.rs` there. The + change has no quest in moq's tree yet; it was found by + [moq-dev/studio#178](https://github.com/moq-dev/studio/pull/178) and is + listed in its Follow-ups for `/quest-plan` there. diff --git a/quest/m0/ux/README.md b/quest/m0/ux/README.md index d609ceef..270c6ebc 100644 --- a/quest/m0/ux/README.md +++ b/quest/m0/ux/README.md @@ -140,7 +140,9 @@ quest links its own. An agent can prove a page matches its mock-up's layout, that nothing clips at any window size, and what a frame costs. It cannot judge these, so each child's -PR hands them to the maintainer as `## Waiting` with what to look at: +PR adds a follow-up quest for the maintainer to try the page, such as +[Try the LIVE page](/quest/m0/ux/try-live.md), with a `Maintainer:` item saying +what to look at, rather than waiting on it: - **Taste:** whether the page reads as bold and calm at once. The PR shows its screenshots beside the mock-up frames. diff --git a/quest/m0/ux/listen-speakers.md b/quest/m0/ux/listen-speakers.md new file mode 100644 index 00000000..14ef3a8e --- /dev/null +++ b/quest/m0/ux/listen-speakers.md @@ -0,0 +1,25 @@ +# [S] Listen warns on speakers + +## Goal + +**Listen** on the MIX page warns, on the listened audio scene's strip and +before it plays, when the output it plays on is not headphones and the audio +scene carries a microphone: the one feedback loop the studio cannot prevent. +Where the system cannot tell what an output is, it says nothing. + +## Plan + +Split from the MIX page, which landed Listen's output picker on the listened +audio scene's strip (`Panels::listen_tag` in `rs/moq-studio/src/ui/mixer.rs`) +but cannot tell headphones from speakers yet. + +- `mix::Speaker` carries the output's kind from + `moq_audio::playback::Device`, and the system default's kind is the + default device's. +- The warning is amber, as everything that needs attention but is not an + error, and does not block Listen: a speaker in another room is fine. +- A headset counts as headphones. + +## Required + +- [moq-audio's output kind is pinned](/quest/m0/pin-output-kind.md) - whether an output is headphones, where the system can tell diff --git a/quest/m0/ux/mix.md b/quest/m0/ux/mix.md deleted file mode 100644 index d22440d9..00000000 --- a/quest/m0/ux/mix.md +++ /dev/null @@ -1,55 +0,0 @@ -# [L] The MIX page - -## Goal - -The MIX page of [`mock/mix.html`](/quest/m0/ux/mock/mix.html) replaces the -Audio panel: - -- One strip per sound, with its icon and name, a tall meter, a fader, and a - mute, and the audio scenes as master strips beside them. -- Under each strip, where it is heard: one dot per audio scene. The audio - scene the stream carries is marked as such, and any can be listened to - with a headphone button. -- A strip opens to its pan, its delay, and its filters. -- The same sends as a matrix, for someone who thinks in routing. - -The LIVE page's sound strip is the same meters and mutes for the sounds the -stream carries. On this page the bottom strip keeps the scene pads and the -panic key and shows a small live picture where that sound strip sits -elsewhere, so an operator riding a fader still sees what is on air. - -## Plan - -Nothing about mixing changes: gain, pan, mute, delay, sends, the audio scene -on program, the recorded one, and Listen are as `README.md`'s Audio scenes -section has them. "Audio scene" stays the word, since it was chosen over -"mix" on 2026-11-06 -([moq-dev/studio#77](https://github.com/moq-dev/studio/pull/77)). - -- A strip opens in place and its neighbours narrow, so no meter is ever - covered during a show. Strips and the matrix are two views of one page. -- The output Listen plays on is picked on the listened audio scene's strip, - not in Settings. -- A fader is gain in decibels, with unity marked and a double click - returning to it, as pan returns to center today. -- The meter keeps the peak tick and the red at full scale of - `rs/moq-studio/src/ui/levels.rs`, drawn as `kit::Meter`'s segments: red is - a clip, an error. A muted sound's control is amber, as `kit.css` draws it. -- A sound heard twice by construction cannot happen here, since an audio - scene plays a sound once however it reaches it. Show that: a dot is filled - when the sound reaches the audio scene by any route, and hollow-ringed when - it does so only through another audio scene. -- A microphone feeding the output it is heard on is the one loop the studio - cannot prevent. Warn on Listen when the output is not headphones, where the - system can tell. -- [Audio filters](/quest/m0/audio-filters.md) and - [clap to measure](/quest/m0/sync-clap.md) add their controls to the opened - strip; this quest leaves their places. - -Human check: a real microphone and headphones, and whether the sends read at -a glance to someone who has never seen an audio scene. - -## Related - -- [Audio filters](/quest/m0/audio-filters.md) - the filter chain an opened strip configures -- [Clap-to-measure sync delay](/quest/m0/sync-clap.md) - the button beside a strip's delay diff --git a/quest/m0/ux/mock/mix-mix.webp b/quest/m0/ux/mock/mix-mix.webp index 7500c745..03ed8ddf 100644 Binary files a/quest/m0/ux/mock/mix-mix.webp and b/quest/m0/ux/mock/mix-mix.webp differ diff --git a/quest/m0/ux/mock/mix-strip.webp b/quest/m0/ux/mock/mix-strip.webp index 0c1a1698..7e4b4bb4 100644 Binary files a/quest/m0/ux/mock/mix-strip.webp and b/quest/m0/ux/mock/mix-strip.webp differ diff --git a/quest/m0/ux/mock/mix.html b/quest/m0/ux/mock/mix.html index 923acee7..3731ff88 100644 --- a/quest/m0/ux/mock/mix.html +++ b/quest/m0/ux/mock/mix.html @@ -67,12 +67,13 @@ /* masters: the audio scenes */ .strip.ms{width:auto;flex:1;background:linear-gradient(#1d1d27,#15151b);border-color:#3b3b4a} .strip.ms .mt i{width:14px} -.strip.ms .hd{gap:10px;justify-content:center} +.strip.ms .hd{gap:6px;justify-content:center} .strip.ms .nm2{font-size:18px;letter-spacing:.08em} .strip.ms.pg{border-color:var(--accent);box-shadow:0 0 28px #4f8cff33} .mtag{display:flex;align-items:center;gap:5px;height:18px;padding:0 7px;border-radius:4px;background:var(--p3);color:var(--dim);font:700 11px/1 var(--disp);letter-spacing:.1em;text-transform:uppercase;white-space:nowrap} .mtag.air{background:var(--accent);color:#fff} .mtag.sel{background:#4f8cff2b;color:#8db4ff} +.mtag.ghost{background:var(--p2);box-shadow:inset 0 0 0 1px var(--line)} .mtag svg.i{width:12px;height:12px} .addms{width:40px;flex:none;border:2px dashed var(--line);border-radius:12px;display:grid;place-items:center;color:var(--dim)} @@ -180,13 +181,15 @@ const dot=(c,i,l=1)=>`${c=='x'||!l?'':SC[i][0]}`; const sends=s=>`
${[...s].map((c,i)=>dot(c,i)).join('')}
`; const scale=`
${[['+6',0],['0',14,'u'],['6',28],['12',42],['24',58],['36',72],['48',86],['60',100]].map(([t,y,c])=>`${t}`).join('')}
`; -const fz=s=>`
${scale}
${s.db} dB`; +const fz=s=>`
${scale}
${s.db} dB`; +/* an audio scene has no gain or mute of its own: its meter on the same scale, and Listen */ +const mfz=s=>`
${scale}
`; const strip=s=>`
${s.ic}
${s.n}
${fz(s)}
${I(s.mi||'vol','sm')}
${sends(s.s)}
`; const master=s=>`
-
${s.id}${s.n}
${s.tag}
${fz(s)} -
${I('vol','sm')}
${I('phones','sm')}
${sends(s.s)}
`; +
${s.id}${s.n}
${s.pg?'':'Put on stream'}${s.tag}
${mfz(s)} +
${I('phones','sm')}
${sends(s.s)}`; const legend=`
Where each sound is heardsentthrough another audio scenenot heard
`; const sub=v=>`
STRIPSROUTES
${legend}
`; const desk=(open)=>`
@@ -246,7 +249,7 @@
`; const V={ - 'mix':{label:'MIX',cls:'M',note:'MIX. One strip per sound with a tall meter, a fader in decibels, and a mute; the audio scenes stand to the right as heavier master strips. The dots under a strip say where it is heard, one per audio scene: filled is sent, a ring arrives through another audio scene, and the accent is the one the stream hears. Any audio scene can be listened to with its headphone button. The dock stays underneath, so the scenes, the panic key, and the live picture stay on screen; the picture takes the slot where the sound strip sits on the other pages, since this page already is the sound.', + 'mix':{label:'MIX',cls:'M',note:'MIX. One strip per sound with a tall meter, a fader in decibels, and a mute; the audio scenes stand to the right as heavier master strips, with their meters on the same scale but no fader or mute, since an audio scene has no gain of its own. The dots under a strip say where it is heard, one per audio scene: filled is sent, a ring arrives through another audio scene, and the accent is the one the stream hears. Any audio scene can be listened to with its headphone button. The dock stays underneath, so the scenes, the panic key, and the live picture stay on screen; the picture takes the slot where the sound strip sits on the other pages, since this page already is the sound.', html:()=>topbar('MIX')+sub('s')+desk()+mixdock(), after:f=>{air(f,'game');const d=f.querySelector('.strip[data-id=mic] .sends .sd.v');d.classList.add('hov'); f.querySelector('.strip[data-id=mic]').insertAdjacentHTML('beforeend','
Desk mic reaches Headphones through Stream
')}}, diff --git a/quest/m0/ux/settings.md b/quest/m0/ux/settings.md index e1694e76..6b56dda0 100644 --- a/quest/m0/ux/settings.md +++ b/quest/m0/ux/settings.md @@ -25,7 +25,7 @@ bar. ## Plan - The mock-up lists a Sound pane. Its one setting, the output Listen plays - on, is on the [MIX page](/quest/m0/ux/mix.md) instead, so there is none. + on, is on the MIX page instead, so there is none. The Guests pane has no frame; it follows the Stream pane's shape. - Each setting keeps its home on disk: the scene file for what belongs to the show (canvas, scene sizes, published scenes), per-user settings for diff --git a/quest/m0/ux/try-mix.md b/quest/m0/ux/try-mix.md new file mode 100644 index 00000000..ff6151c1 --- /dev/null +++ b/quest/m0/ux/try-mix.md @@ -0,0 +1,38 @@ +# [XS] Try the MIX page + +## Goal + +The maintainer tries the MIX page on their own machines and either signs it +off or files what they find as quests: what the epic's +[What a person must check](/quest/m0/ux/README.md) asks of it, which no +headless run exercises. + +## Plan + +The MIX page (its strips, the audio scenes as master strips, the opened +strip, and Routes) is on `main` (`cargo run -p moq-studio --features +capture`; Listen and its output picker are disabled without `capture`). +What to try, beside the [mock-up](/quest/m0/ux/mock/mix-mix.webp): + +- **Real devices:** a real microphone and headphones. **Listen** on an audio + scene, then pick the output from its **On …** tag; the meters moving with + real sound, the peak held, red only at a clip. +- **Feel:** riding a fader with a mouse and a trackpad, whether the taper is + fine enough near unity, a double click returning to it; mute and unmute + during a show; opening a strip while the others narrow. +- **Readability:** whether the send dots (filled, ringed when heard only + through another audio scene, faint when not heard) read at a glance to + someone who has never seen an audio scene. If they do not, **Routes** is + the fallback to point people at, and the legend's wording is the cheapest + thing to change. + +An agent then turns each finding into a quest, or an edit to the quest that +owns that page, and deletes this one. + +## Waiting + +- Maintainer: try the MIX page as the Plan lists and report what to change, + or that nothing needs to. An agent cannot judge feel or taste, and the + agent hosts have no microphone or headphones; a kittest run already covers + the clicks (`shots::mix_sends_and_mutes`), and `just shots` the layout. + Check: a comment or quest records the result. diff --git a/rs/moq-studio/shots/mix.toml b/rs/moq-studio/shots/mix.toml new file mode 100644 index 00000000..a7d86266 --- /dev/null +++ b/rs/moq-studio/shots/mix.toml @@ -0,0 +1,96 @@ +# The show `just shots` renders the MIX views on: the shots' show with more +# sounds, one muted and one turned down, and three audio scenes, one heard +# through another. +version = 1 + +[canvas] +width = 1920 +height = 1080 +framerate = 30 + +[sources.host] +kind = "image" +path = "person.jpg" + +[sources.backdrop] +kind = "color" +color = "#101828" + +[sources.bar] +kind = "color" +color = "#2563ebe6" + +[sources.guest] +kind = "broadcast" +path = "room/guest" + +[sources.caller] +kind = "broadcast" +path = "room/caller" + +# Failed in a build without `capture`, which still gives it a strip. +[sources.mic] +kind = "microphone" + +[sources.music] +kind = "broadcast" +path = "room/music" + +[audio.mic] +gain = 1.585 # +4 dB + +[audio.music] +gain = 0.2 # -14 dB +pan = -0.25 + +[audio.caller] +gain = 1.0 +muted = true + +[[scenes]] +name = "Main" +items = [ + { source = "backdrop", rect = { x = 0, y = 0, width = 1920, height = 1080 } }, + { source = "host", rect = { x = 0, y = 0, width = 1920, height = 1080 } }, + { source = "bar", rect = { x = 80, y = 860, width = 900, height = 120 } }, +] + +[[scenes]] +name = "Interview" +items = [ + { source = "backdrop", rect = { x = 0, y = 0, width = 1920, height = 1080 } }, + { source = "host", rect = { x = 60, y = 270, width = 880, height = 495 } }, + { source = "guest", rect = { x = 980, y = 270, width = 880, height = 495 } }, +] + +[[scenes]] +name = "Break" +safe = true # the scene the panic key cuts to +items = [ + { source = "backdrop", rect = { x = 0, y = 0, width = 1920, height = 1080 } }, + { source = "bar", rect = { x = 460, y = 480, width = 1000, height = 120 } }, +] + +# A scene the program refuses, at a size of its own, whose pad says why. +[[scenes]] +name = "Vertical" +size = { width = 1080, height = 1920 } +items = [ + { source = "backdrop", rect = { x = 0, y = 0, width = 1080, height = 1920 } }, + { source = "host", rect = { x = 0, y = 656, width = 1080, height = 608 } }, +] + +[[audio_scenes]] +name = "Stream" +program = true +sounds = ["caller", "guest", "mic", "music"] + +[[audio_scenes]] +name = "Headphones" +scenes = ["Stream"] +sounds = ["caller"] + +[[audio_scenes]] +name = "Recording" +recorded = true +sounds = ["guest", "mic"] diff --git a/rs/moq-studio/src/kit/widgets.rs b/rs/moq-studio/src/kit/widgets.rs index 25088a56..4ccd5b2a 100644 --- a/rs/moq-studio/src/kit/widgets.rs +++ b/rs/moq-studio/src/kit/widgets.rs @@ -87,6 +87,8 @@ pub struct IconButton<'a> { /// The fill while lit, such as a mute while muted. lit: Option, size: f32, + /// How wide it is, if wider than tall, such as a strip's mute. + width: Option, /// Said under the name, such as why it is disabled. note: Option<&'a str>, pose: Option, @@ -102,11 +104,18 @@ impl<'a> IconButton<'a> { look: Look::Plain, lit: None, size: 30.0, + width: None, note: None, pose: None, } } + /// Make it `width` wide, as a mixer strip's buttons fill the strip. + pub fn width(mut self, width: f32) -> Self { + self.width = Some(width); + self + } + /// The key that does the same, shown in the tooltip. pub fn key(mut self, key: Option<&'a str>) -> Self { self.key = key; @@ -146,7 +155,8 @@ impl<'a> IconButton<'a> { impl Widget for IconButton<'_> { fn ui(self, ui: &mut Ui) -> Response { - let (rect, response) = ui.allocate_exact_size(Vec2::splat(self.size), Sense::click()); + let size = vec2(self.width.unwrap_or(self.size), self.size); + let (rect, response) = ui.allocate_exact_size(size, Sense::click()); response.widget_info(|| WidgetInfo::labeled(WidgetType::Button, ui.is_enabled(), self.name)); if ui.is_rect_visible(rect) { let pose = self.pose.unwrap_or_else(|| Pose::of(&response)); @@ -503,35 +513,43 @@ impl Widget for Slider<'_> { } } -/// A vertical fader over 0 to 1, as on a mixing desk: a slot and a cap. -#[cfg_attr( - not(test), - expect(dead_code, reason = "the MIX page's strips draw with it: quest/m0/ux/mix.md") -)] +/// A vertical fader over 0 to 1, as on a mixing desk: a slot, a cap, and a +/// line across the slot at a mark, such as unity gain. pub struct Fader<'a> { value: &'a mut f32, height: f32, + mark: Option, } -#[cfg_attr( - not(test), - expect(dead_code, reason = "the MIX page's strips draw with it: quest/m0/ux/mix.md") -)] impl<'a> Fader<'a> { + /// How far in from its top and bottom the cap's center stops, so a scale + /// beside it lines up with its travel. + pub const INSET: f32 = 8.0; + pub fn new(value: &'a mut f32) -> Self { - Self { value, height: 160.0 } + Self { + value, + height: 160.0, + mark: None, + } } pub fn height(mut self, height: f32) -> Self { self.height = height; self } + + /// Mark the value `at` across the slot, as unity on a gain fader. + pub fn mark(mut self, at: f32) -> Self { + self.mark = Some(at); + self + } } impl Widget for Fader<'_> { fn ui(self, ui: &mut Ui) -> Response { let (rect, mut response) = ui.allocate_exact_size(vec2(28.0, self.height), Sense::drag()); - let travel = egui::Rangef::new(rect.top() + 8.0, rect.bottom() - 8.0); + let travel = egui::Rangef::new(rect.top() + Self::INSET, rect.bottom() - Self::INSET); // The release too, so a move in its frame is kept. if (response.dragged() || response.drag_stopped()) && let Some(pos) = response.interact_pointer_pos() @@ -548,6 +566,10 @@ impl Widget for Fader<'_> { let painter = ui.painter(); let slot = Rect::from_center_size(rect.center(), vec2(8.0, rect.height())); painter.rect_filled(slot, 4, Color32::BLACK); + if let Some(mark) = self.mark { + let y = egui::lerp(travel.max..=travel.min, mark); + painter.hline(rect.x_range().shrink(2.0), y, Stroke::new(2.0, FAINT)); + } let y = egui::lerp(travel.max..=travel.min, value); let cap = Rect::from_center_size(pos2(rect.center().x, y), vec2(26.0, 16.0)); // A cap lit from above: light on top, darker below, and a groove. @@ -567,7 +589,6 @@ impl Widget for Fader<'_> { } /// Fill `rect`, rounded by `corner`, from `top` down to `bottom`. -#[cfg_attr(not(test), expect(dead_code, reason = "the fader's cap"))] fn gradient(painter: &egui::Painter, rect: Rect, corner: u8, top: Color32, bottom: Color32) { // The rounded body in the bottom color, its top half's gradient over it. painter.rect_filled(rect, corner, bottom); @@ -608,23 +629,31 @@ pub enum Run { pub struct Meter { level: f32, peak: f32, + hot: f32, run: Run, size: Vec2, } -/// Where a meter turns from healthy to hot. -const HOT: f32 = 0.7; - impl Meter { + /// Where a meter turns from healthy to hot, unless told otherwise. + pub const HOT: f32 = 0.7; + pub fn new(level: f32, peak: f32) -> Self { Self { level, peak, + hot: Self::HOT, run: Run::Across, size: vec2(160.0, 9.0), } } + /// Turn hot from `at` along the run, for a meter on a scale of its own. + pub fn hot(mut self, at: f32) -> Self { + self.hot = at; + self + } + /// Run `run`, `size` points along it and across. pub fn run(mut self, run: Run, along: f32, across: f32) -> Self { self.run = run; @@ -657,7 +686,7 @@ impl Widget for Meter { for index in 0..count { let at = (index as f32 + 0.5) / count as f32; let lit = at <= self.level; - let color = match (lit || index + 1 == peak, at >= HOT) { + let color = match (lit || index + 1 == peak, at >= self.hot) { (false, _) => P3, (true, false) => OK, (true, true) if clipped && index + 1 == count => ERR, @@ -1214,7 +1243,7 @@ impl<'a> TallyFrame<'a> { } /// Diagonal stripes of `color` filling `rect`, clipped to it. -fn stripes(rect: Rect, color: Color32) -> Vec { +pub fn stripes(rect: Rect, color: Color32) -> Vec { let mut shapes = Vec::new(); let pitch = 24.0; let rot = Rot2::from_angle(-std::f32::consts::FRAC_PI_4); diff --git a/rs/moq-studio/src/shots.rs b/rs/moq-studio/src/shots.rs index ac5d4dba..f38e1b2c 100644 --- a/rs/moq-studio/src/shots.rs +++ b/rs/moq-studio/src/shots.rs @@ -13,13 +13,14 @@ use std::sync::{Arc, OnceLock}; use std::time::Duration; use eframe::egui_wgpu; +use egui::accesskit::Role; use egui_kittest::Harness; -use egui_kittest::kittest::Queryable; +use egui_kittest::kittest::{NodeT, Queryable}; use moq_scene::Source; use moq_scene::source::Status; use crate::fx::Effects; -use crate::ui::{Inspect, Page, Setting, Side}; +use crate::ui::{Inspect, MixView, Page, Setting, Side}; use crate::{App, Window, autosave, engine, fx, hotkeys, setup}; /// The window's size, the mock-ups'. @@ -49,6 +50,10 @@ enum Open { /// Where a view turns the studio. enum View { Page(Page), + /// The MIX page in a view, with the strip of the sound named open. + Mix(MixView, Option<&'static str>), + /// MIX with Headphones' menu open, as a right click on its strip opens it. + MixMenu, /// The BUILD page with `side` and `inspect` shown, and the layer of the /// scene on program at `index`, front first, selected. Build { @@ -97,6 +102,16 @@ fn studio(view: View) -> Open { } } +/// The studio on the MIX views' show, `size` large, turned to MIX in `view` +/// with the strip of the sound called `opened` open. +fn mixing(view: MixView, opened: Option<&'static str>, size: egui::Vec2) -> Open { + Open::Studio { + view: View::Mix(view, opened), + size, + show: "mix.toml", + } +} + /// Whether a view draws the same picture every time. #[derive(PartialEq)] enum Motion { @@ -167,11 +182,19 @@ views! { build_off_air_refused: Still, studio(View::OffAir("Vertical")); /// Too narrow for the banner beside the live picture, it goes under it. build_off_air_compact: Still, Open::Studio { view: View::OffAir("Break"), size: HALF, show: "show.toml" }; - /// MIX holds the mixer, and a small live picture in the sound's place. - mix: Still, studio(View::Page(Page::Mix)); - /// MIX in a stacked window, its live picture as tall as the sound's slot - /// allows. - mix_compact: Still, Open::Studio { view: View::Page(Page::Mix), size: HALF, show: "show.toml" }; + /// MIX: a strip per sound and per audio scene, with their sends, and a + /// small live picture in the sound's place. + mix: Still, mixing(MixView::Strips, None, SIZE); + /// A sound's strip opened to its gain, pan, delay, and where it is heard, + /// its neighbours narrowed. + mix_strip: Still, mixing(MixView::Strips, Some("mic"), SIZE); + /// An audio scene's menu: rename, move, or remove it. + mix_menu: Still, Open::Studio { view: View::MixMenu, size: SIZE, show: "mix.toml" }; + /// The same sends as a matrix. + mix_routes: Still, mixing(MixView::Routes, None, SIZE); + /// MIX in a stacked window, its strips scrolling, its live picture as + /// tall as the sound's slot allows. + mix_compact: Still, mixing(MixView::Strips, None, HALF); /// REVIEW off air: without a relay the program keeps no past, which the /// strip and both actions say, beside this show's recordings and clips. review: Still, studio(View::Review { picked: false, replays: false }); @@ -538,6 +561,131 @@ fn studio_mode_cues_and_takes() { quit(&engine, &runtime); } +/// MIX's send dots, an opened strip's switches, a mute, and the audio scene +/// tags edit the show as a person clicking them does, each an edit undo +/// names: on the MIX views' show, on a real engine. It draws no view. +#[test] +#[ignore = "draws on a GPU; run with `just shots`"] +fn mix_sends_and_mutes() { + let (_show, runtime, gpu) = lavapipe(); + let _entered = runtime.enter(); + let fixture = Path::new(env!("CARGO_MANIFEST_DIR")); + std::fs::copy(fixture.join("shots/mix.toml"), "show.toml").unwrap(); + let (mut harness, started, _) = window(start(launch(&gpu), &runtime), &gpu, &runtime, SIZE); + harness.step(); + let App::Studio(studio) = &mut harness.state_mut().app else { + panic!("the studio starts on its first frame") + }; + studio.mix(MixView::Strips, None); + let engine = started.get().expect("the studio started").clone(); + runtime + .block_on(kio::clock::timeout(SETTLE, settle(&mut harness, &engine))) + .expect("the sources and pictures settle"); + let scene = |name: &str| { + let state = engine.state.lock().unwrap(); + state.collection().audio_scene(name).cloned().expect("the audio scene") + }; + let undo = || { + let state = engine.state.lock().unwrap(); + let history = state.history(); + let at = history.position().checked_sub(1)?; + history.entries().nth(at).map(|edit| edit.label.clone()) + }; + + // A dot sends a sound in, and again takes it out. + assert!(!scene("Recording").sounds.contains("music")); + harness.get_by_label("Send music to Recording").click(); + harness.run_steps(2); + assert!(scene("Recording").sounds.contains("music"), "sent"); + assert_eq!(undo().as_deref(), Some("Put `music` in `Recording`")); + harness.get_by_label("Send music to Recording").click(); + harness.run_steps(2); + assert!(!scene("Recording").sounds.contains("music"), "taken out"); + + // A sound heard only through Stream, its dot a ring, is sent straight in. + harness.get_by_label("Send guest to Headphones").click(); + harness.run_steps(2); + assert!(scene("Headphones").sounds.contains("guest")); + + // An audio scene sends into another, but never into one it holds. + harness.get_by_label("Send Recording to Headphones").click(); + harness.run_steps(2); + assert!(scene("Headphones").scenes.contains("Recording")); + assert!( + harness + .get_by_label("Send Headphones to Stream") + .accesskit_node() + .is_disabled(), + "Headphones holds Stream" + ); + + // A strip's name opens it to its controls, and again closes it. + assert!(harness.query_by_label("Delay").is_none()); + harness.get_by_label("Open mic").click(); + harness.run_steps(2); + assert!(harness.query_by_label("Delay").is_some(), "opened"); + harness.get_by_label("Close mic").click(); + harness.run_steps(2); + assert!(harness.query_by_label("Delay").is_none(), "closed"); + + // The mute, and putting another audio scene on the stream. + harness.get_all_by_label("Mute").next().expect("a mute").click(); + harness.run_steps(2); + assert_eq!(undo().as_deref(), Some("Mute `guest`")); + harness + .get_all_by_label("Put on stream") + .next() + .expect("one off the stream") + .click(); + harness.run_steps(2); + assert!(scene("Headphones").program && !scene("Stream").program); + + // A new audio scene, with nothing in it. + harness.get_by_label("Add an audio scene").click(); + harness.run_steps(2); + assert_eq!(scene("Audio scene 4").sounds.len(), 0); + + // Its strip's menu renames it in a sheet, moves it, and removes it. + let names = || { + let state = engine.state.lock().unwrap(); + let scenes = &state.collection().audio_scenes; + scenes.iter().map(|scene| scene.name.clone()).collect::>() + }; + harness.get_by_label("Audio scene Audio scene 4").click_secondary(); + harness.run_steps(2); + harness.get_by_label("Rename").click(); + harness.run_steps(2); + harness.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + harness.get_by_role(Role::TextInput).type_text("Talkback"); + harness.run_steps(2); + harness.get_by_label("Rename").click(); + harness.run_steps(2); + assert_eq!(names(), ["Stream", "Headphones", "Recording", "Talkback"]); + assert_eq!( + undo().as_deref(), + Some("Rename audio scene `Audio scene 4` to `Talkback`") + ); + harness.get_by_label("Audio scene Talkback").click_secondary(); + harness.run_steps(2); + harness.get_by_label("Move left").click(); + harness.run_steps(2); + assert_eq!(names(), ["Stream", "Headphones", "Talkback", "Recording"]); + harness.get_by_label("Audio scene Talkback").click_secondary(); + harness.run_steps(2); + harness.get_by_label("Remove").click(); + harness.run_steps(2); + assert_eq!(names(), ["Stream", "Headphones", "Recording"]); + // The one the stream hears cannot be removed. + harness.get_by_label("Audio scene Headphones").click_secondary(); + harness.run_steps(2); + assert!(harness.get_by_label("Remove").accesskit_node().is_disabled()); + harness.key_press(egui::Key::Escape); + harness.run_steps(2); + + drop(harness); + quit(&engine, &runtime); +} + /// Draw what `open` opens on a fresh engine, from a scene file in `shows`. fn shoot(open: &Open, gpu: &engine::Gpu, runtime: &tokio::runtime::Runtime, shows: &Path) -> image::RgbaImage { if let &Open::Kit { sheet } = open { @@ -575,6 +723,8 @@ fn shoot(open: &Open, gpu: &engine::Gpu, runtime: &tokio::runtime::Runtime, show }; match view { View::Page(page) => studio.turn(*page), + View::Mix(view, opened) => studio.mix(*view, *opened), + View::MixMenu => studio.mix(MixView::Strips, None), View::Build { side, inspect, index } => studio.build(*side, *inspect, *index), View::Menu => studio.build(Side::Layers, Inspect::Layer, Some(1)), View::Setting(setting) => studio.open(*setting), @@ -602,6 +752,10 @@ fn shoot(open: &Open, gpu: &engine::Gpu, runtime: &tokio::runtime::Runtime, show .expect("the sources and pictures settle"); match view { View::Hover => hover(&mut harness), + View::MixMenu => { + harness.get_by_label("Audio scene Headphones").click_secondary(); + harness.run_steps(4); + } View::Menu => { // The row comes before the inspector's tab of the same name. harness.get_all_by_label("host").next().unwrap().click_secondary(); @@ -619,12 +773,31 @@ fn shoot(open: &Open, gpu: &engine::Gpu, runtime: &tokio::runtime::Runtime, show } _ => {} } + fresh_glyphs(&mut harness); let image = harness.render().unwrap(); drop(harness); quit(&engine, runtime); image } +/// Pack egui's glyphs anew from the frame about to be drawn. They are packed +/// in the order text first appears, and a glyph's edge samples its +/// neighbour's, so a view whose text changed while it settled, such as the +/// health dot's on a show with a failed source, would otherwise draw a pixel +/// differently from run to run. New font definitions repack the atlas. +fn fresh_glyphs(harness: &mut Harness<'_, Window>) { + let ctx = harness.ctx.clone(); + let fonts = ctx.fonts(|fonts| fonts.definitions().clone()); + let mut other = fonts.clone(); + other + .families + .insert(egui::FontFamily::Name("repack".into()), Vec::new()); + ctx.set_fonts(other); + harness.step(); + ctx.set_fonts(fonts); + harness.step(); +} + /// A window on `app`, `size` large, where the studio it starts is kept, and /// whether it was asked to close. fn window<'a>( diff --git a/rs/moq-studio/src/ui.rs b/rs/moq-studio/src/ui.rs index c6f1aa00..4f4ba85c 100644 --- a/rs/moq-studio/src/ui.rs +++ b/rs/moq-studio/src/ui.rs @@ -41,7 +41,9 @@ mod review; mod settings; mod shell; -use levels::{Levels, meter}; +use levels::Levels; +#[cfg(test)] +pub(crate) use mixer::View as MixView; /// The smallest the window gets, at which every part still has room. pub const MIN_SIZE: egui::Vec2 = layout::MIN; @@ -206,13 +208,13 @@ impl Studio { duration: Transition::default().duration().unwrap_or_default(), cut: STINGER_CUT, drag: None, - new_scene: String::new(), renaming: None, title_needs: 0.0, renaming_audio: None, renaming_pad: None, speaker: None, - speakers: Speakers::default(), + speakers: mixer::Speakers::default(), + desk: mixer::Desk::default(), canvas: None, sizes: BTreeMap::new(), add: AddSource::default(), @@ -247,6 +249,13 @@ impl Studio { self.panels.page = page; } + /// Turn to MIX in `view`, with the strip of the sound called `opened` + /// open, as clicking its name would. + pub(crate) fn mix(&mut self, view: mixer::View, opened: Option<&str>) { + self.panels.page = Page::Mix; + self.panels.desk = mixer::Desk::new(view, opened); + } + /// Open Settings at `setting`, as the gear would. pub(crate) fn open(&mut self, setting: Setting) { self.panels.setting = Some(setting); @@ -504,20 +513,22 @@ struct Panels { drag: Option, /// The layer row being dragged to a new place, by its index. moving: Option, - new_scene: String, /// The scene being renamed, by index, and the name typed so far. renaming: Option<(usize, String)>, /// The width the title bar's full controls took when last drawn, past /// which it draws them compact. title_needs: f32, - /// The audio scene being renamed, by its name, and the name typed so far. + /// The audio scene its strip's sheet renames, by name, and the name typed + /// so far. renaming_audio: Option<(String, String)>, /// The scene its pad's sheet renames, by name, and the name typed so far. renaming_pad: Option<(String, String)>, /// The output Listen plays on, by [`mix::Speaker::id`], or `None` for the /// system default. Saved per user, not in the shared scene file. speaker: Option, - speakers: Speakers, + speakers: mixer::Speakers, + /// The MIX page's view and the strip open on it. + desk: mixer::Desk, /// The canvas the Show tab's form holds until it is applied, or `None` /// while it shows the collection's. canvas: Option, @@ -1239,14 +1250,6 @@ impl SceneAction { } } -/// An edit the Scenes panel makes to the audio scenes, each by its place. -enum AudioAction { - /// To the name typed. - Rename(usize, String), - Move(usize, usize), - Remove(usize), -} - /// A strip of the mixer, which sends into audio scenes: a source's sound, or /// an audio scene, each by name. #[derive(Clone, Copy)] @@ -1334,21 +1337,8 @@ fn strip_controls( let (slid, field) = ui .horizontal(|ui| { let slid = ui.add(kit::Slider::new(&mut at).width(120.0)).on_hover_text("Gain"); - let level = egui::DragValue::new(&mut typed) - .range(MUTE_DB..=12.0) - // A scene file may hold more boost; showing it must not cut it. - .clamp_existing_to_range(false) - .speed(0.1) - .custom_formatter(|db, _| match db <= f64::from(MUTE_DB) { - true => "-inf".to_string(), - false => format!("{db:+.1}"), - }) - .custom_parser(|text| match text.trim() { - "-inf" => Some(f64::from(MUTE_DB)), - text => text.parse().ok(), - }); let field = ui - .add(kit::Number::new(level).unit("dB").width(84.0)) + .add(kit::Number::new(gain_field(&mut typed)).unit("dB").width(84.0)) .on_hover_text("Gain in decibels: drag, or type a level such as 0"); (slid, field) }) @@ -1356,8 +1346,10 @@ fn strip_controls( for (response, db) in [(slid, MUTE_DB + at * (12.0 - MUTE_DB)), (field, typed)] { let gesture = gestures.of(&response); if response.changed() { - let gain = if db <= MUTE_DB { 0.0 } else { 10f32.powf(db / 20.0) }; - let audio = moq_scene::Audio { gain, ..audio.clone() }; + let audio = moq_scene::Audio { + gain: gain(db), + ..audio.clone() + }; change = Some((audio, format!("Set `{name}` gain"), gesture)); } } @@ -2167,6 +2159,10 @@ fn status(ui: &mut Ui, status: &Status) { /// The quietest gain the slider offers, which it treats as silence. const MUTE_DB: f32 = -60.0; +/// Where every page's meters turn from healthy to hot, in dB below full +/// scale: broadcast's usual alignment level. +const HOT_DB: f32 = -18.0; + fn decibels(gain: f32) -> f32 { if gain <= 0.0 { MUTE_DB @@ -2175,6 +2171,29 @@ fn decibels(gain: f32) -> f32 { } } +/// The linear gain of `db` decibels, silence at or below [`MUTE_DB`]. +fn gain(db: f32) -> f32 { + if db <= MUTE_DB { 0.0 } else { 10f32.powf(db / 20.0) } +} + +/// A field for a gain in decibels, `-inf` at [`MUTE_DB`], that drags or +/// takes an exact level typed. +fn gain_field(db: &mut f32) -> egui::DragValue<'_> { + egui::DragValue::new(db) + .range(MUTE_DB..=12.0) + // A scene file may hold more boost; showing it must not cut it. + .clamp_existing_to_range(false) + .speed(0.1) + .custom_formatter(|db, _| match db <= f64::from(MUTE_DB) { + true => "-inf".to_string(), + false => format!("{db:+.1}"), + }) + .custom_parser(|text| match text.trim() { + "-inf" => Some(f64::from(MUTE_DB)), + text => text.parse().ok(), + }) +} + /// The form that adds a source by hand. struct AddSource { name: String, @@ -2418,60 +2437,6 @@ impl AddSource { } } -/// The audio outputs Listen can play on, listed once the Audio panel is shown -/// and again on Refresh. -#[derive(Default)] -struct Speakers { - listing: Option, String>>, - pending: Option, String>>>, -} - -impl Speakers { - /// Pick the output `speaker` names, or `None` for the system default. - /// Returns whether the pick changed. - fn ui(&mut self, ui: &mut Ui, runtime: &tokio::runtime::Handle, speaker: &mut Option) -> bool { - if let Some(listing) = self.pending.as_ref().and_then(|pending| pending.try_recv().ok()) { - self.listing = Some(listing); - self.pending = None; - } - let mut refresh = self.listing.is_none() && self.pending.is_none(); - let before = speaker.clone(); - ui.horizontal(|ui| { - ui.label("Listen on"); - let speakers = self.listing.as_ref().and_then(|listing| listing.as_ref().ok()); - let named = |id: &str| { - let found = speakers.and_then(|speakers| speakers.iter().find(|speaker| speaker.id == id)); - found.map_or_else(|| format!("{id} (not found)"), |speaker| speaker.name.clone()) - }; - let shown = speaker.as_deref().map_or_else(|| "System default".to_string(), named); - egui::ComboBox::from_id_salt("speaker") - .selected_text(shown) - .show_ui(ui, |ui| { - ui.selectable_value(speaker, None, "System default"); - for found in speakers.into_iter().flatten() { - ui.selectable_value(speaker, Some(found.id.clone()), &found.name); - } - }); - if self.pending.is_some() { - ui.spinner(); - } else if ui.small_button("Refresh").clicked() { - refresh = true; - } - }); - if let Some(Err(err)) = &self.listing { - ui.colored_label(ui.visuals().error_fg_color, format!("Cannot list the outputs: {err}")); - } - if refresh { - let (send, recv) = std::sync::mpsc::channel(); - runtime.spawn(async move { - let _ = send.send(mix::speakers().await); - }); - self.pending = Some(recv); - } - *speaker != before - } -} - /// The capture devices this machine has, listed in the background. #[derive(Default)] struct Devices { diff --git a/rs/moq-studio/src/ui/levels.rs b/rs/moq-studio/src/ui/levels.rs index 0fd5c859..2d94f17e 100644 --- a/rs/moq-studio/src/ui/levels.rs +++ b/rs/moq-studio/src/ui/levels.rs @@ -107,7 +107,7 @@ fn push(history: &mut History, level: Level, steps: usize) { } /// Where `amplitude` falls on a meter, from 0 at [`MUTE_DB`] to 1 at full scale. -fn position(amplitude: f32) -> f32 { +pub(super) fn position(amplitude: f32) -> f32 { (1.0 - decibels(amplitude) / MUTE_DB).clamp(0.0, 1.0) } @@ -116,16 +116,15 @@ fn color(level: Level) -> Color32 { if level.peak() >= 1.0 { kit::ERR } else { kit::OK } } -/// A level meter of the latest step: RMS lit, peak held, on a decibel scale. -pub(super) fn meter(ui: &mut Ui, history: Option<&History>) { - let (level, peak) = reading(history); - ui.add(kit::Meter::new(level, peak)); +/// The latest step's level, or silence before the first. +pub(super) fn latest(history: Option<&History>) -> Level { + history.and_then(|history| history.back().copied()).unwrap_or_default() } /// The latest step's RMS and peak where they fall on a meter, from 0 at /// [`MUTE_DB`] to 1 at full scale, as [`kit::Meter::new`] takes them. pub(super) fn reading(history: Option<&History>) -> (f32, f32) { - let level = history.and_then(|history| history.back().copied()).unwrap_or_default(); + let level = latest(history); (position(level.rms()), position(level.peak())) } diff --git a/rs/moq-studio/src/ui/mixer.rs b/rs/moq-studio/src/ui/mixer.rs index ab4bbd56..ae50e1cb 100644 --- a/rs/moq-studio/src/ui/mixer.rs +++ b/rs/moq-studio/src/ui/mixer.rs @@ -1,242 +1,1264 @@ -//! The MIX page: every sound and audio scene, with its sends. +//! The MIX page: a strip per sound, the audio scenes as master strips beside +//! them, and the same sends as a matrix. +use egui::{Align2, CursorIcon, Rect, Sense, Stroke, StrokeKind, UiBuilder, Vec2, pos2, vec2}; +use moq_scene::{AudioScene, Collection}; + +use super::levels::{History, latest}; +use super::shell::rename_sheet; use super::*; +/// The page's two views of the same sends. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(crate) enum View { + /// A strip per sound and per audio scene, as on a mixing desk. + #[default] + Strips, + /// Who hears what, as a matrix of sounds and audio scenes. + Routes, +} + +impl View { + const ALL: [Self; 2] = [Self::Strips, Self::Routes]; + + fn name(self) -> &'static str { + match self { + Self::Strips => "Strips", + Self::Routes => "Routes", + } + } +} + +/// What the MIX page keeps between frames. +#[derive(Default)] +pub(super) struct Desk { + view: View, + /// The sound whose strip is open, by name. + opened: Option, +} + +impl Desk { + /// The page in `view`, with the strip of the sound called `opened` open. + #[cfg(test)] + pub(super) fn new(view: View, opened: Option<&str>) -> Self { + Self { + view, + opened: opened.map(str::to_string), + } + } +} + +/// The decibels marked on a strip's scale, and where each sits on it, from +/// silence at the foot to the most boost at the head: finer near unity, where +/// a fader is ridden, as a desk's taper is. +const SCALE: [(f32, f32); 9] = [ + (MUTE_DB, 0.0), + (-48.0, 0.1), + (-36.0, 0.22), + (-24.0, 0.38), + (-12.0, 0.56), + (-6.0, 0.68), + (0.0, UNITY), + (6.0, 0.9), + (12.0, 1.0), +]; + +/// Where unity gain, and full scale on a meter, sit on a strip's scale. +const UNITY: f32 = 0.8; + +/// Where `db` sits on a strip's scale, from 0 at its foot to 1 at its head. +fn taper(db: f32) -> f32 { + let db = db.clamp(SCALE[0].0, SCALE[SCALE.len() - 1].0); + let next = SCALE + .iter() + .position(|&(mark, _)| mark >= db) + .unwrap_or(SCALE.len() - 1); + if next == 0 { + return 0.0; + } + let ((low, from), (high, to)) = (SCALE[next - 1], SCALE[next]); + egui::lerp(from..=to, (db - low) / (high - low)) +} + +/// The decibels at `at` on a strip's scale, the inverse of [`taper`]. +fn untaper(at: f32) -> f32 { + let at = at.clamp(0.0, 1.0); + let next = SCALE + .iter() + .position(|&(_, mark)| mark >= at) + .unwrap_or(SCALE.len() - 1); + if next == 0 { + return MUTE_DB; + } + let ((low, from), (high, to)) = (SCALE[next - 1], SCALE[next]); + egui::lerp(low..=high, (at - from) / (to - from)) +} + +/// Where a level of `amplitude` lights a strip's meter, which runs from +/// silence at the scale's foot to full scale at unity. +fn lit(amplitude: f32) -> f32 { + (taper(decibels(amplitude)) / UNITY).clamp(0.0, 1.0) +} + +/// A meter on the desk's scale, `level` and `peak` placed by [`lit`], hot +/// from the same level as on every other page. +fn desk_meter(level: f32, peak: f32) -> kit::Meter { + kit::Meter::new(level, peak).hot(lit(gain(HOT_DB))) +} + +/// How a sound or an audio scene reaches an audio scene, as its send dot +/// shows it. +#[derive(Clone, Debug, PartialEq, Eq)] +enum Reach { + /// Sent straight into it. + Sent, + /// Heard in it only through the audio scene named, which it holds. + Through(String), + /// Not heard in it. + Not, + /// A sound published independently, which reaches viewers by its own + /// track and so no audio scene hears, whatever sends it. + Published, + /// The audio scene itself. + Own, + /// An audio scene the sender holds, which would then hold itself. + Loop, +} + +impl Reach { + /// How the strip `from` reaches the audio scene `to` of `collection`. + fn of(collection: &Collection, from: Strip<'_>, to: &AudioScene) -> Self { + match from { + Strip::Sound(name) if collection.published.contains(name) => Self::Published, + Strip::Sound(name) if to.sounds.contains(name) => Self::Sent, + Strip::Sound(name) => { + let through = to.scenes.iter().find(|held| collection.heard(held).contains(name)); + through.map_or(Self::Not, |held| Self::Through(held.clone())) + } + Strip::Scene(name) if name == to.name => Self::Own, + Strip::Scene(name) if collection.nests(name, &to.name) => Self::Loop, + Strip::Scene(name) if to.scenes.contains(name) => Self::Sent, + Strip::Scene(name) => { + let through = to.scenes.iter().find(|held| collection.nests(held, name)); + through.map_or(Self::Not, |held| Self::Through(held.clone())) + } + } + } + + /// Whether it is heard there, by any route. + fn heard(&self) -> bool { + matches!(self, Self::Sent | Self::Through(_)) + } + + /// What its dot says under the pointer, for the strip called `from` + /// reaching the audio scene called `to`. + fn says(&self, from: &str, to: &str) -> String { + match self { + Self::Sent => format!("{from} is sent to {to}; click to take it out"), + Self::Through(held) => { + format!("{from} reaches {to} through {held}, and plays there once; click to send it straight in too") + } + Self::Not => format!("{from} is left out of {to}; click to send it in"), + Self::Own => format!("{to} itself"), + Self::Loop => format!("{from} holds {to}, so {to} cannot hear it back"), + Self::Published => { + format!("{from} is published independently, so no audio scene hears it; stop publishing it in Build") + } + } + } +} + +/// A sound, as its strip shows it. +struct Channel { + name: String, + /// What kind of source it is, such as a microphone. + kind: &'static str, + microphone: bool, + audio: moq_scene::Audio, + /// How it reaches each audio scene, in their order. + reach: Vec, +} + +/// An audio scene, as its master strip shows it. +struct Master { + name: String, + /// The letter its send dots carry. + letter: String, + /// Whether the stream carries it. + program: bool, + /// Whether recordings hold it. + recorded: bool, + /// How it reaches each audio scene, its own included, in their order. + reach: Vec, + /// What it holds, as the matrix's foot says it. + holds: String, +} + +/// What the page shows, copied under one short lock a frame. +struct Board { + sounds: Vec, + masters: Vec, + /// The audio scene Listen plays, by name, and why it stopped, if it did. + listening: Option<(String, Option)>, +} + +impl Board { + fn take(state: &State, listening: Option<(String, Option)>) -> Self { + let collection = state.collection(); + let scenes = &collection.audio_scenes; + let reach = |from: Strip<'_>| scenes.iter().map(|to| Reach::of(collection, from, to)).collect(); + let sounds = state + .sources + .iter() + .filter(|(_, feed)| feed.strip().is_some()) + .map(|(name, _)| { + let source = collection.sources.get(name); + Channel { + name: name.to_string(), + kind: kind(source), + microphone: matches!(source, Some(Source::Microphone { .. })), + audio: collection.audio.get(name).cloned().unwrap_or_default(), + reach: reach(Strip::Sound(name)), + } + }) + .collect(); + let recorded = collection.recorded_audio(); + let masters = scenes + .iter() + .map(|scene| Master { + name: scene.name.clone(), + letter: letter(&scene.name), + program: scene.program, + recorded: recorded == Some(scene.name.as_str()), + reach: reach(Strip::Scene(&scene.name)), + holds: holds(scene), + }) + .collect(); + Self { + sounds, + masters, + listening, + } + } + + /// Whether Listen plays the audio scene called `name`. + fn heard(&self, name: &str) -> bool { + self.listening.as_ref().is_some_and(|(heard, _)| heard == name) + } +} + +/// What a source with sound is, as its opened strip says. +fn kind(source: Option<&Source>) -> &'static str { + match source { + Some(Source::Microphone { .. }) => "Microphone", + Some(Source::SystemAudio) => "System audio", + Some(Source::Media { .. }) => "Media file", + Some(Source::Broadcast { .. }) => "Broadcast", + Some(Source::Browser { .. }) => "Browser", + Some(Source::Replay) => "Replay", + Some(Source::Clone { .. }) => "Clone", + _ => "Source", + } +} + +/// The letter an audio scene's send dots carry: its name's first. +fn letter(name: &str) -> String { + name.chars() + .next() + .map(|first| first.to_uppercase().collect()) + .unwrap_or_default() +} + +/// What `scene` holds, in a few words: "4 sounds", "1 sound and Stream". +fn holds(scene: &AudioScene) -> String { + let sounds = match scene.sounds.len() { + 0 => None, + 1 => Some("1 sound".to_string()), + count => Some(format!("{count} sounds")), + }; + let parts: Vec = sounds.into_iter().chain(scene.scenes.iter().cloned()).collect(); + match parts.as_slice() { + [] => "Nothing".to_string(), + [one] => one.clone(), + [rest @ .., last] => format!("{} and {last}", rest.join(", ")), + } +} + +/// An edit the page asked for this frame, made once it is drawn. +enum Change { + /// One of the sound called `name`'s settings. + Audio { + name: String, + set: Setting, + label: String, + gesture: Option, + }, + /// Send a sound, or an audio scene if `scene`, into the audio scene `to`, + /// or stop. + Send { + scene: bool, + from: String, + to: String, + on: bool, + }, + /// Put the audio scene called this on the stream. + Program(String), + /// Add an audio scene. + Add, + /// Open the sheet renaming the audio scene called this. + Rename(String), + /// Rename the audio scene called `from` to `to`. + Renamed { from: String, to: String }, + /// Move the audio scene called this one place left (-1) or right (1). + Move(String, isize), + /// Remove the audio scene called this. + Remove(String), + /// Listen to the audio scene called this, or stop. + Listen(Option), + /// Listen on this output, or the system default. + Speaker(Option), +} + +/// One setting of a sound's audio, as one control changes it. +/// +/// An edit carries only the setting its control changed, made on the sound's +/// audio as it is when applied: a whole copy from when the page was drawn +/// would undo an edit made in between, such as the panic key's mute from a +/// hotkey's thread. +#[derive(Debug, Clone, Copy, PartialEq)] +enum Setting { + Gain(f32), + Muted(bool), + Pan(f32), + Delay(Duration), +} + +impl Setting { + /// Make it on `audio`. + fn on(self, audio: &mut moq_scene::Audio) { + match self { + Self::Gain(gain) => audio.gain = gain, + Self::Muted(muted) => audio.muted = muted, + Self::Pan(pan) => audio.pan = pan, + Self::Delay(delay) => audio.delay = delay, + } + } +} + +/// Room around the page's contents. +const PAD: f32 = 20.0; +/// The bar over the strips or the matrix. +const BAR: f32 = 44.0; +/// A sound's strip, and as narrow as one gets beside an opened one. +const WIDE: f32 = 128.0; +const NARROW: f32 = 92.0; +/// The smallest and largest a send dot under a strip gets, and the room +/// between dots. +const DOT: [f32; 2] = [12.0, 22.0]; +const DOT_GAP: f32 = 5.0; +/// The narrowest and widest an audio scene's strip gets. +const MASTER: [f32; 2] = [150.0, 240.0]; +/// Room between strips, and between the sounds and the audio scenes. +const GAP: f32 = 8.0; +const GROUP: f32 = 18.0; +/// How wide the room an opened strip grows is. +const OPENED: f32 = 300.0; +/// The tile that adds an audio scene. +const ADD: f32 = 40.0; +/// A caption over a group of strips, with the room under it. +const CAPTION: f32 = 22.0; +/// The shortest a strip's fader gets before the page scrolls. +const FADER_MIN: f32 = 90.0; +/// What a strip has besides its fader: its head, value, buttons, sends, and +/// the room between them. +const STRIP_REST: f32 = 12.0 + 58.0 + 10.0 + 10.0 + 22.0 + 10.0 + 30.0 + 10.0 + 33.0 + 12.0; + impl Panels { - /// The mixer: which audio scene the program carries and which plays to - /// the speakers, and a strip for each sound and each audio scene, with its - /// meter and a send to each audio scene. A sound's strip sets its gain, - /// mute, and delay too. - pub(super) fn audio(&mut self, ui: &mut Ui) { + /// The MIX page in the stage's room: the bar that turns between the + /// strips and the matrix, and the one shown. + pub(super) fn mix(&mut self, ui: &mut Ui, stage: Rect) { let engine = self.engine.clone(); - let mut state = engine.state.lock().unwrap(); - let scenes = state.collection().audio_scenes.clone(); - let listening = engine.listening(); - let mut scenes_change = None; - let mut listen = None; - - egui::Grid::new("audio scenes").show(ui, |ui| { - for scene in &scenes { - let name = &scene.name; - ui.label(name); - meter(ui, self.levels.mix(name)); - if ui.radio(scene.program, "On program").clicked() && !scene.program { - scenes_change = Some(on_program(&scenes, name)); - } - let heard = listening.as_ref().is_some_and(|(heard, _)| heard == name); - let note = match mix::MONITOR { - true => "Play this audio scene to the speakers; use headphones", - false => "Needs a build with the `capture` feature", - }; - let button = IconButton::new(Icon::Listen, "Listen") - .lit(heard, kit::ACCENT) - .small() - .note(Some(note)); - let response = ui.add_enabled(mix::MONITOR, button); - if response.clicked() { - listen = Some((!heard).then(|| name.clone())); - } - ui.end_row(); - } + let board = { + let state = engine.state.lock().unwrap(); + Board::take(&state, engine.listening()) + }; + // A strip opened on a sound since removed or renamed closes. + if let Some(opened) = &self.desk.opened + && !board.sounds.iter().any(|sound| sound.name == *opened) + { + self.desk.opened = None; + } + let bar = Rect::from_min_size(stage.min, vec2(stage.width(), BAR)); + self.mix_bar(ui, bar); + let body = stage.with_min_y(bar.bottom()).shrink2(vec2(PAD, 0.0)); + let body = body.with_max_y(body.bottom() - PAD); + let mut changes = Vec::new(); + match self.desk.view { + View::Strips => self.strips(ui, body, &board, &mut changes), + View::Routes => self.routes(ui, body, &board, &mut changes), + } + if let Some((from, to)) = rename_sheet(ui.ctx(), "rename audio scene", &mut self.renaming_audio) { + changes.push(Change::Renamed { from, to }); + } + self.apply(changes); + } + + /// The switch between the strips and the matrix, and what a send dot's + /// look means. + fn mix_bar(&mut self, ui: &mut Ui, rect: Rect) { + let room = rect.shrink2(vec2(PAD, 0.0)); + let names = View::ALL.map(|view| { + let galley = kit::caps_galley(ui.painter(), view.name(), kit::display(13.0), kit::TEXT, None); + (view, galley) }); - if mix::MONITOR && self.speakers.ui(ui, &self.runtime, &mut self.speaker) { - // The monitor moves to the new output. - if let Some((heard, _)) = &listening { - listen = Some(Some(heard.clone())); + let width: f32 = names.iter().map(|(_, galley)| galley.size().x + 28.0).sum::() + 6.0; + let switch = Rect::from_min_size(pos2(room.left(), room.center().y - 15.0), vec2(width, 30.0)); + ui.painter() + .rect(switch, 8, kit::P2, Stroke::new(1.0, kit::LINE), StrokeKind::Inside); + let mut x = switch.left() + 3.0; + for (view, galley) in names { + let at = Rect::from_min_size(pos2(x, switch.top() + 3.0), vec2(galley.size().x + 28.0, 24.0)); + x = at.right(); + let response = ui.interact(at, ui.id().with(("mix view", view.name())), Sense::click()); + let on = self.desk.view == view; + response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, true, on, view.name())); + if on { + ui.painter().rect_filled(at, 6, kit::P3); + } + let ink = if on || response.hovered() { kit::TEXT } else { kit::DIM }; + ui.painter().galley(at.center() - galley.size() / 2.0, galley, ink); + if response.clicked() { + self.desk.view = view; } } - if let Some((_, Some(failed))) = &listening { - ui.colored_label(ui.visuals().error_fg_color, format!("Listening stopped: {failed}")); + + // The legend, from the right, left out where it would run into the + // switch: each dot's tooltip says its route anyway. + let font = kit::ui(11.5); + let mut right = room.right(); + let keys = [ + (Reach::Not, "not heard"), + (Reach::Through(String::new()), "through another audio scene"), + (Reach::Sent, "sent"), + ] + .map(|(reach, says)| (reach, ui.painter().layout_no_wrap(says.into(), font.clone(), kit::DIM))); + let legend: f32 = keys.iter().map(|(_, galley)| galley.size().x + 7.0 + 14.0 + 16.0).sum(); + if right - legend < switch.right() { + return; + } + for (reach, galley) in keys { + right -= galley.size().x; + ui.painter() + .galley(pos2(right, rect.center().y - galley.size().y / 2.0), galley, kit::DIM); + right -= 7.0 + 14.0; + let at = Rect::from_min_size(pos2(right, rect.center().y - 7.0), Vec2::splat(14.0)); + paint_dot(ui.painter(), at, &reach, "", false); + right -= 16.0; + } + if right - switch.right() > 160.0 { + let galley = ui + .painter() + .layout_no_wrap("Where each sound is heard".into(), font, kit::DIM); + let at = pos2(right - galley.size().x, rect.center().y - galley.size().y / 2.0); + ui.painter().galley(at, galley, kit::DIM); + } + } + + /// A strip for each sound, then one for each audio scene, scrolling + /// sideways when they do not fit. An opened strip grows its controls + /// beside it and its neighbours narrow, so no meter is covered. + fn strips(&mut self, ui: &mut Ui, body: Rect, board: &Board, changes: &mut Vec) { + let sounds = board.sounds.len(); + let masters = board.masters.len(); + let opened = self.desk.opened.clone(); + let grow = if opened.is_some() { OPENED } else { 0.0 }; + let group = if sounds > 0 { GROUP } else { 0.0 }; + // Every strip holds a dot for each audio scene, however many there are. + let floor = strip_floor(masters); + let (narrow, wide) = (NARROW.max(floor), WIDE.max(floor)); + let (master_min, master_max) = (MASTER[0].max(floor), MASTER[1].max(floor)); + let masters_min = masters as f32 * (master_min + GAP) + ADD; + let gaps = sounds.saturating_sub(1) as f32 * GAP; + // The opened strip keeps its width; the others share what is left. + let others = sounds.saturating_sub(usize::from(opened.is_some())); + let room = body.width() - masters_min - group - gaps - grow - if opened.is_some() { wide } else { 0.0 }; + let width = match others { + 0 => wide, + others => (room / others as f32).clamp(narrow, wide), + }; + let sounds_width = others as f32 * width + if opened.is_some() { wide + grow } else { 0.0 } + gaps; + let spare = body.width() - sounds_width - group - ADD - masters as f32 * GAP; + let master = match masters { + 0 => 0.0, + masters => (spare / masters as f32).clamp(master_min, master_max), + }; + let content = vec2( + (sounds_width + group + masters as f32 * (master + GAP) + ADD).max(body.width()), + body.height().max(CAPTION + STRIP_REST + FADER_MIN), + ); + + ui.scope_builder(UiBuilder::new().max_rect(body), |ui| { + egui::ScrollArea::both() + .id_salt("mix strips") + .auto_shrink(false) + .show(ui, |ui| { + let (area, _) = ui.allocate_exact_size(content, Sense::hover()); + let top = area.top() + CAPTION; + let bottom = area.bottom(); + let mut x = area.left(); + if sounds > 0 { + caption(ui, pos2(x, area.top()), "Sounds"); + } + for sound in &board.sounds { + let open = opened.as_deref() == Some(sound.name.as_str()); + let strip_width = if open { wide } else { width }; + let strip = Rect::from_min_max(pos2(x, top), pos2(x + strip_width, bottom)); + if open { + let whole = strip.with_max_x(strip.right() + grow); + ui.painter() + .rect(whole, 12, kit::P1, Stroke::new(1.5, kit::ACCENT), StrokeKind::Inside); + self.sound_strip(ui, strip, sound, board, false, changes); + let panel = whole.with_min_x(strip.right()); + ui.painter().vline( + panel.left() + 0.5, + panel.y_range().shrink(1.0), + Stroke::new(1.0, kit::LINE), + ); + self.opened_strip(ui, panel, sound, board, changes); + x = whole.right() + GAP; + } else { + self.sound_strip(ui, strip, sound, board, true, changes); + x = strip.right() + GAP; + } + } + x += group - if sounds > 0 { GAP } else { 0.0 }; + caption(ui, pos2(x, area.top()), "Audio scenes"); + for index in 0..masters { + let strip = Rect::from_min_max(pos2(x, top), pos2(x + master, bottom)); + self.master_strip(ui, strip, index, board, changes); + x = strip.right() + GAP; + } + let add = Rect::from_min_max(pos2(x, top), pos2(x + ADD, bottom)); + if add_tile(ui, add) { + changes.push(Change::Add); + } + }); + }); + } + + /// A sound's strip in `rect`: its icon and name, which open it, its + /// meter and fader, its gain, its mute, and its sends. Framed unless it + /// is the opened one, whose frame takes in its controls too. + fn sound_strip( + &mut self, + ui: &mut Ui, + rect: Rect, + sound: &Channel, + board: &Board, + framed: bool, + changes: &mut Vec, + ) { + if framed { + ui.painter() + .rect(rect, 12, kit::P1, Stroke::new(1.0, kit::LINE), StrokeKind::Inside); + } + let parts = Parts::of(rect); + let muted = sound.audio.muted; + let name = &sound.name; + + // The head: a click opens the strip, or closes it. + let head = ui.interact(parts.head, ui.id().with(("open strip", name)), Sense::click()); + let open = self.desk.opened.as_deref() == Some(name.as_str()); + let says = if open { + format!("Close {name}") + } else { + format!("Open {name}") + }; + head.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, &says)); + let head = kit::tip(head.on_hover_cursor(CursorIcon::PointingHand), &says, None); + if head.clicked() { + self.desk.opened = (!open).then(|| name.clone()); } - ui.separator(); + ui.scope_builder(UiBuilder::new().max_rect(rect), |ui| { + if muted { + ui.multiply_opacity(0.45); + } + let painter = ui.painter(); + if head.hovered() { + painter.rect_filled(parts.head.expand2(vec2(4.0, 4.0)), 8, kit::P2); + } + let tile = Rect::from_center_size(pos2(parts.head.center().x, parts.head.top() + 16.0), Vec2::splat(32.0)); + let (fill, icon) = match sound.microphone { + true => (kit::ACCENT, Icon::Mic), + false => (kit::P3, Icon::Sound), + }; + painter.rect_filled(tile, 8, fill); + icon.paint(ui, tile, 16.0, kit::TEXT); + let galley = kit::caps_galley(painter, name, kit::display(15.0), kit::TEXT, Some(rect.width() - 16.0)); + let at = pos2( + parts.head.center().x - galley.size().x / 2.0, + parts.head.bottom() - galley.size().y, + ); + painter.galley(at, galley, kit::TEXT); + + // The meter, the scale, and the fader, centered together. + let (level, peak) = reading(self.levels.sound(name)); + let width = 10.0 + 6.0 + 22.0 + 2.0 + 28.0; + let left = parts.fader.center().x - width / 2.0; + meter(ui, parts.fader, left, 10.0, level, peak); + scale(ui.painter(), parts.fader, left + 16.0, true); + let db = decibels(sound.audio.gain); + let mut at = taper(db); + let fader = Rect::from_min_size(pos2(left + 40.0, parts.fader.top()), vec2(28.0, parts.fader.height())); + let response = ui + .put(fader, kit::Fader::new(&mut at).height(fader.height()).mark(UNITY)) + .on_hover_text("Gain: drag; double-click for unity"); + let gesture = self.gestures.of(&response); + let reset = double_clicked(ui, &response); + if response.changed() || reset { + let db = if reset { 0.0 } else { untaper(at) }; + changes.push(gain_change(sound, db, gesture)); + } - // A send toggled from a sound or an audio scene into an audio scene. - let send = |from: Strip<'_>, to: &str, on: bool| { - let mut next = scenes.clone(); - from.send(&mut next, to, on); - (next, from.label(to, on)) + // The gain, to read or type. + let mut typed = db; + let field = Rect::from_center_size(parts.value.center(), vec2((rect.width() - 20.0).min(96.0), 22.0)); + let response = ui + .put( + field, + kit::Number::new(gain_field(&mut typed)).unit("dB").width(field.width()), + ) + .on_hover_text("Gain in decibels: drag, or type a level such as 0"); + let gesture = self.gestures.of(&response); + if response.changed() { + changes.push(gain_change(sound, typed, gesture)); + } + }); + + let (icon, verb) = match (muted, sound.microphone) { + (true, true) => (Icon::MicMuted, "Unmute"), + (true, false) => (Icon::Muted, "Unmute"), + (false, true) => (Icon::Mic, "Mute"), + (false, false) => (Icon::Sound, "Mute"), }; - let mut change = None; - egui::Grid::new("audio").striped(true).show(ui, |ui| { - ui.label(""); - ui.label(""); - ui.label("Gain"); - ui.label("Pan"); - ui.label("Delay") - .on_hover_text("Hold the source back to line it up with a camera that lags it"); - for scene in &scenes { - ui.label(&scene.name); - } - ui.end_row(); - for (name, feed) in state.sources.iter() { - if feed.strip().is_none() { - continue; + let button = IconButton::new(icon, verb) + .look(kit::Look::Boxed) + .lit(muted, kit::CUE) + .width(parts.buttons.width()); + if ui.put(parts.buttons, button).clicked() { + changes.push(Change::Audio { + name: name.clone(), + set: Setting::Muted(!muted), + label: format!("{verb} `{name}`"), + gesture: None, + }); + } + sends(ui, parts.sends, Strip::Sound(name), &sound.reach, board, changes); + } + + /// An audio scene's strip in `rect`: its name and what it is for, its + /// meter, Listen, and its sends into the others. + fn master_strip(&mut self, ui: &mut Ui, rect: Rect, index: usize, board: &Board, changes: &mut Vec) { + let scene = &board.masters[index]; + let painter = ui.painter(); + let (fill, stroke) = match scene.program { + true => (kit::P2, Stroke::new(1.5, kit::ACCENT)), + false => (kit::P2, Stroke::new(1.0, kit::LINE)), + }; + painter.rect(rect, 12, fill, stroke, StrokeKind::Inside); + let parts = Parts::of(rect); + let name = &scene.name; + + // The badge and name, then what it is for. + let galley = kit::caps_galley(painter, name, kit::display(17.0), kit::TEXT, Some(rect.width() - 50.0)); + let line = galley.size().x + 26.0; + let left = parts.head.center().x - line / 2.0; + let badge = Rect::from_center_size(pos2(left + 9.0, parts.head.top() + 10.0), Vec2::splat(18.0)); + paint_dot(painter, badge, &Reach::Sent, &scene.letter, scene.program); + painter.galley( + pos2(left + 26.0, badge.center().y - galley.size().y / 2.0), + galley, + kit::TEXT, + ); + let header = Rect::from_x_y_ranges(rect.x_range().shrink(8.0), rect.top()..=badge.bottom() + 4.0); + scene_menu(ui, header, index, board, changes); + let tags = Rect::from_min_size( + pos2(rect.left() + 8.0, badge.bottom() + 8.0), + vec2(rect.width() - 16.0, 18.0), + ); + self.master_tags(ui, tags, scene, board, changes); + + // The meter on the same scale as the sounds', without a fader: an + // audio scene has no gain of its own. + let (level, peak) = reading(self.levels.mix(name)); + let width = 14.0 + 6.0 + 22.0; + let left = parts.fader.center().x - width / 2.0; + meter(ui, parts.fader, left, 14.0, level, peak); + scale(ui.painter(), parts.fader, left + 20.0, false); + + let failed = board + .listening + .as_ref() + .and_then(|(listened, failed)| (listened == name).then_some(failed.as_deref()).flatten()); + if let Some(failed) = failed { + let response = ui.interact(parts.value, ui.id().with(("listen failed", name)), Sense::hover()); + tag( + ui, + parts.value.shrink2(vec2(4.0, 2.0)), + "Listen stopped", + kit::ERR, + kit::TEXT, + ); + kit::tip(response, &format!("Listening stopped: {failed}"), None); + } + let note = match mix::MONITOR { + true => "Play this audio scene on the output picked under its name; use headphones", + false => "Needs a build with the `capture` feature", + }; + let heard = board.heard(name); + let says = if heard { + "Stop listening".to_string() + } else { + format!("Listen to {name}") + }; + let button = IconButton::new(Icon::Listen, &says) + .look(kit::Look::Boxed) + .lit(heard, kit::ACCENT) + .width(parts.buttons.width()) + .note(Some(note)); + let response = ui + .add_enabled_ui(mix::MONITOR, |ui| ui.put(parts.buttons, button)) + .inner; + if response.clicked() { + changes.push(Change::Listen((!heard).then(|| name.clone()))); + } + sends(ui, parts.sends, Strip::Scene(name), &scene.reach, board, changes); + } + + /// The tag on the audio scene Listen plays, which names the output it + /// plays on and picks another. + fn listen_tag(&mut self, ui: &mut Ui, rect: Rect, changes: &mut Vec) { + let output = self.speakers.named(&self.runtime, self.speaker.as_deref()); + let text = format!("On {output}"); + let response = tag_button(ui, rect, &text, "listen on"); + let response = kit::tip(response, "The output Listen plays on: pick another", None); + if response.clicked() { + self.speakers.refresh(&self.runtime); + } + egui::Popup::from_toggle_button_response(&response) + .frame(kit::popover_frame()) + .align(egui::RectAlign::BOTTOM) + .gap(6.0) + .width(280.0) + .show(|ui| { + let mut speaker = self.speaker.clone(); + if self.speakers.menu(ui, &mut speaker) { + changes.push(Change::Speaker(speaker)); } - let audio = state.collection().audio.get(name).cloned().unwrap_or_default(); - ui.label(name); - if let Some((audio, label, gesture)) = strip_controls(ui, &mut self.gestures, name, &audio) { - change = Some((name.to_string(), audio, label, gesture)); + if ui.add(kit::button(Icon::Reload, "Look for outputs again")).clicked() { + self.speakers.refresh(&self.runtime); } - for scene in &scenes { - let mut sent = scene.sounds.contains(name); - if ui.add(kit::Toggle::new(&mut sent)).changed() { - scenes_change = Some(send(Strip::Sound(name), &scene.name, sent)); + }); + } + + /// The opened strip's controls, in `rect` beside it: its name and kind, + /// its gain, pan, and delay, and where it is heard. + fn opened_strip(&mut self, ui: &mut Ui, rect: Rect, sound: &Channel, board: &Board, changes: &mut Vec) { + let name = &sound.name; + let room = rect.shrink2(vec2(16.0, 14.0)); + ui.scope_builder(UiBuilder::new().max_rect(room), |ui| { + ui.horizontal(|ui| { + ui.label(kit::caps(name, kit::display(18.0)).color(kit::TEXT)); + ui.label(egui::RichText::new(sound.kind).font(kit::ui(12.0)).color(kit::DIM)); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.add(IconButton::new(Icon::Close, "Close").small()).clicked() { + self.desk.opened = None; } - } - meter(ui, self.levels.sound(name)); - ui.end_row(); - } - // Each audio scene sends into others too, never into itself, even - // through others. - for from in &scenes { - let name = &from.name; - ui.label(name).on_hover_text("An audio scene"); - ui.weak("audio scene"); - ui.label(""); - ui.label(""); - ui.label(""); - for scene in &scenes { - let mut sent = scene.scenes.contains(name); - let loops = scene.name == *name || state.collection().nests(name, &scene.name); - let response = ui - .add_enabled(!loops, egui::Checkbox::without_text(&mut sent)) - .on_disabled_hover_text("It would hold itself"); - if response.changed() { - scenes_change = Some(send(Strip::Scene(name), &scene.name, sent)); + }); + }); + ui.add_space(6.0); + let audio = &sound.audio; + egui::Grid::new(("opened strip", name)) + .num_columns(3) + .spacing(vec2(10.0, 8.0)) + .show(ui, |ui| { + // Gain, on the fader's taper. + ui.label("Gain"); + let db = decibels(audio.gain); + let mut at = taper(db); + let slid = ui.add(kit::Slider::new(&mut at).width(130.0)); + let gesture = self.gestures.of(&slid); + let reset = double_clicked(ui, &slid); + if slid.changed() || reset { + let db = if reset { 0.0 } else { untaper(at) }; + changes.push(gain_change(sound, db, gesture)); } - } - meter(ui, self.levels.mix(name)); - ui.end_row(); + let mut typed = db; + let field = ui.add(kit::Number::new(gain_field(&mut typed)).unit("dB").width(84.0)); + let gesture = self.gestures.of(&field); + if field.changed() { + changes.push(gain_change(sound, typed, gesture)); + } + ui.end_row(); + + // Pan, centered by a double click. + ui.label("Pan"); + let mut at = (audio.pan + 1.0) / 2.0; + let slid = ui + .add(kit::Slider::new(&mut at).width(130.0)) + .on_hover_text("Place the sound between left and right; double-click to center"); + let gesture = self.gestures.of(&slid); + let reset = double_clicked(ui, &slid); + let mut pan = audio.pan; + if slid.changed() { + pan = at * 2.0 - 1.0; + } + if reset { + pan = 0.0; + } + let mut typed = audio.pan; + let field = ui.add(kit::Number::new(pan_field(&mut typed)).width(84.0)); + let field_gesture = self.gestures.of(&field); + if field.changed() { + pan = typed; + } + if pan != audio.pan { + changes.push(Change::Audio { + name: name.clone(), + set: Setting::Pan(pan), + label: format!("Set `{name}` pan"), + gesture: gesture.or(field_gesture), + }); + } + ui.end_row(); + + // Delay, up to the bus's most. + ui.label("Delay"); + let max = moq_audio::bus::MAX_DELAY.as_millis() as u64; + let mut at = audio.delay.as_millis() as f32 / max as f32; + let slid = ui + .add(kit::Slider::new(&mut at).width(130.0)) + .on_hover_text("Hold the sound back to line it up with a camera that lags it"); + let mut millis = audio.delay.as_millis() as u64; + let gesture = self.gestures.of(&slid); + if slid.changed() { + millis = (at * max as f32).round() as u64; + } + let mut typed = millis; + let field = ui.add( + kit::Number::new(egui::DragValue::new(&mut typed).range(0..=max)) + .unit("ms") + .width(84.0), + ); + let field_gesture = self.gestures.of(&field); + if field.changed() { + millis = typed; + } + if millis != audio.delay.as_millis() as u64 { + changes.push(Change::Audio { + name: name.clone(), + set: Setting::Delay(Duration::from_millis(millis)), + label: format!("Set `{name}` delay"), + gesture: gesture.or(field_gesture), + }); + } + ui.end_row(); + }); + // Clap to measure (quest/m0/sync-clap.md) goes beside the delay, + // and the filter chain (quest/m0/audio-filters.md) between it and + // where the sound is heard. + + ui.add_space(10.0); + ui.separator(); + ui.label(kit::caps("Heard in", kit::display(11.0)).color(kit::DIM)); + let published = sound.reach.contains(&Reach::Published); + if published { + ui.label( + egui::RichText::new("Published independently, so no audio scene hears it until it stops, in Build") + .color(kit::DIM), + ); + } + for (master, reach) in board.masters.iter().zip(&sound.reach) { + ui.horizontal(|ui| { + let (at, _) = ui.allocate_exact_size(Vec2::splat(18.0), Sense::hover()); + let shown = if reach.heard() { &Reach::Sent } else { &Reach::Not }; + paint_dot(ui.painter(), at, shown, &master.letter, master.program); + ui.label(&master.name); + if let Reach::Through(held) = reach { + ui.label(egui::RichText::new(format!("through {held}")).color(kit::DIM)); + } + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + let mut sent = *reach == Reach::Sent; + let response = ui.add_enabled(!published, kit::Toggle::new(&mut sent)); + let response = kit::tip(response, &reach.says(name, &master.name), None); + if response.changed() { + changes.push(Change::Send { + scene: false, + from: name.clone(), + to: master.name.clone(), + on: sent, + }); + } + }); + }); } }); + } - let mut result = Ok(()); - if let Some((name, audio, label, gesture)) = change { - result = edit(&mut state, label, gesture, |draft| draft.set_audio(&name, Some(audio))); + /// Who hears what: a row for each sound and each audio scene, a column + /// for each audio scene, and a dot where they meet. + fn routes(&mut self, ui: &mut Ui, body: Rect, board: &Board, changes: &mut Vec) { + const ROW: f32 = 50.0; + const HEAD: f32 = 98.0; + const SECTION: f32 = 34.0; + const FOOT: f32 = 36.0; + const COLUMN: f32 = 178.0; + let columns = board.masters.len() as f32; + let label = (body.width() - columns * COLUMN - ADD - 24.0).clamp(300.0, 520.0); + let width = label + columns * COLUMN + ADD + 24.0; + let rows = board.sounds.len() + board.masters.len(); + let height = HEAD + rows as f32 * ROW + SECTION + FOOT; + let size = vec2(width, height); + + ui.scope_builder(UiBuilder::new().max_rect(body), |ui| { + egui::ScrollArea::both() + .id_salt("mix routes") + .auto_shrink(false) + .show(ui, |ui| { + let left = (ui.available_width() - width).max(0.0) / 2.0; + let (area, _) = ui.allocate_exact_size(vec2(width + left, height), Sense::hover()); + let table = Rect::from_min_size(area.min + vec2(left, 0.0), size); + let painter = ui.painter(); + painter.rect(table, 12, kit::P1, Stroke::new(1.0, kit::LINE), StrokeKind::Inside); + let line = Stroke::new(1.0, kit::LINE); + let column = |index: usize| { + let x = table.left() + label + index as f32 * COLUMN; + egui::Rangef::new(x, x + COLUMN) + }; + + // The title, and a head for each audio scene. + let title = table.left_top() + vec2(18.0, 24.0); + painter.text(title, Align2::LEFT_TOP, "WHO HEARS WHAT", kit::display(22.0), kit::TEXT); + painter.text( + title + vec2(0.0, 32.0), + Align2::LEFT_TOP, + "A row is sent into a column. Click a dot to change it.", + kit::ui(11.5), + kit::DIM, + ); + for (index, master) in board.masters.iter().enumerate() { + let x = column(index); + let head = Rect::from_x_y_ranges(x, table.top()..=table.top() + HEAD); + if master.program { + let body = Rect::from_x_y_ranges(x, table.top() + 1.0..=table.bottom() - FOOT); + ui.painter().rect_filled(body, 0, kit::ACCENT.gamma_multiply(0.06)); + } + ui.painter().vline(x.min, table.y_range().shrink(1.0), line); + self.route_head(ui, head, index, board, changes); + } + let add = Rect::from_center_size( + pos2(column(board.masters.len()).min + 12.0 + ADD / 2.0, table.top() + 48.0), + Vec2::splat(28.0), + ); + let response = ui.put( + add, + IconButton::new(Icon::Add, "Add an audio scene").look(kit::Look::Boxed), + ); + if response.clicked() { + changes.push(Change::Add); + } + + // A row for each sound, then each audio scene. + let mut y = table.top() + HEAD; + let painter = ui.painter().clone(); + for sound in &board.sounds { + let row = Rect::from_x_y_ranges(table.x_range(), y..=y + ROW); + painter.hline(row.x_range(), row.top() + 0.5, line); + let name = row.with_max_x(table.left() + label); + self.route_label( + ui, + name, + &sound.name, + RowIcon::Sound(sound), + self.levels.sound(&sound.name), + ); + for (index, (master, reach)) in board.masters.iter().zip(&sound.reach).enumerate() { + let cell = Rect::from_x_y_ranges(column(index), row.y_range()); + let send = Dot { + from: Strip::Sound(&sound.name), + to: master, + reach, + letter: false, + }; + route_cell(ui, cell, send, changes); + } + y += ROW; + } + let section = Rect::from_x_y_ranges(table.x_range(), y..=y + SECTION); + painter.hline(section.x_range(), section.top() + 0.5, line); + painter.rect_filled(section.shrink(1.0), 0, kit::P1); + let at = pos2(section.left() + 18.0, section.center().y); + let caps = kit::caps_galley(&painter, "Audio scenes", kit::display(11.0), kit::DIM, None); + let caps_width = caps.size().x; + painter.galley(pos2(at.x, at.y - caps.size().y / 2.0), caps, kit::DIM); + painter.text( + at + vec2(caps_width + 12.0, 0.0), + Align2::LEFT_CENTER, + "An audio scene can be sent into another. A sound still plays once, however it gets there.", + kit::ui(11.5), + kit::DIM, + ); + y += SECTION; + for from in &board.masters { + let row = Rect::from_x_y_ranges(table.x_range(), y..=y + ROW); + painter.hline(row.x_range(), row.top() + 0.5, line); + let name = row.with_max_x(table.left() + label); + self.route_label(ui, name, &from.name, RowIcon::Scene(from), self.levels.mix(&from.name)); + for (index, (master, reach)) in board.masters.iter().zip(&from.reach).enumerate() { + let cell = Rect::from_x_y_ranges(column(index), row.y_range()); + let send = Dot { + from: Strip::Scene(&from.name), + to: master, + reach, + letter: false, + }; + route_cell(ui, cell, send, changes); + } + y += ROW; + } + + // What each audio scene holds. + let foot = Rect::from_x_y_ranges(table.x_range(), y..=table.bottom()); + painter.hline(foot.x_range(), foot.top() + 0.5, line); + painter.text( + pos2(foot.left() + 18.0, foot.center().y), + Align2::LEFT_CENTER, + "Holds", + kit::ui(11.5), + kit::DIM, + ); + for (index, master) in board.masters.iter().enumerate() { + let x = column(index); + painter.text( + pos2(x.center(), foot.center().y), + Align2::CENTER_CENTER, + &master.holds, + kit::ui(11.5), + kit::DIM, + ); + } + }); + }); + } + + /// What an audio scene is for, as tags from `first` down: whether the + /// stream hears it, or a button putting it on, then Listen's output where + /// it is listened to, or that recordings hold it. + fn master_tags(&mut self, ui: &mut Ui, first: Rect, scene: &Master, board: &Board, changes: &mut Vec) { + let name = &scene.name; + if scene.program { + tag(ui, first, "Stream hears this", kit::ACCENT, kit::TEXT); + } else { + let response = tag_button(ui, first, "Put on stream", ("put on stream", name)); + let response = kit::tip( + response, + &format!("Put {name} on the stream, without dropping or repeating audio"), + None, + ); + if response.clicked() { + changes.push(Change::Program(name.clone())); + } } - if let Some((next, label)) = scenes_change { - result = result.and(edit(&mut state, label, None, |draft| draft.set_audio_scenes(next))); + let second = first.translate(vec2(0.0, 22.0)); + if board.heard(name) { + self.listen_tag(ui, second, changes); + } else if scene.recorded { + tag(ui, second, "In the recording", kit::P3, kit::DIM); } - if let Some(listen) = listen { - result = result.and(self.listen(&mut state, listen.as_deref())); + } + + /// An audio scene's column head: its badge and name, what it is for, + /// and its meter. + fn route_head(&mut self, ui: &mut Ui, head: Rect, index: usize, board: &Board, changes: &mut Vec) { + let master = &board.masters[index]; + let painter = ui.painter(); + let galley = kit::caps_galley( + painter, + &master.name, + kit::display(16.0), + kit::TEXT, + Some(head.width() - 50.0), + ); + let line = galley.size().x + 26.0; + let left = head.center().x - line / 2.0; + let badge = Rect::from_center_size(pos2(left + 9.0, head.top() + 22.0), Vec2::splat(18.0)); + paint_dot(painter, badge, &Reach::Sent, &master.letter, master.program); + painter.galley( + pos2(left + 26.0, badge.center().y - galley.size().y / 2.0), + galley, + kit::TEXT, + ); + let header = Rect::from_x_y_ranges(head.x_range().shrink(12.0), head.top() + 8.0..=badge.bottom() + 4.0); + scene_menu(ui, header, index, board, changes); + let tags = Rect::from_center_size( + pos2(head.center().x, head.top() + 46.0), + vec2(head.width() - 24.0, 18.0), + ); + self.master_tags(ui, tags, master, board, changes); + let (level, peak) = reading(self.levels.mix(&master.name)); + let at = Rect::from_center_size(pos2(head.center().x, head.top() + 88.0), vec2(head.width() - 24.0, 8.0)); + ui.put(at, desk_meter(level, peak).run(kit::Run::Across, at.width(), 8.0)); + } + + /// A row's label: its icon, name, and meter. + fn route_label(&self, ui: &mut Ui, rect: Rect, name: &str, icon: RowIcon<'_>, history: Option<&History>) { + let painter = ui.painter(); + let tile = Rect::from_center_size(pos2(rect.left() + 30.0, rect.center().y), Vec2::splat(24.0)); + let muted = match icon { + RowIcon::Sound(sound) => { + let (fill, glyph) = match sound.microphone { + true => (kit::ACCENT, Icon::Mic), + false => (kit::P3, Icon::Sound), + }; + painter.rect_filled(tile, 6, fill); + glyph.paint(ui, tile, 13.0, kit::TEXT); + sound.audio.muted + } + RowIcon::Scene(master) => { + paint_dot(painter, tile.shrink(2.0), &Reach::Sent, &master.letter, master.program); + false + } + }; + // The name, cut short where it would run into the Muted tag or the + // meter, and then shown whole under the pointer. + let along = (rect.width() * 0.37).min(190.0); + let meter_left = rect.right() - along - 24.0; + let left = tile.right() + 14.0; + let room = meter_left - 16.0 - left - if muted { 10.0 + 54.0 } else { 0.0 }; + let ink = if muted { kit::DIM } else { kit::TEXT }; + let mut job = egui::text::LayoutJob::simple_singleline(name.to_string(), kit::ui_medium(14.0), ink); + job.wrap = egui::text::TextWrapping::truncate_at_width(room.max(0.0)); + let text = painter.layout_job(job); + let shown = Rect::from_min_size(pos2(left, rect.center().y - text.size().y / 2.0), text.size()); + let elided = text.elided; + painter.galley(shown.min, text, ink); + if elided { + let scene = matches!(icon, RowIcon::Scene(_)); + let response = ui.interact(shown, ui.id().with(("route label", name, scene)), Sense::hover()); + kit::tip(response, name, None); } - drop(state); - self.check(result); + if muted { + let at = Rect::from_min_size(pos2(shown.right() + 10.0, rect.center().y - 9.0), vec2(54.0, 18.0)); + tag(ui, at, "Muted", kit::CUE, kit::ON_CUE); + } + let (level, peak) = match muted { + true => (0.0, 0.0), + false => reading(history), + }; + let at = Rect::from_min_size(pos2(meter_left, rect.center().y - 4.0), vec2(along, 8.0)); + ui.put(at, desk_meter(level, peak).run(kit::Run::Across, along, 8.0)); } - /// The audio scenes by name, each renamed, reordered, or removed here, and - /// a new one added. The MIX page's redesign gives each a master strip - /// (quest/m0/ux/mix.md). - pub(super) fn audio_scenes(&mut self, ui: &mut Ui) { + /// Make the edits the page asked for, under one lock. + fn apply(&mut self, changes: Vec) { + if changes.is_empty() { + return; + } let engine = self.engine.clone(); let mut state = engine.state.lock().unwrap(); - let audio_scenes = state.collection().audio_scenes.clone(); - let count = audio_scenes.len(); - let mut audio_action = None; - for (index, scene) in audio_scenes.iter().enumerate() { - ui.horizontal(|ui| { - if let Some((renaming, name)) = &mut self.renaming_audio - && *renaming == scene.name - { - let response = ui.text_edit_singleline(name); - if !response.has_focus() && !response.lost_focus() { - response.request_focus(); - } - if response.lost_focus() { - // Escape cancels; Enter or a click away renames. - if !ui.input(|input| input.key_pressed(egui::Key::Escape)) { - audio_action = Some(AudioAction::Rename(index, name.trim().to_string())); - } - self.renaming_audio = None; + let mut result = Ok(()); + for change in changes { + let done = match change { + Change::Audio { + name, + set, + label, + gesture, + } => { + let mut audio = state.collection().audio.get(&name).cloned().unwrap_or_default(); + set.on(&mut audio); + edit(&mut state, label, gesture, |draft| draft.set_audio(&name, Some(audio))) + } + Change::Send { scene, from, to, on } => { + let strip = if scene { + Strip::Scene(&from) + } else { + Strip::Sound(&from) + }; + let mut scenes = state.collection().audio_scenes.clone(); + match strip.send(&mut scenes, &to, on) { + true => edit(&mut state, strip.label(&to, on), None, |draft| { + draft.set_audio_scenes(scenes) + }), + false => Ok(()), } - return; } - let label = ui.label(kit::caps(&scene.name, kit::display(14.0))); - if scene.program { - ui.add(kit::Tag::new("On air", Tally::Air)); + Change::Program(name) => { + let (scenes, label) = on_program(&state.collection().audio_scenes, &name); + edit(&mut state, label, None, |draft| draft.set_audio_scenes(scenes)) } - let rename = ui.add(IconButton::new(Icon::Rename, "Rename").small()); - if label.double_clicked() || rename.clicked() { - self.renaming_audio = Some((scene.name.clone(), scene.name.clone())); + Change::Add => { + let collection = state.collection(); + let name = (collection.audio_scenes.len() + 1..) + .map(|number| format!("Audio scene {number}")) + .find(|name| collection.audio_scene(name).is_none()) + .expect("a free name"); + let mut scenes = collection.audio_scenes.clone(); + scenes.push(AudioScene::new(name.clone())); + let done = edit(&mut state, format!("Add audio scene `{name}`"), None, |draft| { + draft.set_audio_scenes(scenes) + }); + done.map(|()| self.say(Ok(format!("added audio scene `{name}`; right-click it to rename it")))) } - if ui - .add_enabled(index > 0, IconButton::new(Icon::Up, "Move up").small()) - .clicked() - { - audio_action = Some(AudioAction::Move(index, index - 1)); + Change::Rename(name) => { + self.renaming_audio = Some((name.clone(), name)); + Ok(()) } - let down = IconButton::new(Icon::Down, "Move down").small(); - if ui.add_enabled(index + 1 < count, down).clicked() { - audio_action = Some(AudioAction::Move(index, index + 1)); + Change::Renamed { from, to } => self.rename_audio(&mut state, &from, &to), + Change::Move(name, by) => { + let mut scenes = state.collection().audio_scenes.clone(); + match scenes.iter().position(|scene| scene.name == name) { + Some(from) => { + let to = from.saturating_add_signed(by).min(scenes.len() - 1); + let scene = scenes.remove(from); + scenes.insert(to, scene); + edit(&mut state, format!("Reorder audio scene `{name}`"), None, |draft| { + draft.set_audio_scenes(scenes) + }) + } + None => Err(anyhow::anyhow!("no audio scene is called `{name}`")), + } } - let remove = IconButton::new(Icon::Remove, "Remove").small().note( - scene - .program - .then_some("On program; put another audio scene on program first"), - ); - if ui.add_enabled(!scene.program, remove).clicked() { - audio_action = Some(AudioAction::Remove(index)); + Change::Remove(name) => edit(&mut state, format!("Remove audio scene `{name}`"), None, |draft| { + draft.remove_audio_scene(&name) + }), + Change::Listen(scene) => self.listen(&mut state, scene.as_deref()), + Change::Speaker(speaker) => { + self.speaker = speaker; + // The monitor moves to the new output. + match self.engine.listening() { + Some((heard, _)) => self.listen(&mut state, Some(&heard)), + None => Ok(()), + } } - }); + }; + result = result.and(done); } - ui.horizontal(|ui| { - ui.add(egui::TextEdit::singleline(&mut self.new_scene).hint_text("New audio scene")); - if ui.add(kit::button(Icon::Add, "Add audio scene")).clicked() { - let name = self.new_scene.trim().to_string(); - let mut next = audio_scenes.clone(); - next.push(moq_scene::AudioScene::new(name.clone())); - let result = edit(&mut state, format!("Add audio scene `{name}`"), None, |draft| { - draft.set_audio_scenes(next) - }); - match result { - Ok(()) => self.new_scene.clear(), - Err(err) => self.report(Err(err)), - } - } - }); - if let Some(action) = audio_action { - let result = self.audio_scene(&mut state, &audio_scenes, action); - drop(state); - self.check(result); - } - } - - /// Rename, reorder, or remove one of `scenes`, the audio scenes as their - /// list showed them. Listening follows a renamed one. - pub(super) fn audio_scene( - &self, - state: &mut State, - scenes: &[moq_scene::AudioScene], - action: AudioAction, - ) -> anyhow::Result<()> { - match action { - AudioAction::Rename(index, to) => { - let from = &scenes[index].name; - if *from == to { - return Ok(()); - } - edit(state, format!("Rename audio scene `{from}` to `{to}`"), None, |draft| { - draft.rename_audio_scene(from, &to) - })?; - let listening = self.engine.listening(); - if listening.is_some_and(|(heard, _)| heard == *from) { - self.listen(state, Some(&to))?; - } - Ok(()) - } - AudioAction::Move(from, to) => { - let mut next = scenes.to_vec(); - let scene = next.remove(from); - let label = format!("Reorder audio scene `{}`", scene.name); - next.insert(to, scene); - edit(state, label, None, |draft| draft.set_audio_scenes(next)) - } - AudioAction::Remove(index) => { - let name = &scenes[index].name; - edit(state, format!("Remove audio scene `{name}`"), None, |draft| { - draft.remove_audio_scene(name) - }) - } + drop(state); + self.check(result); + } + + /// Rename the audio scene called `from` to `to`; Listen follows it. + fn rename_audio(&self, state: &mut State, from: &str, to: &str) -> anyhow::Result<()> { + if from == to { + return Ok(()); + } + edit(state, format!("Rename audio scene `{from}` to `{to}`"), None, |draft| { + draft.rename_audio_scene(from, to) + })?; + match self.engine.listening() { + Some((heard, _)) if heard == from => self.listen(state, Some(to)), + _ => Ok(()), } } @@ -250,3 +1272,600 @@ impl Panels { self.engine.listen(state, &self.runtime, listen) } } + +/// What a matrix row stands for. +#[derive(Clone, Copy)] +enum RowIcon<'a> { + Sound(&'a Channel), + Scene(&'a Master), +} + +/// The narrowest a strip gets that still holds a send dot, at its smallest, +/// for each of `masters` audio scenes. +fn strip_floor(masters: usize) -> f32 { + let count = masters as f32; + count * DOT[0] + (count - 1.0).max(0.0) * DOT_GAP + 2.0 * Parts::INSET.x +} + +/// Where a strip's parts sit in it, top to bottom. +struct Parts { + head: Rect, + /// The meter, scale, and fader. + fader: Rect, + value: Rect, + buttons: Rect, + sends: Rect, +} + +impl Parts { + /// Room between a strip's edges and its parts. + const INSET: Vec2 = vec2(10.0, 12.0); + + fn of(rect: Rect) -> Self { + let inner = rect.shrink2(Self::INSET); + let head = Rect::from_min_size(inner.min, vec2(inner.width(), 58.0)); + let sends = Rect::from_min_max(pos2(inner.left(), inner.bottom() - 33.0), inner.max); + let buttons = Rect::from_min_max( + pos2(inner.left(), sends.top() - 10.0 - 30.0), + pos2(inner.right(), sends.top() - 10.0), + ); + let value = Rect::from_min_max( + pos2(inner.left(), buttons.top() - 10.0 - 22.0), + pos2(inner.right(), buttons.top() - 10.0), + ); + let fader = Rect::from_min_max( + pos2(inner.left(), head.bottom() + 10.0), + pos2(inner.right(), value.top() - 10.0), + ); + Self { + head, + fader, + value, + buttons, + sends, + } + } +} + +/// The latest step's RMS and peak where they light a strip's meter. +fn reading(history: Option<&History>) -> (f32, f32) { + let level = latest(history); + (lit(level.rms()), lit(level.peak())) +} + +/// A strip's meter, `width` wide at `left` in its fader's room, from the +/// scale's foot up to unity, which is full scale. +fn meter(ui: &mut Ui, room: Rect, left: f32, width: f32, level: f32, peak: f32) { + let top = fader_y(room, UNITY); + let bottom = fader_y(room, 0.0); + let at = Rect::from_min_max(pos2(left, top), pos2(left + width, bottom)); + ui.put(at, desk_meter(level, peak).run(kit::Run::Up, at.height(), width)); +} + +/// Where `at` on the scale sits in a fader's room, as [`kit::Fader`] places +/// its cap. +fn fader_y(room: Rect, at: f32) -> f32 { + egui::lerp(room.bottom() - kit::Fader::INSET..=room.top() + kit::Fader::INSET, at) +} + +/// The scale's marks, 22 points wide at `left` in a fader's room, the boost +/// above unity too if `boost`. Below unity the marks drop their sign, as a +/// desk prints them. +fn scale(painter: &egui::Painter, room: Rect, left: f32, boost: bool) { + for (db, at) in SCALE { + if db > 0.0 && !boost { + continue; + } + let text = match db { + 0.0 => "0".to_string(), + db if db > 0.0 => format!("+{db:.0}"), + db => format!("{:.0}", -db), + }; + let ink = if db == 0.0 { kit::TEXT } else { kit::FAINT }; + painter.text( + pos2(left + 11.0, fader_y(room, at)), + Align2::CENTER_CENTER, + text, + kit::mono(9.0), + ink, + ); + } +} + +/// The gain edit setting `sound` to `db` decibels. +fn gain_change(sound: &Channel, db: f32, gesture: Option) -> Change { + let name = &sound.name; + Change::Audio { + name: name.clone(), + set: Setting::Gain(gain(db)), + label: format!("Set `{name}` gain"), + gesture, + } +} + +/// A field for a pan, `C` in the middle, `L50` halfway left, that drags or +/// takes a place typed. +fn pan_field(pan: &mut f32) -> egui::DragValue<'_> { + egui::DragValue::new(pan) + .range(-1.0..=1.0) + .speed(0.01) + .custom_formatter(|pan, _| match pan { + 0.0 => "Center".to_string(), + pan if pan < 0.0 => format!("L{:.0}", -pan * 100.0), + pan => format!("R{:.0}", pan * 100.0), + }) + .custom_parser(|text| { + let text = text.trim().to_ascii_uppercase(); + let percent = |side: &str| side.trim().parse::().ok().map(|n| n / 100.0); + match text.as_str() { + "C" | "CENTER" => Some(0.0), + _ if let Some(side) = text.strip_prefix('L') => percent(side).map(|n| -n), + _ if let Some(side) = text.strip_prefix('R') => percent(side), + _ => text.parse().ok(), + } + }) +} + +/// Whether the pointer double-clicked on `response`, which senses drags +/// alone, so a press starts a drag at once. +fn double_clicked(ui: &Ui, response: &egui::Response) -> bool { + response.contains_pointer() && ui.input(|input| input.pointer.button_double_clicked(egui::PointerButton::Primary)) +} + +/// A caption over a group of strips at `at`. +fn caption(ui: &Ui, at: egui::Pos2, text: &str) { + let galley = kit::caps_galley(ui.painter(), text, kit::display(11.0), kit::DIM, None); + ui.painter().galley(at, galley, kit::DIM); +} + +/// A tag reading `text` in `rect`, on `fill`. +fn tag(ui: &Ui, rect: Rect, text: &str, fill: egui::Color32, ink: egui::Color32) { + let galley = kit::caps_galley(ui.painter(), text, kit::display(11.0), ink, Some(rect.width() - 14.0)); + let at = Rect::from_center_size(rect.center(), vec2(galley.size().x + 14.0, rect.height())); + ui.painter().rect_filled(at, 4, fill); + ui.painter().galley(at.center() - galley.size() / 2.0, galley, ink); +} + +/// A tag reading `text` in `rect` that does something when clicked, known by +/// `id` wherever the strips move it. +fn tag_button(ui: &mut Ui, rect: Rect, text: &str, id: impl egui::AsIdSalt) -> egui::Response { + let galley = kit::caps_galley( + ui.painter(), + text, + kit::display(11.0), + kit::DIM, + Some(rect.width() - 14.0), + ); + let at = Rect::from_center_size(rect.center(), vec2(galley.size().x + 14.0, rect.height())); + let response = ui.interact(at, ui.id().with(id), Sense::click()); + response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, text)); + let (fill, ink) = match response.hovered() { + true => (kit::P3, kit::TEXT), + false => (kit::P2, kit::DIM), + }; + ui.painter() + .rect(at, 4, fill, Stroke::new(1.0, kit::LINE), StrokeKind::Inside); + ui.painter().galley(at.center() - galley.size() / 2.0, galley, ink); + response.on_hover_cursor(CursorIcon::PointingHand) +} + +/// The menu a right click on the audio scene at `index` opens over `rect`, +/// its name: rename it, move it, or remove it, as a scene's pad's does. +fn scene_menu(ui: &mut Ui, rect: Rect, index: usize, board: &Board, changes: &mut Vec) { + let scene = &board.masters[index]; + let name = &scene.name; + let response = ui.interact(rect, ui.id().with(("audio scene menu", name)), Sense::click()); + response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, format!("Audio scene {name}"))); + let response = kit::tip( + response, + &format!("Right-click to rename, move, or remove {name}"), + None, + ); + response.context_menu(|ui| { + let mut asked = None; + if ui.button("Rename").clicked() { + asked = Some(Change::Rename(name.clone())); + } + if ui.add_enabled(index > 0, egui::Button::new("Move left")).clicked() { + asked = Some(Change::Move(name.clone(), -1)); + } + let last = index + 1 == board.masters.len(); + if ui.add_enabled(!last, egui::Button::new("Move right")).clicked() { + asked = Some(Change::Move(name.clone(), 1)); + } + let remove = ui + .add_enabled(!scene.program, egui::Button::new("Remove")) + .on_disabled_hover_text("The stream hears it; put another on the stream first"); + if remove.clicked() { + asked = Some(Change::Remove(name.clone())); + } + if let Some(asked) = asked { + changes.push(asked); + ui.close(); + } + }); +} + +/// The dashed tile that adds an audio scene, saying whether it was clicked. +fn add_tile(ui: &mut Ui, rect: Rect) -> bool { + let response = ui.interact(rect, ui.id().with("add audio scene"), Sense::click()); + let response = kit::tip(response, "Add an audio scene", None); + response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "Add an audio scene")); + let ink = if response.hovered() { kit::TEXT } else { kit::FAINT }; + let stroke = Stroke::new(1.5, ink.gamma_multiply(0.6)); + ui.painter() + .rect_stroke(rect.shrink(1.0), 12, stroke, StrokeKind::Inside); + Icon::Add.paint(ui, rect, 18.0, ink); + response.clicked() +} + +/// A send dot: from a strip into an audio scene, and how it reaches it. +#[derive(Clone, Copy)] +struct Dot<'a> { + from: Strip<'a>, + to: &'a Master, + reach: &'a Reach, + /// Whether the dot carries its audio scene's letter, as a strip's do and + /// the matrix's, under their columns' names, do not. + letter: bool, +} + +impl Dot<'_> { + fn from(&self) -> &str { + match self.from { + Strip::Sound(name) | Strip::Scene(name) => name, + } + } + + /// Draw it in `at`, and toggle the send on a click. + fn ui(self, ui: &mut Ui, at: Rect, changes: &mut Vec) { + let (from, to, reach) = (self.from(), &self.to.name, self.reach); + let scene = matches!(self.from, Strip::Scene(_)); + let enabled = !matches!(reach, Reach::Own | Reach::Loop | Reach::Published); + let sense = if enabled { Sense::click() } else { Sense::hover() }; + let response = ui.interact(at, ui.id().with(("send", from, scene, to)), sense); + response.widget_info(|| { + let name = format!("Send {from} to {to}"); + egui::WidgetInfo::selected(egui::WidgetType::Checkbox, enabled, *reach == Reach::Sent, name) + }); + let response = kit::tip(response, &reach.says(from, to), None); + let letter = if self.letter { self.to.letter.as_str() } else { "" }; + paint_dot(ui.painter(), at, reach, letter, self.to.program); + if response.hovered() && enabled { + ui.painter() + .circle_stroke(at.center(), at.width() / 2.0 + 3.0, Stroke::new(2.0, kit::ACCENT)); + } + if response.on_hover_cursor(CursorIcon::PointingHand).clicked() { + changes.push(Change::Send { + scene, + from: from.to_string(), + to: to.clone(), + on: *reach != Reach::Sent, + }); + } + } +} + +/// A dot under a strip for each audio scene, in `rect`, from `from`, which +/// reaches each as `reach` says. +fn sends(ui: &mut Ui, rect: Rect, from: Strip<'_>, reach: &[Reach], board: &Board, changes: &mut Vec) { + ui.painter() + .hline(rect.x_range(), rect.top() + 0.5, Stroke::new(1.0, kit::LINE)); + let dots = dot_rects(rect, board.masters.len()); + for ((to, reach), at) in board.masters.iter().zip(reach).zip(dots) { + let send = Dot { + from, + to, + reach, + letter: true, + }; + send.ui(ui, at, changes); + } +} + +/// Where `count` send dots sit in `rect`: a row along its foot, centered, as +/// big as fit. +fn dot_rects(rect: Rect, count: usize) -> impl Iterator { + let n = count.max(1) as f32; + let size = ((rect.width() - (n - 1.0) * DOT_GAP) / n).clamp(DOT[0], DOT[1]); + let left = rect.center().x - (n * size + (n - 1.0) * DOT_GAP) / 2.0; + let y = rect.bottom() - size / 2.0; + (0..count).map(move |at| { + let x = left + at as f32 * (size + DOT_GAP) + size / 2.0; + Rect::from_center_size(pos2(x, y), Vec2::splat(size)) + }) +} + +/// A matrix cell: its dot, and the audio scene a sound comes through, or +/// stripes where an audio scene meets itself. +fn route_cell(ui: &mut Ui, cell: Rect, send: Dot<'_>, changes: &mut Vec) { + if *send.reach == Reach::Own { + let painter = ui.painter().with_clip_rect(cell.shrink(1.0)); + painter.extend(kit::stripes(cell, kit::LINE.gamma_multiply(0.5))); + return; + } + let center = match send.reach { + Reach::Through(_) => cell.center() - vec2(0.0, 6.0), + _ => cell.center(), + }; + let at = Rect::from_center_size(center, Vec2::splat(22.0)); + send.ui(ui, at, changes); + if let Reach::Through(held) = send.reach { + ui.painter().text( + pos2(cell.center().x, at.bottom() + 8.0), + Align2::CENTER_CENTER, + format!("through {held}"), + kit::ui(10.5), + kit::DIM, + ); + } +} + +/// Paint a send dot in `at`: filled where it is sent, the accent's fill on +/// the audio scene the stream carries; ringed where it is heard only through +/// another audio scene; a faint dot where it is not heard; a dash where it +/// cannot be sent. `letter` names its audio scene. +fn paint_dot(painter: &egui::Painter, at: Rect, reach: &Reach, letter: &str, program: bool) { + let center = at.center(); + let radius = at.width() / 2.0; + let font = kit::display((at.width() * 0.55).round()); + let ink = match reach { + Reach::Sent => { + let fill = if program { kit::ACCENT } else { kit::TEXT }; + painter.circle_filled(center, radius, fill); + if program { + kit::TEXT + } else { + egui::Color32::from_rgb(0x11, 0x11, 0x11) + } + } + Reach::Through(_) => { + painter.circle_stroke(center, radius - 1.0, Stroke::new(2.0, kit::TEXT)); + kit::TEXT + } + Reach::Not => { + painter.circle_filled(center, radius, kit::P2); + kit::FAINT + } + Reach::Own | Reach::Loop | Reach::Published => { + let half = (radius * 0.4).max(3.0); + painter.hline(center.x - half..=center.x + half, center.y, Stroke::new(2.0, kit::LINE)); + return; + } + }; + if !letter.is_empty() && at.width() >= 14.0 { + painter.text(center, Align2::CENTER_CENTER, letter, font, ink); + } +} + +/// The audio outputs Listen can play on, listed when its tag first names a +/// picked output or its menu opens, and again on asking. +#[derive(Default)] +pub(super) struct Speakers { + listing: Option, String>>, + pending: Option, String>>>, +} + +impl Speakers { + /// List the outputs again, in the background. + fn refresh(&mut self, runtime: &tokio::runtime::Handle) { + if self.pending.is_some() { + return; + } + let (send, recv) = std::sync::mpsc::channel(); + runtime.spawn(async move { + let _ = send.send(mix::speakers().await); + }); + self.pending = Some(recv); + } + + /// Take a listing that has come back. + fn poll(&mut self) { + if let Some(listing) = self.pending.as_ref().and_then(|pending| pending.try_recv().ok()) { + self.listing = Some(listing); + self.pending = None; + } + } + + /// The output `speaker` names, as the listing calls it, listing the + /// outputs first if nothing has yet; marked if the listing lacks it, + /// such as a headset since unplugged. + fn named(&mut self, runtime: &tokio::runtime::Handle, speaker: Option<&str>) -> String { + self.poll(); + let Some(id) = speaker else { + return "System default".to_string(); + }; + match &self.listing { + Some(Ok(speakers)) => speakers + .iter() + .find(|found| found.id == id) + .map_or_else(|| format!("{id} (not found)"), |found| found.name.clone()), + Some(Err(_)) => id.to_string(), + None => { + self.refresh(runtime); + id.to_string() + } + } + } + + /// The outputs to pick `speaker` from, or the system default; whether + /// the pick changed. + fn menu(&mut self, ui: &mut Ui, speaker: &mut Option) -> bool { + self.poll(); + let before = speaker.clone(); + ui.label(kit::caps("Listen on", kit::display(11.0)).color(kit::DIM)); + ui.selectable_value(speaker, None, "System default"); + match &self.listing { + Some(Ok(speakers)) => { + for found in speakers { + ui.selectable_value(speaker, Some(found.id.clone()), &found.name); + } + } + Some(Err(err)) => { + ui.colored_label(kit::ERR, format!("Cannot list the outputs: {err}")); + } + None => { + ui.spinner(); + } + } + *speaker != before + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The taper runs from silence to the most boost, unity where the fader + /// marks it, and back again. + #[test] + fn the_taper_runs_both_ways() { + assert_eq!(taper(MUTE_DB), 0.0); + assert_eq!(taper(-200.0), 0.0, "quieter than silence is silence"); + assert_eq!(taper(0.0), UNITY); + assert_eq!(taper(12.0), 1.0); + assert_eq!(taper(40.0), 1.0, "a scene file's bigger boost tops the fader out"); + let mut last = -1.0; + for step in 0..=720 { + let db = MUTE_DB + step as f32 * 0.1; + let at = taper(db); + assert!(at > last, "rises at {db} dB"); + last = at; + assert!((untaper(at) - db).abs() < 1e-3, "{db} dB comes back"); + } + assert_eq!(untaper(0.0), MUTE_DB); + } + + /// However many audio scenes there are, a strip at its narrowest holds + /// each one's dot within it, so a click never lands on a neighbour's. + #[test] + fn every_send_dot_fits_its_strip() { + for masters in 0..=16 { + let floor = strip_floor(masters); + for width in [NARROW.max(floor), WIDE.max(floor), MASTER[0].max(floor)] { + let strip = Rect::from_min_size(pos2(0.0, 0.0), vec2(width, 400.0)); + let room = Parts::of(strip).sends; + let dots: Vec = dot_rects(room, masters).collect(); + assert_eq!(dots.len(), masters); + for (at, dot) in dots.iter().enumerate() { + assert!( + room.expand(1e-3).contains_rect(*dot), + "{masters} dots at {width}: {dot:?} in {room:?}" + ); + assert!(dot.width() >= DOT[0]); + if let Some(next) = dots.get(at + 1) { + assert!(dot.right() < next.left(), "{masters} dots at {width} overlap"); + } + } + } + } + } + + /// A desk meter turns hot at the same level as LIVE's, which places + /// levels evenly in decibels from silence and keeps the meter's own + /// threshold. + #[test] + fn meters_agree_on_hot() { + let hot = gain(HOT_DB); + assert!((super::super::levels::position(hot) - kit::Meter::HOT).abs() < 1e-5); + assert!((untaper(lit(hot) * UNITY) - HOT_DB).abs() < 1e-3); + assert!(lit(gain(HOT_DB - 1.0)) < lit(hot) && lit(hot) < lit(gain(HOT_DB + 1.0))); + } + + /// A meter lights to its top at full scale, which is unity on the scale. + #[test] + fn full_scale_lights_the_meter() { + assert_eq!(lit(1.0), 1.0); + assert_eq!(lit(0.0), 0.0); + assert!((lit(0.5) - taper(-6.0206) / UNITY).abs() < 1e-3); + } + + /// "Mics" holds the microphone and "Stream" holds Mics, so the microphone + /// reaches Stream through Mics, and "Headphones" holds Stream and the + /// microphone too, which it still hears once. + fn collection() -> Collection { + let mut collection = Collection::default(); + let scene = |name: &str, sounds: &[&str], scenes: &[&str]| AudioScene { + sounds: sounds.iter().map(|name| name.to_string()).collect(), + scenes: scenes.iter().map(|name| name.to_string()).collect(), + ..AudioScene::new(name) + }; + collection.audio_scenes = vec![ + AudioScene { + program: true, + ..scene("Stream", &["music"], &["Mics"]) + }, + scene("Mics", &["mic"], &[]), + scene("Headphones", &["mic"], &["Stream"]), + scene("Recording", &[], &[]), + ]; + collection + } + + fn reach(collection: &Collection, from: Strip<'_>, to: &str) -> Reach { + Reach::of(collection, from, collection.audio_scene(to).unwrap()) + } + + /// A dot is filled where a sound is sent, even if it also comes through + /// another audio scene, ringed where it comes only through one, and empty + /// where it is not heard. + #[test] + fn a_sound_reaches_by_any_route() { + let collection = collection(); + let mic = Strip::Sound("mic"); + assert_eq!(reach(&collection, mic, "Mics"), Reach::Sent); + assert_eq!(reach(&collection, mic, "Stream"), Reach::Through("Mics".into())); + assert_eq!( + reach(&collection, mic, "Headphones"), + Reach::Sent, + "sent and through Stream" + ); + assert_eq!(reach(&collection, mic, "Recording"), Reach::Not); + let music = Strip::Sound("music"); + assert_eq!(reach(&collection, music, "Headphones"), Reach::Through("Stream".into())); + assert_eq!(reach(&collection, music, "Mics"), Reach::Not); + assert!(!reach(&collection, Strip::Sound("other"), "Stream").heard()); + } + + /// A sound published independently is heard in no audio scene, however + /// it is sent, as the mixes leave it out; nor through one that holds it. + #[test] + fn a_published_sound_is_heard_nowhere() { + let mut collection = collection(); + collection.published.insert("mic".into()); + for to in ["Mics", "Stream", "Headphones", "Recording"] { + assert_eq!(reach(&collection, Strip::Sound("mic"), to), Reach::Published, "{to}"); + } + let music = Strip::Sound("music"); + assert_eq!(reach(&collection, music, "Stream"), Reach::Sent, "others are as before"); + } + + /// An audio scene cannot be sent into itself, nor into one it holds, + /// even through another; it can reach one through another that holds it. + #[test] + fn an_audio_scene_never_holds_itself() { + let collection = collection(); + let mics = Strip::Scene("Mics"); + assert_eq!(reach(&collection, mics, "Mics"), Reach::Own); + assert_eq!(reach(&collection, mics, "Stream"), Reach::Sent); + assert_eq!(reach(&collection, mics, "Headphones"), Reach::Through("Stream".into())); + assert_eq!(reach(&collection, mics, "Recording"), Reach::Not); + let headphones = Strip::Scene("Headphones"); + assert_eq!(reach(&collection, headphones, "Stream"), Reach::Loop); + assert_eq!(reach(&collection, headphones, "Mics"), Reach::Loop, "through Stream"); + assert_eq!(reach(&collection, Strip::Scene("Stream"), "Headphones"), Reach::Sent); + } + + /// The matrix's foot says what each audio scene holds. + #[test] + fn what_an_audio_scene_holds() { + let collection = collection(); + let holds = |name: &str| holds(collection.audio_scene(name).unwrap()); + assert_eq!(holds("Stream"), "1 sound and Mics"); + assert_eq!(holds("Recording"), "Nothing"); + let mut scene = AudioScene::new("All"); + scene.sounds = ["a", "b"].map(String::from).into(); + scene.scenes = ["Mics", "Stream"].map(String::from).into(); + assert_eq!(super::holds(&scene), "2 sounds, Mics and Stream"); + } +} diff --git a/rs/moq-studio/src/ui/shell.rs b/rs/moq-studio/src/ui/shell.rs index cc82d16a..c7fb51b0 100644 --- a/rs/moq-studio/src/ui/shell.rs +++ b/rs/moq-studio/src/ui/shell.rs @@ -262,11 +262,7 @@ impl Panels { } Page::Build => self.build(ui, fx, stage, snapshot), // The sound slot holds the live picture instead. - Page::Mix => region(ui, stage, |ui| { - self.audio_scenes(ui); - ui.separator(); - self.audio(ui); - }), + Page::Mix => self.mix(ui, stage), // The program's card in "replay what" is the live picture. Page::Review => self.review(ui, fx, stage, snapshot.shape), } @@ -750,7 +746,10 @@ impl Panels { }); }); }); - let action = action.or_else(|| self.rename_sheet(ui.ctx())); + let action = action.or_else(|| { + let (from, to) = rename_sheet(ui.ctx(), "rename scene", &mut self.renaming_pad)?; + Some(PadAction::Renamed(from, to)) + }); let Some(action) = action else { return }; let engine = self.engine.clone(); let mut state = engine.state.lock().unwrap(); @@ -792,7 +791,6 @@ impl Panels { Ok(()) } PadAction::Renamed(name, to) => at(&state, &name).and_then(|index| { - let to = to.trim().to_string(); if to == name { return Ok(()); } @@ -829,32 +827,6 @@ impl Panels { } } - /// The sheet renaming a scene, while one is renamed, and the rename it - /// asked for. - fn rename_sheet(&mut self, ctx: &egui::Context) -> Option { - let (from, mut to) = self.renaming_pad.clone()?; - let mut asked = None; - let title = format!("Rename {from}"); - let closed = kit::Sheet::new(egui::Id::new("rename scene"), &title).show(ctx, |ui| { - let field = ui.add(egui::TextEdit::singleline(&mut to).desired_width(f32::INFINITY)); - if !field.has_focus() && !field.lost_focus() { - field.request_focus(); - } - let entered = field.lost_focus() && ui.input(|input| input.key_pressed(egui::Key::Enter)); - ui.add_space(8.0); - ui.with_layout(egui::Layout::right_to_left(Align::Center), |ui| { - if ui.button("Rename").clicked() || entered { - asked = Some(PadAction::Renamed(from.clone(), to.clone())); - } - }); - }); - self.renaming_pad = match closed || asked.is_some() { - true => None, - false => Some((from, to)), - }; - asked - } - /// The pad of the scene at `index`, or the one that adds a scene past /// the last, `width` wide, and what it asked for. fn strip_pad(&mut self, ui: &mut Ui, snapshot: &Snapshot, index: usize, width: f32) -> Option { @@ -1428,14 +1400,33 @@ pub(super) fn bar(ui: &Ui, fx: &mut Fx, rect: Rect) { } } -/// A page's room: what `add` draws, scrolled, inside `rect`. -fn region(ui: &mut Ui, rect: Rect, add: impl FnOnce(&mut Ui)) { - ui.scope_builder(UiBuilder::new().max_rect(rect.shrink(MARGIN + 4.0)), |ui| { - egui::ScrollArea::both() - .id_salt(("page", rect.min.x as i32)) - .auto_shrink(false) - .show(ui, add); +/// The sheet renaming what `renaming` names, while it names one, as the name +/// it holds and the name typed so far; the rename once asked for. +pub(super) fn rename_sheet( + ctx: &egui::Context, + id: &str, + renaming: &mut Option<(String, String)>, +) -> Option<(String, String)> { + let (from, to) = renaming.as_mut()?; + let mut asked = None; + let title = format!("Rename {from}"); + let closed = kit::Sheet::new(egui::Id::new(id), &title).show(ctx, |ui| { + let field = ui.add(egui::TextEdit::singleline(to).desired_width(f32::INFINITY)); + if !field.has_focus() && !field.lost_focus() { + field.request_focus(); + } + let entered = field.lost_focus() && ui.input(|input| input.key_pressed(egui::Key::Enter)); + ui.add_space(8.0); + ui.with_layout(egui::Layout::right_to_left(Align::Center), |ui| { + if ui.button("Rename").clicked() || entered { + asked = Some((from.clone(), to.trim().to_string())); + } + }); }); + if closed || asked.is_some() { + *renaming = None; + } + asked } /// The pages' font and the room either side of each name, smaller when