diff --git a/CLAUDE.md b/CLAUDE.md index b5c917a3..9559f795 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1 +1,5 @@ Read `AGENTS.md`: it is the entry point for agents in this repo. + +New workspace crates must include `[lints] workspace = true` in `Cargo.toml` to inherit +`[workspace.lints]`. Production crate roots must declare the required crate-level lint attributes +before modules or other crate items; crate-level `//!` documentation may precede them. diff --git a/Cargo.toml b/Cargo.toml index d2a9c4fc..6f14fa4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,9 +70,11 @@ image = { version = "0.25", default-features = false, features = ["png"] } unsafe_code = "forbid" [workspace.lints.clippy] +cognitive_complexity = "deny" +wildcard_imports = "deny" dbg_macro = "warn" todo = "warn" -too_many_arguments = "allow" +too_many_arguments = "deny" type_complexity = "allow" [profile.dev.package."*"] diff --git a/clippy.toml b/clippy.toml index 6244ea8c..2a016f24 100644 --- a/clippy.toml +++ b/clippy.toml @@ -6,3 +6,4 @@ allow-indexing-slicing-in-tests = true disallowed-types = [ { path = "egui::ComboBox", reason = "use craft_ui::ComboBox: egui's combo box menu can't grow back while open (egui#5225)" }, ] +cognitive-complexity-threshold = 30 diff --git a/crates/au-host/Cargo.toml b/crates/au-host/Cargo.toml index af793163..122c4919 100644 --- a/crates/au-host/Cargo.toml +++ b/crates/au-host/Cargo.toml @@ -20,7 +20,9 @@ log.workspace = true unsafe_code = "deny" [lints.clippy] +cognitive_complexity = "deny" +wildcard_imports = "deny" dbg_macro = "warn" todo = "warn" -too_many_arguments = "allow" +too_many_arguments = "deny" type_complexity = "allow" diff --git a/crates/clap-host/Cargo.toml b/crates/clap-host/Cargo.toml index 96c7de7c..27eaf561 100644 --- a/crates/clap-host/Cargo.toml +++ b/crates/clap-host/Cargo.toml @@ -32,7 +32,9 @@ libloading = "0.9" unsafe_code = "deny" [lints.clippy] +cognitive_complexity = "deny" +wildcard_imports = "deny" dbg_macro = "warn" todo = "warn" -too_many_arguments = "allow" +too_many_arguments = "deny" type_complexity = "allow" diff --git a/crates/clap-host/src/ffi.rs b/crates/clap-host/src/ffi.rs index ab589957..8ae25c52 100644 --- a/crates/clap-host/src/ffi.rs +++ b/crates/clap-host/src/ffi.rs @@ -54,7 +54,7 @@ pub(crate) use events::Event; #[cfg(test)] use events::transport_event; pub(crate) use gui::GuiLink; -pub(crate) use instance::{Instance, NotePort, PortBuffers, PortInfo, RawParam}; +pub(crate) use instance::{Instance, NotePort, PortBuffers, PortInfo, ProcessInput, RawParam}; /// Most plugins one factory may report (hostile-input cap). const MAX_PLUGINS_PER_FACTORY: u32 = 1024; diff --git a/crates/clap-host/src/ffi/bundle.rs b/crates/clap-host/src/ffi/bundle.rs index e3cdeddc..503b6391 100644 --- a/crates/clap-host/src/ffi/bundle.rs +++ b/crates/clap-host/src/ffi/bundle.rs @@ -1,7 +1,12 @@ use super::gui::{GuiInner, GuiLink}; use super::host::{HostState, host_get_extension, host_request_callback, host_request_process, host_request_restart}; use super::instance::Instance; -use super::*; +use super::{ + Arc, CLAP_EXT_AUDIO_PORTS, CLAP_EXT_GUI, CLAP_EXT_LATENCY, CLAP_EXT_NOTE_PORTS, CLAP_EXT_PARAMS, CLAP_EXT_STATE, CLAP_PLUGIN_FACTORY_ID, + CLAP_VERSION, CString, ClapError, EVENT_CAP, HashMap, MAX_FEATURES, MAX_PLUGINS_PER_FACTORY, Mutex, OnceLock, Path, PathBuf, PoisonError, c_void, + clap_host, clap_plugin_audio_ports, clap_plugin_entry, clap_plugin_factory, clap_plugin_gui, clap_plugin_latency, clap_plugin_note_ports, + clap_plugin_params, clap_plugin_state, clap_version_is_compatible, read_cstr, scan, +}; // ---- loaded binaries ----------------------------------------------------------------------- diff --git a/crates/clap-host/src/ffi/events.rs b/crates/clap-host/src/ffi/events.rs index 1bc2f3f8..d3bb04fe 100644 --- a/crates/clap-host/src/ffi/events.rs +++ b/crates/clap-host/src/ffi/events.rs @@ -1,4 +1,10 @@ -use super::*; +use super::{ + CLAP_BEATTIME_FACTOR, CLAP_CORE_EVENT_SPACE_ID, CLAP_EVENT_MIDI, CLAP_EVENT_NOTE_OFF, CLAP_EVENT_NOTE_ON, CLAP_EVENT_PARAM_VALUE, + CLAP_EVENT_TRANSPORT, CLAP_SECTIME_FACTOR, CLAP_TRANSPORT_HAS_BEATS_TIMELINE, CLAP_TRANSPORT_HAS_SECONDS_TIMELINE, CLAP_TRANSPORT_HAS_TEMPO, + CLAP_TRANSPORT_HAS_TIME_SIGNATURE, CLAP_TRANSPORT_IS_PLAYING, CLAP_TRANSPORT_IS_RECORDING, MAX_STATE_BYTES, Transport, c_void, clap_event_header, + clap_event_midi, clap_event_note, clap_event_param_value, clap_event_transport, clap_input_events, clap_istream, clap_ostream, + clap_output_events, +}; // ---- events -------------------------------------------------------------------------------- diff --git a/crates/clap-host/src/ffi/gui.rs b/crates/clap-host/src/ffi/gui.rs index 5a911a95..92619742 100644 --- a/crates/clap-host/src/ffi/gui.rs +++ b/crates/clap-host/src/ffi/gui.rs @@ -1,6 +1,9 @@ use super::events::ostream_write; use super::host::HostState; -use super::*; +use super::{ + Arc, CStr, CString, Mutex, Ordering, PoisonError, c_void, clap_ostream, clap_plugin, clap_plugin_gui, clap_plugin_state, clap_window, + clap_window_handle, hostwin, +}; // ---- editor (clap.gui) --------------------------------------------------------------------- diff --git a/crates/clap-host/src/ffi/host.rs b/crates/clap-host/src/ffi/host.rs index 882c0377..408546dc 100644 --- a/crates/clap-host/src/ffi/host.rs +++ b/crates/clap-host/src/ffi/host.rs @@ -1,4 +1,8 @@ -use super::*; +use super::{ + AtomicBool, AtomicU64, CLAP_EXT_GUI, CLAP_EXT_LATENCY, CLAP_EXT_LOG, CLAP_EXT_PARAMS, CLAP_EXT_STATE, CLAP_LOG_ERROR, CLAP_LOG_FATAL, + CLAP_LOG_WARNING, CStr, EDIT_CAP, Mutex, Ordering, c_char, c_void, clap_host, clap_host_gui, clap_host_latency, clap_host_log, clap_host_params, + clap_host_state, clap_id, clap_log_severity, clap_param_clear_flags, clap_param_rescan_flags, read_cstr, +}; // ---- host side ----------------------------------------------------------------------------- diff --git a/crates/clap-host/src/ffi/instance.rs b/crates/clap-host/src/ffi/instance.rs index fec2bd44..3a537fb6 100644 --- a/crates/clap-host/src/ffi/instance.rs +++ b/crates/clap-host/src/ffi/instance.rs @@ -2,7 +2,12 @@ use super::bundle::Bundle; use super::events::{Event, RawEvent, ReadCursor, in_events_get, in_events_size, istream_read, out_events_try_push, transport_event}; use super::gui::GuiLink; use super::host::HostState; -use super::*; +use super::{ + Arc, CLAP_AUDIO_PORT_IS_MAIN, CLAP_PROCESS_ERROR, CStr, ClapError, MAX_PARAMS, MAX_PORT_CHANNELS, MAX_PORTS, Ordering, Transport, c_char, c_void, + clap_audio_buffer, clap_audio_port_info, clap_host, clap_input_events, clap_istream, clap_note_port_info, clap_output_events, clap_param_info, + clap_plugin, clap_plugin_audio_ports, clap_plugin_latency, clap_plugin_note_ports, clap_plugin_params, clap_plugin_state, clap_process, + read_name, +}; // ---- audio buffers ------------------------------------------------------------------------- @@ -312,16 +317,8 @@ impl Instance { /// Runs one `clap_process` call over `frames` frames. Events must be sorted by time; their /// times are made relative to `offset`. Returns the CLAP process status. - pub fn process( - &mut self, - frames: usize, - steady_time: i64, - inputs: &mut PortBuffers, - outputs: &mut PortBuffers, - events: &[Event], - offset: u32, - transport: Option<&Transport>, - ) -> Result { + pub fn process(&mut self, input: ProcessInput<'_>) -> Result { + let ProcessInput { frames, steady_time, inputs, outputs, events, offset, transport } = input; if !self.active { return Err(ClapError::Process("not active".into())); } @@ -386,6 +383,16 @@ impl Instance { } } +pub(crate) struct ProcessInput<'a> { + pub frames: usize, + pub steady_time: i64, + pub inputs: &'a mut PortBuffers, + pub outputs: &'a mut PortBuffers, + pub events: &'a [Event], + pub offset: u32, + pub transport: Option<&'a Transport>, +} + impl Drop for Instance { fn drop(&mut self) { // Editors first (CLAP: destroy the GUI before the plugin), and no handle may call in after. diff --git a/crates/clap-host/src/plugin.rs b/crates/clap-host/src/plugin.rs index 4b685cf8..8e048e2a 100644 --- a/crates/clap-host/src/plugin.rs +++ b/crates/clap-host/src/plugin.rs @@ -1,6 +1,6 @@ //! `ClapPlugin`: a loaded CLAP instance behind the safe `soundcraft_dsp::Plugin` trait. -use crate::ffi::{EVENT_CAP, Event, GuiLink, Instance, NotePort, PortBuffers}; +use crate::ffi::{EVENT_CAP, Event, GuiLink, Instance, NotePort, PortBuffers, ProcessInput}; use clap_sys::ext::note_ports::{CLAP_NOTE_DIALECT_CLAP, CLAP_NOTE_DIALECT_MIDI}; use soundcraft_dsp::{Plugin, PluginEditor, PluginInfo, Transport}; use std::sync::Arc; @@ -231,7 +231,15 @@ impl Plugin for ClapPlugin { let k = self.queue.iter().position(|e| e.time() >= end).unwrap_or(self.queue.len()); let offset = u32::try_from(start).unwrap_or(u32::MAX); let events = self.queue.get(..k).unwrap_or(&[]); - let r = self.inst.process(n, self.steady, &mut self.inputs, &mut self.outputs, events, offset, self.transport.as_ref()); + let r = self.inst.process(ProcessInput { + frames: n, + steady_time: self.steady, + inputs: &mut self.inputs, + outputs: &mut self.outputs, + events, + offset, + transport: self.transport.as_ref(), + }); self.queue.drain(..k); self.steady = self.steady.saturating_add(i64::try_from(n).unwrap_or(0)); match (r, self.out_main) { diff --git a/crates/engine/src/io/bundle.rs b/crates/engine/src/io/bundle.rs index 27bad519..cfe6e0af 100644 --- a/crates/engine/src/io/bundle.rs +++ b/crates/engine/src/io/bundle.rs @@ -1,6 +1,6 @@ use super::render::sanitize_name; use super::session::decode_source; -use super::*; +use super::{Arc, EncodeOptions, Engine, EngineError, FileFormat, OsStr, Path, Result, Session, SourceAudio, read_bundle, write_file}; // ─── Hosts without a filesystem (the browser) ───────────────────────────────────────────────── diff --git a/crates/engine/src/io/import.rs b/crates/engine/src/io/import.rs index f34b5f29..a2011def 100644 --- a/crates/engine/src/io/import.rs +++ b/crates/engine/src/io/import.rs @@ -1,5 +1,8 @@ use super::render::sanitize_name; -use super::*; +use super::{ + Arc, AudioBuffer, ChannelFormat, Clip, Engine, EngineError, FileFormat, OsStr, Path, PathBuf, Result, Samples, Session, Source, SourceAudio, + SourceId, TrackId, TrackKind, Value, file_found, file_key, json, read_file, stored_path, +}; /// Largest import we accept (samples per channel × channels), ~4 GiB of f32. pub(crate) const MAX_IMPORT_SAMPLES: usize = 1 << 30; diff --git a/crates/engine/src/io/midi.rs b/crates/engine/src/io/midi.rs index 405f1282..f97b5709 100644 --- a/crates/engine/src/io/midi.rs +++ b/crates/engine/src/io/midi.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{ChannelFormat, Clip, ClipContent, Engine, EngineError, Result, Samples, TrackId, TrackKind}; /// Most meter changes a MIDI file may bring into the tempo map; scores have a handful. pub(super) const MAX_MIDI_METERS: usize = 10_000; diff --git a/crates/engine/src/io/record.rs b/crates/engine/src/io/record.rs index ece38abf..32e20420 100644 --- a/crates/engine/src/io/record.rs +++ b/crates/engine/src/io/record.rs @@ -1,7 +1,7 @@ use super::import::add_source; #[cfg(test)] use super::session::{save_copy, save_session}; -use super::*; +use super::{Clip, Engine, EngineError, FileFormat, OsStr, PathBuf, Range, Samples, TrackId, read_file}; /// A recording's plan, made when it starts: each record-enabled track's take file, where the /// takes start on the timeline, the loop length they split into when loop recording, and the @@ -117,6 +117,7 @@ pub fn live_span(start: Samples, pass: Option, punch: Option, le #[cfg(test)] mod record_tests { + use super::super::{AudioBuffer, Path, plan_takes}; use super::*; use serde_json::json; diff --git a/crates/engine/src/io/render.rs b/crates/engine/src/io/render.rs index cb8b42c5..5d0f0f94 100644 --- a/crates/engine/src/io/render.rs +++ b/crates/engine/src/io/render.rs @@ -1,7 +1,7 @@ use super::import::add_source; #[cfg(test)] use super::midi::{MAX_MIDI_METERS, import_midi_bytes}; -use super::*; +use super::{AudioBuffer, Clip, ClipContent, Engine, EngineError, FileFormat, Range, Result, Samples, Session, TrackId, TrackKind}; /// Consolidate `r` on a track into one new clip backed by a rendered source. The clip covers the /// whole range, silence included; a range too long to render is refused rather than shortened. diff --git a/crates/engine/src/io/session.rs b/crates/engine/src/io/session.rs index 0c934f58..8fcd47f8 100644 --- a/crates/engine/src/io/session.rs +++ b/crates/engine/src/io/session.rs @@ -2,7 +2,9 @@ use super::bundle::save_session_bundle; #[cfg(test)] use super::import::add_source; use super::import::{MAX_IMPORT_SAMPLES, at_rate, relink_videos}; -use super::*; +use super::{ + Arc, Engine, EngineError, OsStr, Path, PathBuf, Result, Session, SourceAudio, file_key, read_file, read_text, stored_path, write_session, +}; /// Save the session to `path` (a `.scraft` file). Audio that only exists in memory is written /// as 32-bit float WAV into `Audio Files/` next to it. Returns how many audio files were written. @@ -92,7 +94,9 @@ pub(crate) fn decode_file(files: &mut DecodedFiles, p: &Path, rate: u32) -> std: #[cfg(test)] mod save_tests { + use super::super::{AudioBuffer, ChannelFormat, Clip, Engine, FileFormat, Range, TrackKind, back_up, copy_new}; use super::*; + use serde_json::json; #[test] fn autosave_copy_leaves_document_untouched_and_reuses_audio() { @@ -315,7 +319,9 @@ mod save_tests { #[cfg(test)] mod open_tests { + use super::super::{AudioBuffer, ChannelFormat, Clip, EncodeOptions, Engine, FileFormat, SourceId, TrackKind}; use super::*; + use serde_json::json; #[test] fn a_file_named_by_many_sources_is_decoded_once() { @@ -428,7 +434,9 @@ mod open_tests { #[cfg(all(test, unix))] mod pipe_tests { + use super::super::{AudioBuffer, FileFormat}; use super::*; + use serde_json::json; #[test] fn a_pipe_named_as_media_is_refused_not_waited_on() { diff --git a/crates/engine/src/io/strip.rs b/crates/engine/src/io/strip.rs index 6591b873..ec808241 100644 --- a/crates/engine/src/io/strip.rs +++ b/crates/engine/src/io/strip.rs @@ -1,6 +1,6 @@ #[cfg(test)] use super::import::add_source; -use super::*; +use super::{Arc, AtomicBool, Clip, EngineError, Ordering, Range, Result, Samples, Session, TrackId}; /// Limit a range used for rendering to the track's material (plus a second), so a huge /// selection never renders hours of silence. @@ -244,7 +244,9 @@ fn non_silent( #[cfg(test)] mod strip_tests { + use super::super::{AudioBuffer, Engine, FileFormat, Value}; use super::*; + use serde_json::json; /// A stereo track with `length`-long clips at `starts`, each playing a second of steady tone. fn tones(starts: &[Samples], length: Samples) -> (Engine, TrackId) { diff --git a/crates/mix/src/automation.rs b/crates/mix/src/automation.rs index 20b20225..12a17f9e 100644 --- a/crates/mix/src/automation.rs +++ b/crates/mix/src/automation.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{AutoParam, Samples, Session, TICKS_PER_QUARTER, Track, Transport, db_to_gain}; pub(crate) fn transport_at(s: &Session, pos: Samples, playing: bool, recording: bool) -> Transport { let sr = s.sample_rate; diff --git a/crates/mix/src/clips.rs b/crates/mix/src/clips.rs index 03449de8..807f64a7 100644 --- a/crates/mix/src/clips.rs +++ b/crates/mix/src/clips.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{Bound, Clip, ClipContent, Plugin, Range, Samples, Session, db_to_gain}; /// Clip Effects: an EQ and a compressor per clip, configured from `edit.values` /// (`clip_fx..eq.`, `clip_fx..comp.`, `clip_fx..gain`). diff --git a/crates/mix/src/instances.rs b/crates/mix/src/instances.rs index 5050007e..8bb5dbf9 100644 --- a/crates/mix/src/instances.rs +++ b/crates/mix/src/instances.rs @@ -1,5 +1,5 @@ use super::strip::strip_channels; -use super::*; +use super::{Plugin, Session, TrackId, main_channels}; /// Slot index used for a track's instrument plugin in [`InstanceSpec`] and [`PreparedInstance`] /// (insert slots use their index). diff --git a/crates/mix/src/lib.rs b/crates/mix/src/lib.rs index b32202a0..3dbd9d0c 100644 --- a/crates/mix/src/lib.rs +++ b/crates/mix/src/lib.rs @@ -59,7 +59,6 @@ use soundcraft_model::{ use soundcraft_time::{Range, Samples, TICKS_PER_QUARTER}; use std::collections::HashMap; use std::ops::Bound; -use std::sync::Arc; mod automation; mod clips; @@ -82,7 +81,9 @@ pub use offline::{ pub use pan::itu_stereo_fold; use pan::{PanSpec, apply_matrix, apply_matrix_span, build_matrix}; use routing::{processing_order, structure_fingerprint}; -use strip::{BusBuf, LiveInput, SlotCtx, Strip, apply_params, contain_failure, note_insert_latency, process_strip, strip_channels, strip_format}; +use strip::{ + Block, BusBuf, LiveInput, SlotCtx, Strip, apply_params, contain_failure, note_insert_latency, process_strip, strip_channels, strip_format, +}; #[cfg(test)] use strip::{DelayLine, MAX_LATENCY}; use threads::run_strips; @@ -349,6 +350,44 @@ impl MixEngine { /// Render `frames` (≤ max_block) samples starting at `pos` into `out` (≥ 2 channels). pub fn render(&mut self, s: &Session, pos: Samples, frames: usize, out: &mut [Vec]) { let frames = frames.min(self.max_block); + self.begin_block(s, pos, frames); + let blk = Block { pos, frames, any_solo: s.tracks.iter().any(|t| t.mixer.solo && !t.inactive) }; + let order = std::mem::take(&mut self.order); + // Phase 1: independent strips (audio, instrument, MIDI) in parallel. + let mut work = std::mem::take(&mut self.work); + work.clear(); + for &i in &order { + let Some(t) = s.tracks.get(i).filter(|t| is_live(t) && !is_aux(t)) else { continue }; + if let Some(st) = self.strips.remove(&t.id) { + work.push((i, st)); + } + } + for (_, st) in work.iter_mut() { + if st.scratch.len() < self.max_block { + st.scratch.resize(self.max_block, 0.0); + } + } + let busses = std::mem::take(&mut self.busses); + let live_in = LiveInput { input: &self.input, monitor_only: self.monitor_only, recording: self.recording }; + run_strips(&mut work, |(i, st)| { + if let Some(t) = s.tracks.get(*i) { + process_strip(s, t, st, &busses, &live_in, blk); + } + }); + self.busses = busses; + // Delay compensation: align every strip to the slowest. Direct-to-main outputs also wait + // for the slowest aux return. Aux latency has to be read before those outputs are routed, + // or the first block (and a one-block bounce probe) still sees zero. + self.compensate_delay(s, &mut work, blk); + self.work = work; + self.render_auxes(s, &order, blk); + self.order = order; + self.render_masters(s, blk); + self.write_output(s, blk, out); + } + + /// Silence what a seek leaves ringing, sync to the session and clear the block's accumulators. + fn begin_block(&mut self, s: &Session, pos: Samples, frames: usize) { if pos != self.last_end { // Seek: silence held notes and ringing plugins. for st in self.strips.values_mut() { @@ -369,35 +408,11 @@ impl MixEngine { for c in &mut self.main { c.iter_mut().take(frames).for_each(|x| *x = 0.0); } - let any_solo = s.tracks.iter().any(|t| t.mixer.solo && !t.inactive); - let order = std::mem::take(&mut self.order); - let live = |t: &Track| !(t.kind == TrackKind::Master || t.inactive || t.kind == TrackKind::Vca); - let is_aux = |t: &Track| matches!(t.kind, TrackKind::Aux | TrackKind::Folder); - // Phase 1: independent strips (audio, instrument, MIDI) in parallel. - let mut work = std::mem::take(&mut self.work); - work.clear(); - for &i in &order { - let Some(t) = s.tracks.get(i).filter(|t| live(t) && !is_aux(t)) else { continue }; - if let Some(st) = self.strips.remove(&t.id) { - work.push((i, st)); - } - } - for (_, st) in work.iter_mut() { - if st.scratch.len() < self.max_block { - st.scratch.resize(self.max_block, 0.0); - } - } - let busses = std::mem::take(&mut self.busses); - let live_in = LiveInput { input: &self.input, monitor_only: self.monitor_only, recording: self.recording }; - run_strips(&mut work, |(i, st)| { - if let Some(t) = s.tracks.get(*i) { - process_strip(s, t, st, &busses, &live_in, pos, frames, any_solo); - } - }); - self.busses = busses; - // Delay compensation: align every strip to the slowest. Direct-to-main outputs also wait - // for the slowest aux return. Aux latency has to be read before those outputs are routed, - // or the first block (and a one-block bounce probe) still sees zero. + } + + /// Delay compensation for the independent strips (`work`), then route them back into the engine. + fn compensate_delay(&mut self, s: &Session, work: &mut Vec<(usize, Strip)>, blk: Block) { + let Block { pos, frames, .. } = blk; let pdc = s.edit.delay_compensation; if pdc { for t in s.tracks.iter().filter(|t| is_aux(t) && !t.inactive) { @@ -427,7 +442,7 @@ impl MixEngine { self.aux_latency = lmax_a; self.aux_fed.clear(); if pdc { - for t in s.tracks.iter().filter(|t| is_aux(t) && live(t)) { + for t in s.tracks.iter().filter(|t| is_aux(t) && is_live(t)) { if let Route::Bus(b) = &t.mixer.output && !self.aux_fed.contains(b) { @@ -461,16 +476,20 @@ impl MixEngine { self.strips.insert(t.id, st); } } - self.work = work; - let is_aux = |t: &Track| matches!(t.kind, TrackKind::Aux | TrackKind::Folder); - // Phase 2: auxes in dependency order (they read busses). - for t in order.iter().filter_map(|&i| s.tracks.get(i)).filter(|t| live(t)).filter(|t| is_aux(t)) { + } + + /// Phase 2: auxes in dependency order (they read busses). + fn render_auxes(&mut self, s: &Session, order: &[usize], blk: Block) { + let Block { pos, frames, .. } = blk; + let pdc = s.edit.delay_compensation; + let lmax_a = self.aux_latency; + for t in order.iter().filter_map(|&i| s.tracks.get(i)).filter(|t| is_live(t)).filter(|t| is_aux(t)) { let Some(mut st) = self.strips.remove(&t.id) else { continue }; if st.scratch.len() < self.max_block { st.scratch.resize(self.max_block, 0.0); } let live_in = LiveInput { input: &self.input, monitor_only: self.monitor_only, recording: self.recording }; - process_strip(s, t, &mut st, &self.busses, &live_in, pos, frames, any_solo); + process_strip(s, t, &mut st, &self.busses, &live_in, blk); let ch = st.buf.len(); st.align.set(if pdc { lmax_a.saturating_sub(st.latency) } else { 0 }, ch); st.align.process(&mut st.buf, frames); @@ -478,7 +497,11 @@ impl MixEngine { self.route_strip(t, &mut st, pos, frames); self.strips.insert(t.id, st); } - self.order = order; + } + + /// Master faders process the main mix. + fn render_masters(&mut self, s: &Session, blk: Block) { + let Block { pos, frames, .. } = blk; // Master faders process the main mix. let transport = transport_at(s, pos, !self.monitor_only, self.recording); for t in s.tracks.iter().filter(|t| t.kind == TrackKind::Master && !t.inactive) { @@ -522,6 +545,11 @@ impl MixEngine { m.plugin_failures = strip.failures; } } + } + + /// Add the metronome and copy the main mix to `out`. + fn write_output(&mut self, s: &Session, blk: Block, out: &mut [Vec]) { + let Block { pos, frames, .. } = blk; // Metronome: summed after the master fader, so it reaches the speakers without passing // through any track, send or insert. Only the playback engine sets `metronome`. // Not while monitoring only (transport stopped): the position does not advance there, so the @@ -629,6 +657,14 @@ std::thread_local! { static AUDIO_THREAD: std::cell::Cell = const { std::cell::Cell::new(false) }; } +fn is_live(t: &Track) -> bool { + !(t.kind == TrackKind::Master || t.inactive || t.kind == TrackKind::Vca) +} + +fn is_aux(t: &Track) -> bool { + matches!(t.kind, TrackKind::Aux | TrackKind::Folder) +} + /// Metronome: a short synthesized blip on every beat of the tempo map, accented on beat 1 of /// each bar. Settings come from `setup.click_countoff` (`click.*` values). mod click; diff --git a/crates/mix/src/midi.rs b/crates/mix/src/midi.rs index 2e49790c..3b182956 100644 --- a/crates/mix/src/midi.rs +++ b/crates/mix/src/midi.rs @@ -1,6 +1,7 @@ use super::clips::Key; use super::strip::{PluginSlot, Strip}; -use super::*; +use super::{ClipContent, CtrlKind, Range, SampleSource, Samples, Session, SourceAudio, Track}; +use std::sync::Arc; /// The track's MIDI Track Offset (`midi.offset.` in the edit values), in samples. Looked up /// without formatting the key, since this runs on the audio thread every block. diff --git a/crates/mix/src/offline.rs b/crates/mix/src/offline.rs index ad70b918..d544b0fc 100644 --- a/crates/mix/src/offline.rs +++ b/crates/mix/src/offline.rs @@ -1,6 +1,6 @@ use super::clips::{ClipFx, clip_fx_params, render_clip}; use super::pan::itu_stereo_fold; -use super::*; +use super::{AutoParam, BusId, ChannelFormat, MixEngine, Range, Route, Samples, Session, TrackId, main_channels}; /// Ceiling on one in-memory offline render (samples summed over channels): 2^29 f32 = 2 GiB, about /// 93 minutes of stereo at 48 kHz. Longer requests are truncated rather than exhausting memory; diff --git a/crates/mix/src/pan.rs b/crates/mix/src/pan.rs index 6221ae4f..cb8047a7 100644 --- a/crates/mix/src/pan.rs +++ b/crates/mix/src/pan.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{ChannelFormat, MAX_CHANNELS, PanLaw, Speaker, SpeakerPos, SurroundPan, SurroundParams, db_to_gain, gains}; /// Per-block routing gains: `m[src][dst]`. type Matrix = [[f32; MAX_CHANNELS]; MAX_CHANNELS]; @@ -48,6 +48,22 @@ fn surround_params(sp: &SurroundPan, dx: f32) -> SurroundParams { SurroundParams { x: (sp.x + dx).clamp(-1.0, 1.0), y: sp.y, z: sp.z, divergence: sp.divergence, center: sp.center / 100.0 } } +/// One source → destination routing: channel counts and formats, plus how the source is panned. +struct Link<'a> { + src_fmt: ChannelFormat, + ns: usize, + dst_fmt: ChannelFormat, + nd: usize, + pan: &'a PanSpec<'a>, + law: PanLaw, +} + +fn add_gain(m: &mut Matrix, s: usize, d: usize, g: f32) { + if let Some(x) = m.get_mut(s).and_then(|r| r.get_mut(d)) { + *x += g; + } +} + /// Routing gains from a source (`ns` channels of `src_fmt`) into a destination (`nd` channels of /// `dst_fmt`). See the crate docs for the rules. pub(crate) fn build_matrix(src_fmt: ChannelFormat, ns: usize, dst_fmt: ChannelFormat, nd: usize, pan: &PanSpec<'_>, law: PanLaw) -> Matrix { @@ -56,11 +72,6 @@ pub(crate) fn build_matrix(src_fmt: ChannelFormat, ns: usize, dst_fmt: ChannelFo if ns == 0 || nd == 0 { return m; } - let set = |m: &mut Matrix, s: usize, d: usize, g: f32| { - if let Some(x) = m.get_mut(s).and_then(|r| r.get_mut(d)) { - *x += g; - } - }; if nd == 1 { // Mono destination: the stereo routing summed at -6 dB. let st = build_matrix(src_fmt, ns, ChannelFormat::Stereo, 2, pan, law); @@ -69,131 +80,168 @@ pub(crate) fn build_matrix(src_fmt: ChannelFormat, ns: usize, dst_fmt: ChannelFo } return m; } - let src_sp = src_fmt.speakers(); + let link = Link { src_fmt, ns, dst_fmt, nd, pan, law }; let src_amb = src_fmt.is_ambisonic() && ns >= 4; if nd == 2 && !dst_fmt.is_ambisonic() { - match ns { - 1 => { - let (gl, gr) = gains(pan.stereo[0], law); - m[0][0] = gl; - m[0][1] = gr; - } - 2 if !src_amb => { - let (a_l, a_r) = gains(pan.stereo[0], PanLaw::Zero); - let (b_l, b_r) = gains(pan.stereo[1], PanLaw::Zero); - m[0] = [0.0; MAX_CHANNELS]; - m[0][0] = a_l; - m[0][1] = a_r; - m[1][0] = b_l; - m[1][1] = b_r; - } - _ if src_amb => { - for (d, az) in [(0usize, -30.0f32), (1, 30.0)] { - let g = soundcraft_dsp::pan::ambisonic_decode(az, 0.0, 2); - for (c, gc) in g.iter().enumerate() { - set(&mut m, c, d, *gc); - } - } - } - _ if src_sp.len() == ns => { - for (s, sp) in src_sp.iter().enumerate() { - let (l, r) = itu_stereo_fold(*sp); - set(&mut m, s, 0, l); - set(&mut m, s, 1, r); - } - } - _ => { - // Unknown layout: first two channels to L/R, the rest to the centre. - let h = std::f32::consts::FRAC_1_SQRT_2; - m[0][0] = 1.0; - m[1][1] = 1.0; - for row in m.iter_mut().take(ns).skip(2) { - row[0] = h; - row[1] = h; - } - } - } - return m; + return stereo_dest(&link, src_amb); } if ns > 2 && src_fmt == dst_fmt && ns == nd { return identity_matrix(ns); } - // Multichannel destination. - let mut layout = [SpeakerPos::default(); MAX_CHANNELS]; - let nl = layout_of(dst_fmt, &mut layout); - let layout = layout.get(..nl).unwrap_or(&[]); - let dst_sp = dst_fmt.speakers(); - let dst_amb = dst_fmt.is_ambisonic(); - let find = |sp: Speaker| dst_sp.iter().position(|x| *x == sp).filter(|i| *i < nd); - let mut g = [0.0f32; MAX_CHANNELS]; - if dst_amb { - if src_amb || (src_fmt.is_ambisonic() && ns > 2) { - return identity_matrix(ns.min(nd)); - } - let mut enc = |m: &mut Matrix, s: usize, az: f32, el: f32, spread: f32| { - soundcraft_dsp::pan::ambisonic_encode(az, el, spread, &mut g); - for (d, gd) in g.iter().enumerate().take(nd) { - set(m, s, d, *gd); - } - }; - match (ns, pan.surround) { - (1 | 2, Some(sp)) => { - for s in 0..ns { - let dx = if ns == 2 { if s == 0 { -1.0 } else { 1.0 } } else { 0.0 }; - let p = surround_params(sp, dx); - enc(&mut m, s, soundcraft_dsp::pan::puck_azimuth(&[], p.x, p.y), p.z * 90.0, p.divergence); - } - } - (1, None) => enc(&mut m, 0, pan.stereo[0].clamp(-1.0, 1.0) * 30.0, 0.0, 0.0), - (2, None) => { - enc(&mut m, 0, -30.0, 0.0, 0.0); - enc(&mut m, 1, 30.0, 0.0, 0.0); - } - _ => { - for (s, sp) in src_sp.iter().enumerate().take(ns) { - if !sp.is_lfe() { - enc(&mut m, s, sp.azimuth(), sp.elevation(), 0.0); - } - } - } - } - return m; + if dst_fmt.is_ambisonic() { + return ambisonic_dest(&link, src_amb); } + layout_dest(&link, src_amb) +} + +/// Routing into a stereo destination. +fn stereo_dest(l: &Link<'_>, src_amb: bool) -> Matrix { + let mut m = [[0.0f32; MAX_CHANNELS]; MAX_CHANNELS]; + let src_sp = l.src_fmt.speakers(); + match l.ns { + 1 => { + let (gl, gr) = gains(l.pan.stereo[0], l.law); + m[0][0] = gl; + m[0][1] = gr; + } + 2 if !src_amb => { + let (a_l, a_r) = gains(l.pan.stereo[0], PanLaw::Zero); + let (b_l, b_r) = gains(l.pan.stereo[1], PanLaw::Zero); + m[0] = [0.0; MAX_CHANNELS]; + m[0][0] = a_l; + m[0][1] = a_r; + m[1][0] = b_l; + m[1][1] = b_r; + } + _ if src_amb => { + for (d, az) in [(0usize, -30.0f32), (1, 30.0)] { + let g = soundcraft_dsp::pan::ambisonic_decode(az, 0.0, 2); + for (c, gc) in g.iter().enumerate() { + add_gain(&mut m, c, d, *gc); + } + } + } + ns if src_sp.len() == ns => { + for (s, sp) in src_sp.iter().enumerate() { + let (left, right) = itu_stereo_fold(*sp); + add_gain(&mut m, s, 0, left); + add_gain(&mut m, s, 1, right); + } + } + ns => { + // Unknown layout: first two channels to L/R, the rest to the centre. + let h = std::f32::consts::FRAC_1_SQRT_2; + m[0][0] = 1.0; + m[1][1] = 1.0; + for row in m.iter_mut().take(ns).skip(2) { + row[0] = h; + row[1] = h; + } + } + } + m +} + +/// `±1` offset spreading a stereo source's two channels left and right of the surround pan. +fn spread_dx(ns: usize, s: usize) -> f32 { + if ns != 2 { + 0.0 + } else if s == 0 { + -1.0 + } else { + 1.0 + } +} + +/// Routing into an ambisonic destination. +fn ambisonic_dest(l: &Link<'_>, src_amb: bool) -> Matrix { + let mut m = [[0.0f32; MAX_CHANNELS]; MAX_CHANNELS]; + let (ns, nd) = (l.ns, l.nd); + if src_amb || (l.src_fmt.is_ambisonic() && ns > 2) { + return identity_matrix(ns.min(nd)); + } + let src_sp = l.src_fmt.speakers(); + let mut g = [0.0f32; MAX_CHANNELS]; + let mut enc = |m: &mut Matrix, s: usize, az: f32, el: f32, spread: f32| { + soundcraft_dsp::pan::ambisonic_encode(az, el, spread, &mut g); + for (d, gd) in g.iter().enumerate().take(nd) { + add_gain(m, s, d, *gd); + } + }; + match (ns, l.pan.surround) { + (1 | 2, Some(sp)) => { + for s in 0..ns { + let p = surround_params(sp, spread_dx(ns, s)); + enc(&mut m, s, soundcraft_dsp::pan::puck_azimuth(&[], p.x, p.y), p.z * 90.0, p.divergence); + } + } + (1, None) => enc(&mut m, 0, l.pan.stereo[0].clamp(-1.0, 1.0) * 30.0, 0.0, 0.0), + (2, None) => { + enc(&mut m, 0, -30.0, 0.0, 0.0); + enc(&mut m, 1, 30.0, 0.0, 0.0); + } + _ => { + for (s, sp) in src_sp.iter().enumerate().take(ns) { + if !sp.is_lfe() { + enc(&mut m, s, sp.azimuth(), sp.elevation(), 0.0); + } + } + } + } + m +} + +/// A surround-panned mono or stereo source into a speaker layout, plus its LFE send. +fn surround_into_layout(m: &mut Matrix, l: &Link<'_>, layout: &[SpeakerPos], sp: &SurroundPan) { + let (ns, nd) = (l.ns, l.nd); + let dst_sp = l.dst_fmt.speakers(); + let mut g = [0.0f32; MAX_CHANNELS]; + for s in 0..ns { + soundcraft_dsp::pan::surround_gains(layout, &surround_params(sp, spread_dx(ns, s)), &mut g); + for (d, gd) in g.iter().enumerate().take(nd) { + add_gain(m, s, d, *gd); + } + if let Some(lfe) = dst_sp.iter().position(|x| *x == Speaker::Lfe).filter(|i| *i < nd) + && sp.lfe_db > -143.9 + { + add_gain(m, s, lfe, db_to_gain(sp.lfe_db.min(12.0)) / ns as f32); + } + } +} + +/// Routing into a speaker layout other than stereo or ambisonic. +fn layout_dest(l: &Link<'_>, src_amb: bool) -> Matrix { + let mut m = [[0.0f32; MAX_CHANNELS]; MAX_CHANNELS]; + let (ns, nd) = (l.ns, l.nd); + let src_sp = l.src_fmt.speakers(); + let mut layout = [SpeakerPos::default(); MAX_CHANNELS]; + let nl = layout_of(l.dst_fmt, &mut layout); + let layout = layout.get(..nl).unwrap_or(&[]); + let dst_sp = l.dst_fmt.speakers(); if dst_sp.len() != nd { // No layout to pan on: channel for channel. return identity_matrix(ns.min(nd)); } + let find = |sp: Speaker| dst_sp.iter().position(|x| *x == sp).filter(|i| *i < nd); let (li, ri) = (find(Speaker::L), find(Speaker::R)); - match (ns, pan.surround) { - (1 | 2, Some(sp)) if !src_amb => { - for s in 0..ns { - let dx = if ns == 2 { if s == 0 { -1.0 } else { 1.0 } } else { 0.0 }; - soundcraft_dsp::pan::surround_gains(layout, &surround_params(sp, dx), &mut g); - for (d, gd) in g.iter().enumerate().take(nd) { - set(&mut m, s, d, *gd); - } - if let Some(lfe) = find(Speaker::Lfe) - && sp.lfe_db > -143.9 - { - set(&mut m, s, lfe, db_to_gain(sp.lfe_db.min(12.0)) / ns as f32); - } - } - } + let mut g = [0.0f32; MAX_CHANNELS]; + match (ns, l.pan.surround) { + (1 | 2, Some(sp)) if !src_amb => surround_into_layout(&mut m, l, layout, sp), (1, None) => { - let (gl, gr) = gains(pan.stereo[0], law); - if let (Some(l), Some(r)) = (li, ri) { - set(&mut m, 0, l, gl); - set(&mut m, 0, r, gr); + let (gl, gr) = gains(l.pan.stereo[0], l.law); + if let (Some(left), Some(right)) = (li, ri) { + add_gain(&mut m, 0, left, gl); + add_gain(&mut m, 0, right, gr); } } (2, None) if !src_amb => { - let (a_l, a_r) = gains(pan.stereo[0], PanLaw::Zero); - let (b_l, b_r) = gains(pan.stereo[1], PanLaw::Zero); - if let (Some(l), Some(r)) = (li, ri) { - set(&mut m, 0, l, a_l); - set(&mut m, 0, r, a_r); - set(&mut m, 1, l, b_l); - set(&mut m, 1, r, b_r); + let (a_l, a_r) = gains(l.pan.stereo[0], PanLaw::Zero); + let (b_l, b_r) = gains(l.pan.stereo[1], PanLaw::Zero); + if let (Some(left), Some(right)) = (li, ri) { + add_gain(&mut m, 0, left, a_l); + add_gain(&mut m, 0, right, a_r); + add_gain(&mut m, 1, left, b_l); + add_gain(&mut m, 1, right, b_r); } } _ if src_amb => { @@ -204,20 +252,19 @@ pub(crate) fn build_matrix(src_fmt: ChannelFormat, ns: usize, dst_fmt: ChannelFo } let dec = soundcraft_dsp::pan::ambisonic_decode(sp.azimuth(), sp.elevation(), n_spk); for (c, gc) in dec.iter().enumerate() { - set(&mut m, c, d, *gc); + add_gain(&mut m, c, d, *gc); } } } _ if src_sp.len() == ns => { for (s, sp) in src_sp.iter().enumerate() { if let Some(d) = find(*sp) { - set(&mut m, s, d, 1.0); - } else if sp.is_lfe() { - // No LFE in the destination: dropped. - } else { + add_gain(&mut m, s, d, 1.0); + } else if !sp.is_lfe() { + // (An LFE with no LFE in the destination is dropped.) soundcraft_dsp::pan::direction_gains(layout, sp.azimuth(), sp.elevation(), &mut g); for (d, gd) in g.iter().enumerate().take(nd) { - set(&mut m, s, d, *gd); + add_gain(&mut m, s, d, *gd); } } } diff --git a/crates/mix/src/routing.rs b/crates/mix/src/routing.rs index 5b924d50..fcebca85 100644 --- a/crates/mix/src/routing.rs +++ b/crates/mix/src/routing.rs @@ -1,4 +1,6 @@ -use super::*; +use super::{BusId, Route, Session, Track, TrackKind}; +use std::collections::HashMap; +use std::sync::Arc; /// Hash of everything `sync`/`processing_order` depend on (tracks, kinds, routing, plugins, sends). pub(crate) fn structure_fingerprint(s: &Session) -> usize { @@ -153,6 +155,7 @@ pub(crate) fn receives_solo(s: &Session, t: &Track) -> bool { #[allow(clippy::unwrap_used)] mod processing_order_tests { use super::*; + use soundcraft_model::ChannelFormat; use soundcraft_model::SendSlot; #[test] diff --git a/crates/mix/src/strip.rs b/crates/mix/src/strip.rs index 45565feb..01f4a11a 100644 --- a/crates/mix/src/strip.rs +++ b/crates/mix/src/strip.rs @@ -4,7 +4,12 @@ use super::instances::{INSTRUMENT_SLOT, PluginFailures, PreparedInstance, create use super::midi::{chase_controllers, schedule_notes}; use super::pan::{PanSpec, apply_matrix, build_matrix, identity_matrix}; use super::routing::receives_solo; -use super::*; +use super::{ + AutoParam, BusId, ChannelFormat, MAX_CHANNELS, PanLaw, Plugin, Range, Route, Samples, Session, SourceAudio, Track, TrackId, TrackKind, Transport, + db_to_gain, +}; +use std::collections::HashMap; +use std::sync::Arc; /// A bus: its format and one buffer per channel. pub(crate) struct BusBuf { @@ -199,131 +204,130 @@ pub(crate) struct LiveInput<'a> { pub(crate) recording: bool, } -pub(crate) fn process_strip( - s: &Session, - t: &Track, - strip: &mut Strip, - busses: &HashMap, - live: &LiveInput<'_>, - pos: Samples, - frames: usize, - any_solo: bool, -) { - for c in strip.buf.iter_mut() { +/// The block being rendered: where it starts, its length, and whether any track is soloed. +#[derive(Clone, Copy)] +pub(crate) struct Block { + pub(crate) pos: Samples, + pub(crate) frames: usize, + pub(crate) any_solo: bool, +} + +fn zero_buf(buf: &mut [Vec], frames: usize) { + for c in buf.iter_mut() { c.iter_mut().take(frames).for_each(|x| *x = 0.0); } - let transport = transport_at(s, pos, !live.monitor_only, live.recording); - match t.kind { - TrackKind::Audio if !live.input.is_empty() && (t.mixer.input_monitor || (t.mixer.record_arm && (live.monitor_only || live.recording))) => { - // Input monitoring: the track hears its hardware input instead of its clips. - let first = match &t.mixer.input { - Route::Hardware(h) => { - h.trim_start_matches("In ").split(['-', ' ']).next().and_then(|n| n.parse::().ok()).map_or(0, |n| n.saturating_sub(1)) - } - _ => 0, - }; - let n_in = live.input.len().max(1); - for (k, dst) in strip.buf.iter_mut().enumerate() { - if let Some(src) = live.input.get((first + k) % n_in) { - for (d, x) in dst.iter_mut().zip(src.iter()).take(frames) { - *d = *x; - } - } - } +} + +/// Input monitoring: the track hears its hardware input instead of its clips. +fn monitor_input(t: &Track, strip: &mut Strip, live: &LiveInput<'_>, frames: usize) { + let first = match &t.mixer.input { + Route::Hardware(h) => { + h.trim_start_matches("In ").split(['-', ' ']).next().and_then(|n| n.parse::().ok()).map_or(0, |n| n.saturating_sub(1)) } - TrackKind::Audio if live.monitor_only => {} - TrackKind::Instrument | TrackKind::Midi if live.monitor_only => {} - TrackKind::Audio => { - let block = Range { start: pos, end: pos.saturating_add(frames as i64) }; - for clip in t.clips() { - if clip.muted || !clip.is_audio() || !clip.range().overlaps(&block) { - continue; - } - match clip_fx_params(s, clip.id.0) { - None => render_clip(s, clip, pos, frames, &mut strip.buf, &mut strip.scratch), - Some(params) => { - let nch = strip.buf.len(); - if strip.clipbuf.len() != nch || strip.clipbuf.first().map_or(0, Vec::len) < frames { - strip.clipbuf = vec![vec![0.0; frames.max(strip.scratch.len())]; nch]; - } - for c in strip.clipbuf.iter_mut() { - c.iter_mut().take(frames).for_each(|x| *x = 0.0); - } - render_clip(s, clip, pos, frames, &mut strip.clipbuf, &mut strip.scratch); - let fx = strip.clip_fx.entry(clip.id.0).or_insert_with(|| ClipFx::new(s.sample_rate.as_f64() as f32, nch)); - fx.configure(params); - fx.process(&mut strip.clipbuf, frames); - for (d, c) in strip.buf.iter_mut().zip(strip.clipbuf.iter()) { - for (x, y) in d.iter_mut().zip(c.iter()).take(frames) { - *x += *y; - } - } - } - } + _ => 0, + }; + let n_in = live.input.len().max(1); + for (k, dst) in strip.buf.iter_mut().enumerate() { + if let Some(src) = live.input.get((first + k) % n_in) { + for (d, x) in dst.iter_mut().zip(src.iter()).take(frames) { + *d = *x; } } - TrackKind::Aux | TrackKind::Folder => { - if let Route::Bus(b) = &t.mixer.input - && let Some(src) = busses.get(b) - { - let (ns, nd) = (src.buf.len(), strip.buf.len()); - let dst_fmt = strip_format(t, nd); - let m = if src.fmt == dst_fmt && ns == nd { - identity_matrix(ns) - } else if ns == 1 && nd == 2 { - // A mono bus feeds both sides of a stereo aux. - let mut m = [[0.0; MAX_CHANNELS]; MAX_CHANNELS]; - m[0][0] = 1.0; - m[0][1] = 1.0; - m - } else { - let stereo = if ns == 1 { [0.0, 0.0] } else { [-1.0, 1.0] }; - build_matrix(src.fmt, ns, dst_fmt, nd, &PanSpec { stereo, surround: None }, PanLaw::Minus3) - }; - apply_matrix(&src.buf, frames, &mut strip.buf, &m, 1.0); - } - } - TrackKind::Instrument | TrackKind::Midi => { - if let Some(inst) = strip.instrument.as_mut().filter(|i| !i.failed) { - // #152: push the instrument's stored parameter values into the live plugin - // before processing, the same way inserts do below. Without this, the values - // loaded from a session (or set via `mix.insert_state`) never reach the plugin - // instance and the rendered audio matches the plugin's defaults — e.g. a saved - // `level = -12 dB` sounds the same as `level = -6 dB`. - if let Some(ins) = &t.instrument { - apply_params(inst, ins, t, INSTRUMENT_SLOT, pos); - } - inst.plugin.set_transport(&transport); - if std::mem::take(&mut strip.chase) { - chase_controllers(s, t, pos, inst); - } - schedule_notes(s, t, pos, frames, inst, &mut strip.held); - if strip.buf.len() >= 2 { - inst.plugin.process(&mut strip.buf, frames); - } else if let Some(c0) = strip.buf.first_mut() { - // Mono MIDI track: render the instrument in stereo and fold. - let mut st = [std::mem::take(c0), std::mem::take(&mut strip.scratch)]; - inst.plugin.process(&mut st, frames); - let [a, b] = st; - *c0 = a; - strip.scratch = b; - for (x, y) in c0.iter_mut().zip(strip.scratch.iter()).take(frames) { - *x = 0.5 * (*x + *y); - } - } - if contain_failure(inst, &mut strip.buf, frames) { - strip.failures.record(INSTRUMENT_SLOT); - } - } - } - _ => {} - } - let trim = db_to_gain(t.mixer.trim_db) * if t.mixer.phase_invert { -1.0 } else { 1.0 }; - if (trim - 1.0).abs() > f32::EPSILON { - for c in strip.buf.iter_mut() { - c.iter_mut().take(frames).for_each(|x| *x *= trim); - } } +} + +/// Render the audio clips overlapping the block into the strip, through their clip effects. +fn render_audio_clips(s: &Session, t: &Track, strip: &mut Strip, blk: Block) { + let Block { pos, frames, .. } = blk; + let block = Range { start: pos, end: pos.saturating_add(frames as i64) }; + for clip in t.clips() { + if clip.muted || !clip.is_audio() || !clip.range().overlaps(&block) { + continue; + } + match clip_fx_params(s, clip.id.0) { + None => render_clip(s, clip, pos, frames, &mut strip.buf, &mut strip.scratch), + Some(params) => { + let nch = strip.buf.len(); + if strip.clipbuf.len() != nch || strip.clipbuf.first().map_or(0, Vec::len) < frames { + strip.clipbuf = vec![vec![0.0; frames.max(strip.scratch.len())]; nch]; + } + zero_buf(&mut strip.clipbuf, frames); + render_clip(s, clip, pos, frames, &mut strip.clipbuf, &mut strip.scratch); + let fx = strip.clip_fx.entry(clip.id.0).or_insert_with(|| ClipFx::new(s.sample_rate.as_f64() as f32, nch)); + fx.configure(params); + fx.process(&mut strip.clipbuf, frames); + for (d, c) in strip.buf.iter_mut().zip(strip.clipbuf.iter()) { + for (x, y) in d.iter_mut().zip(c.iter()).take(frames) { + *x += *y; + } + } + } + } + } +} + +/// An aux or folder reads the bus its input is routed from. +fn read_aux_bus(t: &Track, strip: &mut Strip, busses: &HashMap, frames: usize) { + if let Route::Bus(b) = &t.mixer.input + && let Some(src) = busses.get(b) + { + let (ns, nd) = (src.buf.len(), strip.buf.len()); + let dst_fmt = strip_format(t, nd); + let m = if src.fmt == dst_fmt && ns == nd { + identity_matrix(ns) + } else if ns == 1 && nd == 2 { + // A mono bus feeds both sides of a stereo aux. + let mut m = [[0.0; MAX_CHANNELS]; MAX_CHANNELS]; + m[0][0] = 1.0; + m[0][1] = 1.0; + m + } else { + let stereo = if ns == 1 { [0.0, 0.0] } else { [-1.0, 1.0] }; + build_matrix(src.fmt, ns, dst_fmt, nd, &PanSpec { stereo, surround: None }, PanLaw::Minus3) + }; + apply_matrix(&src.buf, frames, &mut strip.buf, &m, 1.0); + } +} + +/// Render the track's instrument into the strip. +fn render_instrument(s: &Session, t: &Track, strip: &mut Strip, blk: Block, transport: &Transport) { + let Block { pos, frames, .. } = blk; + if let Some(inst) = strip.instrument.as_mut().filter(|i| !i.failed) { + // #152: push the instrument's stored parameter values into the live plugin + // before processing, the same way inserts do below. Without this, the values + // loaded from a session (or set via `mix.insert_state`) never reach the plugin + // instance and the rendered audio matches the plugin's defaults — e.g. a saved + // `level = -12 dB` sounds the same as `level = -6 dB`. + if let Some(ins) = &t.instrument { + apply_params(inst, ins, t, INSTRUMENT_SLOT, pos); + } + inst.plugin.set_transport(transport); + if std::mem::take(&mut strip.chase) { + chase_controllers(s, t, pos, inst); + } + schedule_notes(s, t, pos, frames, inst, &mut strip.held); + if strip.buf.len() >= 2 { + inst.plugin.process(&mut strip.buf, frames); + } else if let Some(c0) = strip.buf.first_mut() { + // Mono MIDI track: render the instrument in stereo and fold. + let mut st = [std::mem::take(c0), std::mem::take(&mut strip.scratch)]; + inst.plugin.process(&mut st, frames); + let [a, b] = st; + *c0 = a; + strip.scratch = b; + for (x, y) in c0.iter_mut().zip(strip.scratch.iter()).take(frames) { + *x = 0.5 * (*x + *y); + } + } + if contain_failure(inst, &mut strip.buf, frames) { + strip.failures.record(INSTRUMENT_SLOT); + } + } +} + +/// Run the active inserts in order; records their latency and the first gain reduction. +fn run_inserts(t: &Track, strip: &mut Strip, blk: Block, transport: &Transport) { + let Block { pos, frames, .. } = blk; let mut gr = 0.0f32; let mut latency = 0usize; for (i, ins) in t.mixer.inserts.iter().enumerate() { @@ -332,7 +336,7 @@ pub(crate) fn process_strip( continue; } apply_params(slot, ins, t, i, pos); - slot.plugin.set_transport(&transport); + slot.plugin.set_transport(transport); slot.plugin.process(&mut strip.buf, frames); if contain_failure(slot, &mut strip.buf, frames) { strip.failures.record(i); @@ -345,15 +349,11 @@ pub(crate) fn process_strip( } strip.gr = gr; strip.latency = latency.min(MAX_LATENCY); - let soloed_out = any_solo && !t.mixer.solo && !t.mixer.solo_safe && !receives_solo(s, t); - strip.muted = t.mixer.mute || soloed_out; - if t.mixer.sends.iter().flatten().any(|x| x.pre_fader) { - strip.pre.resize_with(strip.buf.len(), Vec::new); - for (d, src) in strip.pre.iter_mut().zip(strip.buf.iter()) { - d.clear(); - d.extend_from_slice(src.get(..frames).unwrap_or(&[])); - } - } +} + +/// Fader gain (volume, trim and VCA automation), ramped across the block. +fn apply_fader(s: &Session, t: &Track, strip: &mut Strip, blk: Block) { + let Block { pos, frames, .. } = blk; // Endpoints are the first and last sample of this block. The next block starts on its own // first sample, so a breakpoint there does not ramp the block before it. let last = pos.saturating_add(frames.saturating_sub(1) as i64); @@ -386,9 +386,13 @@ pub(crate) fn process_strip( *x *= ramp.at(i); } } - // Track mute gates the audio and its pre-fader tap without splitting processor blocks. - // Keep the existing curve interpolation, first-lane and mode policy, and render-position - // clock. Processor/bus age and Master/VCA automation policy are separate concerns. +} + +/// Track mute gates the audio and its pre-fader tap without splitting processor blocks. +/// Keep the existing curve interpolation, first-lane and mode policy, and render-position +/// clock. Processor/bus age and Master/VCA automation policy are separate concerns. +fn gate_mute_automation(t: &Track, strip: &mut Strip, blk: Block) { + let Block { pos, frames, .. } = blk; if t.mixer.automation_mode.reads() && let Some(lane) = t.lane(&AutoParam::Mute).filter(|lane| !lane.points.is_empty()) { @@ -405,6 +409,41 @@ pub(crate) fn process_strip( } } +pub(crate) fn process_strip(s: &Session, t: &Track, strip: &mut Strip, busses: &HashMap, live: &LiveInput<'_>, blk: Block) { + let Block { pos, frames, any_solo } = blk; + zero_buf(&mut strip.buf, frames); + let transport = transport_at(s, pos, !live.monitor_only, live.recording); + match t.kind { + TrackKind::Audio if !live.input.is_empty() && (t.mixer.input_monitor || (t.mixer.record_arm && (live.monitor_only || live.recording))) => { + monitor_input(t, strip, live, frames); + } + TrackKind::Audio if live.monitor_only => {} + TrackKind::Instrument | TrackKind::Midi if live.monitor_only => {} + TrackKind::Audio => render_audio_clips(s, t, strip, blk), + TrackKind::Aux | TrackKind::Folder => read_aux_bus(t, strip, busses, frames), + TrackKind::Instrument | TrackKind::Midi => render_instrument(s, t, strip, blk, &transport), + _ => {} + } + let trim = db_to_gain(t.mixer.trim_db) * if t.mixer.phase_invert { -1.0 } else { 1.0 }; + if (trim - 1.0).abs() > f32::EPSILON { + for c in strip.buf.iter_mut() { + c.iter_mut().take(frames).for_each(|x| *x *= trim); + } + } + run_inserts(t, strip, blk, &transport); + let soloed_out = any_solo && !t.mixer.solo && !t.mixer.solo_safe && !receives_solo(s, t); + strip.muted = t.mixer.mute || soloed_out; + if t.mixer.sends.iter().flatten().any(|x| x.pre_fader) { + strip.pre.resize_with(strip.buf.len(), Vec::new); + for (d, src) in strip.pre.iter_mut().zip(strip.buf.iter()) { + d.clear(); + d.extend_from_slice(src.get(..frames).unwrap_or(&[])); + } + } + apply_fader(s, t, strip, blk); + gate_mute_automation(t, strip, blk); +} + /// Silences a block the plugin filled with NaN or infinity and takes the plugin out until the /// next [`MixEngine::reset`]. True then. pub(crate) fn contain_failure(slot: &mut PluginSlot, buf: &mut [Vec], frames: usize) -> bool { diff --git a/crates/mix/src/threads.rs b/crates/mix/src/threads.rs index cfb2dd98..c71fb3fc 100644 --- a/crates/mix/src/threads.rs +++ b/crates/mix/src/threads.rs @@ -1,4 +1,4 @@ -use super::*; +use super::AUDIO_THREAD; /// Mark the calling thread as the realtime audio thread (`soundcraft_playback`'s cpal callbacks /// do, every call: one thread-local store). Strips [`MixEngine::render`] hands to worker threads diff --git a/crates/playback/src/callback.rs b/crates/playback/src/callback.rs index fc89c16e..5e931fe0 100644 --- a/crates/playback/src/callback.rs +++ b/crates/playback/src/callback.rs @@ -1,4 +1,8 @@ -use super::*; +use super::{ + Arc, AtomicBool, BLOCK, BTreeMap, Cmd, HashMap, HashSet, MAX_CHANNELS, MeterSnapshot, MixEngine, OLD_SESSIONS, Ordering, OutputTrouble, Plugin, + PluginEditor, PluginStateReader, PluginStates, PreparedInstance, Range, Receiver, Samples, Sender, Session, Shared, SyncSender, TrackId, + itu_stereo_fold, main_channels, record, sync_channel, +}; /// How the audio output is doing, for the UI. #[derive(Default)] diff --git a/crates/playback/src/stream.rs b/crates/playback/src/stream.rs index af90a912..a1f78530 100644 --- a/crates/playback/src/stream.rs +++ b/crates/playback/src/stream.rs @@ -1,5 +1,5 @@ use super::callback::{AudioState, Fault, guarded_block}; -use super::*; +use super::{Arc, AudioConfig, Cmd, Ordering, Plugin, Receiver, Sender, Shared, devices, mark_audio_thread}; /// Start a freshly built output stream. `false` when the device refuses to start it, so the caller /// treats it like a stream that failed to build and tries the next configuration. diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index b0807213..35ca5974 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -1,6 +1,10 @@ use super::services::Services; use super::state::MainWindow; -use super::*; +use super::{ + ControlRequest, Engine, Gesture, HashMap, MeterDisplay, OutputTrouble, Player, Range, Receiver, Samples, SoundApp, TrackId, TransportRequest, + Trouble, UiState, Value, VecDeque, bounce, dialogs, edit_window, extra_windows, fonts, import, json, menus, midi_editor, mix_window, ops_windows, + palette, panels, recovery, sampler_window, score_editor, shortcuts, theme, updates_ui, video_track, video_window, windows, +}; mod audio; mod commands; diff --git a/crates/ui-egui/src/app/audio.rs b/crates/ui-egui/src/app/audio.rs index 7a9f9bf1..3ec92849 100644 --- a/crates/ui-egui/src/app/audio.rs +++ b/crates/ui-egui/src/app/audio.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{MeterDisplay, OutputTrouble, Player, SoundApp, Trouble}; impl SoundApp { /// Whether the meters are live: while playing, and while armed or input-monitored tracks are heard. diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 2ae62f9b..422ff784 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{SoundApp, Value, bounce, menus}; impl SoundApp { /// Run a command; errors go to the status line. Returns the result. diff --git a/crates/ui-egui/src/app/control.rs b/crates/ui-egui/src/app/control.rs index ab5f91a6..1ba17caf 100644 --- a/crates/ui-egui/src/app/control.rs +++ b/crates/ui-egui/src/app/control.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{SoundApp, Value, json}; use crate::control; impl SoundApp { diff --git a/crates/ui-egui/src/app/files.rs b/crates/ui-egui/src/app/files.rs index 22ce03dd..6b230d34 100644 --- a/crates/ui-egui/src/app/files.rs +++ b/crates/ui-egui/src/app/files.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{SoundApp, midi_editor}; impl SoundApp { /// The open document was replaced outside [`Self::run`] (Recover, a session the browser read diff --git a/crates/ui-egui/src/app/input.rs b/crates/ui-egui/src/app/input.rs index cafbed86..c6167d5a 100644 --- a/crates/ui-egui/src/app/input.rs +++ b/crates/ui-egui/src/app/input.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{SoundApp, json, sampler_window}; impl SoundApp { /// Feed queued synthetic input (from the control channel) into egui: one pointer event per diff --git a/crates/ui-egui/src/app/playback.rs b/crates/ui-egui/src/app/playback.rs index fd17adc4..65c6c194 100644 --- a/crates/ui-egui/src/app/playback.rs +++ b/crates/ui-egui/src/app/playback.rs @@ -1,6 +1,6 @@ use super::audio::feed_meter; use super::plugins::failed_plugin_status; -use super::*; +use super::{Gesture, MainWindow, MeterDisplay, Range, Samples, SoundApp, TrackId, TransportRequest, json}; /// Whether another automation-write pass is due: the transport moved at least /// `step` since `last` in either direction. `last == i64::MIN` marks "never diff --git a/crates/ui-egui/src/app/plugins.rs b/crates/ui-egui/src/app/plugins.rs index da316f9b..19feaee7 100644 --- a/crates/ui-egui/src/app/plugins.rs +++ b/crates/ui-egui/src/app/plugins.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{SoundApp, TrackId, json, mix_window}; /// The plugins whose state the session takes this frame, out of those changed in their editors /// (`pending`, kept across frames). A plugin editor doesn't say when a gesture ends: a plugin's diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 17543691..fc18e173 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -281,6 +281,175 @@ fn clear_message(s: &Session, clips: &[ClipId], sources: &[SourceId]) -> Option< Some(text) } +/// The New Tracks form; the command to run once Create is pressed. +fn new_tracks_form( + ui: &mut egui::Ui, + count: &mut u32, + format: &mut String, + kind: &mut String, + timebase: &mut String, + name: &mut String, + enter: bool, +) -> Option<(String, Value)> { + ui.horizontal(|ui| { + let create = ui.label("Create"); + ui.add(egui::DragValue::new(count).range(1..=128)).labelled_by(create.id); + ui.label("new"); + let fmt = craft_ui::ComboBox::from_id_salt("nt_fmt").selected_text(format.as_str()).show_ui(ui, |ui| { + for f in soundcraft_model::ChannelFormat::ALL { + ui.selectable_value(format, f.label().to_string(), f.label()); + } + }); + fmt.response.accessible_name("Channel format"); + let kind_combo = craft_ui::ComboBox::from_id_salt("nt_kind") + .selected_text(soundcraft_model::TrackKind::from_id(kind).map_or("Audio Track", |k| k.label())) + .show_ui(ui, |ui| { + for k in soundcraft_model::TrackKind::ALL { + if ui.selectable_label(kind == k.id(), k.label()).clicked() { + *kind = k.id().to_string(); + *name = k.default_name().to_string(); + } + } + }); + kind_combo.response.accessible_name("Track kind"); + ui.label("in"); + let tb = craft_ui::ComboBox::from_id_salt("nt_tb").selected_text(if timebase == "ticks" { "Ticks" } else { "Samples" }).show_ui(ui, |ui| { + ui.selectable_value(timebase, "samples".to_string(), "Samples"); + ui.selectable_value(timebase, "ticks".to_string(), "Ticks"); + }); + tb.response.accessible_name("Timebase"); + }); + ui.horizontal(|ui| { + ui.label("Name:"); + crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(name).desired_width(240.0)), "Name"); + }); + if buttons(ui, "Create", enter) { + return Some(("track.new".into(), json!({"count": count, "format": format, "kind": kind, "timebase": timebase, "name": name}))); + } + None +} + +/// The Bounce form's file row: a path field and a Browse button that also picks the format. +fn bounce_file_row(app: &mut SoundApp, ui: &mut egui::Ui, path: &mut String, format: &mut FileFormat) { + ui.horizontal(|ui| { + ui.label("File"); + crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(path).desired_width(420.0)), "File"); + if let Some(pick) = &app.services.pick_save + && ui.button("Browse…").clicked() + && let Some(p) = pick("file.bounce_mix", std::path::Path::new(path.as_str())) + { + match unicode(&p) { + Ok(text) => { + *path = text.to_owned(); + if let Some(picked) = p.extension().and_then(std::ffi::OsStr::to_str).and_then(FileFormat::from_write_extension) { + *format = picked; + } + } + Err(e) => app.ui.status = e.to_string(), + } + } + }); +} + +/// The path prompt's form; the command to run once it is confirmed. +fn path_prompt_form(app: &mut SoundApp, ui: &mut egui::Ui, cmd: &str, path: &mut String, key: &str, enter: bool) -> Option<(String, Value)> { + crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(path).desired_width(420.0)), "Path"); + let mut picked = None; + if let Some(pick) = &app.services.pick_open + && cmd != "session.save_as" + && cmd != "session.save_copy" + && key != "dir" + && ui.button("Browse…").clicked() + && let Some(p) = pick(cmd, &[]) + { + picked = Some(p); + } + if let Some(pick) = &app.services.pick_folder + && key == "dir" + && ui.button("Browse…").clicked() + && let Some(p) = pick(std::path::Path::new(path.as_str())) + { + picked = Some(p); + } + if let Some(pick) = &app.services.pick_save + && (cmd == "session.save_as" || cmd == "session.save_copy") + && ui.button("Browse…").clicked() + && let Some(p) = pick(cmd, std::path::Path::new(path.as_str())) + { + picked = Some(p); + } + match picked.as_deref().map(unicode) { + Some(Ok(text)) => *path = text.to_owned(), + Some(Err(e)) => app.ui.status = e.to_string(), + None => {} + } + let file = soundcraft_engine::io::session_file(path); + let replaces = soundcraft_engine::io::replaces(&app.engine, cmd, &file); + if replaces { + ui.label(format!("“{}” already exists. Do you want to replace it?", file.file_name().unwrap_or_default().to_string_lossy())); + } + if buttons(ui, if replaces { "Replace" } else { "OK" }, enter && !replaces) { + let mut v = json!({}); + v[key] = json!(path); + if replaces { + v["replace"] = json!(true); + } + return Some((cmd.to_string(), v)); + } + None +} + +/// The Strip Silence form: the Strip Silence command to run, or the values to carry into Rename. +fn strip_silence_form( + ui: &mut egui::Ui, + values: &mut [f64; 4], + strip_names: &StripNames, + enter: bool, +) -> (Option<(String, Value)>, Option<[f64; 4]>) { + let mut action = None; + let mut rename_from = None; + let [threshold, min_ms, pre_ms, post_ms] = values; + let before = [*threshold, *min_ms, *pre_ms, *post_ms]; + ui.add(egui::Slider::new(threshold, -96.0..=0.0).text("Threshold dB")); + ui.add(egui::Slider::new(min_ms, 0.0..=4000.0).text("Min strip duration ms")); + ui.add(egui::Slider::new(pre_ms, 0.0..=500.0).text("Clip start pad ms")); + ui.add(egui::Slider::new(post_ms, 0.0..=2000.0).text("Clip end pad ms")); + let values = [*threshold, *min_ms, *pre_ms, *post_ms]; + // The Edit window is drawn before this window, so it draws the rectangles for new values in the next frame. + if values != before { + ui.ctx().request_repaint(); + } + if !strip_names.name.trim().is_empty() { + let names = format!("New clips are named {}, {} and so on.", strip_names.clip_name(0), strip_names.clip_name(1)); + ui.label(egui::RichText::new(names).small().color(Tokens::current().text_dim)); + } + ui.separator(); + ui.horizontal(|ui| { + let mut mode = None; + let mut rename = false; + if ui.button("Extract").clicked() { + mode = Some("extract"); + } + if ui.button("Rename…").clicked() { + rename = true; + } + if ui.button("Separate").clicked() { + mode = Some("separate"); + } + // Enter on a focused Extract or Separate clicks that button, so Enter presses Strip only when neither was clicked. + if buttons(ui, "Strip", enter) && mode.is_none() { + mode = Some("strip"); + } + // Enter on a focused Rename… also presses Strip, so Rename… goes first. + if rename { + rename_from = Some(values); + } else if let Some(mode) = mode { + action = Some(strip_silence_action(mode, values, strip_names)); + } + }); + (action, rename_from) +} + /// Show the open dialog, if any. pub fn show(app: &mut SoundApp, ctx: &egui::Context) { audiosuite_window(app, ctx); @@ -328,44 +497,8 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) { ui.spacing_mut().item_spacing = vec2(8.0, 8.0); match &mut d { Dialog::NewTracks { count, format, kind, timebase, name } => { - ui.horizontal(|ui| { - let create = ui.label("Create"); - ui.add(egui::DragValue::new(count).range(1..=128)).labelled_by(create.id); - ui.label("new"); - let fmt = craft_ui::ComboBox::from_id_salt("nt_fmt").selected_text(format.as_str()).show_ui(ui, |ui| { - for f in soundcraft_model::ChannelFormat::ALL { - ui.selectable_value(format, f.label().to_string(), f.label()); - } - }); - fmt.response.accessible_name("Channel format"); - let kind_combo = craft_ui::ComboBox::from_id_salt("nt_kind") - .selected_text(soundcraft_model::TrackKind::from_id(kind).map_or("Audio Track", |k| k.label())) - .show_ui(ui, |ui| { - for k in soundcraft_model::TrackKind::ALL { - if ui.selectable_label(kind == k.id(), k.label()).clicked() { - *kind = k.id().to_string(); - *name = k.default_name().to_string(); - } - } - }); - kind_combo.response.accessible_name("Track kind"); - ui.label("in"); - let tb = craft_ui::ComboBox::from_id_salt("nt_tb").selected_text(if timebase == "ticks" { "Ticks" } else { "Samples" }).show_ui( - ui, - |ui| { - ui.selectable_value(timebase, "samples".to_string(), "Samples"); - ui.selectable_value(timebase, "ticks".to_string(), "Ticks"); - }, - ); - tb.response.accessible_name("Timebase"); - }); - ui.horizontal(|ui| { - ui.label("Name:"); - crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(name).desired_width(240.0)), "Name"); - }); - if buttons(ui, "Create", enter) { - action = - Some(("track.new".into(), json!({"count": count, "format": format, "kind": kind, "timebase": timebase, "name": name}))); + if let Some(cmd) = new_tracks_form(ui, count, format, kind, timebase, name, enter) { + action = Some(cmd); } } Dialog::NewSession { name, sample_rate, demo } => { @@ -387,24 +520,7 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) { } } Dialog::Bounce { path, format, bit_depth, normalize, dither } => { - ui.horizontal(|ui| { - ui.label("File"); - crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(path).desired_width(420.0)), "File"); - if let Some(pick) = &app.services.pick_save - && ui.button("Browse…").clicked() - && let Some(p) = pick("file.bounce_mix", std::path::Path::new(path.as_str())) - { - match unicode(&p) { - Ok(text) => { - *path = text.to_owned(); - if let Some(picked) = p.extension().and_then(std::ffi::OsStr::to_str).and_then(FileFormat::from_write_extension) { - *format = picked; - } - } - Err(e) => app.ui.status = e.to_string(), - } - } - }); + bounce_file_row(app, ui, path, format); ui.horizontal(|ui| { craft_ui::ComboBox::from_label("Format").selected_text(format.name()).show_ui(ui, |ui| { for &f in FileFormat::WRITE { @@ -463,48 +579,8 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) { } } Dialog::PathPrompt { cmd, path, key, .. } => { - crate::widgets::named_text_field(ui.add(egui::TextEdit::singleline(path).desired_width(420.0)), "Path"); - let mut picked = None; - if let Some(pick) = &app.services.pick_open - && cmd != "session.save_as" - && cmd != "session.save_copy" - && key != "dir" - && ui.button("Browse…").clicked() - && let Some(p) = pick(cmd, &[]) - { - picked = Some(p); - } - if let Some(pick) = &app.services.pick_folder - && key == "dir" - && ui.button("Browse…").clicked() - && let Some(p) = pick(std::path::Path::new(path.as_str())) - { - picked = Some(p); - } - if let Some(pick) = &app.services.pick_save - && (cmd == "session.save_as" || cmd == "session.save_copy") - && ui.button("Browse…").clicked() - && let Some(p) = pick(cmd, std::path::Path::new(path.as_str())) - { - picked = Some(p); - } - match picked.as_deref().map(unicode) { - Some(Ok(text)) => *path = text.to_owned(), - Some(Err(e)) => app.ui.status = e.to_string(), - None => {} - } - let file = soundcraft_engine::io::session_file(path); - let replaces = soundcraft_engine::io::replaces(&app.engine, cmd, &file); - if replaces { - ui.label(format!("“{}” already exists. Do you want to replace it?", file.file_name().unwrap_or_default().to_string_lossy())); - } - if buttons(ui, if replaces { "Replace" } else { "OK" }, enter && !replaces) { - let mut v = json!({}); - v[key.as_str()] = json!(path); - if replaces { - v["replace"] = json!(true); - } - action = Some((cmd.clone(), v)); + if let Some(c) = path_prompt_form(app, ui, cmd, path, key, enter) { + action = Some(c); } } Dialog::Number { cmd, key, value, suffix, .. } => { @@ -543,44 +619,11 @@ pub fn show(app: &mut SoundApp, ctx: &egui::Context) { } } Dialog::StripSilence { threshold, min_ms, pre_ms, post_ms } => { - let before = [*threshold, *min_ms, *pre_ms, *post_ms]; - ui.add(egui::Slider::new(threshold, -96.0..=0.0).text("Threshold dB")); - ui.add(egui::Slider::new(min_ms, 0.0..=4000.0).text("Min strip duration ms")); - ui.add(egui::Slider::new(pre_ms, 0.0..=500.0).text("Clip start pad ms")); - ui.add(egui::Slider::new(post_ms, 0.0..=2000.0).text("Clip end pad ms")); - let values = [*threshold, *min_ms, *pre_ms, *post_ms]; - // The Edit window is drawn before this window, so it draws the rectangles for new values in the next frame. - if values != before { - ui.ctx().request_repaint(); - } - if !strip_names.name.trim().is_empty() { - let names = format!("New clips are named {}, {} and so on.", strip_names.clip_name(0), strip_names.clip_name(1)); - ui.label(egui::RichText::new(names).small().color(Tokens::current().text_dim)); - } - ui.separator(); - ui.horizontal(|ui| { - let mut mode = None; - let mut rename = false; - if ui.button("Extract").clicked() { - mode = Some("extract"); - } - if ui.button("Rename…").clicked() { - rename = true; - } - if ui.button("Separate").clicked() { - mode = Some("separate"); - } - // Enter on a focused Extract or Separate clicks that button, so Enter presses Strip only when neither was clicked. - if buttons(ui, "Strip", enter) && mode.is_none() { - mode = Some("strip"); - } - // Enter on a focused Rename… also presses Strip, so Rename… goes first. - if rename { - rename_from = Some(values); - } else if let Some(mode) = mode { - action = Some(strip_silence_action(mode, values, &strip_names)); - } - }); + let mut values = [*threshold, *min_ms, *pre_ms, *post_ms]; + let (a, r) = strip_silence_form(ui, &mut values, &strip_names, enter); + [*threshold, *min_ms, *pre_ms, *post_ms] = values; + action = a.or(action.take()); + rename_from = r.or(rename_from); } Dialog::StripSilenceRename { names, .. } => { strip_name_fields(ui, names, STRIP_NAME_LABELS); diff --git a/crates/ui-egui/src/dialogs/audiosuite.rs b/crates/ui-egui/src/dialogs/audiosuite.rs index fbc650e6..759565a8 100644 --- a/crates/ui-egui/src/dialogs/audiosuite.rs +++ b/crates/ui-egui/src/dialogs/audiosuite.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{SoundApp, Value, json}; /// The speed ramp's choices in the AudioSuite window: param id, label, default, and (value, name) pairs. pub(super) const SPEED_RAMP_CHOICES: [(&str, &str, f32, &[(f32, &str)]); 3] = [ diff --git a/crates/ui-egui/src/dialogs/files.rs b/crates/ui-egui/src/dialogs/files.rs index a2d8d0a0..4c8e4281 100644 --- a/crates/ui-egui/src/dialogs/files.rs +++ b/crates/ui-egui/src/dialogs/files.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{Dialog, SoundApp, Value, json}; /// When `id` would close a session with unsaved changes, ask first; true when the question is /// open instead. A take in progress is stopped first, so it counts as a change. diff --git a/crates/ui-egui/src/dialogs/strip_silence.rs b/crates/ui-egui/src/dialogs/strip_silence.rs index a128436c..2d5b98e9 100644 --- a/crates/ui-egui/src/dialogs/strip_silence.rs +++ b/crates/ui-egui/src/dialogs/strip_silence.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{Arc, ClipId, Dialog, SoundApp, StripNames, Tokens, TrackId, Value, json, vec2}; /// What Strip keeps on each track: the Strip Silence rectangles. pub type StripRects = Arc)>>; diff --git a/crates/ui-egui/src/edit_window/automation.rs b/crates/ui-egui/src/edit_window/automation.rs index 61d2b3a5..d3127982 100644 --- a/crates/ui-egui/src/edit_window/automation.rs +++ b/crates/ui-egui/src/edit_window/automation.rs @@ -1,5 +1,5 @@ use super::layout::{sample_at, x_of}; -use super::*; +use super::{Align2, AutoParam, Color32, Pos2, Rect, Samples, Session, Shape, Stroke, Tokens, Track, TrackKind, pos2, regular}; pub(super) fn draw_automation(painter: &egui::Painter, s: &Session, track: &Track, p: &AutoParam, tl: Rect, lane: Rect) { let t = Tokens::current(); diff --git a/crates/ui-egui/src/edit_window/clips.rs b/crates/ui-egui/src/edit_window/clips.rs index 6445c95c..5887132d 100644 --- a/crates/ui-egui/src/edit_window/clips.rs +++ b/crates/ui-egui/src/edit_window/clips.rs @@ -1,7 +1,10 @@ use super::drag::fade_drag_preview; use super::input::{clip_gain_icon, near_fade_midpoint}; use super::layout::{sample_at, x_of}; -use super::*; +use super::{ + Align2, AutoParam, Clip, ClipContent, Color32, CornerRadius, Gesture, GridValue, NoteValue, Pos2, Range, Rect, Session, Shape, SoundApp, Stroke, + StrokeKind, TimeFormat, Tokens, Tool, Track, bold, clip_colors, format_position, pos2, regular, vec2, +}; /// The outline of the Strip Silence rectangles. pub fn strip_silence_stroke() -> Stroke { @@ -138,18 +141,39 @@ fn grid_veil(line: Color32, bg: Color32) -> Color32 { } } -/// `sorted`: whether `track.clips()` is sorted by start (worked out once per track and frame). -pub(super) fn draw_clip( - app: &SoundApp, - painter: &egui::Painter, - s: &Session, - track: &Track, - sorted: bool, - clip: &Clip, - tl: Rect, - lane: Rect, +/// What `draw_clip` draws into: the app, painter and session, and the track lane the clips sit on. +#[derive(Clone, Copy)] +pub(super) struct ClipLane<'a> { + pub(super) app: &'a SoundApp, + pub(super) painter: &'a egui::Painter, + pub(super) s: &'a Session, + pub(super) track: &'a Track, + /// Whether `track.clips()` is sorted by start (worked out once per track and frame). + pub(super) sorted: bool, + pub(super) tl: Rect, + pub(super) lane: Rect, +} + +/// Where and in what colours one clip is drawn, shared by the parts of `draw_clip`. +#[derive(Clone, Copy)] +struct ClipGeom<'a> { + r: Rect, + body_r: Rect, + vis: Rect, + x0: f32, + x1: f32, + name_h: f32, + body: Color32, + wave: Color32, + selected: bool, dim: bool, -) { + fade_in: soundcraft_model::Fade, + fade_out: soundcraft_model::Fade, + prev: Option<&'a Clip>, +} + +pub(super) fn draw_clip(cl: &ClipLane<'_>, clip: &Clip, dim: bool) { + let ClipLane { app, painter, s, track, tl, lane, .. } = *cl; let x0 = x_of(s, tl, clip.start); let x1 = x_of(s, tl, clip.end()).max(x0 + 1.0); let r = Rect::from_min_max(pos2(x0, lane.min.y + 1.0), pos2(x1, lane.max.y - 1.0)); @@ -186,6 +210,19 @@ pub(super) fn draw_clip( let vis = body_r.intersect(lane); // The waveform and fade curve share the same temporary envelope every frame. let (fade_in, fade_out) = clip_preview_fades(clip, app.gesture.as_ref().filter(|_| !dim)); + let prev = crossfade_partner(track, clip); + let g = ClipGeom { r, body_r, vis, x0, x1, name_h, body, wave, selected, dim, fade_in, fade_out, prev }; + draw_clip_content(cl, clip, &g); + draw_crossfade_tail(cl, &g); + draw_gain_line(cl, clip, &g); + draw_fades(cl, clip, &g); + draw_clip_overlays(cl, clip, &g); +} + +/// The clip body: waveform, MIDI notes or the offline notice. +fn draw_clip_content(cl: &ClipLane<'_>, clip: &Clip, g: &ClipGeom<'_>) { + let ClipLane { painter, s, track, tl, .. } = *cl; + let ClipGeom { body_r, vis, body, wave, selected, fade_in, fade_out, .. } = *g; match &clip.content { _ if track.view == "blocks" => {} ClipContent::Audio { source, .. } => { @@ -247,7 +284,12 @@ pub(super) fn draw_clip( } } } - let prev = crossfade_partner(track, clip); +} + +/// The waveform of the clip this one crossfades with, drawn over the overlap. +fn draw_crossfade_tail(cl: &ClipLane<'_>, g: &ClipGeom<'_>) { + let ClipLane { painter, s, track, tl, .. } = *cl; + let ClipGeom { body_r, vis, x0, x1, dim, prev, .. } = *g; if s.edit.flag("waveform.overlapped_crossfades") && track.view != "blocks" && let Some(prev) = prev @@ -262,6 +304,12 @@ pub(super) fn draw_clip( }; crate::waveform::draw(crate::waveform::Canvas { painter: &painter.with_clip_rect(xf), s, tl }, prev, audio, track.channels(), body_r, tail); } +} + +/// The clip gain line and its icon. +fn draw_gain_line(cl: &ClipLane<'_>, clip: &Clip, g: &ClipGeom<'_>) { + let ClipLane { painter, s, track, tl, lane, .. } = *cl; + let ClipGeom { body_r, x0, x1, dim, .. } = *g; // Clip gain line. let show_gain = s.edit.flag("view.clip.gain_line") && (!clip.gain_env.is_empty() || clip.gain_db.abs() > 0.01 || track.view == "clip_gain"); if show_gain && body_r.height() > 12.0 { @@ -286,6 +334,12 @@ pub(super) fn draw_clip( painter.rect_filled(Rect::from_center_size(pos2(icon.center().x, icon.center().y + 2.0), vec2(icon.width(), 3.0)), 1.0, col); painter.text(pos2(icon.max.x + 3.0, icon.center().y), Align2::LEFT_CENTER, format!("{:+.1} dB", clip.gain_db), regular(9.5), col); } +} + +/// The fade curves, their midpoint handles and the crossfade partner's fade-out. +fn draw_fades(cl: &ClipLane<'_>, clip: &Clip, g: &ClipGeom<'_>) { + let ClipLane { app, painter, s, track, tl, .. } = *cl; + let ClipGeom { body_r, vis, x0, x1, body, dim, fade_in, fade_out, prev, .. } = *g; let fade_col = Color32::from_rgba_unmultiplied(255, 255, 255, 90); for (side, fade) in [(true, fade_in), (false, fade_out)] { if fade.len <= 0 { @@ -309,6 +363,12 @@ pub(super) fn draw_clip( let pts = fade_curve_points(c.fade_out, false, a, b, body_r, painter.ctx().pixels_per_point()); painter.with_clip_rect(vis).add(Shape::line(pts, Stroke::new(1.2, fade_col))); } +} + +/// Optional clip overlays (View › Clip). +fn draw_clip_overlays(cl: &ClipLane<'_>, clip: &Clip, g: &ClipGeom<'_>) { + let ClipLane { painter, s, track, sorted, tl, lane, .. } = *cl; + let ClipGeom { r, name_h, .. } = *g; // Optional clip overlays (View › Clip). let ov = Color32::from_rgb(16, 16, 16); if s.edit.flag("view.clip.rating") && clip.rating > 0 && name_h > 0.0 { @@ -323,23 +383,7 @@ pub(super) fn draw_clip( )); painter.line_segment([pos2(x, r.min.y + name_h), pos2(x, r.max.y)], Stroke::new(1.0, Color32::from_rgba_unmultiplied(255, 255, 255, 90))); } - let time_text = if s.edit.flag("view.clip.time.current") { - Some(s.format_position(clip.start, s.edit.main_counter)) - } else if s.edit.flag("view.clip.time.original") || s.edit.flag("view.clip.time.user") { - clip.source().and_then(|src| s.source(src)).map(|src| { - format_position( - i64::try_from(src.time_reference).unwrap_or(0).saturating_add(clip.source_offset()).saturating_sub(s.timecode_start), - TimeFormat::Timecode, - s.sample_rate, - &s.tempo, - s.frame_rate, - 0, - 0, - ) - }) - } else { - None - }; + let time_text = clip_time_text(s, clip); if let Some(tt) = time_text && r.height() > 30.0 { @@ -369,7 +413,27 @@ pub(super) fn draw_clip( if clip.edit_locked || clip.time_locked { painter.text(pos2(r.max.x - 10.0, r.min.y + 7.0), Align2::CENTER_CENTER, "🔒", regular(9.0), Color32::WHITE); } - let _ = app; +} + +/// The clip's start (or original) time label for the overlays, when one is shown. +fn clip_time_text(s: &Session, clip: &Clip) -> Option { + if s.edit.flag("view.clip.time.current") { + Some(s.format_position(clip.start, s.edit.main_counter)) + } else if s.edit.flag("view.clip.time.original") || s.edit.flag("view.clip.time.user") { + clip.source().and_then(|src| s.source(src)).map(|src| { + format_position( + i64::try_from(src.time_reference).unwrap_or(0).saturating_add(clip.source_offset()).saturating_sub(s.timecode_start), + TimeFormat::Timecode, + s.sample_rate, + &s.tempo, + s.frame_rate, + 0, + 0, + ) + }) + } else { + None + } } /// The earlier clip of a crossfade pair: the one before `clip` in its playlist, overlapping its start. diff --git a/crates/ui-egui/src/edit_window/drag.rs b/crates/ui-egui/src/edit_window/drag.rs index 09c6b326..9ec78710 100644 --- a/crates/ui-egui/src/edit_window/drag.rs +++ b/crates/ui-egui/src/edit_window/drag.rs @@ -1,7 +1,9 @@ use super::automation::{auto_value, automation_id, lane_range, plugin_range}; use super::input::{new_midi_clip, snap}; use super::layout::{lane_rect, rect, sample_at}; -use super::*; +use super::{ + Arc, AutoParam, Clip, ClipId, Dialog, EditMode, Gesture, HashSet, Pos2, Range, Rect, Samples, Session, SoundApp, Spot, TrackId, json, ops, pos2, +}; /// The same bounded range drives the preview and the committed fade. pub(super) fn fade_drag_range(clip: &Clip, fade_in: bool, to: Samples) -> Option { diff --git a/crates/ui-egui/src/edit_window/header.rs b/crates/ui-egui/src/edit_window/header.rs index df0d1905..f8a002a2 100644 --- a/crates/ui-egui/src/edit_window/header.rs +++ b/crates/ui-egui/src/edit_window/header.rs @@ -1,12 +1,34 @@ use super::tracks::{click_track, folder_depth, height_targets, in_edit_window, track_context_menu, view_label, view_menu}; -use super::*; +use super::{ + Align2, AutoParam, Color32, Pos2, Rect, Sense, Shape, SoundApp, Stroke, Tokens, Track, TrackHeight, TrackId, TrackKind, Ui, bold, groove, json, + level_pos, meter_pos, pos2, raised_key, regular, rgb, shade, vec2, +}; /// Draw one track header: resize edge, selection and context menu, folder/options control, automation-lane toggle, /// name and the remaining track controls. pub(super) fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, selected: bool, head_bg: Color32) { - let t = Tokens::current(); let id = track.id; - // Drag the bottom edge to resize (snaps to the height presets). + // Click on header background selects the track. + let bg = ui.interact(head, ui.scope_id().with(("hdr", id.0)), Sense::click()); + if bg.clicked() { + click_track(app, id, ui.input(|i| i.modifiers), in_edit_window); + } + bg.context_menu(|ui| track_context_menu(app, ui, id)); + resize_edge(app, ui, track, head); + strip_buttons(app, ui, track, head); + name_plate(app, ui, track, head, selected); + playlist_menu(app, ui, track, head, head_bg); + toggle_keys(app, ui, track, head); + view_selector(app, ui, track, head); + voice_row(app, ui, track, head); + field_row(app, ui, track, head, head_bg); + let md = app.meters.get(&id).copied().unwrap_or_default(); + header_meter(ui, Rect::from_min_max(pos2(head.max.x - 8.0, head.min.y + 1.0), pos2(head.max.x, head.max.y - 1.5)), &md, track.channels() >= 2); +} + +/// Drag the bottom edge of the header to resize the track (snaps to the height presets). +fn resize_edge(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect) { + let id = track.id; let edge = Rect::from_min_max(pos2(head.min.x, head.max.y - 4.0), pos2(head.max.x, head.max.y + 2.0)); let eresp = ui.interact(edge, ui.scope_id().with(("hedge", id.0)), Sense::drag()); if eresp.hovered() || eresp.dragged() { @@ -34,17 +56,15 @@ pub(super) fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: ui.ctx().request_repaint(); } } +} + +/// The strip's round options/folder button and the automation-lane toggle. +fn strip_buttons(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect) { + let id = track.id; let h = head.height(); - // Click on header background selects the track. - let bg = ui.interact(head, ui.scope_id().with(("hdr", id.0)), Sense::click()); - if bg.clicked() { - click_track(app, id, ui.input(|i| i.modifiers), in_edit_window); - } - bg.context_menu(|ui| track_context_menu(app, ui, id)); let at = |x: f32, y: f32, w: f32, h: f32| Rect::from_min_size(pos2(head.min.x + x, head.min.y + y), vec2(w, h)); let strip = rgb(track.color); let ink = shade(strip, -0.8); - // The strip's round button opens a folder, or any other track's options. let round = at(2.0, 2.0, 10.0, 10.0); let rresp = ui.interact(round, ui.scope_id().with(("options", id.0)), Sense::click()); ui.painter().circle_filled(round.center(), 5.0, shade(strip, if rresp.hovered() { 0.6 } else { 0.45 })); @@ -76,7 +96,13 @@ pub(super) fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: let _ = app.run("track.view", json!({"track": id.0, "view": view})); } } - // Name, highlighted when the track is selected, and the playlist menu beside it. +} + +/// The track name, highlighted when selected, with click, rename and drag-to-reorder. +fn name_plate(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, selected: bool) { + let t = Tokens::current(); + let id = track.id; + let at = |x: f32, y: f32, w: f32, h: f32| Rect::from_min_size(pos2(head.min.x + x, head.min.y + y), vec2(w, h)); let indent = folder_depth(app.engine.session(), track) as f32 * 10.0; let name_r = at(16.0 + indent, 1.5, 97.0 - indent, 15.0); if selected { @@ -128,6 +154,13 @@ pub(super) fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: let _ = app.run("track.move", json!({"track": id.0, "to": to})); } } +} + +/// The playlist menu button beside the name. +fn playlist_menu(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, head_bg: Color32) { + let t = Tokens::current(); + let id = track.id; + let at = |x: f32, y: f32, w: f32, h: f32| Rect::from_min_size(pos2(head.min.x + x, head.min.y + y), vec2(w, h)); let pl_r = at(114.0, 1.0, 11.0, 14.5); let presp = ui.interact(pl_r, ui.scope_id().with(("pl", id.0)), Sense::click()); ui.painter().rect_filled(pl_r, 1.5, shade(head_bg, if presp.hovered() { -0.2 } else { -0.35 })); @@ -149,6 +182,13 @@ pub(super) fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: } } }); +} + +/// Record, input, solo and mute keys. +fn toggle_keys(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect) { + let id = track.id; + let h = head.height(); + let at = |x: f32, y: f32, w: f32, h: f32| Rect::from_min_size(pos2(head.min.x + x, head.min.y + y), vec2(w, h)); let m = &track.mixer; if h >= 37.0 { let (records, mixes) = (track.kind.has_playlist(), track.kind != TrackKind::Master); @@ -165,6 +205,14 @@ pub(super) fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: } } } +} + +/// The track view selector. +fn view_selector(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect) { + let t = Tokens::current(); + let id = track.id; + let h = head.height(); + let at = |x: f32, y: f32, w: f32, h: f32| Rect::from_min_size(pos2(head.min.x + x, head.min.y + y), vec2(w, h)); if h >= 58.0 { let vresp = header_selector( ui, @@ -177,6 +225,15 @@ pub(super) fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: ); egui::Popup::menu(&vresp).show(|ui| view_menu(app, ui, track)); } +} + +/// Freeze, voice and automation mode. +fn voice_row(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect) { + let t = Tokens::current(); + let id = track.id; + let h = head.height(); + let at = |x: f32, y: f32, w: f32, h: f32| Rect::from_min_size(pos2(head.min.x + x, head.min.y + y), vec2(w, h)); + let m = &track.mixer; if h >= 76.0 { if matches!(track.kind, TrackKind::Audio | TrackKind::Instrument | TrackKind::Aux) { let r = at(17.0, 57.0, 13.0, 17.0); @@ -220,6 +277,14 @@ pub(super) fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: } }); } +} + +/// Timebase key and the instrument or elastic field. +fn field_row(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, head_bg: Color32) { + let t = Tokens::current(); + let id = track.id; + let h = head.height(); + let at = |x: f32, y: f32, w: f32, h: f32| Rect::from_min_size(pos2(head.min.x + x, head.min.y + y), vec2(w, h)); if h >= 94.0 { if track.kind.has_playlist() { let r = at(17.0, 75.0, 13.0, 17.0); @@ -277,8 +342,6 @@ pub(super) fn track_header(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: } } } - let md = app.meters.get(&id).copied().unwrap_or_default(); - header_meter(ui, Rect::from_min_max(pos2(head.max.x - 8.0, head.min.y + 1.0), pos2(head.max.x, head.max.y - 1.5)), &md, track.channels() >= 2); } /// A small menu arrow, pointing down, or right for a closed folder. diff --git a/crates/ui-egui/src/edit_window/input.rs b/crates/ui-egui/src/edit_window/input.rs index 9875a1e6..88538b99 100644 --- a/crates/ui-egui/src/edit_window/input.rs +++ b/crates/ui-egui/src/edit_window/input.rs @@ -2,7 +2,10 @@ use super::automation::{automation_id, breakpoint_at, plugin_range, shown_automa use super::clips::{clip_name_height, fade_control_position}; use super::drag::{drag_snap, fade_curve_drag_value, fade_drag_range, spot_or_run}; use super::layout::{sample_at, x_of}; -use super::*; +use super::{ + Align2, AutoParam, Clip, ClipContent, ClipId, Color32, Dialog, EditMode, Gesture, GridValue, NoteValue, Pos2, Range, Rect, Samples, Sense, + Session, SoundApp, Spot, Stroke, StrokeKind, Tokens, Tool, Track, TrackId, Ui, json, ops, pos2, regular, vec2, +}; // ---- interaction ----------------------------------------------------------------------------- @@ -86,9 +89,40 @@ fn clip_gain_popup(ctx: &egui::Context, fader_bottom: Pos2, pos: f32) { p.text(pos2(panel.center().x, panel.min.y + 12.0), Align2::CENTER_CENTER, format!("{db:+.1}"), regular(10.5), t.counter_text); } -/// Handle one track lane: clip context menus, drag-and-drop sources, sampler drops, and the active tool's selection, -/// trim, fade, automation and clip editing. -pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, lane: Rect, tl: Rect) { +/// What a lane interaction knows before it hit-tests: the lane, the session and the press point. +struct LaneInput<'a> { + track: &'a Track, + lane: Rect, + tl: Rect, + s: &'a Session, + tool: Tool, + origin: Pos2, + at_origin: Samples, + auto_view: Option, +} + +/// What is under the pointer: a breakpoint, fade handles and the clip body. +struct LaneHits<'a> { + point_hit: Option<(AutoParam, Samples)>, + fade_handle: Option<(&'a Clip, bool)>, + curve_handle: Option<(&'a Clip, bool)>, + hit: Option, +} + +/// Everything the lane's gesture, click and cursor handling reads. +struct LaneCtx<'a> { + inp: LaneInput<'a>, + hits: LaneHits<'a>, + mods: egui::Modifiers, + p: Pos2, + at: Samples, + eff: Tool, + fade_corner: Option, + selector_press: bool, +} + +/// The lane's response, with its clip context menu and the drops it accepts. +fn lane_menu_and_drops(app: &mut SoundApp, ui: &mut Ui, track: &Track, lane: Rect, tl: Rect) -> egui::Response { let id = ui.scope_id().with(("lane", track.id.0)); let resp = ui.interact(lane, id, Sense::click_and_drag()); // The clip menu is attached before the early return below: once the menu is open the pointer @@ -121,16 +155,15 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l if track.kind == soundcraft_model::TrackKind::Instrument { crate::sampler_window::drop_target(app, ui.ctx(), lane, track.id); } - let s = app.engine.session_arc(); - let tool = s.edit.tool; - let mods = ui.input(|i| i.modifiers); - let Some(p) = resp.interact_pointer_pos().or_else(|| resp.hover_pos()) else { return }; - let at = sample_at(&s, tl, p.x).max(0); - let origin = if resp.drag_started() { ui.input(|i| i.pointer.press_origin()).unwrap_or(p) } else { p }; - let at_origin = sample_at(&s, tl, origin.x).max(0); - let auto_view = AutoParam::parse(&track.view); + resp +} + +/// Hit-test the press point against breakpoints, fade handles and clip bodies. +fn hit_test<'a>(inp: &LaneInput<'a>) -> LaneHits<'a> { + let (track, lane, tl, s, tool, origin, at_origin) = (inp.track, inp.lane, inp.tl, inp.s, inp.tool, inp.origin, inp.at_origin); + let auto_view = &inp.auto_view; let point_hit = if matches!(tool, Tool::Smart | Tool::Grabber | Tool::Pencil) { - shown_automation(track).and_then(|param| breakpoint_at(&s, tl, lane, track, ¶m, origin).map(|at| (param, at))) + shown_automation(track).and_then(|param| breakpoint_at(s, tl, lane, track, ¶m, origin).map(|at| (param, at))) } else { None }; @@ -143,34 +176,40 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l .iter() .rev() .filter(|c| body_hit.is_none_or(|front| front.id == c.id || front.start == c.end())) - .find_map(|c| fade_handle_at(&s, tl, lane, c, origin).map(|side| (c, side))) + .find_map(|c| fade_handle_at(s, tl, lane, c, origin).map(|side| (c, side))) } else { None }; let curve_handle = if tool == Tool::Smart && auto_view.is_none() && fade_handle.is_none() { - body_hit.and_then(|c| fade_curve_handle_at(&s, tl, lane, c, origin).map(|side| (c, side))) + body_hit.and_then(|c| fade_curve_handle_at(s, tl, lane, c, origin).map(|side| (c, side))) } else { None }; let hit = fade_handle.or(curve_handle).map(|(c, _)| c).or(body_hit).cloned(); - let upper = origin.y < lane.center().y; - let near_edge = |c: &Clip| -> Option { - let xs = x_of(&s, tl, c.start); - let xe = x_of(&s, tl, c.end()); - if (origin.x - xs).abs() < 6.0 { - Some(true) - } else if (origin.x - xe).abs() < 6.0 { - Some(false) - } else { - None - } - }; - // Effective tool under the Smart tool; on a breakpoint every tool that edits one grabs it. - let eff = match tool { - _ if point_hit.is_some() => Tool::Grabber, + LaneHits { point_hit, fade_handle, curve_handle, hit } +} + +/// Which edge of `c` the press point is within a few points of: `Some(true)` the start, `Some(false)` the end. +fn near_edge(inp: &LaneInput<'_>, c: &Clip) -> Option { + let xs = x_of(inp.s, inp.tl, c.start); + let xe = x_of(inp.s, inp.tl, c.end()); + if (inp.origin.x - xs).abs() < 6.0 { + Some(true) + } else if (inp.origin.x - xe).abs() < 6.0 { + Some(false) + } else { + None + } +} + +/// Effective tool under the Smart tool; on a breakpoint every tool that edits one grabs it. +fn effective_tool(inp: &LaneInput<'_>, hits: &LaneHits<'_>) -> Tool { + let upper = inp.origin.y < inp.lane.center().y; + match inp.tool { + _ if hits.point_hit.is_some() => Tool::Grabber, Tool::Smart => { - if let Some(c) = &hit { - if near_edge(c).is_some() { + if let Some(c) = &hits.hit { + if near_edge(inp, c).is_some() { Tool::Trim } else if upper { Tool::Selector @@ -182,23 +221,18 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l } } other => other, - }; - if resp.hovered() { - let icon = match eff { - Tool::Trim => egui::CursorIcon::ResizeHorizontal, - Tool::Grabber => egui::CursorIcon::Grab, - Tool::Selector => egui::CursorIcon::Text, - Tool::Zoom => egui::CursorIcon::ZoomIn, - Tool::Pencil => egui::CursorIcon::Crosshair, - _ => egui::CursorIcon::Default, - }; - ui.ctx().set_cursor_icon(icon); } - let fade_corner = if tool == Tool::Smart && point_hit.is_none() { +} + +/// The fade corner (`true` for the fade-in) the Smart tool is pressing, if any. +fn fade_corner_at(inp: &LaneInput<'_>, hits: &LaneHits<'_>) -> Option { + let (s, tl, lane, origin) = (inp.s, inp.tl, inp.lane, inp.origin); + let (tool, point_hit, fade_handle, hit) = (inp.tool, &hits.point_hit, hits.fade_handle, &hits.hit); + if tool == Tool::Smart && point_hit.is_none() { fade_handle.map(|(_, side)| side).or_else(|| { hit.as_ref().and_then(|c| { - let xs = x_of(&s, tl, c.start); - let xe = x_of(&s, tl, c.end()); + let xs = x_of(s, tl, c.start); + let xe = x_of(s, tl, c.end()); let top = origin.y < lane.min.y + lane.height() * 0.28; if !top { None @@ -213,25 +247,15 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l }) } else { None - }; - if fade_corner.is_some() && resp.hovered() { - ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeNwSe); } - if curve_handle.is_some() && resp.hovered() { - ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeVertical); - } - let gain_icon_at = |q: Pos2| { - let c = clip_at(track, sample_at(&s, tl, q.x).max(0)).filter(|_| auto_view.is_none())?; - clip_gain_icon(&s, tl, lane, c).filter(|r| r.contains(q)).map(|_| c.clone()) - }; - if resp.hovered() && gain_icon_at(p).is_some() { - ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeVertical); - } - let selector_press = eff == Tool::Selector && fade_corner.is_none() && curve_handle.is_none() && gain_icon_at(origin).is_none(); - if selector_press && resp.is_pointer_button_down_on() && ui.input(|i| i.pointer.primary_pressed()) { - place_insertion(app, ui, &s, track.id, at, mods.shift); - } - let gain_drag = if resp.drag_started() { gain_icon_at(origin) } else { None }; +} + +/// Start the gesture a press begins: a breakpoint, clip gain, a fade or the tool's own. +fn start_gesture(app: &mut SoundApp, ui: &mut Ui, c: &LaneCtx<'_>, resp: &egui::Response, gain_drag: Option) { + let LaneCtx { inp, hits, mods, p, at, eff, fade_corner, .. } = c; + let (track, lane, s, origin, at_origin, auto_view) = (inp.track, inp.lane, inp.s, inp.origin, inp.at_origin, &inp.auto_view); + let (point_hit, curve_handle, hit) = (&hits.point_hit, hits.curve_handle, &hits.hit); + let (mods, p, at, eff, fade_corner) = (*mods, *p, *at, *eff, *fade_corner); if resp.drag_started() && let Some((param, at)) = &point_hit { @@ -252,7 +276,7 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l { let fade = if fade_in { c.fade_in } else { c.fade_out }; let anchor = (origin.y, fade.gain(0.5)); - let height = (lane.height() - 2.0 - clip_name_height(&s, lane)).max(1.0); + let height = (lane.height() - 2.0 - clip_name_height(s, lane)).max(1.0); app.gesture = Some(Gesture::FadeCurve { clip: c.id, fade_in, curvature: fade_curve_drag_value(fade, anchor, height, p.y), anchor, height }); ui.ctx().request_repaint(); } else if resp.drag_started() @@ -267,12 +291,12 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l ui.ctx().request_repaint(); } else if resp.drag_started() { app.gesture = match eff { - Tool::Selector | Tool::Smart => Some(Gesture::Select { track: track.id, anchor: snap(&s, at_origin), tracks: vec![track.id] }), + Tool::Selector | Tool::Smart => Some(Gesture::Select { track: track.id, anchor: snap(s, at_origin), tracks: vec![track.id] }), Tool::Grabber => hit.as_ref().map(|c| { let clips = if s.edit.selected_clips.contains(&c.id) { s.edit.selected_clips.clone() } else { vec![c.id] }; Gesture::MoveClips { clips, grab_at: at_origin, delta: 0, from_track: track.id, to_track: track.id } }), - Tool::Trim => hit.as_ref().map(|c| match near_edge(c) { + Tool::Trim => hit.as_ref().map(|c| match near_edge(inp, c) { Some(false) => Gesture::TrimEnd { clip: c.id, track: track.id, to: c.end() }, _ if at_origin - c.start < c.end() - at_origin => Gesture::TrimStart { clip: c.id, track: track.id, to: c.start }, _ => Gesture::TrimEnd { clip: c.id, track: track.id, to: c.end() }, @@ -285,6 +309,11 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l Tool::Zoom => Some(Gesture::ZoomBox { start: origin.x }), }; } +} + +/// While a clip-gain gesture is dragged, the fader follows the pointer. +fn drag_clip_gain(app: &mut SoundApp, ui: &mut Ui, c: &LaneCtx<'_>, resp: &egui::Response) { + let (track, s, mods, p) = (c.inp.track, c.inp.s, c.mods, c.p); if let Some(Gesture::ClipGain { clip, track: t, pos, anchor, fine, fader_bottom }) = &mut app.gesture && *t == track.id && resp.dragged() @@ -303,6 +332,13 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeVertical); clip_gain_popup(ui.ctx(), *fader_bottom, *pos); } +} + +/// Click handling for the lane: breakpoints, clip selection, spot dialogs, zoom and pencil points. +fn lane_click(app: &mut SoundApp, ui: &mut Ui, c: &LaneCtx<'_>, resp: &egui::Response) { + let (track, lane, s, auto_view) = (c.inp.track, c.inp.lane, c.inp.s, &c.inp.auto_view); + let (point_hit, curve_handle, hit) = (&c.hits.point_hit, c.hits.curve_handle, &c.hits.hit); + let (mods, p, at, eff, selector_press) = (c.mods, c.p, c.at, c.eff, c.selector_press); if resp.clicked() && let Some((param, at)) = &point_hit { @@ -319,14 +355,14 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l ids.push(c.id.0); let _ = app.run("edit.select", json!({"clips": ids})); if s.edit.edit_mode == EditMode::Spot { - app.dialogs.open = Spot::clips(&s, vec![c.id], None, 0).map(Dialog::Spot); + app.dialogs.open = Spot::clips(s, vec![c.id], None, 0).map(Dialog::Spot); } } } Tool::Trim if s.edit.edit_mode == EditMode::Spot && hit.is_some() => { if let Some(c) = &hit { let start = at - c.start < c.end() - at; - app.dialogs.open = Spot::trim(&s, c.id, start, if start { c.start } else { c.end() }).map(Dialog::Spot); + app.dialogs.open = Spot::trim(s, c.id, start, if start { c.start } else { c.end() }).map(Dialog::Spot); } } Tool::Zoom => { @@ -352,11 +388,11 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l Some(c) => { let _ = app.run("edit.select", json!({"clips": [c.id.0]})); } - None => new_midi_clip(app, &s, track.id, at, at), + None => new_midi_clip(app, s, track.id, at, at), } } } - _ if !selector_press => place_insertion(app, ui, &s, track.id, at, mods.shift), + _ if !selector_press => place_insertion(app, ui, s, track.id, at, mods.shift), _ => {} } } @@ -373,6 +409,58 @@ pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, l } } +/// Handle one track lane: clip context menus, drag-and-drop sources, sampler drops, and the active tool's selection, +/// trim, fade, automation and clip editing. +pub(super) fn lane_interaction(app: &mut SoundApp, ui: &mut Ui, track: &Track, lane: Rect, tl: Rect) { + let resp = lane_menu_and_drops(app, ui, track, lane, tl); + let s = app.engine.session_arc(); + let tool = s.edit.tool; + let mods = ui.input(|i| i.modifiers); + let Some(p) = resp.interact_pointer_pos().or_else(|| resp.hover_pos()) else { return }; + let at = sample_at(&s, tl, p.x).max(0); + let origin = if resp.drag_started() { ui.input(|i| i.pointer.press_origin()).unwrap_or(p) } else { p }; + let at_origin = sample_at(&s, tl, origin.x).max(0); + let auto_view = AutoParam::parse(&track.view); + let inp = LaneInput { track, lane, tl, s: &s, tool, origin, at_origin, auto_view }; + let hits = hit_test(&inp); + let eff = effective_tool(&inp, &hits); + if resp.hovered() { + let icon = match eff { + Tool::Trim => egui::CursorIcon::ResizeHorizontal, + Tool::Grabber => egui::CursorIcon::Grab, + Tool::Selector => egui::CursorIcon::Text, + Tool::Zoom => egui::CursorIcon::ZoomIn, + Tool::Pencil => egui::CursorIcon::Crosshair, + _ => egui::CursorIcon::Default, + }; + ui.ctx().set_cursor_icon(icon); + } + let fade_corner = fade_corner_at(&inp, &hits); + let curve_handle = hits.curve_handle; + if fade_corner.is_some() && resp.hovered() { + ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeNwSe); + } + if curve_handle.is_some() && resp.hovered() { + ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeVertical); + } + let gain_icon_at = |q: Pos2| { + let c = clip_at(track, sample_at(&s, tl, q.x).max(0)).filter(|_| inp.auto_view.is_none())?; + clip_gain_icon(&s, tl, lane, c).filter(|r| r.contains(q)).map(|_| c.clone()) + }; + if resp.hovered() && gain_icon_at(p).is_some() { + ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeVertical); + } + let selector_press = eff == Tool::Selector && fade_corner.is_none() && curve_handle.is_none() && gain_icon_at(origin).is_none(); + if selector_press && resp.is_pointer_button_down_on() && ui.input(|i| i.pointer.primary_pressed()) { + place_insertion(app, ui, &s, track.id, at, mods.shift); + } + let gain_drag = if resp.drag_started() { gain_icon_at(origin) } else { None }; + let cx = LaneCtx { inp, hits, mods, p, at, eff, fade_corner, selector_press }; + start_gesture(app, ui, &cx, &resp, gain_drag); + drag_clip_gain(app, ui, &cx, &resp); + lane_click(app, ui, &cx, &resp); +} + fn place_insertion(app: &mut SoundApp, ui: &Ui, s: &Session, track: TrackId, at: Samples, extend: bool) { let at = snap(s, at); let sel = s.edit.selection; diff --git a/crates/ui-egui/src/edit_window/layout.rs b/crates/ui-egui/src/edit_window/layout.rs index afc114bb..83452fcc 100644 --- a/crates/ui-egui/src/edit_window/layout.rs +++ b/crates/ui-egui/src/edit_window/layout.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{Rect, Samples, Session, Track, pos2}; pub const HEADER_W: f32 = 136.0; diff --git a/crates/ui-egui/src/edit_window/overlay.rs b/crates/ui-egui/src/edit_window/overlay.rs index 985e392c..d5a3934a 100644 --- a/crates/ui-egui/src/edit_window/overlay.rs +++ b/crates/ui-egui/src/edit_window/overlay.rs @@ -2,7 +2,7 @@ use super::drag::drag_plan; use super::input::midi_clip_span; use super::layout::x_of; use super::scroll::{FollowFrame, follow_update}; -use super::*; +use super::{Color32, Gesture, Rect, Samples, Shape, SoundApp, Stroke, StrokeKind, Tokens, TrackId, Ui, json, ops, pos2, rgb}; /// Selection overlay, insertion point and playhead across rulers and tracks. /// While playing, auto-scroll keeps the playhead in view for the follow diff --git a/crates/ui-egui/src/edit_window/rulers.rs b/crates/ui-egui/src/edit_window/rulers.rs index 250549cc..c665f907 100644 --- a/crates/ui-egui/src/edit_window/rulers.rs +++ b/crates/ui-egui/src/edit_window/rulers.rs @@ -1,6 +1,9 @@ use super::input::snap; use super::layout::{COLUMN_TITLES_H, COLUMNS, Column, HEADER_W, RULER_H, edit_columns, header_width, sample_at, x_of}; -use super::*; +use super::{ + Align2, Color32, GridValue, NoteValue, Range, Rect, Samples, Sense, Session, Shape, SoundApp, Stroke, TimeFormat, Tokens, Ui, bold, + format_position, json, pos2, regular, rgb, vec2, +}; // ---- rulers ---------------------------------------------------------------------------------- @@ -170,7 +173,7 @@ pub(super) fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect, } else { soundcraft_time::FrameRate::Fps25 }; - timebase_ruler(ui, &painter, &s, tl, row, view, TimeFormat::Timecode, alt, i + 1 == rulers.len()); + timebase_ruler(&painter, &s, &RulerRow { tl, row, view, bottom: i + 1 == rulers.len() }, TimeFormat::Timecode, alt); } "markers" | "markers2" | "markers3" | "markers4" | "markers5" | "chords" => { let lane: u8 = match id.as_str() { @@ -225,7 +228,7 @@ pub(super) fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect, } _ => { if let Some(f) = fmt { - timebase_ruler(ui, &painter, &s, tl, row, view, f, s.frame_rate, i + 1 == rulers.len()); + timebase_ruler(&painter, &s, &RulerRow { tl, row, view, bottom: i + 1 == rulers.len() }, f, s.frame_rate); // Clicking a timebase ruler locates. let resp = ui.interact(row, ui.scope_id().with(("ruler", id.as_str())), Sense::click_and_drag()); if (resp.clicked() || resp.dragged()) @@ -247,17 +250,17 @@ pub(super) fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect, ui.painter().line_segment([pos2(area.min.x, area.max.y - 1.0), pos2(area.max.x, area.max.y - 1.0)], Stroke::new(1.0, t.border)); } -fn timebase_ruler( - _ui: &Ui, - painter: &egui::Painter, - s: &Session, +/// Where a timebase ruler sits: the timeline, its row and the visible range, and whether it is the bottom ruler. +#[derive(Clone, Copy)] +struct RulerRow { tl: Rect, row: Rect, view: Range, - f: TimeFormat, - rate: soundcraft_time::FrameRate, bottom: bool, -) { +} + +fn timebase_ruler(painter: &egui::Painter, s: &Session, place: &RulerRow, f: TimeFormat, rate: soundcraft_time::FrameRate) { + let RulerRow { tl, row, view, bottom } = *place; let t = Tokens::current(); let sr = s.sample_rate; let spp = s.edit.zoom.samples_per_px; diff --git a/crates/ui-egui/src/edit_window/scroll.rs b/crates/ui-egui/src/edit_window/scroll.rs index c54b34b7..0b78e262 100644 --- a/crates/ui-egui/src/edit_window/scroll.rs +++ b/crates/ui-egui/src/edit_window/scroll.rs @@ -1,7 +1,7 @@ use super::layout::track_height; use super::tracks::in_edit_window; use super::universe::content_end; -use super::*; +use super::{Rect, Samples, Sense, SoundApp, Tokens, TrackId, Ui, json, pos2, vec2}; /// Scroll Into View: track `id` to the top, as far as the scroll bar reaches. A track the Edit /// window doesn't show (hidden, or in a closed folder) leaves the view where it is. diff --git a/crates/ui-egui/src/edit_window/tests.rs b/crates/ui-egui/src/edit_window/tests.rs index f7f94bbe..8b6fff5f 100644 --- a/crates/ui-egui/src/edit_window/tests.rs +++ b/crates/ui-egui/src/edit_window/tests.rs @@ -591,128 +591,129 @@ fn waveform_geometry(h: &Harness, clip_rect: Rect) -> Vec> { .collect() } +/// Replace the clip's mono source with a stereo one whose right channel is half the left, and make the track stereo. +fn make_stereo(h: &mut Harness<'static, SoundApp>, tid: soundcraft_model::TrackId, source: soundcraft_model::SourceId) { + let audio = h.state().engine.session().pool.get(source).unwrap().clone(); + let left = audio.buffer.channels[0].clone(); + let right = left.iter().map(|sample| sample * 0.5).collect(); + let audio = + soundcraft_model::SourceAudio::new(soundcraft_audio_io::AudioBuffer { sample_rate: audio.buffer.sample_rate, channels: vec![left, right] }); + let s = h.state_mut().engine.session_mut(); + s.pool.insert(source, std::sync::Arc::new(audio)); + s.track_mut(tid).unwrap().format = soundcraft_model::ChannelFormat::Stereo; +} + +/// Where a fade drag presses and where it releases: the curve midpoint (bend) or the fade's end (length). +fn fade_drag_points( + session: &soundcraft_model::Session, + tl: Rect, + lane: Rect, + clip: soundcraft_model::ClipId, + fade_in: bool, + bend: bool, +) -> (Pos2, Pos2) { + let c = session.find_clip(clip).unwrap().1; + let (xs, xe) = (x_of(session, tl, c.start), x_of(session, tl, c.end())); + if bend { + let midpoint = fade_midpoint(session, tl, lane, c, fade_in).unwrap(); + (midpoint, midpoint - vec2(0.0, 16.0)) + } else { + (pos2(if fade_in { xs + 6.0 } else { xe - 6.0 }, lane.min.y + 4.0), pos2(xs + (xe - xs) * if fade_in { 0.7 } else { 0.3 }, lane.min.y + 4.0)) + } +} + +fn fade_waveform_case(channels: usize, fade_in: bool, bend: bool, mode: &str) { + let mut h = smart_demo(); + let tl = timeline(&h); + let (clip, lane) = clip_on_screen(&h, |xs, xe| xs > tl.min.x + 30.0 && xe < tl.max.x - 100.0 && xe - xs > 100.0); + let (tid, _) = h.state().engine.session().find_clip(clip.id).unwrap(); + let neighbour = h.state().engine.session().track(tid).unwrap().clips().iter().find(|c| c.id != clip.id && c.start >= clip.end()).unwrap().clone(); + let source = clip.source().unwrap(); + if channels == 2 { + make_stereo(&mut h, tid, source); + } + if mode == "power" { + h.state_mut().engine.execute("view.waveform_power", &json!({})).unwrap(); + } + h.state_mut().engine.execute("view.waveform_rectified", &json!({"value": mode == "rectified"})).unwrap(); + h.state_mut().engine.execute("view.waveform_outlines", &json!({"value": mode == "outlines"})).unwrap(); + h.state_mut().engine.execute("view.waveform_zoom", &json!({"value": 1.4})).unwrap(); + let c = h.state_mut().engine.session_mut().find_clip_mut(clip.id).unwrap(); + c.gain_db = -3.0; + c.gain_env = vec![(0, -3.0), (c.length, 3.0)]; + let target = soundcraft_model::Fade { + len: if bend { c.length * 2 / 3 } else { c.length / 4 }, + shape: soundcraft_model::FadeShape::SCurve, + curvature: 0.0, + }; + let other = soundcraft_model::Fade { len: c.length / 8, shape: soundcraft_model::FadeShape::Linear, curvature: 0.2 }; + (c.fade_in, c.fade_out) = if fade_in { (target, other) } else { (other, target) }; + h.step(); + let before = h.state().engine.session().clone(); + let undo_len = h.state().engine.undo_history().len(); + let audio = before.pool.get(source).unwrap().clone(); + let (xs, xe) = (x_of(&before, tl, clip.start), x_of(&before, tl, clip.end())); + let clip_rect = Rect::from_min_max(pos2(xs, lane.min.y), pos2(xe, lane.max.y)).intersect(tl); + let neighbour_rect = + Rect::from_min_max(pos2(x_of(&before, tl, neighbour.start), lane.min.y), pos2(x_of(&before, tl, neighbour.end()), lane.max.y)).intersect(tl); + let original = waveform_geometry(&h, clip_rect); + let neighbour_wave = waveform_geometry(&h, neighbour_rect); + assert_eq!(original.len(), channels); + let side = if fade_in { "in" } else { "out" }; + let operation = if bend { "bend" } else { "length" }; + let screenshot = channels == 1 && mode == "peak"; + if screenshot { + fade_shot(&mut h, &format!("waveform-{side}-{operation}-before")); + } + let (press, to) = fade_drag_points(&before, tl, lane, clip.id, fade_in, bend); + h.hover_at(press); + h.drag_at(press); + h.hover_at(to); + h.step(); + h.step(); + assert!(if bend { + matches!(h.state().gesture, Some(Gesture::FadeCurve { .. })) + } else { + matches!(h.state().gesture, Some(Gesture::Fade { .. })) + }); + let preview = waveform_geometry(&h, clip_rect); + assert_eq!(preview.len(), channels); + for channel in 0..channels { + assert_ne!( + preview[channel], original[channel], + "the waveform must respond while the button is held: {side}, {operation}, {mode}, channel={channel}" + ); + } + assert_eq!(waveform_geometry(&h, neighbour_rect), neighbour_wave); + assert_eq!(h.state().engine.session(), &before); + assert_eq!(h.state().engine.undo_history().len(), undo_len); + assert!(std::sync::Arc::ptr_eq(h.state().engine.session().pool.get(source).unwrap(), &audio), "preview must retain cached media and peaks"); + if screenshot { + fade_shot(&mut h, &format!("waveform-{side}-{operation}-preview")); + } + h.drop_at(to); + h.step(); + assert!(h.state().gesture.is_none()); + assert_eq!(waveform_geometry(&h, clip_rect), preview, "release must not cause a waveform jump"); + assert_eq!(waveform_geometry(&h, neighbour_rect), neighbour_wave); + assert_eq!(h.state().engine.undo_history().len(), undo_len + 1); + assert!(std::sync::Arc::ptr_eq(h.state().engine.session().pool.get(source).unwrap(), &audio)); + if screenshot { + fade_shot(&mut h, &format!("waveform-{side}-{operation}-released")); + } + assert!(h.state_mut().engine.undo()); + assert_eq!(h.state().engine.session(), &before); + h.step(); + assert_eq!(waveform_geometry(&h, clip_rect), original); +} + #[test] fn waveforms_follow_fade_drags_before_release_and_match_the_committed_waveform() { for channels in [1, 2] { for fade_in in [true, false] { for bend in [false, true] { for mode in ["peak", "power", "rectified", "outlines"] { - let mut h = smart_demo(); - let tl = timeline(&h); - let (clip, lane) = clip_on_screen(&h, |xs, xe| xs > tl.min.x + 30.0 && xe < tl.max.x - 100.0 && xe - xs > 100.0); - let (tid, _) = h.state().engine.session().find_clip(clip.id).unwrap(); - let neighbour = h - .state() - .engine - .session() - .track(tid) - .unwrap() - .clips() - .iter() - .find(|c| c.id != clip.id && c.start >= clip.end()) - .unwrap() - .clone(); - let source = clip.source().unwrap(); - if channels == 2 { - let audio = h.state().engine.session().pool.get(source).unwrap().clone(); - let left = audio.buffer.channels[0].clone(); - let right = left.iter().map(|sample| sample * 0.5).collect(); - let audio = soundcraft_model::SourceAudio::new(soundcraft_audio_io::AudioBuffer { - sample_rate: audio.buffer.sample_rate, - channels: vec![left, right], - }); - let s = h.state_mut().engine.session_mut(); - s.pool.insert(source, std::sync::Arc::new(audio)); - s.track_mut(tid).unwrap().format = soundcraft_model::ChannelFormat::Stereo; - } - if mode == "power" { - h.state_mut().engine.execute("view.waveform_power", &json!({})).unwrap(); - } - h.state_mut().engine.execute("view.waveform_rectified", &json!({"value": mode == "rectified"})).unwrap(); - h.state_mut().engine.execute("view.waveform_outlines", &json!({"value": mode == "outlines"})).unwrap(); - h.state_mut().engine.execute("view.waveform_zoom", &json!({"value": 1.4})).unwrap(); - let c = h.state_mut().engine.session_mut().find_clip_mut(clip.id).unwrap(); - c.gain_db = -3.0; - c.gain_env = vec![(0, -3.0), (c.length, 3.0)]; - let target = soundcraft_model::Fade { - len: if bend { c.length * 2 / 3 } else { c.length / 4 }, - shape: soundcraft_model::FadeShape::SCurve, - curvature: 0.0, - }; - let other = soundcraft_model::Fade { len: c.length / 8, shape: soundcraft_model::FadeShape::Linear, curvature: 0.2 }; - (c.fade_in, c.fade_out) = if fade_in { (target, other) } else { (other, target) }; - h.step(); - let before = h.state().engine.session().clone(); - let undo_len = h.state().engine.undo_history().len(); - let audio = before.pool.get(source).unwrap().clone(); - let (xs, xe) = (x_of(&before, tl, clip.start), x_of(&before, tl, clip.end())); - let clip_rect = Rect::from_min_max(pos2(xs, lane.min.y), pos2(xe, lane.max.y)).intersect(tl); - let neighbour_rect = Rect::from_min_max( - pos2(x_of(&before, tl, neighbour.start), lane.min.y), - pos2(x_of(&before, tl, neighbour.end()), lane.max.y), - ) - .intersect(tl); - let original = waveform_geometry(&h, clip_rect); - let neighbour_wave = waveform_geometry(&h, neighbour_rect); - assert_eq!(original.len(), channels); - let side = if fade_in { "in" } else { "out" }; - let operation = if bend { "bend" } else { "length" }; - let screenshot = channels == 1 && mode == "peak"; - if screenshot { - fade_shot(&mut h, &format!("waveform-{side}-{operation}-before")); - } - let (press, to) = if bend { - let c = before.find_clip(clip.id).unwrap().1; - let midpoint = fade_midpoint(&before, tl, lane, c, fade_in).unwrap(); - (midpoint, midpoint - vec2(0.0, 16.0)) - } else { - ( - pos2(if fade_in { xs + 6.0 } else { xe - 6.0 }, lane.min.y + 4.0), - pos2(xs + (xe - xs) * if fade_in { 0.7 } else { 0.3 }, lane.min.y + 4.0), - ) - }; - h.hover_at(press); - h.drag_at(press); - h.hover_at(to); - h.step(); - h.step(); - assert!(if bend { - matches!(h.state().gesture, Some(Gesture::FadeCurve { .. })) - } else { - matches!(h.state().gesture, Some(Gesture::Fade { .. })) - }); - let preview = waveform_geometry(&h, clip_rect); - assert_eq!(preview.len(), channels); - for channel in 0..channels { - assert_ne!( - preview[channel], original[channel], - "the waveform must respond while the button is held: {side}, {operation}, {mode}, channel={channel}" - ); - } - assert_eq!(waveform_geometry(&h, neighbour_rect), neighbour_wave); - assert_eq!(h.state().engine.session(), &before); - assert_eq!(h.state().engine.undo_history().len(), undo_len); - assert!( - std::sync::Arc::ptr_eq(h.state().engine.session().pool.get(source).unwrap(), &audio), - "preview must retain cached media and peaks" - ); - if screenshot { - fade_shot(&mut h, &format!("waveform-{side}-{operation}-preview")); - } - h.drop_at(to); - h.step(); - assert!(h.state().gesture.is_none()); - assert_eq!(waveform_geometry(&h, clip_rect), preview, "release must not cause a waveform jump"); - assert_eq!(waveform_geometry(&h, neighbour_rect), neighbour_wave); - assert_eq!(h.state().engine.undo_history().len(), undo_len + 1); - assert!(std::sync::Arc::ptr_eq(h.state().engine.session().pool.get(source).unwrap(), &audio)); - if screenshot { - fade_shot(&mut h, &format!("waveform-{side}-{operation}-released")); - } - assert!(h.state_mut().engine.undo()); - assert_eq!(h.state().engine.session(), &before); - h.step(); - assert_eq!(waveform_geometry(&h, clip_rect), original); + fade_waveform_case(channels, fade_in, bend, mode); } } } @@ -998,85 +999,100 @@ fn fade_midpoint_handles_remain_visible_and_can_be_grabbed_again() { } } +/// `session` with the clip's fade-in (or fade-out) curvature set. +fn with_curvature(session: &soundcraft_model::Session, clip: soundcraft_model::ClipId, fade_in: bool, curvature: f32) -> soundcraft_model::Session { + let mut expected = session.clone(); + let c = expected.find_clip_mut(clip).unwrap(); + if fade_in { + c.fade_in.curvature = curvature; + } else { + c.fade_out.curvature = curvature; + } + expected +} + +/// The screenshot name for a fade bend drag. +fn bend_shot_name(fade_in: bool, dy: f32, stage: &str) -> String { + let side = if fade_in { "in" } else { "out" }; + let direction = if dy < 0.0 { "up" } else { "down" }; + format!("fade-{side}-bend-{direction}-{stage}") +} + +fn fade_midpoint_bend_case(fade_in: bool, show_name: bool, dy: f32) { + let mut h = smart_demo(); + h.state_mut().engine.execute("view.clip_name", &json!({"value": show_name})).unwrap(); + h.step(); + let tl = timeline(&h); + let (clip, lane) = clip_on_screen(&h, |xs, xe| xs > tl.min.x + 30.0 && xe < tl.max.x - 100.0 && xe - xs > 100.0); + let c = h.state_mut().engine.session_mut().find_clip_mut(clip.id).unwrap(); + let target = soundcraft_model::Fade { len: c.length * 2 / 3, shape: soundcraft_model::FadeShape::SCurve, curvature: 0.0 }; + let other = soundcraft_model::Fade { len: c.length / 8, shape: soundcraft_model::FadeShape::Linear, curvature: -0.2 }; + (c.fade_in, c.fade_out) = if fade_in { (target, other) } else { (other, target) }; + h.step(); + let before = h.state().engine.session().clone(); + let clip_rect = Rect::from_min_max(pos2(x_of(&before, tl, clip.start), lane.min.y), pos2(x_of(&before, tl, clip.end()), lane.max.y)); + let original = fade_curves(&h, clip_rect); + let (edited, opposite) = if fade_in { (0, 1) } else { (1, 0) }; + let middle = original[edited].0.points.len() / 2; + let midpoint = original[edited].0.points[middle]; + let undo_len = h.state().engine.undo_history().len(); + h.hover_at(midpoint); + h.step(); + assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::ResizeVertical); + click(&mut h, midpoint); + assert_eq!(h.state().engine.session(), &before, "clicking the curve control should not change the selection"); + h.drag_at(midpoint); + let to = midpoint + vec2(0.0, dy); + h.hover_at(to); + h.step(); + h.step(); // Paint the frame requested when the drag gesture starts. + let curvature = match h.state().gesture { + Some(Gesture::FadeCurve { clip: id, fade_in: side, curvature, .. }) if id == clip.id && side == fade_in => curvature, + _ => panic!("the midpoint must start a curve gesture"), + }; + assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::ResizeVertical); + assert!(if dy < 0.0 { curvature > 0.0 } else { curvature < 0.0 }); + let preview = fade_curves(&h, clip_rect); + assert_eq!(preview.len(), 2); + assert_eq!(preview[opposite], original[opposite]); + assert_eq!(preview[edited].0.points.first(), original[edited].0.points.first()); + assert_eq!(preview[edited].0.points.last(), original[edited].0.points.last()); + assert_eq!(preview[edited].0.points[middle].x, midpoint.x); + assert!( + (preview[edited].0.points[middle].y - (midpoint.y + dy)).abs() < 0.001, + "side={fade_in}, names={show_name}, dy={dy}, midpoint={midpoint:?}, drawn={:?}, gesture={:?}", + preview[edited].0.points[middle], + h.state().gesture + ); + assert_eq!(h.state().engine.session(), &before); + if show_name { + fade_shot(&mut h, &bend_shot_name(fade_in, dy, "preview")); + } + h.drop_at(to); + h.step(); + let expected = with_curvature(&before, clip.id, fade_in, curvature); + assert_eq!(h.state().engine.session(), &expected); + assert_eq!(fade_curves(&h, clip_rect), preview, "the released envelope must match the preview"); + let rel = if fade_in { target.len / 2 } else { clip.length - target.len / 2 }; + let previous_gain = before.find_clip(clip.id).unwrap().1.gain_at(rel); + let new_gain = expected.find_clip(clip.id).unwrap().1.gain_at(rel); + assert!(if dy < 0.0 { new_gain > previous_gain } else { new_gain < previous_gain }); + assert_eq!(h.state().engine.undo_history().len(), undo_len + 1); + if show_name { + fade_shot(&mut h, &bend_shot_name(fade_in, dy, "released")); + } + assert!(h.state_mut().engine.undo()); + assert_eq!(h.state().engine.session(), &before); + assert!(h.state_mut().engine.redo()); + assert_eq!(h.state().engine.session(), &expected); +} + #[test] fn fade_midpoints_drag_vertically_without_moving_the_endpoints() { for fade_in in [true, false] { for show_name in [true, false] { for dy in [-16.0, 16.0] { - let mut h = smart_demo(); - h.state_mut().engine.execute("view.clip_name", &json!({"value": show_name})).unwrap(); - h.step(); - let tl = timeline(&h); - let (clip, lane) = clip_on_screen(&h, |xs, xe| xs > tl.min.x + 30.0 && xe < tl.max.x - 100.0 && xe - xs > 100.0); - let c = h.state_mut().engine.session_mut().find_clip_mut(clip.id).unwrap(); - let target = soundcraft_model::Fade { len: c.length * 2 / 3, shape: soundcraft_model::FadeShape::SCurve, curvature: 0.0 }; - let other = soundcraft_model::Fade { len: c.length / 8, shape: soundcraft_model::FadeShape::Linear, curvature: -0.2 }; - (c.fade_in, c.fade_out) = if fade_in { (target, other) } else { (other, target) }; - h.step(); - let before = h.state().engine.session().clone(); - let clip_rect = Rect::from_min_max(pos2(x_of(&before, tl, clip.start), lane.min.y), pos2(x_of(&before, tl, clip.end()), lane.max.y)); - let original = fade_curves(&h, clip_rect); - let (edited, opposite) = if fade_in { (0, 1) } else { (1, 0) }; - let middle = original[edited].0.points.len() / 2; - let midpoint = original[edited].0.points[middle]; - let undo_len = h.state().engine.undo_history().len(); - h.hover_at(midpoint); - h.step(); - assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::ResizeVertical); - click(&mut h, midpoint); - assert_eq!(h.state().engine.session(), &before, "clicking the curve control should not change the selection"); - h.drag_at(midpoint); - let to = midpoint + vec2(0.0, dy); - h.hover_at(to); - h.step(); - h.step(); // Paint the frame requested when the drag gesture starts. - let curvature = match h.state().gesture { - Some(Gesture::FadeCurve { clip: id, fade_in: side, curvature, .. }) if id == clip.id && side == fade_in => curvature, - _ => panic!("the midpoint must start a curve gesture"), - }; - assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::ResizeVertical); - assert!(if dy < 0.0 { curvature > 0.0 } else { curvature < 0.0 }); - let preview = fade_curves(&h, clip_rect); - assert_eq!(preview.len(), 2); - assert_eq!(preview[opposite], original[opposite]); - assert_eq!(preview[edited].0.points.first(), original[edited].0.points.first()); - assert_eq!(preview[edited].0.points.last(), original[edited].0.points.last()); - assert_eq!(preview[edited].0.points[middle].x, midpoint.x); - assert!( - (preview[edited].0.points[middle].y - (midpoint.y + dy)).abs() < 0.001, - "side={fade_in}, names={show_name}, dy={dy}, midpoint={midpoint:?}, drawn={:?}, gesture={:?}", - preview[edited].0.points[middle], - h.state().gesture - ); - assert_eq!(h.state().engine.session(), &before); - let side = if fade_in { "in" } else { "out" }; - let direction = if dy < 0.0 { "up" } else { "down" }; - if show_name { - fade_shot(&mut h, &format!("fade-{side}-bend-{direction}-preview")); - } - h.drop_at(to); - h.step(); - let mut expected = before.clone(); - let c = expected.find_clip_mut(clip.id).unwrap(); - if fade_in { - c.fade_in.curvature = curvature; - } else { - c.fade_out.curvature = curvature; - } - assert_eq!(h.state().engine.session(), &expected); - assert_eq!(fade_curves(&h, clip_rect), preview, "the released envelope must match the preview"); - let rel = if fade_in { target.len / 2 } else { clip.length - target.len / 2 }; - let previous_gain = before.find_clip(clip.id).unwrap().1.gain_at(rel); - let new_gain = expected.find_clip(clip.id).unwrap().1.gain_at(rel); - assert!(if dy < 0.0 { new_gain > previous_gain } else { new_gain < previous_gain }); - assert_eq!(h.state().engine.undo_history().len(), undo_len + 1); - if show_name { - fade_shot(&mut h, &format!("fade-{side}-bend-{direction}-released")); - } - assert!(h.state_mut().engine.undo()); - assert_eq!(h.state().engine.session(), &before); - assert!(h.state_mut().engine.redo()); - assert_eq!(h.state().engine.session(), &expected); + fade_midpoint_bend_case(fade_in, show_name, dy); } } } diff --git a/crates/ui-egui/src/edit_window/tracks.rs b/crates/ui-egui/src/edit_window/tracks.rs index 8eb6c593..8da99821 100644 --- a/crates/ui-egui/src/edit_window/tracks.rs +++ b/crates/ui-egui/src/edit_window/tracks.rs @@ -1,9 +1,12 @@ use super::automation::draw_automation; -use super::clips::{draw_clip, draw_live_take, draw_strip_silence, grid_lines}; +use super::clips::{ClipLane, draw_clip, draw_live_take, draw_strip_silence, grid_lines}; use super::header::{header_column, track_header}; use super::input::lane_interaction; use super::layout::{Column, HEADER_W, LANE_H, STRIP_W, lane_rect, sample_at, x_of}; -use super::*; +use super::{ + Align2, AutoParam, Range, Rect, Sense, Session, SoundApp, Stroke, Tokens, Track, TrackHeight, TrackId, TrackKind, Ui, json, ops, pos2, regular, + rgb, vec2, +}; pub(super) fn in_edit_window(s: &Session, t: &Track) -> bool { ops::shown(s, t) @@ -90,7 +93,7 @@ pub(super) fn track_row(app: &mut SoundApp, ui: &mut Ui, id: TrackId, row: Rect, if !clip.range().overlaps(&view) { continue; } - draw_clip(app, &painter, &s, track, sorted, clip, tl, lane, auto_view.is_some()); + draw_clip(&ClipLane { app, painter: &painter, s: &s, track, sorted, tl, lane }, clip, auto_view.is_some()); } draw_live_take(app, &painter, &s, track, tl, lane); crate::video_track::draw_lane(app, &painter, &s, track, tl, lane); @@ -140,7 +143,7 @@ fn playlist_lanes(app: &mut SoundApp, ui: &mut Ui, track: &Track, row: Rect, mai let sorted = track.clips().is_sorted_by_key(|c| c.start); let view = Range::new(sample_at(&s, tl, tl.min.x), sample_at(&s, tl, tl.max.x)); for c in pl.clips.iter().filter(|c| c.range().overlaps(&view)) { - draw_clip(app, &painter, &s, track, sorted, c, tl, lane, false); + draw_clip(&ClipLane { app, painter: &painter, s: &s, track, sorted, tl, lane }, c, false); } let resp = ui.interact(lane, ui.scope_id().with(("pl_lane", track.id.0, i)), Sense::click()); if resp.clicked() diff --git a/crates/ui-egui/src/edit_window/universe.rs b/crates/ui-egui/src/edit_window/universe.rs index 3175caaa..3a9331a8 100644 --- a/crates/ui-egui/src/edit_window/universe.rs +++ b/crates/ui-egui/src/edit_window/universe.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{Color32, Rect, Samples, Sense, Session, SoundApp, Stroke, StrokeKind, Tokens, Track, Ui, json, pos2, rgb, vec2}; /// The Universe's clip blocks for one document revision and area: each track's clips merged into /// runs of one colour. A drum edit holds tens of thousands of clips a pixel or less wide here; diff --git a/crates/ui-egui/src/mix_window.rs b/crates/ui-egui/src/mix_window.rs index 5bf55078..f089499b 100644 --- a/crates/ui-egui/src/mix_window.rs +++ b/crates/ui-egui/src/mix_window.rs @@ -151,6 +151,30 @@ fn section_label(ui: &Ui, r: Rect, text: &str, narrow: bool) { /// Draw one mixer channel strip: preamp, instrument, object, insert and send sections, EQ curve, routing, fader and /// meters, with its interactions. +/// The height of the strip's footer: volume readout and name plate. +const STRIP_FOOT_H: f32 = 64.0; + +/// One mixer strip's geometry and settings, shared by its sections. +struct StripCtx<'a> { + track: &'a Track, + id: TrackId, + r: Rect, + w: f32, + x0: f32, + inner_w: f32, + narrow: bool, + selected: bool, + views: Vec, + all: bool, +} + +impl StripCtx<'_> { + /// Whether the mixer shows the section named `view`. + fn shows(&self, view: &str) -> bool { + self.all || self.views.iter().any(|x| x == view) + } +} + fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapshot>, scroll_here: bool) { let t = Tokens::current(); let session = app.engine.session_arc(); @@ -173,12 +197,30 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh let inner_w = w - 8.0; let x0 = r.min.x + 4.0; let views = app.ui.mix_views.clone(); - let has = |v: &str| views.iter().any(|x| x == v); - let all = has("all"); + let all = views.iter().any(|x| x == "all"); + let cx = StripCtx { track, id, r, w, x0, inner_w, narrow, selected, views, all }; // Preamp, instrument and object rows keep their height on every strip so sections line up; // their controls only appear on tracks with an input. + y = preamp_section(app, ui, &cx, y); + y = instrument_section(app, ui, &cx, y); + y = object_section(app, ui, &cx, y); + y = slot_sections(app, ui, &cx, y); + y = eq_section(ui, &cx, y); + y = comments_section(app, ui, &cx, y); + y = io_section(app, ui, &cx, y); + y = group_selector(app, ui, &cx, y); + y = pan_section(app, ui, &cx, y); + y = toggle_rows(app, ui, &cx, y); + fader_and_meter(app, ui, &cx, y, snap); + name_and_readout(app, ui, &cx); +} + +/// Input gain stage ahead of the inserts (the track's trim). +fn preamp_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, id, narrow, x0, inner_w, .. } = *cx; + let t = Tokens::current(); let has_input = matches!(track.kind, TrackKind::Audio | TrackKind::Instrument | TrackKind::Midi); - if all || has("mic_preamps") { + if cx.shows("mic_preamps") { // Input gain stage ahead of the inserts (the track's trim). let sec = Rect::from_min_size(pos2(x0, y), vec2(inner_w, 34.0)); inset(ui.painter(), sec, t.strip_section); @@ -199,7 +241,14 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh } y = sec.max.y + 4.0; } - if all || has("instruments") { + y +} + +/// The instrument slot. +fn instrument_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, narrow, x0, inner_w, .. } = *cx; + let t = Tokens::current(); + if cx.shows("instruments") { let sec = Rect::from_min_size(pos2(x0, y), vec2(inner_w, 34.0)); inset(ui.painter(), sec, t.strip_section); section_label(ui, sec, "INSTRUMENT", narrow); @@ -215,7 +264,14 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh } y = sec.max.y + 4.0; } - if all || has("object") { + y +} + +/// The object/bed switch. +fn object_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, id, x0, inner_w, .. } = *cx; + let has_input = matches!(track.kind, TrackKind::Audio | TrackKind::Instrument | TrackKind::Midi); + if cx.shows("object") { let sec = Rect::from_min_size(pos2(x0, y), vec2(inner_w, 20.0)); if has_input { let on = app.engine.session().edit.flag(&format!("object.{}", id.0)); @@ -227,13 +283,20 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh } y = sec.max.y + 4.0; } + y +} + +/// The insert and send sections. +fn slot_sections(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, narrow, x0, inner_w, .. } = *cx; + let t = Tokens::current(); for (key, title, off, is_send) in [ ("inserts_ae", "INSERTS A-E", 0usize, false), ("inserts_fj", "INSERTS F-J", 5, false), ("sends_ae", "SENDS A-E", 0, true), ("sends_fj", "SENDS F-J", 5, true), ] { - if !(all || has(key)) { + if !cx.shows(key) { continue; } let sec = Rect::from_min_size(pos2(x0, y), vec2(inner_w, 16.0 + 5.0 * 17.0)); @@ -249,7 +312,14 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh } y = sec.max.y + 4.0; } - if all || has("eq_curve") { + y +} + +/// The EQ curve. +fn eq_section(ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, x0, inner_w, .. } = *cx; + let t = Tokens::current(); + if cx.shows("eq_curve") { let sec = Rect::from_min_size(pos2(x0, y), vec2(inner_w, 40.0)); ui.painter().rect_filled(sec, 2.0, Color32::from_rgb(14, 18, 22)); let eq = track.mixer.inserts.iter().flatten().find(|i| i.plugin == "eq_7band" || i.plugin == "eq_1band"); @@ -278,7 +348,13 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh } y = sec.max.y + 4.0; } - if all || has("comments") { + y +} + +/// The comments field. +fn comments_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, id, narrow, x0, inner_w, .. } = *cx; + if cx.shows("comments") { let sec = Rect::from_min_size(pos2(x0, y), vec2(inner_w, 44.0)); let mut c = track.comments.clone(); let mut child = ui.new_child(egui::UiBuilder::new().max_rect(sec)); @@ -292,7 +368,14 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh } y = sec.max.y + 4.0; } - if all || has("io") { + y +} + +/// Input, output and automation mode selectors. +fn io_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, id, narrow, x0, inner_w, .. } = *cx; + let t = Tokens::current(); + if cx.shows("io") { let sec = Rect::from_min_size(pos2(x0, y), vec2(inner_w, 16.0 + 2.0 * 18.0 + 30.0)); inset(ui.painter(), sec, t.strip_section); section_label(ui, sec, "I / O", narrow); @@ -357,6 +440,13 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh }); y = sec.max.y + 4.0; } + y +} + +/// The group selector. +fn group_selector(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, id, narrow, x0, inner_w, .. } = *cx; + let t = Tokens::current(); // Group selector. let no_group = if narrow { "nogrp" } else { "no group" }; let gname = app.engine.session().groups.iter().find(|g| g.members.contains(&id)).map_or(no_group.to_string(), |g| g.name.clone()); @@ -371,6 +461,13 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh SelectorLabels { name: &format!("Group for {}", track.name), value: &gname, tooltip: "Group" }, ); y += 22.0; + y +} + +/// Pan: a surround panner when the output is multichannel, else the stereo knobs. +fn pan_section(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, id, x0, inner_w, .. } = *cx; + let t = Tokens::current(); // Pan: a surround panner when the output is multichannel, else the stereo knobs. let out_fmt = output_format(app, track); let surround_out = @@ -429,6 +526,13 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh } y += 54.0; } + y +} + +/// Rec / Input / Solo / Mute. +fn toggle_rows(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, mut y: f32) -> f32 { + let StripCtx { track, id, x0, inner_w, .. } = *cx; + let t = Tokens::current(); // Rec / Input / Solo / Mute. let bw = (inner_w - 6.0) / 2.0; let mut row = ui.new_child( @@ -461,9 +565,15 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh } } y += 26.0; + y +} + +/// The fader and its meter. +fn fader_and_meter(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>, y: f32, snap: Option<&MeterSnapshot>) { + let StripCtx { track, id, narrow, x0, inner_w, r, .. } = *cx; + let t = Tokens::current(); // Fader + meter. - let bottom_h = 64.0; - let fh = (r.max.y - bottom_h - y).max(120.0); + let fh = (r.max.y - STRIP_FOOT_H - y).max(120.0); let wide_meter = (if track.kind == TrackKind::Master { app.engine.session().main_format() } else { track.format }).channels() > 2; let fr = Rect::from_min_size(pos2(x0, y), vec2(inner_w * if wide_meter { 0.45 } else { 0.6 }, fh)); if let Some(db) = fader(ui, fr, track.mixer.volume_db, ui.scope_id().with(("fader", id.0)), &format!("{} level", track.name)) { @@ -495,8 +605,14 @@ fn strip(app: &mut SoundApp, ui: &mut Ui, id: TrackId, snap: Option<&MeterSnapsh if md.gr > 0.1 { ui.painter().text(pos2(mr.center().x, mr.min.y - 3.0), Align2::CENTER_BOTTOM, format!("-{:.0}", md.gr), regular(8.0), t.meter_yellow); } +} + +/// Volume readout, kind label and name plate. +fn name_and_readout(app: &mut SoundApp, ui: &mut Ui, cx: &StripCtx<'_>) { + let StripCtx { track, id, x0, inner_w, r, w, selected, .. } = *cx; + let t = Tokens::current(); // Volume readout and name. - let vr = Rect::from_min_size(pos2(x0 + 4.0, r.max.y - bottom_h + 4.0), vec2(inner_w - 8.0, 15.0)); + let vr = Rect::from_min_size(pos2(x0 + 4.0, r.max.y - STRIP_FOOT_H + 4.0), vec2(inner_w - 8.0, 15.0)); inset(ui.painter(), vr, t.counter_bg); let (_, entered) = editable_readout( ui, diff --git a/crates/ui-egui/src/mix_window/plugin_menu.rs b/crates/ui-egui/src/mix_window/plugin_menu.rs index c9923dc4..ea077887 100644 --- a/crates/ui-egui/src/mix_window/plugin_menu.rs +++ b/crates/ui-egui/src/mix_window/plugin_menu.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{Color32, Rect, SoundApp, TrackId, Ui, json, pos2, vec2}; /// An insert's menu: a search field that takes typing at once, then (until something is typed) /// Bypass and removal and the plugins by category and format. diff --git a/crates/ui-egui/src/mix_window/routing.rs b/crates/ui-egui/src/mix_window/routing.rs index 63e21f5b..176a8cba 100644 --- a/crates/ui-egui/src/mix_window/routing.rs +++ b/crates/ui-egui/src/mix_window/routing.rs @@ -1,5 +1,5 @@ use super::plugin_menu::{MenuSearch, menu_row}; -use super::*; +use super::{Route, SoundApp, Track, TrackId, Ui, json}; pub(crate) fn route_name(app: &SoundApp, r: &Route) -> String { match r { diff --git a/crates/ui-egui/src/mix_window/sends.rs b/crates/ui-egui/src/mix_window/sends.rs index 1a9dabdc..e0c3da4a 100644 --- a/crates/ui-egui/src/mix_window/sends.rs +++ b/crates/ui-egui/src/mix_window/sends.rs @@ -1,7 +1,10 @@ use super::plugin_menu::{MenuSearch, menu_row}; use super::routing::route_name; use super::slots::{COMPACT_SLOT, Mark, empty_slot, level_icon, used_slot}; -use super::*; +use super::{ + Align2, Rect, Route, Sense, SoundApp, Stroke, StrokeKind, Tokens, Track, TrackId, TrackKind, Ui, db_text, fader, json, pan_knob, pan_text, pos2, + regular, text_toggle, vec2, +}; use craft_ui::combo::Menu; /// Draw one send slot: the route area picks a destination, while the level area opens or edits the send level. diff --git a/crates/ui-egui/src/mix_window/slots.rs b/crates/ui-egui/src/mix_window/slots.rs index 0c7e45cd..866b60eb 100644 --- a/crates/ui-egui/src/mix_window/slots.rs +++ b/crates/ui-egui/src/mix_window/slots.rs @@ -1,5 +1,5 @@ use super::plugin_menu::{instrument_menu, plugin_info, plugin_menu}; -use super::*; +use super::{Align2, Color32, Rect, Sense, SoundApp, Stroke, Tokens, Track, Ui, bold, groove, json, pos2, raised_key, shade, vec2}; /// An instrument track's INSTRUMENT row: click opens a hosted instrument's plugin window, /// right-click (or click, when none is set) picks another instrument. A built-in's parameters diff --git a/crates/ui-egui/src/panels/about.rs b/crates/ui-egui/src/panels/about.rs index 36074c25..504bf9f7 100644 --- a/crates/ui-egui/src/panels/about.rs +++ b/crates/ui-egui/src/panels/about.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{SoundApp, bold, mono, vec2}; /// About window tabs for version information, contributors and models. pub(super) fn about(app: &mut SoundApp, ctx: &egui::Context) { diff --git a/crates/ui-egui/src/panels/clips.rs b/crates/ui-egui/src/panels/clips.rs index 4c127b97..cbdd28d5 100644 --- a/crates/ui-egui/src/panels/clips.rs +++ b/crates/ui-egui/src/panels/clips.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{Align2, ClipId, Rect, Sense, SoundApp, SourceId, Tokens, Ui, bold, json, panel_header, pos2, regular, rgb, vec2, with_pending_offset}; /// One row of the clip list: a whole file, or a clip on a track (positions in the session). #[derive(Clone, Copy)] diff --git a/crates/ui-egui/src/panels/history.rs b/crates/ui-egui/src/panels/history.rs index da145ca9..3d284f75 100644 --- a/crates/ui-egui/src/panels/history.rs +++ b/crates/ui-egui/src/panels/history.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{SoundApp, Tokens, Ui, json}; pub(crate) fn undo_history(app: &mut SoundApp, ui: &mut Ui) { let hist = app.engine.undo_history(); diff --git a/crates/ui-egui/src/panels/memory.rs b/crates/ui-egui/src/panels/memory.rs index 313713aa..d849e382 100644 --- a/crates/ui-egui/src/panels/memory.rs +++ b/crates/ui-egui/src/panels/memory.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{SoundApp, Ui, json, mono}; /// One recallable memory-location row: number button, editable name, time, length, kind and delete. fn memory_location_row(app: &mut SoundApp, ui: &mut Ui, n: u32, name: &str, loc: &str, len: &str, kind: &str) { diff --git a/crates/ui-egui/src/panels/midi_editor.rs b/crates/ui-egui/src/panels/midi_editor.rs index 2515dbbb..2e48be01 100644 --- a/crates/ui-egui/src/panels/midi_editor.rs +++ b/crates/ui-egui/src/panels/midi_editor.rs @@ -69,6 +69,50 @@ fn is_black(p: i32) -> bool { } /// Draw the MIDI editor for the target clip, with an empty-state prompt that adds a clip on the selected MIDI track. +/// The piano roll's geometry: where it is drawn and how ticks and pitches map onto it. +#[derive(Clone, Copy)] +struct Roll<'a> { + sequence: &'a soundcraft_midi::Sequence, + roll: Rect, + keys: Rect, + vel: Rect, + px_per_tick: f32, + len_ticks: i64, + scroll: f32, + grid_ticks: i64, + rows: i32, +} + +impl Roll<'_> { + fn x_of(&self, tick: i64) -> f32 { + self.roll.min.x + tick as f32 * self.px_per_tick + } + + fn y_of(&self, pitch: i32) -> f32 { + self.roll.min.y + (127 - pitch) as f32 * ROW_H - self.scroll + } + + fn pitch_at(&self, y: f32) -> i32 { + 127 - ((y - self.roll.min.y + self.scroll) / ROW_H).floor() as i32 + } + + /// The note under `pos` and whether `pos` is on its resize edge. + fn hit(&self, pos: egui::Pos2) -> Option<(usize, bool)> { + self.sequence.notes.iter().enumerate().rev().find_map(|(i, n)| { + let r = Rect::from_min_max( + pos2(self.x_of(n.start), self.y_of(i32::from(n.pitch))), + pos2(self.x_of(n.start + n.length).max(self.x_of(n.start) + 3.0), self.y_of(i32::from(n.pitch)) + ROW_H), + ); + r.contains(pos).then_some((i, pos.x > r.max.x - 5.0)) + }) + } + + /// `tk` ticks, rounded to the grid. + fn snap(&self, tk: f32) -> i64 { + ((tk / self.grid_ticks as f32).round() as i64) * self.grid_ticks + } +} + pub fn show(app: &mut SoundApp, ui: &mut Ui) { // Painter-only contents do not advance the layout. Claim the resized // space, including the empty state, so the dock keeps its chosen height. @@ -80,24 +124,7 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { ui.painter().rect_filled(header, 0.0, t.panel_bg2); ui.painter().text(pos2(header.min.x + 8.0, header.center().y), Align2::LEFT_CENTER, "MIDI EDITOR", bold(11.5), t.header_text); let Some(cid) = target_clip(app) else { - let track = { - let s = app.engine.session(); - s.tracks.iter().find(|t| t.kind.is_midi() && s.edit.selected_tracks.contains(&t.id)).map(|t| (t.id, t.name.clone())) - }; - let msg = match &track { - Some((_, name)) => format!("Click to add a MIDI clip to {name} at the insertion point."), - None => "Select a MIDI clip (or a MIDI track) to edit its notes.".into(), - }; - ui.painter().text(full.center(), Align2::CENTER_CENTER, msg, regular(13.0), t.text_dim); - let body = Rect::from_min_max(pos2(full.min.x, header.max.y), full.max); - if let Some((id, _)) = track - && ui.interact(body, ui.scope_id().with("new_clip"), Sense::click()).clicked() - { - let sel = app.engine.session().edit.selection; - if let Ok(v) = app.run("midi.new_clip", json!({"track": id.0, "start": sel.start, "end": sel.end})) { - let _ = app.run("edit.select", json!({"clips": [v["clip"]]})); - } - } + empty_state(app, ui, full, header); return; }; let Some((track, clip)) = app.engine.session().find_clip(cid).map(|(t, c)| (t, c.clone())) else { return }; @@ -115,40 +142,8 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { regular(11.0), t.text_dim, ); - let grid_ticks = match app.engine.session().edit.grid { - soundcraft_time::GridValue::Note { value, dotted, triplet } => { - let mut g = value.ticks(); - if dotted { - g = g * 3 / 2; - } - if triplet { - g = g * 2 / 3; - } - g.max(1) - } - _ => TICKS_PER_QUARTER / 4, - }; - // Header toolbar: whole-clip MIDI operations. - let bar = Rect::from_min_max(pos2(header.max.x - 330.0, header.min.y + 1.0), pos2(header.max.x - 6.0, header.max.y - 1.0)); - let mut tb = ui.new_child(egui::UiBuilder::new().max_rect(bar).layout(egui::Layout::right_to_left(egui::Align::Center))); - tb.spacing_mut().item_spacing.x = 4.0; - let mut op: Option<(&str, serde_json::Value)> = None; - if tb.small_button("Legato").clicked() { - op = Some(("event.change_duration", json!({"legato": 0}))); - } - if tb.small_button("Vel −10").clicked() { - op = Some(("event.change_velocity", json!({"add": -10}))); - } - if tb.small_button("Vel +10").clicked() { - op = Some(("event.change_velocity", json!({"add": 10}))); - } - if tb.small_button("Quantize").on_hover_text("Quantize to the grid").clicked() { - op = Some(("event.quantize", json!({"grid": grid_ticks, "strength": 100}))); - } - if let Some((id, mut p)) = op { - p["clips"] = json!([cid.0]); - let _ = app.run(id, p); - } + let grid_ticks = grid_ticks_of(app); + midi_toolbar(app, ui, header, cid, grid_ticks); let s = app.engine.session_arc(); let sr = s.sample_rate; let base_tick = s.tempo.samples_to_ticks(clip.start, sr); @@ -170,15 +165,90 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { app.midi.scroll = app.midi.scroll.clamp(0.0, (128.0 * ROW_H - roll.height()).max(0.0)); let scroll = app.midi.scroll; let px_per_tick = roll.width() / len_ticks as f32; - let x_of = |tick: i64| roll.min.x + tick as f32 * px_per_tick; - let y_of = |pitch: i32| roll.min.y + (127 - pitch) as f32 * ROW_H - scroll; - let pitch_at = |y: f32| 127 - ((y - roll.min.y + scroll) / ROW_H).floor() as i32; + let g = Roll { sequence, roll, keys, vel, px_per_tick, len_ticks, scroll, grid_ticks, rows }; let painter = ui.painter().with_clip_rect(roll.union(keys)); + draw_roll_background(&painter, &g); + // Notes. + let base = egui::Color32::from_rgb(track_color[0], track_color[1], track_color[2]); + draw_notes(app, &painter, &g, base); + note_pointer(app, ui, &g, cid, s.edit.tool == Tool::Pencil); + note_keys(app, ui, &g, cid); + velocity_lane(app, ui, &g, cid, base, full.min.x + 6.0); +} + +/// The empty state: a prompt that adds a clip on the selected MIDI track when clicked. +fn empty_state(app: &mut SoundApp, ui: &mut Ui, full: Rect, header: Rect) { + let t = Tokens::current(); + let track = { + let s = app.engine.session(); + s.tracks.iter().find(|t| t.kind.is_midi() && s.edit.selected_tracks.contains(&t.id)).map(|t| (t.id, t.name.clone())) + }; + let msg = match &track { + Some((_, name)) => format!("Click to add a MIDI clip to {name} at the insertion point."), + None => "Select a MIDI clip (or a MIDI track) to edit its notes.".into(), + }; + ui.painter().text(full.center(), Align2::CENTER_CENTER, msg, regular(13.0), t.text_dim); + let body = Rect::from_min_max(pos2(full.min.x, header.max.y), full.max); + if let Some((id, _)) = track + && ui.interact(body, ui.scope_id().with("new_clip"), Sense::click()).clicked() + { + let sel = app.engine.session().edit.selection; + if let Ok(v) = app.run("midi.new_clip", json!({"track": id.0, "start": sel.start, "end": sel.end})) { + let _ = app.run("edit.select", json!({"clips": [v["clip"]]})); + } + } +} + +/// The grid step in ticks, from the Edit window's grid value. +fn grid_ticks_of(app: &SoundApp) -> i64 { + match app.engine.session().edit.grid { + soundcraft_time::GridValue::Note { value, dotted, triplet } => { + let mut g = value.ticks(); + if dotted { + g = g * 3 / 2; + } + if triplet { + g = g * 2 / 3; + } + g.max(1) + } + _ => TICKS_PER_QUARTER / 4, + } +} + +/// The header toolbar: whole-clip MIDI operations. +fn midi_toolbar(app: &mut SoundApp, ui: &mut Ui, header: Rect, cid: ClipId, grid_ticks: i64) { + // Header toolbar: whole-clip MIDI operations. + let bar = Rect::from_min_max(pos2(header.max.x - 330.0, header.min.y + 1.0), pos2(header.max.x - 6.0, header.max.y - 1.0)); + let mut tb = ui.new_child(egui::UiBuilder::new().max_rect(bar).layout(egui::Layout::right_to_left(egui::Align::Center))); + tb.spacing_mut().item_spacing.x = 4.0; + let mut op: Option<(&str, serde_json::Value)> = None; + if tb.small_button("Legato").clicked() { + op = Some(("event.change_duration", json!({"legato": 0}))); + } + if tb.small_button("Vel −10").clicked() { + op = Some(("event.change_velocity", json!({"add": -10}))); + } + if tb.small_button("Vel +10").clicked() { + op = Some(("event.change_velocity", json!({"add": 10}))); + } + if tb.small_button("Quantize").on_hover_text("Quantize to the grid").clicked() { + op = Some(("event.quantize", json!({"grid": grid_ticks, "strength": 100}))); + } + if let Some((id, mut p)) = op { + p["clips"] = json!([cid.0]); + let _ = app.run(id, p); + } +} + +/// Background rows, piano keys and the grid. +fn draw_roll_background(painter: &egui::Painter, g: &Roll<'_>) { + let Roll { roll, keys, px_per_tick, len_ticks, grid_ticks, rows, .. } = *g; // Background rows and keys. - let top = pitch_at(roll.min.y); + let top = g.pitch_at(roll.min.y); for r in 0..=rows + 1 { let pitch = top - r; - let y = y_of(pitch); + let y = g.y_of(pitch); let row = Rect::from_min_size(pos2(roll.min.x, y), vec2(roll.width(), ROW_H)); painter.rect_filled(row, 0.0, if is_black(pitch) { Color32::from_rgb(30, 30, 32) } else { Color32::from_rgb(40, 40, 42) }); let key = Rect::from_min_size(pos2(keys.min.x, y), vec2(KEY_W - 2.0, ROW_H - 1.0)); @@ -198,7 +268,7 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { let mut tick = 0; while tick <= len_ticks { let bar = tick % (TICKS_PER_QUARTER * 4) == 0; - let x = x_of(tick); + let x = g.x_of(tick); if grid_ticks as f32 * px_per_tick > 4.0 || bar { painter.line_segment( [pos2(x, roll.min.y), pos2(x, roll.max.y)], @@ -207,8 +277,12 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { } tick += grid_ticks; } +} + +/// The notes, with a drag in progress applied. +fn draw_notes(app: &SoundApp, painter: &egui::Painter, g: &Roll<'_>, base: Color32) { + let Roll { sequence, grid_ticks, .. } = *g; // Notes. - let base = egui::Color32::from_rgb(track_color[0], track_color[1], track_color[2]); let drag = app.midi.drag.clone(); for (i, n) in sequence.notes.iter().enumerate() { let (mut st, mut ln, mut p) = (n.start, n.length, i32::from(n.pitch)); @@ -224,7 +298,7 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { p = (p + d.dy_semi as i32).clamp(0, 127); } } - let r = Rect::from_min_max(pos2(x_of(st), y_of(p) + 1.0), pos2(x_of(st + ln).max(x_of(st) + 3.0), y_of(p) + ROW_H - 1.0)); + let r = Rect::from_min_max(pos2(g.x_of(st), g.y_of(p) + 1.0), pos2(g.x_of(st + ln).max(g.x_of(st) + 3.0), g.y_of(p) + ROW_H - 1.0)); let sel = app.midi.selected.contains(&i); let k = 0.45 + 0.55 * f32::from(n.velocity) / 127.0; let fill = if sel { @@ -234,22 +308,17 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { }; painter.rect(r, 2.0, fill, Stroke::new(1.0, Color32::BLACK), StrokeKind::Inside); } +} + +/// Pointer interaction on the roll: note and band selection, drags and clicks. +fn note_pointer(app: &mut SoundApp, ui: &mut Ui, g: &Roll<'_>, cid: ClipId, pencil: bool) { + let Roll { sequence, roll, px_per_tick, grid_ticks, .. } = *g; // Interaction. let resp = ui.interact(roll, ui.scope_id().with(("roll", cid.0)), Sense::click_and_drag()); - let hit = |pos: egui::Pos2| -> Option<(usize, bool)> { - sequence.notes.iter().enumerate().rev().find_map(|(i, n)| { - let r = Rect::from_min_max( - pos2(x_of(n.start), y_of(i32::from(n.pitch))), - pos2(x_of(n.start + n.length).max(x_of(n.start) + 3.0), y_of(i32::from(n.pitch)) + ROW_H), - ); - r.contains(pos).then_some((i, pos.x > r.max.x - 5.0)) - }) - }; - let snap = |tk: f32| -> i64 { ((tk / grid_ticks as f32).round() as i64) * grid_ticks }; if resp.drag_started() && let Some(p) = resp.interact_pointer_pos() { - if let Some((i, edge)) = hit(p) { + if let Some((i, edge)) = g.hit(p) { if !app.midi.selected.contains(&i) { app.midi.selected = vec![i]; } @@ -275,8 +344,8 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { .enumerate() .filter(|(_, n)| { let r = Rect::from_min_max( - pos2(x_of(n.start), y_of(i32::from(n.pitch))), - pos2(x_of(n.start + n.length), y_of(i32::from(n.pitch)) + ROW_H), + pos2(g.x_of(n.start), g.y_of(i32::from(n.pitch))), + pos2(g.x_of(n.start + n.length), g.y_of(i32::from(n.pitch)) + ROW_H), ); r.intersects(band) }) @@ -288,7 +357,7 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { if resp.dragged() && let (Some(d), Some(p)) = (&mut app.midi.drag, resp.interact_pointer_pos()) { - d.dx_ticks = snap((p.x - d.start.x) / px_per_tick); + d.dx_ticks = g.snap((p.x - d.start.x) / px_per_tick); d.dy_semi = -((p.y - d.start.y) / ROW_H).round() as i64; } if resp.drag_stopped() @@ -306,7 +375,7 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { if resp.clicked() && let Some(p) = resp.interact_pointer_pos() { - match hit(p) { + match g.hit(p) { Some((i, _)) => { if ui.input(|x| x.modifiers.shift) { if !app.midi.selected.contains(&i) { @@ -317,8 +386,8 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { } // Audition on the instrument would go here (needs a live MIDI input path). } - None if s.edit.tool == Tool::Pencil => { - let pitch = pitch_at(p.y).clamp(0, 127); + None if pencil => { + let pitch = g.pitch_at(p.y).clamp(0, 127); let start = (((p.x - roll.min.x) / px_per_tick) as i64 / grid_ticks) * grid_ticks; let _ = app.run( "midi.note_add", @@ -329,6 +398,11 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { None => app.midi.selected.clear(), } } +} + +/// Keyboard: select all, move and delete notes. +fn note_keys(app: &mut SoundApp, ui: &mut Ui, g: &Roll<'_>, cid: ClipId) { + let Roll { sequence, roll, grid_ticks, .. } = *g; // Keyboard: Cmd+A select all; arrows transpose (Shift = octave) and move by the grid. if ui.rect_contains_pointer(roll) && !ui.ctx().egui_wants_keyboard_input() { let (all, up, down, left, right, shift) = ui.input(|i| { @@ -373,12 +447,18 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { let _ = app.run("midi.note_delete", json!({"clip": cid.0, "indices": app.midi.selected})); app.midi.selected.clear(); } +} + +/// The velocity lane under the roll. +fn velocity_lane(app: &mut SoundApp, ui: &mut Ui, g: &Roll<'_>, cid: ClipId, base: Color32, label_x: f32) { + let Roll { sequence, vel, .. } = *g; + let t = Tokens::current(); // Velocity lane. ui.painter().rect_filled(vel, 0.0, Color32::from_rgb(28, 28, 30)); - ui.painter().text(pos2(full.min.x + 6.0, vel.center().y), Align2::LEFT_CENTER, "Velocity", regular(10.0), t.text_dim); + ui.painter().text(pos2(label_x, vel.center().y), Align2::LEFT_CENTER, "Velocity", regular(10.0), t.text_dim); let vresp = ui.interact(vel, ui.scope_id().with(("vel", cid.0)), Sense::click_and_drag()); for (i, n) in sequence.notes.iter().enumerate() { - let x = x_of(n.start); + let x = g.x_of(n.start); let h = (vel.height() - 4.0) * f32::from(n.velocity) / 127.0; let col = if app.midi.selected.contains(&i) { Color32::WHITE } else { base }; ui.painter().line_segment([pos2(x + 1.0, vel.max.y - 2.0), pos2(x + 1.0, vel.max.y - 2.0 - h)], Stroke::new(3.0, col)); @@ -386,8 +466,8 @@ pub fn show(app: &mut SoundApp, ui: &mut Ui) { } if (vresp.clicked() || vresp.dragged()) && let Some(p) = vresp.interact_pointer_pos() - && let Some((i, n)) = sequence.notes.iter().enumerate().min_by_key(|(_, n)| ((x_of(n.start) - p.x).abs() * 10.0) as i64) - && (x_of(n.start) - p.x).abs() < 8.0 + && let Some((i, n)) = sequence.notes.iter().enumerate().min_by_key(|(_, n)| ((g.x_of(n.start) - p.x).abs() * 10.0) as i64) + && (g.x_of(n.start) - p.x).abs() < 8.0 { let v = (((vel.max.y - 2.0 - p.y) / (vel.height() - 4.0)) * 127.0).round().clamp(1.0, 127.0) as i64; let _ = app.engine.execute_merged("midi.note_edit", &json!({"clip": cid.0, "index": i, "velocity": v}), &format!("vel:{}", cid.0)); diff --git a/crates/ui-egui/src/panels/plugins.rs b/crates/ui-egui/src/panels/plugins.rs index 7a7e575c..29125432 100644 --- a/crates/ui-egui/src/panels/plugins.rs +++ b/crates/ui-egui/src/panels/plugins.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{Color32, Sense, SoundApp, Stroke, Tokens, Ui, json, mono, pos2, vec2}; /// Draw plugin windows for instrument and insert slots that still exist, removing closed ones from the list. pub(super) fn plugin_windows(app: &mut SoundApp, ctx: &egui::Context) { diff --git a/crates/ui-egui/src/panels/tracks.rs b/crates/ui-egui/src/panels/tracks.rs index 41b82794..b4fd9bb8 100644 --- a/crates/ui-egui/src/panels/tracks.rs +++ b/crates/ui-egui/src/panels/tracks.rs @@ -1,4 +1,7 @@ -use super::*; +use super::{ + Align2, Color32, Rect, Sense, SoundApp, Stroke, Tokens, Ui, bold, gutter_scroll, json, panel_header, pos2, regular, rgb, vec2, + with_pending_offset, +}; /// Drag a Tracks list row to reorder the tracks: a line marks the gap it will land in. fn drag_track(app: &mut SoundApp, ui: &Ui, row: Rect, id: u64, dropped: bool) { diff --git a/crates/ui-egui/src/panels/transport.rs b/crates/ui-egui/src/panels/transport.rs index 511ccb51..9451c1d6 100644 --- a/crates/ui-egui/src/panels/transport.rs +++ b/crates/ui-egui/src/panels/transport.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{Align2, Rect, Sense, SoundApp, Tokens, json, mono, pos2, vec2}; pub(super) fn transport_window(app: &mut SoundApp, ctx: &egui::Context) { let mut open = app.ui.show_transport; diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index 35319f1c..e2c9a979 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -177,6 +177,14 @@ fn fixed(app: &mut SoundApp, key: Key, m: Modifiers, mac: bool) -> bool { let _ = app.run("edit.tool", json!({"tool": tool})); true } + _ => fixed_more(app, key, m, mac), + } +} + +/// The rest of [`fixed`]'s keys. +fn fixed_more(app: &mut SoundApp, key: Key, m: Modifiers, mac: bool) -> bool { + let plain = !m.command && !m.alt && !m.ctrl && !m.shift; + match key { // `=` is what a bare press of the unlabeled +/- key reports (it has no shift held, so it // can never be `Key::Plus`); `Key::Plus` is kept for a keyboard that does send it (e.g. a // numpad +). `!m.alt` leaves Alt+= to `window.event_list`'s registered shortcut. diff --git a/crates/ui-egui/src/tests.rs b/crates/ui-egui/src/tests.rs index bbbba444..892b5393 100644 --- a/crates/ui-egui/src/tests.rs +++ b/crates/ui-egui/src/tests.rs @@ -1387,6 +1387,56 @@ fn transport_simulation_advances_by_monotonic_elapsed_time_only() { app.engine.transport.playing = false; app.sim = None; } +/// One host iteration: the raw-input hook, then logic and UI in a single pass. +fn run_host_frame(app: &mut SoundApp, ctx: &egui::Context, mut input: egui::RawInput) { + app.raw_input_hook(ctx, &mut input); + let mut output = ctx.run_ui(input, |ui| { + app.logic(ui.ctx()); + app.ui(ui); + }); + output.textures_delta.clear(); +} + +/// Run one frame that discards its first layout pass; returns how many passes ran. +fn run_host_frame_discarding_first_pass(app: &mut SoundApp, ctx: &egui::Context, input: egui::RawInput) -> usize { + let mut passes = 0; + let mut output = ctx.run_ui(input, |ui| { + app.logic(ui.ctx()); + passes += 1; + if passes == 1 { + ui.ctx().request_discard("plugin gesture must survive a second layout pass"); + } + app.ui(ui); + }); + output.textures_delta.clear(); + passes +} + +/// The editor-poll fixture: each poll counts itself, takes the queued edits and, for the gestures that +/// ended, captures the instrument state `latest` holds. +fn poll_fixture( + reports: &std::rc::Rc>>, + latest: &std::rc::Rc>, + polls: &std::rc::Rc>, + captures: &std::rc::Rc>>, +) -> super::EditorPollFixture { + let (poll_reports, poll_latest, poll_count, poll_captures) = (reports.clone(), latest.clone(), polls.clone(), captures.clone()); + Box::new(move |pending| { + poll_count.set(poll_count.get() + 1); + let edits = std::mem::take(&mut *poll_reports.borrow_mut()); + super::editor_changes( + pending, + edits, + Vec::new(), + |_| false, + |due| { + poll_captures.borrow_mut().extend_from_slice(due); + due.iter().map(|&(track, slot)| (track, slot, vec![poll_latest.get(), 1])).collect() + }, + ) + }) +} + /// Plugin queues are drained once per host iteration, rather than once per discarded pass. /// The fixture replaces only native polling; the real raw hook, logic/UI, scheduler and /// engine state/undo commands run normally. @@ -1400,13 +1450,7 @@ fn editor_gesture_polling_survives_discarded_and_hidden_host_iterations() { let rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1400.0, 900.0)); let raw = || egui::RawInput { screen_rect: Some(rect), ..Default::default() }; for _ in 0..4 { - let mut input = raw(); - app.raw_input_hook(&ctx, &mut input); - let mut output = ctx.run_ui(input, |ui| { - app.logic(ui.ctx()); - app.ui(ui); - }); - output.textures_delta.clear(); + run_host_frame(&mut app, &ctx, raw()); } let keys = app.engine.session().track_by_name("Keys").unwrap().id; let original = instrument_state(&app, keys); @@ -1421,34 +1465,11 @@ fn editor_gesture_polling_survives_discarded_and_hidden_host_iterations() { let latest = Rc::new(Cell::new(1u8)); let polls = Rc::new(Cell::new(0)); let captures = Rc::new(RefCell::new(Vec::new())); - let (poll_reports, poll_latest, poll_count, poll_captures) = (reports.clone(), latest.clone(), polls.clone(), captures.clone()); - app.editor_poll_fixture = Some(Box::new(move |pending| { - poll_count.set(poll_count.get() + 1); - let edits = std::mem::take(&mut *poll_reports.borrow_mut()); - super::editor_changes( - pending, - edits, - Vec::new(), - |_| false, - |due| { - poll_captures.borrow_mut().extend_from_slice(due); - due.iter().map(|&(track, slot)| (track, slot, vec![poll_latest.get(), 1])).collect() - }, - ) - })); + app.editor_poll_fixture = Some(poll_fixture(&reports, &latest, &polls, &captures)); reports.borrow_mut().push((keys, soundcraft_mix::INSTRUMENT_SLOT, "cutoff".into(), 1.0)); let mut input = raw(); app.raw_input_hook(&ctx, &mut input); - let mut passes = 0; - let mut output = ctx.run_ui(input, |ui| { - app.logic(ui.ctx()); - passes += 1; - if passes == 1 { - ui.ctx().request_discard("plugin gesture must survive a second layout pass"); - } - app.ui(ui); - }); - output.textures_delta.clear(); + let passes = run_host_frame_discarding_first_pass(&mut app, &ctx, input); assert!(passes >= 2, "the regression must actually force a discarded pass"); assert_eq!(polls.get(), 1, "the rerun cannot drain an empty editor queue"); assert!(captures.borrow().is_empty(), "the ongoing gesture has not ended"); @@ -1490,7 +1511,12 @@ fn editor_gesture_polling_survives_discarded_and_hidden_host_iterations() { let _ = ctx.run_logic(&hidden, |ctx| app.logic(ctx)); assert_eq!(polls.get(), 4, "a fresh empty host iteration is still live"); assert_eq!(captures.borrow().len(), 1, "the completed gesture is captured once"); - assert_eq!(app.engine.revision, settled_revision); + assert_history_unchanged(&app, settled_revision, undo, &history, &journal); +} + +/// The engine's revision, undo history and journal are as they were. +fn assert_history_unchanged(app: &SoundApp, revision: u64, undo: usize, history: &[String], journal: &[(String, serde_json::Value)]) { + assert_eq!(app.engine.revision, revision); assert_eq!(app.engine.undo_history().len(), undo); assert_eq!(app.engine.undo_history(), history); assert_eq!(app.engine.journal, journal); diff --git a/crates/ui-egui/tests/mix_instrument_contrast.rs b/crates/ui-egui/tests/mix_instrument_contrast.rs index bcdc5c28..2bb6cd92 100644 --- a/crates/ui-egui/tests/mix_instrument_contrast.rs +++ b/crates/ui-egui/tests/mix_instrument_contrast.rs @@ -72,6 +72,19 @@ fn contrast(a: Color32, b: Color32) -> f64 { (a.max(b) + 0.05) / (a.min(b) + 0.05) } +/// A primary click at `at`, one frame for the press and one for the release. +fn press_and_release(harness: &mut egui_kittest::Harness<'_, SoundApp>, at: egui::Pos2) { + for pressed in [true, false] { + harness.input_mut().events.push(Event::PointerButton { + pos: at, + button: egui::PointerButton::Primary, + pressed, + modifiers: Default::default(), + }); + harness.step(); + } +} + /// In real frames, populated and empty instrument and insert slots use the active theme's readable palette across /// light, dark and system modes, and a plugin change leaves the rest of the session and audio pool intact. #[test] @@ -169,15 +182,7 @@ fn populated_mix_instrument_uses_readable_palette_in_real_frames() { // Exercise the same painted control's ordinary click/picker path after palette changes. let at = slot(harness.output(), "SubSynth").rect.center(); - for pressed in [true, false] { - harness.input_mut().events.push(Event::PointerButton { - pos: at, - button: egui::PointerButton::Primary, - pressed, - modifiers: Default::default(), - }); - harness.step(); - } + press_and_release(&mut harness, at); harness.step(); // Let the popup finish its initial sizing pass. harness.get_by_label("Built-in").hover(); harness.step(); diff --git a/crates/video/src/h264/deblock.rs b/crates/video/src/h264/deblock.rs index 12ba8da5..e918476c 100644 --- a/crates/video/src/h264/deblock.rs +++ b/crates/video/src/h264/deblock.rs @@ -279,7 +279,6 @@ fn filter_horizontal(pix: &mut [u8], stride: usize, x: usize, y: } /// Deblock one macroblock (macroblocks must be processed in raster order). -#[allow(clippy::needless_range_loop)] pub fn deblock_mb(pic: &mut PicState, addr: usize, mb_w: usize) { let q = &pic.mbs[addr]; if q.slice_num == u32::MAX { @@ -303,6 +302,23 @@ pub fn deblock_mb(pic: &mut PicState, addr: usize, mb_w: usize) { let alpha_off = sq.alpha_offset; let beta_off = sq.beta_offset; let t8 = q.transform_8x8; + let bs = boundary_strengths(pic, q, sq, left, top, t8); + let qp_of = |st: &MbState| if st.kind == MbKind::IPcm { 0 } else { st.qp as i32 }; + let q_qp = qp_of(q); + let q_qpc = q.qpc; + let p_qp = [left.map(|n| (qp_of(&pic.mbs[n]), pic.mbs[n].qpc)), top.map(|n| (qp_of(&pic.mbs[n]), pic.mbs[n].qpc))]; + let params = |qpp: i32, qpq: i32, b: [u8; 4]| { + let qpav = (qpp + qpq + 1) >> 1; + let index_a = (qpav + alpha_off).clamp(0, 51) as usize; + let index_b = (qpav + beta_off).clamp(0, 51) as usize; + let tc = |b: u8| if (1..4).contains(&b) { TC0[index_a][b as usize - 1] as i32 } else { 0 }; + EdgeParams { bs: b, tc0: [tc(b[0]), tc(b[1]), tc(b[2]), tc(b[3])], alpha: ALPHA[index_a] as i32, beta: BETA[index_b] as i32 } + }; + filter_luma(pic, (mx, my), &bs, q_qp, p_qp, ¶ms); + filter_chroma(pic, (mx, my), &bs, q_qpc, p_qp, ¶ms); +} + +fn boundary_strengths(pic: &PicState, q: &MbState, sq: &SliceInfo, left: Option, top: Option, t8: bool) -> [[[u8; 4]; 4]; 2] { // bS[dir][edge][segment]; dir 0 = vertical edges (x), 1 = horizontal edges (y) let mut bs = [[[0u8; 4]; 4]; 2]; if q.kind.is_intra() { @@ -356,20 +372,18 @@ pub fn deblock_mb(pic: &mut PicState, addr: usize, mb_w: usize) { } } } - let qp_of = |st: &MbState| if st.kind == MbKind::IPcm { 0 } else { st.qp as i32 }; - let q_qp = qp_of(q); - let q_qpc = q.qpc; - let p_qp = [left.map(|n| (qp_of(&pic.mbs[n]), pic.mbs[n].qpc)), top.map(|n| (qp_of(&pic.mbs[n]), pic.mbs[n].qpc))]; - let params = |qpp: i32, qpq: i32, b: [u8; 4]| { - let qpav = (qpp + qpq + 1) >> 1; - let index_a = (qpav + alpha_off).clamp(0, 51) as usize; - let index_b = (qpav + beta_off).clamp(0, 51) as usize; - let tc = |b: u8| if (1..4).contains(&b) { TC0[index_a][b as usize - 1] as i32 } else { 0 }; - EdgeParams { bs: b, tc0: [tc(b[0]), tc(b[1]), tc(b[2]), tc(b[3])], alpha: ALPHA[index_a] as i32, beta: BETA[index_b] as i32 } - }; + bs +} + +fn filter_luma( + pic: &mut PicState, + (mx, my): (usize, usize), + bs: &[[[u8; 4]; 4]; 2], + q_qp: i32, + p_qp: [Option<(i32, [u8; 2])>; 2], + params: &impl Fn(i32, i32, [u8; 4]) -> EdgeParams, +) { let width = pic.planes.width; - let cwidth = pic.planes.cwidth; - // luma for dir in 0..2 { for e in 0..4 { if bs[dir][e] == [0; 4] { @@ -389,6 +403,17 @@ pub fn deblock_mb(pic: &mut PicState, addr: usize, mb_w: usize) { } } } +} + +fn filter_chroma( + pic: &mut PicState, + (mx, my): (usize, usize), + bs: &[[[u8; 4]; 4]; 2], + q_qpc: [u8; 2], + p_qp: [Option<(i32, [u8; 2])>; 2], + params: &impl Fn(i32, i32, [u8; 4]) -> EdgeParams, +) { + let cwidth = pic.planes.cwidth; // chroma (4:2:0): edges at chroma samples 0 and 4 use the bS of luma edges 0 and 2 for c in 0..2 { for dir in 0..2 { diff --git a/crates/video/src/h264/intra.rs b/crates/video/src/h264/intra.rs index decf2965..176d9a11 100644 --- a/crates/video/src/h264/intra.rs +++ b/crates/video/src/h264/intra.rs @@ -61,125 +61,146 @@ fn load_edge(plane: &[u8], stride: usize, x: usize, y: usize, n: usize, a: Avail /// Directional / DC prediction shared by Intra_4x4 and Intra_8x8 (8.3.1.2.x, 8.3.2.2.x). fn pred_nxn(mode: u8, e: &Edge, n: i32, a: Avail, out: &mut [u8], stride: usize) { - let mut put = |x: i32, y: i32, v: i32| out[y as usize * stride + x as usize] = v as u8; match mode { - 0 => { - for y in 0..n { - for x in 0..n { - put(x, y, e.t(x)); - } - } + 0 => pred_vertical(e, n, out, stride), + 1 => pred_horizontal(e, n, out, stride), + 2 => pred_dc(e, n, a, out, stride), + 3 => pred_down_left(e, n, out, stride), + 4 => pred_down_right(e, n, out, stride), + 5 => pred_vertical_right(e, n, out, stride), + 6 => pred_horizontal_down(e, n, out, stride), + 7 => pred_vertical_left(e, n, out, stride), + _ => pred_horizontal_up(e, n, out, stride), + } +} + +fn put(out: &mut [u8], stride: usize, x: i32, y: i32, value: i32) { + out[y as usize * stride + x as usize] = value as u8; +} + +fn pred_vertical(e: &Edge, n: i32, out: &mut [u8], stride: usize) { + for y in 0..n { + for x in 0..n { + put(out, stride, x, y, e.t(x)); } - 1 => { - for y in 0..n { - for x in 0..n { - put(x, y, e.l(y)); - } - } + } +} + +fn pred_horizontal(e: &Edge, n: i32, out: &mut [u8], stride: usize) { + for y in 0..n { + for x in 0..n { + put(out, stride, x, y, e.l(y)); } - 2 => { - let shift = if n == 4 { 2 } else { 3 }; - let st: i32 = (0..n).map(|x| e.t(x)).sum(); - let sl: i32 = (0..n).map(|y| e.l(y)).sum(); - let dc = match (a.top, a.left) { - (true, true) => (st + sl + n) >> (shift + 1), - (false, true) => (sl + (n >> 1)) >> shift, - (true, false) => (st + (n >> 1)) >> shift, - _ => 128, + } +} + +fn pred_dc(e: &Edge, n: i32, a: Avail, out: &mut [u8], stride: usize) { + let shift = if n == 4 { 2 } else { 3 }; + let st: i32 = (0..n).map(|x| e.t(x)).sum(); + let sl: i32 = (0..n).map(|y| e.l(y)).sum(); + let dc = match (a.top, a.left) { + (true, true) => (st + sl + n) >> (shift + 1), + (false, true) => (sl + (n >> 1)) >> shift, + (true, false) => (st + (n >> 1)) >> shift, + _ => 128, + }; + for y in 0..n { + for x in 0..n { + put(out, stride, x, y, dc); + } + } +} + +fn pred_down_left(e: &Edge, n: i32, out: &mut [u8], stride: usize) { + for y in 0..n { + for x in 0..n { + let value = if x == n - 1 && y == n - 1 { + (e.t(2 * n - 2) + 3 * e.t(2 * n - 1) + 2) >> 2 + } else { + (e.t(x + y) + 2 * e.t(x + y + 1) + e.t(x + y + 2) + 2) >> 2 }; - for y in 0..n { - for x in 0..n { - put(x, y, dc); - } - } + put(out, stride, x, y, value); } - 3 => { - for y in 0..n { - for x in 0..n { - let v = if x == n - 1 && y == n - 1 { - (e.t(2 * n - 2) + 3 * e.t(2 * n - 1) + 2) >> 2 - } else { - (e.t(x + y) + 2 * e.t(x + y + 1) + e.t(x + y + 2) + 2) >> 2 - }; - put(x, y, v); - } - } + } +} + +fn pred_down_right(e: &Edge, n: i32, out: &mut [u8], stride: usize) { + for y in 0..n { + for x in 0..n { + let value = if x > y { + (e.t(x - y - 2) + 2 * e.t(x - y - 1) + e.t(x - y) + 2) >> 2 + } else if x < y { + (e.l(y - x - 2) + 2 * e.l(y - x - 1) + e.l(y - x) + 2) >> 2 + } else { + (e.t(0) + 2 * e.tl + e.l(0) + 2) >> 2 + }; + put(out, stride, x, y, value); } - 4 => { - for y in 0..n { - for x in 0..n { - let v = if x > y { - (e.t(x - y - 2) + 2 * e.t(x - y - 1) + e.t(x - y) + 2) >> 2 - } else if x < y { - (e.l(y - x - 2) + 2 * e.l(y - x - 1) + e.l(y - x) + 2) >> 2 - } else { - (e.t(0) + 2 * e.tl + e.l(0) + 2) >> 2 - }; - put(x, y, v); - } - } + } +} + +fn pred_vertical_right(e: &Edge, n: i32, out: &mut [u8], stride: usize) { + for y in 0..n { + for x in 0..n { + let z = 2 * x - y; + let value = if z >= 0 && z % 2 == 0 { + (e.t(x - (y >> 1) - 1) + e.t(x - (y >> 1)) + 1) >> 1 + } else if z > 0 { + (e.t(x - (y >> 1) - 2) + 2 * e.t(x - (y >> 1) - 1) + e.t(x - (y >> 1)) + 2) >> 2 + } else if z == -1 { + (e.l(0) + 2 * e.tl + e.t(0) + 2) >> 2 + } else { + (e.l(y - 2 * x - 1) + 2 * e.l(y - 2 * x - 2) + e.l(y - 2 * x - 3) + 2) >> 2 + }; + put(out, stride, x, y, value); } - 5 => { - for y in 0..n { - for x in 0..n { - let z = 2 * x - y; - let v = if z >= 0 && z % 2 == 0 { - (e.t(x - (y >> 1) - 1) + e.t(x - (y >> 1)) + 1) >> 1 - } else if z > 0 { - (e.t(x - (y >> 1) - 2) + 2 * e.t(x - (y >> 1) - 1) + e.t(x - (y >> 1)) + 2) >> 2 - } else if z == -1 { - (e.l(0) + 2 * e.tl + e.t(0) + 2) >> 2 - } else { - (e.l(y - 2 * x - 1) + 2 * e.l(y - 2 * x - 2) + e.l(y - 2 * x - 3) + 2) >> 2 - }; - put(x, y, v); - } - } + } +} + +fn pred_horizontal_down(e: &Edge, n: i32, out: &mut [u8], stride: usize) { + for y in 0..n { + for x in 0..n { + let z = 2 * y - x; + let value = if z >= 0 && z % 2 == 0 { + (e.l(y - (x >> 1) - 1) + e.l(y - (x >> 1)) + 1) >> 1 + } else if z > 0 { + (e.l(y - (x >> 1) - 2) + 2 * e.l(y - (x >> 1) - 1) + e.l(y - (x >> 1)) + 2) >> 2 + } else if z == -1 { + (e.l(0) + 2 * e.tl + e.t(0) + 2) >> 2 + } else { + (e.t(x - 2 * y - 1) + 2 * e.t(x - 2 * y - 2) + e.t(x - 2 * y - 3) + 2) >> 2 + }; + put(out, stride, x, y, value); } - 6 => { - for y in 0..n { - for x in 0..n { - let z = 2 * y - x; - let v = if z >= 0 && z % 2 == 0 { - (e.l(y - (x >> 1) - 1) + e.l(y - (x >> 1)) + 1) >> 1 - } else if z > 0 { - (e.l(y - (x >> 1) - 2) + 2 * e.l(y - (x >> 1) - 1) + e.l(y - (x >> 1)) + 2) >> 2 - } else if z == -1 { - (e.l(0) + 2 * e.tl + e.t(0) + 2) >> 2 - } else { - (e.t(x - 2 * y - 1) + 2 * e.t(x - 2 * y - 2) + e.t(x - 2 * y - 3) + 2) >> 2 - }; - put(x, y, v); - } - } + } +} + +fn pred_vertical_left(e: &Edge, n: i32, out: &mut [u8], stride: usize) { + for y in 0..n { + for x in 0..n { + let i = x + (y >> 1); + let value = if y % 2 == 0 { (e.t(i) + e.t(i + 1) + 1) >> 1 } else { (e.t(i) + 2 * e.t(i + 1) + e.t(i + 2) + 2) >> 2 }; + put(out, stride, x, y, value); } - 7 => { - for y in 0..n { - for x in 0..n { - let i = x + (y >> 1); - let v = if y % 2 == 0 { (e.t(i) + e.t(i + 1) + 1) >> 1 } else { (e.t(i) + 2 * e.t(i + 1) + e.t(i + 2) + 2) >> 2 }; - put(x, y, v); - } - } - } - _ => { - let zmax = 2 * n - 3; - for y in 0..n { - for x in 0..n { - let z = x + 2 * y; - let i = y + (x >> 1); - let v = if z < zmax && z % 2 == 0 { - (e.l(i) + e.l(i + 1) + 1) >> 1 - } else if z < zmax { - (e.l(i) + 2 * e.l(i + 1) + e.l(i + 2) + 2) >> 2 - } else if z == zmax { - (e.l(n - 2) + 3 * e.l(n - 1) + 2) >> 2 - } else { - e.l(n - 1) - }; - put(x, y, v); - } - } + } +} + +fn pred_horizontal_up(e: &Edge, n: i32, out: &mut [u8], stride: usize) { + let zmax = 2 * n - 3; + for y in 0..n { + for x in 0..n { + let z = x + 2 * y; + let i = y + (x >> 1); + let value = if z < zmax && z % 2 == 0 { + (e.l(i) + e.l(i + 1) + 1) >> 1 + } else if z < zmax { + (e.l(i) + 2 * e.l(i + 1) + e.l(i + 2) + 2) >> 2 + } else if z == zmax { + (e.l(n - 2) + 3 * e.l(n - 1) + 2) >> 2 + } else { + e.l(n - 1) + }; + put(out, stride, x, y, value); } } } diff --git a/crates/video/src/h264/slice.rs b/crates/video/src/h264/slice.rs index bd641d5a..06a2bc67 100644 --- a/crates/video/src/h264/slice.rs +++ b/crates/video/src/h264/slice.rs @@ -214,101 +214,9 @@ impl SliceHeader { num_ref_idx_active[0] = 0; } ensure!(nal.nal_unit_type != 20 && nal.nal_unit_type != 21, "MVC slices are not supported"); - let mut ref_pic_list_mod = [Vec::new(), Vec::new()]; - for (list, mods) in ref_pic_list_mod.iter_mut().enumerate() { - let present = if list == 0 { !slice_type.is_intra() } else { slice_type.is_b() }; - if present && r.read_flag()? { - loop { - let idc = r.read_ue()?; - ensure!(idc <= 5, "modification_of_pic_nums_idc out of range"); - if idc == 3 { - break; - } - let value = r.read_ue()?; - mods.push(RefPicListMod { idc, value }); - ensure!(mods.len() <= 66, "too many ref_pic_list_modification entries"); - } - } - } - let mut pred_weight_table = None; - if (pps.weighted_pred && slice_type.is_p()) || (pps.weighted_bipred_idc == 1 && slice_type.is_b()) { - let luma_log2_denom = r.read_ue()?; - ensure!(luma_log2_denom <= 7, "luma_log2_weight_denom out of range"); - let mut chroma_log2_denom = 0; - if sps.chroma_array_type() != 0 { - chroma_log2_denom = r.read_ue()?; - ensure!(chroma_log2_denom <= 7, "chroma_log2_weight_denom out of range"); - } - let mut lists = [Vec::new(), Vec::new()]; - let nlists = if slice_type.is_b() { 2 } else { 1 }; - for (l, entries) in lists.iter_mut().enumerate().take(nlists) { - for _ in 0..num_ref_idx_active[l] { - let mut e = WeightEntry { luma_weight: 1 << luma_log2_denom, chroma_weight: [1 << chroma_log2_denom; 2], ..Default::default() }; - if r.read_flag()? { - e.luma_flag = true; - e.luma_weight = r.read_se()?; - e.luma_offset = r.read_se()?; - ensure!((-128..=127).contains(&e.luma_weight) && (-128..=127).contains(&e.luma_offset), "luma weight out of range"); - } - if sps.chroma_array_type() != 0 && r.read_flag()? { - e.chroma_flag = true; - for j in 0..2 { - e.chroma_weight[j] = r.read_se()?; - e.chroma_offset[j] = r.read_se()?; - ensure!( - (-128..=127).contains(&e.chroma_weight[j]) && (-128..=127).contains(&e.chroma_offset[j]), - "chroma weight out of range" - ); - } - } - entries.push(e); - } - } - let [l0, l1] = lists; - pred_weight_table = Some(PredWeightTable { luma_log2_denom, chroma_log2_denom, l0, l1 }); - } - let mut no_output_of_prior_pics = false; - let mut long_term_reference = false; - let mut adaptive_ref_pic_marking = false; - let mut mmcos = Vec::new(); - if nal.nal_ref_idc != 0 { - if idr { - no_output_of_prior_pics = r.read_flag()?; - long_term_reference = r.read_flag()?; - } else { - adaptive_ref_pic_marking = r.read_flag()?; - if adaptive_ref_pic_marking { - loop { - let op = r.read_ue()?; - ensure!(op <= 6, "memory_management_control_operation out of range"); - if op == 0 { - break; - } - let mut m = Mmco { - op, - difference_of_pic_nums_minus1: 0, - long_term_pic_num: 0, - long_term_frame_idx: 0, - max_long_term_frame_idx_plus1: 0, - }; - if op == 1 || op == 3 { - m.difference_of_pic_nums_minus1 = r.read_ue()?; - } - if op == 2 { - m.long_term_pic_num = r.read_ue()?; - } - if op == 3 || op == 6 { - m.long_term_frame_idx = r.read_ue()?; - } - if op == 4 { - m.max_long_term_frame_idx_plus1 = r.read_ue()?; - } - mmcos.push(m); - ensure!(mmcos.len() <= 66, "too many MMCOs"); - } - } - } - } + let ref_fields = parse_reference_fields(&mut r, nal, idr, slice_type, pps, sps, num_ref_idx_active)?; + let RefFields { ref_pic_list_mod, pred_weight_table, no_output_of_prior_pics, long_term_reference, adaptive_ref_pic_marking, mmcos } = + ref_fields; let mut cabac_init_idc = 0; if pps.entropy_coding_mode && !slice_type.is_intra() { cabac_init_idc = r.read_ue()?; @@ -391,6 +299,134 @@ impl SliceHeader { } } +struct RefFields { + ref_pic_list_mod: [Vec; 2], + pred_weight_table: Option, + no_output_of_prior_pics: bool, + long_term_reference: bool, + adaptive_ref_pic_marking: bool, + mmcos: Vec, +} + +fn parse_reference_fields( + r: &mut BitReader<'_>, + nal: NalHeader, + idr: bool, + slice_type: SliceType, + pps: &Pps, + sps: &Sps, + num_ref_idx_active: [u32; 2], +) -> Result { + let ref_pic_list_mod = parse_ref_pic_list_mod(r, slice_type)?; + let pred_weight_table = parse_pred_weight_table(r, slice_type, pps, sps, num_ref_idx_active)?; + let (no_output_of_prior_pics, long_term_reference, adaptive_ref_pic_marking, mmcos) = parse_ref_pic_marking(r, nal, idr)?; + Ok(RefFields { ref_pic_list_mod, pred_weight_table, no_output_of_prior_pics, long_term_reference, adaptive_ref_pic_marking, mmcos }) +} + +fn parse_ref_pic_list_mod(r: &mut BitReader<'_>, slice_type: SliceType) -> Result<[Vec; 2]> { + let mut lists = [Vec::new(), Vec::new()]; + for (list, mods) in lists.iter_mut().enumerate() { + let present = if list == 0 { !slice_type.is_intra() } else { slice_type.is_b() }; + if present && r.read_flag()? { + loop { + let idc = r.read_ue()?; + ensure!(idc <= 5, "modification_of_pic_nums_idc out of range"); + if idc == 3 { + break; + } + mods.push(RefPicListMod { idc, value: r.read_ue()? }); + ensure!(mods.len() <= 66, "too many ref_pic_list_modification entries"); + } + } + } + Ok(lists) +} + +fn parse_pred_weight_table(r: &mut BitReader<'_>, slice_type: SliceType, pps: &Pps, sps: &Sps, active: [u32; 2]) -> Result> { + let weighted = (pps.weighted_pred && slice_type.is_p()) || (pps.weighted_bipred_idc == 1 && slice_type.is_b()); + if !weighted { + return Ok(None); + } + let luma_log2_denom = r.read_ue()?; + ensure!(luma_log2_denom <= 7, "luma_log2_weight_denom out of range"); + let chroma_log2_denom = if sps.chroma_array_type() == 0 { 0 } else { r.read_ue()? }; + ensure!(chroma_log2_denom <= 7, "chroma_log2_weight_denom out of range"); + let mut lists = [Vec::new(), Vec::new()]; + let list_count = if slice_type.is_b() { 2 } else { 1 }; + for (list, entries) in lists.iter_mut().enumerate().take(list_count) { + for _ in 0..active[list] { + entries.push(parse_weight_entry(r, luma_log2_denom, chroma_log2_denom, sps.chroma_array_type() != 0)?); + } + } + let [l0, l1] = lists; + Ok(Some(PredWeightTable { luma_log2_denom, chroma_log2_denom, l0, l1 })) +} + +fn parse_weight_entry(r: &mut BitReader<'_>, luma_denom: u32, chroma_denom: u32, has_chroma: bool) -> Result { + let mut entry = WeightEntry { luma_weight: 1 << luma_denom, chroma_weight: [1 << chroma_denom; 2], ..Default::default() }; + if r.read_flag()? { + entry.luma_flag = true; + entry.luma_weight = r.read_se()?; + entry.luma_offset = r.read_se()?; + ensure!((-128..=127).contains(&entry.luma_weight) && (-128..=127).contains(&entry.luma_offset), "luma weight out of range"); + } + if has_chroma && r.read_flag()? { + entry.chroma_flag = true; + for index in 0..2 { + entry.chroma_weight[index] = r.read_se()?; + entry.chroma_offset[index] = r.read_se()?; + ensure!( + (-128..=127).contains(&entry.chroma_weight[index]) && (-128..=127).contains(&entry.chroma_offset[index]), + "chroma weight out of range" + ); + } + } + Ok(entry) +} + +fn parse_ref_pic_marking(r: &mut BitReader<'_>, nal: NalHeader, idr: bool) -> Result<(bool, bool, bool, Vec)> { + let (mut no_output, mut long_term, mut adaptive) = (false, false, false); + let mut mmcos = Vec::new(); + if nal.nal_ref_idc == 0 { + return Ok((no_output, long_term, adaptive, mmcos)); + } + if idr { + no_output = r.read_flag()?; + long_term = r.read_flag()?; + } else { + adaptive = r.read_flag()?; + if adaptive { + parse_mmcos(r, &mut mmcos)?; + } + } + Ok((no_output, long_term, adaptive, mmcos)) +} + +fn parse_mmcos(r: &mut BitReader<'_>, mmcos: &mut Vec) -> Result<()> { + loop { + let op = r.read_ue()?; + ensure!(op <= 6, "memory_management_control_operation out of range"); + if op == 0 { + return Ok(()); + } + let mut m = Mmco { op, difference_of_pic_nums_minus1: 0, long_term_pic_num: 0, long_term_frame_idx: 0, max_long_term_frame_idx_plus1: 0 }; + if op == 1 || op == 3 { + m.difference_of_pic_nums_minus1 = r.read_ue()?; + } + if op == 2 { + m.long_term_pic_num = r.read_ue()?; + } + if op == 3 || op == 6 { + m.long_term_frame_idx = r.read_ue()?; + } + if op == 4 { + m.max_long_term_frame_idx_plus1 = r.read_ue()?; + } + mmcos.push(m); + ensure!(mmcos.len() <= 66, "too many MMCOs"); + } +} + /// POC decoding state carried between pictures (8.2.1). #[derive(Clone, Debug, Default)] pub struct PocState { diff --git a/crates/vst3-host/Cargo.toml b/crates/vst3-host/Cargo.toml index 00ace628..7a956118 100644 --- a/crates/vst3-host/Cargo.toml +++ b/crates/vst3-host/Cargo.toml @@ -54,7 +54,9 @@ harness = false unsafe_code = "deny" [lints.clippy] +cognitive_complexity = "deny" +wildcard_imports = "deny" dbg_macro = "warn" todo = "warn" -too_many_arguments = "allow" +too_many_arguments = "deny" type_complexity = "allow" diff --git a/crates/vst3-host/src/ffi.rs b/crates/vst3-host/src/ffi.rs index ed42bd6f..dc65b7a8 100644 --- a/crates/vst3-host/src/ffi.rs +++ b/crates/vst3-host/src/ffi.rs @@ -39,7 +39,7 @@ mod process; pub(crate) use editor::EditorLink; pub(crate) use host::HostContext; -pub(crate) use instance::{Instance, PARAM_BYPASS, PARAM_HIDDEN, PARAM_READ_ONLY, RawParam}; +pub(crate) use instance::{Instance, PARAM_BYPASS, PARAM_HIDDEN, PARAM_READ_ONLY, ProcessInput, RawParam}; #[cfg(test)] use instance::{apply_transport, split_state}; pub(crate) use module::{Bundle, exit_modules, hex}; diff --git a/crates/vst3-host/src/ffi/editor.rs b/crates/vst3-host/src/ffi/editor.rs index aba22549..89d344fa 100644 --- a/crates/vst3-host/src/ffi/editor.rs +++ b/crates/vst3-host/src/ffi/editor.rs @@ -1,5 +1,8 @@ use super::instance::Instance; -use super::*; +use super::{ + Class, ComPtr, ComWrapper, IComponentTrait, IEditControllerTrait, IPlugFrameTrait, IPlugViewTrait, Mutex, Ordering, PoisonError, STATE_MAGIC, + c_void, ok, read_c16, sb, sv, window, +}; // ---- editor (IPlugView) -------------------------------------------------------------------- diff --git a/crates/vst3-host/src/ffi/host.rs b/crates/vst3-host/src/ffi/host.rs index a2a2a216..5157e6b2 100644 --- a/crates/vst3-host/src/ffi/host.rs +++ b/crates/vst3-host/src/ffi/host.rs @@ -1,5 +1,8 @@ use super::module::tuid_bytes; -use super::*; +use super::{ + AtomicBool, Class, ComWrapper, EDIT_CAP, FUnknown, IComponentHandlerTrait, IHostApplicationTrait, Interface, Mutex, Ordering, PoisonError, + c_void, sb, sv, write_c16, +}; // ---- host-side COM objects ----------------------------------------------------------------- diff --git a/crates/vst3-host/src/ffi/instance.rs b/crates/vst3-host/src/ffi/instance.rs index 64e9d4e0..7bc23e2a 100644 --- a/crates/vst3-host/src/ffi/instance.rs +++ b/crates/vst3-host/src/ffi/instance.rs @@ -2,7 +2,11 @@ use super::editor::EditorLink; use super::host::HostContext; use super::module::Bundle; use super::process::{BusBuffers, EventList, MemoryStream, NoteEvent, ParamChanges}; -use super::*; +use super::{ + Arc, ComPtr, ComWrapper, EVENT_CAP, IAudioProcessorTrait, IComponentTrait, IConnectionPointTrait, IEditControllerTrait, IMidiMappingTrait, + IPluginBaseTrait, MAX_BUS_CHANNELS, MAX_BUSES, MAX_PARAMS, MAX_STATE_BYTES, Mutex, Ordering, PARAM_QUEUE_CAP, PITCH_BEND, STATE_MAGIC, Transport, + Vst3Error, ok, read_c16, sb, sv, +}; // ---- instances ----------------------------------------------------------------------------- @@ -32,7 +36,9 @@ pub(crate) struct Layout { } pub(super) fn apply_transport(c: &mut sv::ProcessContext, t: &Transport) { - use sv::ProcessContext_::StatesAndFlags_::*; + use sv::ProcessContext_::StatesAndFlags_::{ + kBarPositionValid, kContTimeValid, kPlaying, kProjectTimeMusicValid, kRecording, kTempoValid, kTimeSigValid, + }; let mut state = kTempoValid | kTimeSigValid | kProjectTimeMusicValid | kBarPositionValid | kContTimeValid; if t.playing { state |= kPlaying; @@ -375,18 +381,8 @@ impl Instance { /// `controls` (time, id, value) one point at their time when it falls in this call (the last /// one wins per parameter), and `notes` (sorted by time) have their times made relative to /// `offset`. - pub fn process( - &mut self, - frames: usize, - inputs: &mut BusBuffers, - outputs: &mut BusBuffers, - params: &[(u32, f64)], - controls: &[(u32, u32, f64)], - notes: &[NoteEvent], - offset: u32, - project_time: i64, - transport: Option<&Transport>, - ) -> Result<(), Vst3Error> { + pub fn process(&mut self, input: ProcessInput<'_>) -> Result<(), Vst3Error> { + let ProcessInput { frames, inputs, outputs, params, controls, notes, offset, project_time, transport } = input; if !self.active { return Err(Vst3Error::Process("not active".into())); } @@ -450,6 +446,18 @@ impl Instance { } } +pub(crate) struct ProcessInput<'a> { + pub frames: usize, + pub inputs: &'a mut BusBuffers, + pub outputs: &'a mut BusBuffers, + pub params: &'a [(u32, f64)], + pub controls: &'a [(u32, u32, f64)], + pub notes: &'a [NoteEvent], + pub offset: u32, + pub project_time: i64, + pub transport: Option<&'a Transport>, +} + impl Drop for Instance { fn drop(&mut self) { // Close any editor and cut its handles off before the controller goes away. diff --git a/crates/vst3-host/src/ffi/module.rs b/crates/vst3-host/src/ffi/module.rs index 574156b7..61e415b4 100644 --- a/crates/vst3-host/src/ffi/module.rs +++ b/crates/vst3-host/src/ffi/module.rs @@ -2,7 +2,11 @@ use super::editor::{EditorInner, EditorLink}; use super::host::HostContext; use super::instance::Instance; use super::process::{EventList, ParamChanges}; -use super::*; +use super::{ + Arc, AtomicBool, AtomicUsize, ComPtr, ComWrapper, Duration, EVENT_CAP, FUnknown, IComponentTrait, IConnectionPointTrait, IEditControllerTrait, + IPluginBaseTrait, IPluginFactory2Trait, IPluginFactoryTrait, Interface, MAX_CLASSES, ManuallyDrop, Mutex, OnceLock, Ordering, PARAM_QUEUE_CAP, + Path, PathBuf, PoisonError, TryLockError, Vst3Error, c_char, c_int, c_void, mpsc, ok, read_c8, sb, scan, sv, +}; // ---- loaded binaries ----------------------------------------------------------------------- diff --git a/crates/vst3-host/src/ffi/process.rs b/crates/vst3-host/src/ffi/process.rs index 9e978496..bd0e1edd 100644 --- a/crates/vst3-host/src/ffi/process.rs +++ b/crates/vst3-host/src/ffi/process.rs @@ -1,4 +1,7 @@ -use super::*; +use super::{ + AtomicI32, AtomicU32, AtomicU64, Class, ComWrapper, IBStreamTrait, IEventListTrait, IParamValueQueueTrait, IParameterChangesTrait, + MAX_BUS_CHANNELS, MAX_BUSES, MAX_STATE_BYTES, Mutex, Ordering, PoisonError, c_void, sb, sv, +}; /// One parameter's points for one block. SoundCraft sends at most one point per block; a /// plugin writing output changes may add more, of which only the last is kept. diff --git a/crates/vst3-host/src/plugin.rs b/crates/vst3-host/src/plugin.rs index b2264a66..726ad98b 100644 --- a/crates/vst3-host/src/plugin.rs +++ b/crates/vst3-host/src/plugin.rs @@ -1,6 +1,6 @@ //! `Vst3Plugin`: a loaded VST3 instance behind the safe `soundcraft_dsp::Plugin` trait. -use crate::ffi::{AFTERTOUCH, BusBuffers, EVENT_CAP, EditorLink, HostContext, Instance, NoteEvent, PARAM_QUEUE_CAP, PITCH_BEND}; +use crate::ffi::{AFTERTOUCH, BusBuffers, EVENT_CAP, EditorLink, HostContext, Instance, NoteEvent, PARAM_QUEUE_CAP, PITCH_BEND, ProcessInput}; use soundcraft_dsp::{Plugin, PluginEditor, PluginInfo, Transport, Unit}; use std::collections::HashMap; use std::sync::Arc; @@ -324,17 +324,17 @@ impl Plugin for Vst3Plugin { let k = self.notes.iter().position(|e| e.time() >= end).unwrap_or(self.notes.len()); let offset = u32::try_from(start).unwrap_or(u32::MAX); let notes = self.notes.get(..k).unwrap_or(&[]); - let r = self.inst.process( - n, - &mut self.inputs, - &mut self.outputs, - &self.pending, - &self.controls, + let r = self.inst.process(ProcessInput { + frames: n, + inputs: &mut self.inputs, + outputs: &mut self.outputs, + params: &self.pending, + controls: &self.controls, notes, offset, - self.position, - self.transport.as_ref(), - ); + project_time: self.position, + transport: self.transport.as_ref(), + }); self.pending.clear(); self.notes.drain(..k); self.position = self.position.saturating_add(i64::try_from(n).unwrap_or(0));