diff --git a/crates/Cargo.lock b/crates/Cargo.lock index 511650f67..0c6856579 100644 --- a/crates/Cargo.lock +++ b/crates/Cargo.lock @@ -415,6 +415,7 @@ version = "0.0.0" dependencies = [ "abi", "dsp", + "libm", "math", ] diff --git a/crates/device-mastering/src/lib.rs b/crates/device-mastering/src/lib.rs index 93d009891..4828b6a42 100644 --- a/crates/device-mastering/src/lib.rs +++ b/crates/device-mastering/src/lib.rs @@ -14,6 +14,9 @@ //! the dry signal delayed to the FIR center), gain `[11]`, width `[12]` (side level), ceiling `[13]` (limiter //! threshold); limiter `[14]` is a plain field. Compare `[15]` and mono `[16]` are local listening switches, mono //! folds the output after the meters. The latency is constant: half the FIR plus the limiter lookahead. +//! Auto gain: the output trim `[27]` (linear, 1.0 = untouched) scales the mastered path after the limiter and the +//! final amplitude, before the meters. Compare plays the original at the mastered INTEGRATED loudness (a meter pair +//! restarted on every setting change), falling back to the 3 s short-term loudness until the first gated block. #![cfg_attr(target_family = "wasm", no_std)] @@ -30,7 +33,7 @@ use dsp::low_band_analyser::{LowBandAnalyser, LOW_NUM_BINS}; use dsp::rfft::FftTables; use math::value_mapping::Linear; use hyrax::Hyrax; -use meters::{Correlation, Loudness}; +use meters::{Correlation, Loudness, TruePeak}; #[cfg(target_family = "wasm")] #[panic_handler] @@ -45,14 +48,17 @@ const CEILING_FIELD: [u16; 1] = [13]; const LIMITER_FIELD: [u16; 1] = [14]; const COMPARE_FIELD: [u16; 1] = [15]; // no box field: a local value, delivered only by `override_device_field` const MONO_FIELD: [u16; 1] = [16]; // no box field: a local value, delivered only by `override_device_field` -const METERS_FIELD: [u16; 1] = [0]; // peak left, peak right, short-term LUFS, integrated LUFS, correlation -const METER_COUNT: usize = 5; +// peak left, peak right, short-term LUFS, integrated LUFS, correlation, original short-term LUFS, original integrated +// LUFS, A/B offset dB (mastered minus original), true peak (linear, since the last broadcast), A/B basis (1 integrated) +const METERS_FIELD: [u16; 1] = [0]; +const METER_COUNT: usize = 10; const SPECTRUM_FIELD: [u16; 1] = [0xFFF]; // TS MasteringDeviceBoxAdapter.spectrum const SPECTRUM_LOW_FIELD: [u16; 1] = [0xFFE]; // TS MasteringDeviceBoxAdapter.spectrumLow const MATCH_GAIN_FIELD: [u16; 1] = [20]; const FINAL_AMPLITUDE_FIELD: [u16; 1] = [21]; const MID_CURVE_FIELD: [u16; 1] = [22]; const SIDE_CURVE_FIELD: [u16; 1] = [23]; +const OUTPUT_TRIM_FIELD: [u16; 1] = [27]; const AMOUNT_MAPPING: Linear = Linear::unipolar(); // MasteringDeviceBoxAdapter ValueMapping.unipolar() const GAIN_MAPPING: Linear = Linear {min: -12.0, max: 12.0}; @@ -128,9 +134,15 @@ pub struct MasteringState { final_amplitude: f32, compare: bool, compare_gain: f32, + compare_offset_db: f32, + compare_basis: f32, mono: bool, + output_trim: f32, output_loudness: Loudness, dry_loudness: Loudness, + ab_mastered: Loudness, + ab_original: Loudness, + true_peak: TruePeak, correlation: Correlation, peaks: [f32; 2], meters_id: u32, @@ -151,7 +163,8 @@ pub struct MasteringState { match_gain_id: u32, final_amplitude_id: u32, mid_curve_id: u32, - side_curve_id: u32 + side_curve_id: u32, + output_trim_id: u32 } pub struct MasteringDevice; @@ -177,8 +190,16 @@ impl MasteringDevice { state.convolver.begin_load(&state.mid_fir[..taps], &state.side_fir[..taps], true, false, false, 1.0); } + /// Any change of what the mastering does restarts the A/B integrated meters, so they measure the current settings. + fn restart_ab(state: &mut MasteringState) { + state.ab_mastered.clear(); + state.ab_original.clear(); + } + pub fn field_changed(state: &mut MasteringState, id: u32, value: FieldValue) { + if id != state.compare_id && id != state.mono_id {Self::restart_ab(state);} match value { + FieldValue::Float(real) if id == state.output_trim_id => state.output_trim = real, FieldValue::Bool(flag) if id == state.limiter_id => state.limiter = flag, FieldValue::Bool(flag) if id == state.compare_id => state.compare = flag, FieldValue::Bool(flag) if id == state.mono_id => state.mono = flag, @@ -201,9 +222,21 @@ impl MasteringDevice { fn meter(state: &mut MasteringState, left: &[f32], right: &[f32]) { if !state.compare { state.output_loudness.process(left, right); - let (output, dry) = (state.output_loudness.short_term_mean_square(), state.dry_loudness.short_term_mean_square()); - if output > 1e-10 && dry > 1e-10 {state.compare_gain = libm::sqrt(output / dry).clamp(1e-3, 1e3) as f32;} + state.ab_mastered.process(left, right); + let integrated = state.ab_mastered.integrated_mean_square().zip(state.ab_original.integrated_mean_square()); + let (output, dry, basis) = match integrated { + Some((output, dry)) => (output, dry, 1.0), + None => (state.output_loudness.short_term_mean_square(), state.dry_loudness.short_term_mean_square(), 0.0) + }; + if output > 1e-10 && dry > 1e-10 { + let ratio = (output / dry).clamp(1e-6, 1e6); + state.compare_gain = libm::sqrt(ratio) as f32; + state.compare_offset_db = (10.0 * libm::log10(ratio)) as f32; + state.compare_basis = basis; + } } + let metering = state.meters_ptr != 0 || abi::broadcast_active(state.meters_id); + if metering {state.true_peak.process(left, right);} for (peak, channel) in state.peaks.iter_mut().zip([left, right]) { *peak = channel.iter().fold(*peak, |peak, value| peak.max(value.abs())); } @@ -216,6 +249,11 @@ impl MasteringDevice { values[2] = state.output_loudness.short_term(); values[3] = state.output_loudness.integrated(); values[4] = state.correlation.value(); + values[5] = state.dry_loudness.short_term(); + values[6] = state.ab_original.integrated(); + values[7] = state.compare_offset_db; + values[8] = state.true_peak.take(); + values[9] = state.compare_basis; state.peaks = [0.0; 2]; } } @@ -260,9 +298,15 @@ impl AudioEffect for MasteringDevice { state.final_amplitude = 1.0; state.compare = false; state.compare_gain = 1.0; + state.compare_offset_db = 0.0; + state.compare_basis = 0.0; state.mono = false; + state.output_trim = 1.0; state.output_loudness.init(sample_rate); state.dry_loudness.init(sample_rate); + state.ab_mastered.init(sample_rate); + state.ab_original.init(sample_rate); + state.true_peak.init(); state.correlation.init(sample_rate); state.peaks = [0.0; 2]; state.meters_id = abi::bind_broadcast(&METERS_FIELD, METER_COUNT as u32); @@ -284,10 +328,12 @@ impl AudioEffect for MasteringDevice { state.final_amplitude_id = abi::observe_field(&FINAL_AMPLITUDE_FIELD); state.mid_curve_id = abi::observe_field(&MID_CURVE_FIELD); state.side_curve_id = abi::observe_field(&SIDE_CURVE_FIELD); + state.output_trim_id = abi::observe_field(&OUTPUT_TRIM_FIELD); abi::set_latency(Self::latency(state) as u32); } fn parameter_changed(state: &mut MasteringState, id: u32, value: ParamValue) { + Self::restart_ab(state); if id == state.amount_id { state.amount = float_value(value, &AMOUNT_MAPPING); } else if id == state.gain_id { @@ -305,6 +351,9 @@ impl AudioEffect for MasteringDevice { state.dry.iter_mut().for_each(|channel| channel.fill(0.0)); state.output_loudness.clear(); state.dry_loudness.clear(); + state.ab_mastered.clear(); + state.ab_original.clear(); + state.true_peak.clear(); state.correlation.clear(); state.peaks = [0.0; 2]; } @@ -362,7 +411,14 @@ impl AudioEffect for MasteringDevice { out_right[index] *= state.final_amplitude; } } + if state.output_trim != 1.0 { // the auto gain trim, 1.0 leaves the output bit-identical + for index in s0..s1 { + out_left[index] *= state.output_trim; + out_right[index] *= state.output_trim; + } + } state.dry_loudness.process(&dry_left[s0..s1], &dry_right[s0..s1]); + state.ab_original.process(&dry_left[s0..s1], &dry_right[s0..s1]); } Self::meter(state, &out_left[s0..s1], &out_right[s0..s1]); Self::spectrum(state, &out_left[s0..s1], &out_right[s0..s1]); diff --git a/crates/device-mastering/src/meters.rs b/crates/device-mastering/src/meters.rs index 38c4b54e3..2cc880d06 100644 --- a/crates/device-mastering/src/meters.rs +++ b/crates/device-mastering/src/meters.rs @@ -1,6 +1,7 @@ //! Output meters for the mastering device: peak, ITU-R BS.1770 loudness (short-term over 3 s, integrated with the //! absolute -70 LUFS and relative -10 LU gates) and stereo correlation. Integrated loudness keeps a fixed histogram //! of 400 ms block energies (the libebur128 approach), so memory never grows while playing. Built in place. +//! True peak follows BS.1770-4 Annex 2: 4x oversampling with a 16-tap Blackman windowed sinc per phase. const BLOCK_SECONDS: f64 = 0.1; const SHORT_TERM_BLOCKS: usize = 30; @@ -130,6 +131,15 @@ impl Loudness { if count == 0 {SILENCE} else {loudness_of(sum / count as f64) as f32} } + /// The gated integrated loudness as a K-weighted mean square, `None` until the first 400 ms block passed the gate. + pub fn integrated_mean_square(&self) -> Option { + let (count, sum) = self.gated(HISTOGRAM_MIN); + if count == 0 {return None;} + let relative = loudness_of(sum / count as f64) - 10.0; + let (count, sum) = self.gated(relative); + if count == 0 {None} else {Some(sum / count as f64)} + } + fn gated(&self, threshold: f64) -> (u64, f64) { let first = (((threshold - HISTOGRAM_MIN) / HISTOGRAM_STEP).max(0.0) as usize).min(HISTOGRAM_BINS); (first..HISTOGRAM_BINS).fold((0, 0.0), |(count, sum), bin| @@ -137,6 +147,81 @@ impl Loudness { } } +pub const TRUE_PEAK_TAPS: usize = 16; +pub const TRUE_PEAK_PHASES: usize = 4; + +/// The interpolation kernels for the 3 in-between phases of 4x oversampling (phase 0 is the sample itself), each a +/// Blackman windowed sinc normalised to unity DC gain. Mirrors `AutoGain.ts` in studio-matchering. +pub fn true_peak_kernels() -> [[f32; TRUE_PEAK_TAPS]; TRUE_PEAK_PHASES - 1] { + let mut kernels = [[0.0f32; TRUE_PEAK_TAPS]; TRUE_PEAK_PHASES - 1]; + let half = (TRUE_PEAK_TAPS / 2) as f64; + for (phase, kernel) in kernels.iter_mut().enumerate() { + let fraction = (phase + 1) as f64 / TRUE_PEAK_PHASES as f64; + let mut taps = [0.0f64; TRUE_PEAK_TAPS]; + let mut sum = 0.0; + for (tap, value) in taps.iter_mut().enumerate() { + let distance = tap as f64 - (half - 1.0) - fraction; + let pi_d = core::f64::consts::PI * distance; + let sinc = if distance.abs() < 1e-12 {1.0} else {libm::sin(pi_d) / pi_d}; + let x = core::f64::consts::PI * distance / half; + let window = (0.42 + 0.5 * libm::cos(x) + 0.08 * libm::cos(2.0 * x)).max(0.0); + *value = sinc * window; + sum += *value; + } + for (out, value) in kernel.iter_mut().zip(taps) {*out = (value / sum) as f32;} + } + kernels +} + +/// A streaming true-peak meter: the highest absolute value of the 4x interpolated signal since the last `take`. +pub struct TruePeak { + kernels: [[f32; TRUE_PEAK_TAPS]; TRUE_PEAK_PHASES - 1], + history: [[f32; TRUE_PEAK_TAPS * 2]; 2], + position: usize, + peak: f32 +} + +impl TruePeak { + pub fn init(&mut self) { + self.kernels = true_peak_kernels(); + self.clear(); + } + + pub fn clear(&mut self) { + self.history = [[0.0; TRUE_PEAK_TAPS * 2]; 2]; + self.position = 0; + self.peak = 0.0; + } + + pub fn process(&mut self, left: &[f32], right: &[f32]) { + for (left, right) in left.iter().zip(right) { + // a doubled ring: each sample is written twice so the last 16 are always one contiguous slice + let write = self.position; + for (channel, value) in [*left, *right].into_iter().enumerate() { + self.history[channel][write] = value; + self.history[channel][write + TRUE_PEAK_TAPS] = value; + self.peak = self.peak.max(value.abs()); + } + self.position = (self.position + 1) % TRUE_PEAK_TAPS; + let start = self.position; + for channel in 0..2 { + let window = &self.history[channel][start..start + TRUE_PEAK_TAPS]; + for kernel in &self.kernels { + let sum: f32 = kernel.iter().zip(window).map(|(k, v)| k * v).sum(); + self.peak = self.peak.max(sum.abs()); + } + } + } + } + + /// The linear true peak since the last call, then starts over. + pub fn take(&mut self) -> f32 { + let peak = self.peak; + self.peak = 0.0; + peak + } +} + pub struct Correlation { coefficient: f64, left_right: f64, diff --git a/crates/device-mastering/tests/meters.rs b/crates/device-mastering/tests/meters.rs index fd16083f5..77aa6abc2 100644 --- a/crates/device-mastering/tests/meters.rs +++ b/crates/device-mastering/tests/meters.rs @@ -77,3 +77,98 @@ fn correlation_spans_mono_to_inverted() { correlation.clear(); assert_eq!(correlation.value(), 0.0); } + +// ---- auto gain: integrated mean square, EBU 3341 reference cases and BS.1770-4 Annex 2 true peak ---- +use device_mastering::meters::TruePeak; + +fn tone(seconds: f64, frequency: f64, db: f64, phase_deg: f64) -> Vec { + let amplitude = 10f64.powf(db / 20.0); + let phase = phase_deg * PI / 180.0; + (0..(seconds * SAMPLE_RATE as f64).round() as usize) + .map(|index| (amplitude * (phase + 2.0 * PI * frequency * index as f64 / SAMPLE_RATE as f64).sin()) as f32).collect() +} + +fn lufs(mean_square: f64) -> f64 {-0.691 + 10.0 * mean_square.log10()} + +#[test] +fn integrated_mean_square_matches_ebu_3341_cases() { + // 997 Hz 0 dBFS on one channel: -3.01 LUFS (BS.1770-4 calibration) + let mut meter = loudness(); + let signal = tone(5.0, 997.0, 0.0, 0.0); + feed(&mut meter, &signal, &vec![0.0; signal.len()]); + let value = lufs(meter.integrated_mean_square().unwrap()); + println!("997 Hz 0 dBFS left only: {value:.3} LUFS"); + assert!((value - -3.01).abs() < 0.1, "{value}"); + // case 1 and 2: 1 kHz stereo -23 / -33 dBFS, 20 s + for db in [-23.0, -33.0] { + let mut meter = loudness(); + let signal = tone(20.0, 1000.0, db, 0.0); + feed(&mut meter, &signal, &signal); + let value = lufs(meter.integrated_mean_square().unwrap()); + println!("1 kHz {db} dBFS stereo: {value:.3} LUFS"); + assert!((value - db).abs() < 0.1, "{value}"); + } + // case 5: -36 / -23 / -36 (10 / 60 / 10 s) reads -23 + let mut meter = loudness(); + let mut signal = tone(10.0, 1000.0, -36.0, 0.0); + signal.extend(tone(60.0, 1000.0, -23.0, 0.0)); + signal.extend(tone(10.0, 1000.0, -36.0, 0.0)); + feed(&mut meter, &signal, &signal); + let value = lufs(meter.integrated_mean_square().unwrap()); + println!("EBU 3341 case 5: {value:.3} LUFS"); + assert!((value - -23.0).abs() < 0.1, "{value}"); + assert!((meter.integrated() as f64 - value).abs() < 0.11, "histogram {} vs {value}", meter.integrated()); +} + +#[test] +fn integrated_mean_square_is_none_before_the_first_gated_block() { + let mut meter = loudness(); + assert!(meter.integrated_mean_square().is_none()); + let signal = tone(0.3, 1000.0, -20.0, 0.0); + feed(&mut meter, &signal, &signal); + assert!(meter.integrated_mean_square().is_none()); + let silence = vec![0.0f32; 48000]; + feed(&mut meter, &silence, &silence); + feed(&mut meter, &silence, &silence); + let mut quiet = loudness(); + feed(&mut quiet, &silence, &silence); + assert!(quiet.integrated_mean_square().is_none(), "silence never passes the -70 LUFS gate"); +} + +#[test] +fn true_peak_finds_the_peak_between_samples() { + // EBU 3341 cases 15 to 19 style: -6.02 dBFS sines, the reading must be within +0.2 / -0.4 dB + for (frequency, phase) in [(12000.0, 0.0), (12000.0, 45.0), (8000.0, 60.0), (997.0, 0.0)] { + let mut meter: Box = unsafe {Box::new_zeroed().assume_init()}; + meter.init(); + let signal = tone(1.0, frequency, -6.02, phase); + // the first 0.1 s primes the 16-tap history (a start from silence rings like any real onset), then is dropped + for chunk in signal[..4800].chunks(128) {meter.process(chunk, chunk);} + meter.take(); + for chunk in signal[4800..].chunks(128) {meter.process(chunk, chunk);} + let value = 20.0 * (meter.take() as f64).log10(); + println!("true peak {frequency} Hz {phase} deg: {value:.3} dBTP"); + assert!(value - -6.02 < 0.2 && -6.02 - value < 0.4, "{value}"); + } + // the 12 kHz 45 deg case hides its peak: the sample peak is 3 dB lower + let signal = tone(1.0, 12000.0, -6.02, 45.0); + let sample_peak = signal.iter().fold(0.0f32, |peak, value| peak.max(value.abs())); + println!("sample peak of 12 kHz 45 deg: {:.3} dBFS", 20.0 * (sample_peak as f64).log10()); + assert!(20.0 * (sample_peak as f64).log10() < -8.9); +} + +#[test] +fn true_peak_take_starts_over_and_kernels_have_unity_dc() { + for kernel in device_mastering::meters::true_peak_kernels() { + let sum: f32 = kernel.iter().sum(); + assert!((sum - 1.0).abs() < 1e-6, "{sum}"); + } + let mut meter: Box = unsafe {Box::new_zeroed().assume_init()}; + meter.init(); + let ones = vec![0.5f32; 256]; + meter.process(&ones, &ones); + meter.take(); // the step from silence overshoots (Gibbs), a steady DC level then reads exactly + meter.process(&ones, &ones); + assert!((meter.take() - 0.5).abs() < 1e-5); + assert_eq!(meter.take(), 0.0); +} diff --git a/crates/dsp/src/biquad.rs b/crates/dsp/src/biquad.rs index f6ed2a5be..8f18a40ba 100644 --- a/crates/dsp/src/biquad.rs +++ b/crates/dsp/src/biquad.rs @@ -85,14 +85,20 @@ impl BiquadCoeff { } pub fn set_low_shelf_params(&mut self, frequency: f64, db_gain: f64) { + self.set_low_shelf_params_slope(frequency, db_gain, 1.0) + } + + /// ESC: the low shelf with an adjustable RBJ shelf slope `S` (1 = the classic steepest shelf without + /// overshoot, the old fixed value; below 1 = a gentler, wider transition; above 1 = steeper with a small + /// resonant bump on each side). `S` = 1 takes the old constant path bit for bit. + pub fn set_low_shelf_params_slope(&mut self, frequency: f64, db_gain: f64, slope: f64) { let frequency = clamp(frequency, 0.0, 1.0); let a = libm::pow(10.0, db_gain / 40.0); if frequency == 1.0 { self.set_normalized_coefficients(a * a, 0.0, 0.0, 1.0, 0.0, 0.0); } else if frequency > 0.0 { let w0 = TAU * frequency; - // shelf slope S = 1, so the TS `sqrt((a + 1/a)*(1/S - 1) + 2)` collapses to a constant sqrt(2). - let alpha = 0.5 * libm::sin(w0) * core::f64::consts::SQRT_2; + let alpha = 0.5 * libm::sin(w0) * shelf_alpha_factor(a, slope); let k = libm::cos(w0); let k2 = 2.0 * libm::sqrt(a) * alpha; let a_plus_one = a + 1.0; @@ -110,14 +116,20 @@ impl BiquadCoeff { } pub fn set_high_shelf_params(&mut self, frequency: f64, db_gain: f64) { + self.set_high_shelf_params_slope(frequency, db_gain, 1.0) + } + + /// ESC: the high shelf with an adjustable RBJ shelf slope `S` (1 = the classic steepest shelf without + /// overshoot, the old fixed value; below 1 = a gentler, wider transition; above 1 = steeper with a small + /// resonant bump on each side). `S` = 1 takes the old constant path bit for bit. + pub fn set_high_shelf_params_slope(&mut self, frequency: f64, db_gain: f64, slope: f64) { let frequency = clamp(frequency, 0.0, 1.0); let a = libm::pow(10.0, db_gain / 40.0); if frequency == 1.0 { self.set_normalized_coefficients(1.0, 0.0, 0.0, 1.0, 0.0, 0.0); } else if frequency > 0.0 { let w0 = TAU * frequency; - // shelf slope S = 1, so the TS `sqrt((a + 1/a)*(1/S - 1) + 2)` collapses to a constant sqrt(2). - let alpha = 0.5 * libm::sin(w0) * core::f64::consts::SQRT_2; + let alpha = 0.5 * libm::sin(w0) * shelf_alpha_factor(a, slope); let k = libm::cos(w0); let k2 = 2.0 * libm::sqrt(a) * alpha; let a_plus_one = a + 1.0; @@ -612,3 +624,13 @@ mod tests { (plain.a1, plain.a2, plain.b0, plain.b1, plain.b2)); } } + +/// ESC: the RBJ shelf term `sqrt((a + 1/a) * (1/S - 1) + 2)`. At `S` = 1 it is exactly sqrt(2) (the constant +/// the pre-slope code used, returned directly so the old coefficients stay bit-identical). The radicand is kept +/// positive so an out-of-range slope can never produce NaN. +pub fn shelf_alpha_factor(a: f64, slope: f64) -> f64 { + if slope == 1.0 || !(slope > 0.0) { + return core::f64::consts::SQRT_2; + } + libm::sqrt(((a + 1.0 / a) * (1.0 / slope - 1.0) + 2.0).max(1.0e-6)) +} diff --git a/crates/dsp/src/freeverb.rs b/crates/dsp/src/freeverb.rs index 51e6d032d..24b915ab1 100644 --- a/crates/dsp/src/freeverb.rs +++ b/crates/dsp/src/freeverb.rs @@ -19,6 +19,10 @@ const RIGHT_COMB_TAPS: [usize; 8] = [1640, 1580, 1514, 1445, 1379, 1300, 1211, 1 const MAX_DELAY_SIZE: usize = 32768; // next_pow_of_2(ceil(0.5 * 48000)); the pre-delay ring (per channel) const MAGIC_GAIN: f32 = 0.01; +/// ESC: the comb feedback that `extend` = 1 reaches (the old ceiling is 0.98 at room 100%). Below 1, so the tail +/// still decays, just very slowly; `freeze` is the only setting with feedback exactly 1. +pub const EXTEND_FEEDBACK: f32 = 0.998; +const FREEZE_FADE_SECONDS: f32 = 0.01; /// The Freeverb DSP. Built with `new`; a device holds it in its state (large: ~700 KB of fixed buffers) and /// drives it per block. `room_size` / `damp` / `wet_gain` / `dry_gain` / `predelay_in_samples` are set directly. @@ -28,6 +32,12 @@ pub struct FreeVerb { pub predelay_in_samples: usize, pub wet_gain: f32, pub dry_gain: f32, + /// ESC "Long" (0..1): pushes the comb feedback from the Room-Size value toward `EXTEND_FEEDBACK`. 0 = old. + pub extend: f32, + /// ESC Freeze: feedback 1, no damping, the input fades out of the tank (10 ms) so the tail holds forever. + pub freeze: bool, + input_gain: f32, // 1 normally, 0 while frozen (faded) + input_step: f32, // the per-sample fade step comb_left: [[f32; 2048]; 8], comb_right: [[f32; 2048]; 8], lp_left: [f32; 8], @@ -64,6 +74,10 @@ impl FreeVerb { self.dry_gain = 1.0 - 0.3333; self.index = 0; self.delay_position = 0; + self.extend = 0.0; + self.freeze = false; + self.input_gain = 1.0; + self.input_step = 1.0 / (FREEZE_FADE_SECONDS * sample_rate).max(1.0); } /// Clear the sounding state on a transport STOP (TS `FreeVerb.clear`): all rings + feedback histories + @@ -99,6 +113,10 @@ impl FreeVerb { predelay_in_samples: (0.008 * sample_rate) as usize, wet_gain: 0.3333, dry_gain: 1.0 - 0.3333, + extend: 0.0, + freeze: false, + input_gain: 1.0, + input_step: 1.0 / (FREEZE_FADE_SECONDS * sample_rate).max(1.0), comb_left: [[0.0; 2048]; 8], comb_right: [[0.0; 2048]; 8], lp_left: [0.0; 8], @@ -123,9 +141,22 @@ impl FreeVerb { } pub fn process(&mut self, in_left: &[f32], in_right: &[f32], out_left: &mut [f32], out_right: &mut [f32], from: usize, to: usize) { - let p0 = 0.4 * self.damp; - let p1 = 1.0 - p0; - let p2 = 0.7 + 0.28 * self.room_size; + let (p0, p1, p2) = if self.freeze { + (0.0, 1.0, 1.0) // lossless tank: no damping, unity feedback + } else { + let p0 = 0.4 * self.damp; + let base = 0.7 + 0.28 * self.room_size; + // Long moves the feedback's distance from 1 geometrically (equal knob steps = equal decay-time + // ratios). Long 0 takes the old value untouched, bit for bit. + let p2 = if self.extend > 0.0 && base < EXTEND_FEEDBACK { + let ratio = (1.0 - EXTEND_FEEDBACK) / (1.0 - base); + 1.0 - (1.0 - base) * libm::powf(ratio, self.extend.min(1.0)) + } else { + base + }; + (p0, 1.0 - p0, p2) + }; + let input_target = if self.freeze {0.0} else {1.0}; let predelay = self.predelay_in_samples.min(self.delay_size); for i in from..to { let inp_left = in_left[i]; @@ -133,8 +164,19 @@ impl FreeVerb { // stereo pre-delay // `delay_size` is a power of two: mask instead of a per-sample integer division. let delay_read = (self.delay_position + self.delay_size - predelay) & (self.delay_size - 1); - let read_left = self.delay_buffer[delay_read << 1] * MAGIC_GAIN; - let read_right = self.delay_buffer[(delay_read << 1) + 1] * MAGIC_GAIN; + let mut read_left = self.delay_buffer[delay_read << 1] * MAGIC_GAIN; + let mut read_right = self.delay_buffer[(delay_read << 1) + 1] * MAGIC_GAIN; + if self.input_gain != input_target { + self.input_gain = if input_target > self.input_gain { + (self.input_gain + self.input_step).min(1.0) + } else { + (self.input_gain - self.input_step).max(0.0) + }; + } + if self.input_gain != 1.0 { // untouched (bit-exact) unless Freeze is or was just engaged + read_left *= self.input_gain; + read_right *= self.input_gain; + } self.delay_buffer[self.delay_position << 1] = inp_left; self.delay_buffer[(self.delay_position << 1) + 1] = inp_right; self.delay_position = (self.delay_position + 1) & (self.delay_size - 1); @@ -241,3 +283,148 @@ mod tests { } } } + +#[cfg(test)] +mod golden_tests { + //! ESC: the pre-upgrade sound must survive bit for bit. The hash below was produced by THIS test body run + //! on the untouched readybase `FreeVerb` (no extend / freeze fields existed then). + use super::FreeVerb; + + pub fn render_hash() -> (u64, f64) { + let mut verb = FreeVerb::new(48_000.0); + verb.room_size = 0.73; + verb.damp = 0.41; + verb.wet_gain = 0.7; + verb.dry_gain = 0.9; + verb.predelay_in_samples = 333; + let mut seed = 0x2468_ace1u32; + let mut hash = 0xcbf2_9ce4_8422_2325u64; + let mut energy = 0.0f64; + for block in 0..400 { + let (mut in_l, mut in_r) = ([0.0f32; 128], [0.0f32; 128]); + if block < 100 { + for i in 0..128 { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + in_l[i] = ((seed >> 8) as f32 / 8_388_608.0 - 1.0) * 0.5; + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + in_r[i] = ((seed >> 8) as f32 / 8_388_608.0 - 1.0) * 0.5; + } + } + let (mut out_l, mut out_r) = ([0.0f32; 128], [0.0f32; 128]); + verb.process(&in_l, &in_r, &mut out_l, &mut out_r, 0, 128); + for i in 0..128 { + for v in [out_l[i], out_r[i]] { + hash = (hash ^ v.to_bits() as u64).wrapping_mul(0x0100_0000_01b3); + energy += (v as f64) * (v as f64); + } + } + } + (hash, energy) + } + + #[test] + fn golden_render() { + let (hash, energy) = render_hash(); + println!("GOLDEN {hash:#018x} energy {energy}"); + { let expected = "0xf7da9e6ae22753a7"; // readybase output + assert_eq!(format!("{hash:#018x}"), expected); + } + } +} + +#[cfg(test)] +mod esc_upgrade_tests { + use super::FreeVerb; + + fn verb() -> FreeVerb { + let mut verb = FreeVerb::new(48_000.0); + verb.room_size = 1.0; + verb.damp = 0.3; + verb.wet_gain = 1.0; + verb.dry_gain = 0.0; + verb.predelay_in_samples = 100; + verb + } + + // Feed a 0.1 s noise burst, then silence; return the wet energy per 0.1 s window over `seconds`. + fn windows(verb: &mut FreeVerb, seconds: usize, freeze_at: Option) -> Vec { + let mut seed = 99u32; + let per_window = 4800 / 128 + 1; // ~0.1 s in 128-blocks + let mut out = Vec::new(); + let mut energy = 0.0f64; + for block in 0..(seconds * 10 * per_window) { + if Some(block) == freeze_at {verb.freeze = true;} + let (mut in_l, mut in_r) = ([0.0f32; 128], [0.0f32; 128]); + if block < per_window { + for i in 0..128 { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + in_l[i] = (seed >> 8) as f32 / 8_388_608.0 - 1.0; + in_r[i] = -in_l[i] * 0.5; + } + } else if freeze_at.is_some() { + // keep feeding loud input: a frozen tank must NOT take it in + for i in 0..128 { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + in_l[i] = (seed >> 8) as f32 / 8_388_608.0 - 1.0; + in_r[i] = in_l[i]; + } + } + let (mut out_l, mut out_r) = ([0.0f32; 128], [0.0f32; 128]); + verb.process(&in_l, &in_r, &mut out_l, &mut out_r, 0, 128); + assert!(out_l.iter().chain(out_r.iter()).all(|v| v.is_finite())); + energy += out_l.iter().chain(out_r.iter()).map(|v| (*v as f64).powi(2)).sum::(); + if (block + 1) % per_window == 0 { + out.push(energy); + energy = 0.0; + } + } + out + } + + // Seconds for the tail to fall 30 dB from the 0.3 s window, x2 (a T60 estimate from T30). + fn t60(w: &[f64]) -> f64 { + let reference = w[3]; + for (index, e) in w.iter().enumerate().skip(3) { + if 10.0 * (e / reference).log10() < -30.0 { + return (index - 3) as f64 * 0.1 * 2.0; + } + } + f64::INFINITY + } + + #[test] + fn long_extends_the_decay_far_past_the_old_ceiling() { + let mut old = verb(); + let old_t60 = t60(&windows(&mut old, 12, None)); + let mut half = verb(); + half.extend = 0.5; + let half_t60 = t60(&windows(&mut half, 30, None)); + let mut full = verb(); + full.extend = 1.0; + let w = windows(&mut full, 30, None); + let full_drop = 10.0 * (w[w.len() - 1] / w[3]).log10(); + println!("T60 room 100%: Long 0 = {old_t60:.1} s, Long 50% = {half_t60:.1} s, Long 100%: {full_drop:.1} dB after 30 s"); + assert!(half_t60 > old_t60 * 1.8, "Long 50% nearly doubles the decay or more"); + assert!(full_drop < -3.0, "Long 100% still decays (not a freeze)"); + assert!(full_drop > -30.0, "but very slowly"); + } + + #[test] + fn freeze_holds_the_tail_and_ignores_new_input() { + let mut v = verb(); + let freeze_at = 4800 / 128 + 1; // right after the burst + let w = windows(&mut v, 20, Some(freeze_at)); + let hold = 10.0 * (w[w.len() - 1] / w[5]).log10(); + println!("freeze: level change 0.5 s -> 20 s = {hold:+.2} dB (new loud input kept feeding the whole time)"); + assert!(hold.abs() < 1.0, "the frozen tail neither dies nor grows ({hold} dB)"); + } + + #[test] + fn unfreeze_returns_to_a_normal_decay() { + let mut v = verb(); + windows(&mut v, 1, Some(10)); + v.freeze = false; + let w = windows(&mut v, 12, None); + assert!(w[w.len() - 1] < w[3] * 1e-2, "the tail decays again after Freeze is released"); + } +} diff --git a/crates/dsp/src/lib.rs b/crates/dsp/src/lib.rs index 2fdcf9840..21fa004b3 100644 --- a/crates/dsp/src/lib.rs +++ b/crates/dsp/src/lib.rs @@ -28,6 +28,7 @@ pub mod resampler; pub mod rfft; pub mod simple_limiter; pub mod smooth; +pub mod stereo_scope; pub mod tidal; pub mod vocoder; pub mod waveshaper; diff --git a/crates/dsp/src/panning.rs b/crates/dsp/src/panning.rs index 9a6fdd810..7835e324b 100644 --- a/crates/dsp/src/panning.rs +++ b/crates/dsp/src/panning.rs @@ -45,40 +45,36 @@ pub struct StereoParams { pub swap: bool } -/// Compute the mixing matrix for `params` under `mixing`, a port of `StereoMatrix.update`. Panning gains scale -/// each channel; `stereo < 0` folds toward mono (mid), `> 0` widens (side); inverts negate a channel; swap -/// exchanges the output channels. +/// Compute the mixing matrix for `params` under `mixing` (ESC rewrite of the lib-dsp `StereoMatrix.update` port, +/// one fixed signal order instead of two): 1. channel ops on the INPUT (invert left / right, then swap), +/// 2. WIDTH as mid/side (mid always 1, side gain `1 + stereo`: -1 = mono, 0 = unchanged, +1 = side doubled), +/// 3. PANNING as an output balance under `mixing`, 4. gain. At the defaults (gain 1, centre, width 0, no ops) +/// the matrix is exactly the identity. +/// +/// Fixed vs the port (ESC, Nov 2026): (a) widening also turned the MID down (to silence at +100%), so a wide +/// setting hollowed out the centre and +100% left the two outputs as exact polarity opposites (mono sum 0); +/// (b) below 0 width the pan and invert were applied to the INPUT channels (pan picked a source channel and +/// was then folded back to the centre, invert cancelled the mono fold), above 0 to the OUTPUTS. Now the mono +/// sum never depends on the width, and pan / invert mean the same thing at every width. pub fn update_matrix(matrix: &mut Matrix, params: &StereoParams, mixing: Mixing) { let [pan_left, pan_right] = panning_to_gains(params.panning, mixing); - let mut left_gain = pan_left * params.gain; - let mut right_gain = pan_right * params.gain; - if params.invert_l {left_gain *= -1.0;} - if params.invert_r {right_gain *= -1.0;} - let mono = (-params.stereo).max(0.0); - let expand = params.stereo.max(0.0); - let mid_gain = 1.0 - expand; - let side_gain = 1.0 + expand; - let mono_amount = mono * 0.5; - let stereo_width = 1.0 - mono; - let m00 = (mid_gain + side_gain) * 0.5; - let m01 = (mid_gain - side_gain) * 0.5; - let m10 = (mid_gain - side_gain) * 0.5; - let m11 = (mid_gain + side_gain) * 0.5; - let ll = (left_gain * (mono_amount + stereo_width)) * m00 + (right_gain * mono_amount) * m01; - let rl = (left_gain * (mono_amount + stereo_width)) * m10 + (right_gain * mono_amount) * m11; - let lr = (left_gain * mono_amount) * m00 + (right_gain * (mono_amount + stereo_width)) * m01; - let rr = (left_gain * mono_amount) * m10 + (right_gain * (mono_amount + stereo_width)) * m11; - if params.swap { - matrix.ll = rl; - matrix.rl = ll; - matrix.lr = rr; - matrix.rr = lr; - } else { - matrix.ll = ll; - matrix.lr = lr; - matrix.rl = rl; - matrix.rr = rr; - } + let left_gain = pan_left * params.gain; + let right_gain = pan_right * params.gain; + // Input channel ops: polarity first, then the swap (column ops on the input vector). + let sign_l = if params.invert_l {-1.0} else {1.0}; + let sign_r = if params.invert_r {-1.0} else {1.0}; + // Mid/side width: out_l = d * a + c * b, out_r = c * a + d * b with d = (1 + side) / 2, c = (1 - side) / 2. + let side = 1.0 + clamp(params.stereo, -1.0, 1.0); + let direct = (1.0 + side) * 0.5; + let cross = (1.0 - side) * 0.5; + // a, b = the input after the ops: invert gives a = sign_l * L, b = sign_r * R; swap then exchanges a and b + // (a = sign_r * R, b = sign_l * L), so a channel keeps its own polarity flag wherever it goes. + // out_l = left_gain * (direct * a + cross * b), out_r = right_gain * (cross * a + direct * b). + let (via_l, via_r) = if params.swap {(cross, direct)} else {(direct, cross)}; // in L -> out L / in R -> out L + matrix.ll = left_gain * via_l * sign_l; // in L -> out L + matrix.rl = left_gain * via_r * sign_r; // in R -> out L + matrix.lr = right_gain * via_r * sign_l; // in L -> out R + matrix.rr = right_gain * via_l * sign_r; // in R -> out R } #[cfg(test)] @@ -144,4 +140,96 @@ mod tests { let [hard_left, hard_right] = panning_to_gains(-1.0, Mixing::EqualPower); assert!((hard_left - 1.0).abs() < 1.0e-5 && hard_right.abs() < 1.0e-5, "hard left is all left"); } + + fn apply(matrix: &Matrix, l: f32, r: f32) -> (f32, f32) { + (matrix.ll * l + matrix.rl * r, matrix.lr * l + matrix.rr * r) + } + + fn full(gain: f32, panning: f32, stereo: f32, invert_l: bool, invert_r: bool, swap: bool) -> StereoParams { + StereoParams {gain, panning, stereo, invert_l, invert_r, swap} + } + + #[test] + fn defaults_are_exactly_the_identity() { + let mut matrix = Matrix::default(); + update_matrix(&mut matrix, ¶ms(1.0, 0.0, 0.0), Mixing::Linear); + assert_eq!((matrix.ll, matrix.rl, matrix.lr, matrix.rr), (1.0, 0.0, 0.0, 1.0)); + } + + #[test] + fn the_mono_sum_never_depends_on_the_width() { + // Mono compatibility: at centre pan, L+R of the output equals L+R of the input at every width. + for step in -10..=10 { + let mut matrix = Matrix::default(); + update_matrix(&mut matrix, ¶ms(1.0, 0.0, step as f32 / 10.0), Mixing::Linear); + for &(l, r) in &[(1.0f32, 0.0f32), (0.0, 1.0), (0.7, -0.3), (0.5, 0.5)] { + let (out_l, out_r) = apply(&matrix, l, r); + assert!(((out_l + out_r) - (l + r)).abs() < 1e-6, "width {step}: mono sum changed"); + } + } + } + + #[test] + fn full_width_keeps_the_centre_and_never_inverts_a_mono_source() { + // The port's +100% output L-R / R-L: a centred (mono) source cancelled to silence. Now it passes as is. + let mut matrix = Matrix::default(); + update_matrix(&mut matrix, ¶ms(1.0, 0.0, 1.0), Mixing::Linear); + let (out_l, out_r) = apply(&matrix, 0.5, 0.5); + assert!((out_l - 0.5).abs() < 1e-6 && (out_r - 0.5).abs() < 1e-6, "mono stays mono at full width"); + // The side doubles: L-only input -> side 0.5 -> 1.0, mid 0.5 kept. + let (out_l, out_r) = apply(&matrix, 1.0, 0.0); + assert!((out_l - 1.5).abs() < 1e-6 && (out_r + 0.5).abs() < 1e-6, "side x2: {out_l} {out_r}"); + // Half width: side x1.5. + update_matrix(&mut matrix, ¶ms(1.0, 0.0, 0.5), Mixing::Linear); + let (out_l, out_r) = apply(&matrix, 1.0, 0.0); + assert!(((out_l - out_r) * 0.5 - 0.75).abs() < 1e-6); + } + + #[test] + fn pan_after_mono_really_pans() { + // The port picked a SOURCE channel below 0 width and folded it back to the centre (hard right + mono gave + // 0.5 * R on both sides). Now the mono fold is panned like any signal. + let mut matrix = Matrix::default(); + update_matrix(&mut matrix, ¶ms(1.0, 1.0, -1.0), Mixing::Linear); + let (out_l, out_r) = apply(&matrix, 0.8, 0.2); + assert!(out_l.abs() < 1e-6, "hard right: the left output is silent, got {out_l}"); + assert!((out_r - 0.5).abs() < 1e-6, "the mono fold (L+R)/2 sits on the right, got {out_r}"); + } + + #[test] + fn invert_means_the_same_at_every_width() { + // Invert L flips the INPUT left channel. At width 0 that is the left output; at full mono a mono source + // with one side flipped folds to the side signal (L - R) / 2 on both, never silence-by-accident on one side. + let mut matrix = Matrix::default(); + update_matrix(&mut matrix, &full(1.0, 0.0, 0.0, true, false, false), Mixing::Linear); + assert_eq!(apply(&matrix, 0.4, 0.3), (-0.4, 0.3)); + for step in -10..=10 { + let width = step as f32 / 10.0; + let mut plain = Matrix::default(); + update_matrix(&mut plain, ¶ms(1.0, 0.0, width), Mixing::Linear); + update_matrix(&mut matrix, &full(1.0, 0.0, width, true, false, false), Mixing::Linear); + let (l, r) = (0.4f32, 0.3f32); + let expected = apply(&plain, -l, r); + let got = apply(&matrix, l, r); + assert!((got.0 - expected.0).abs() < 1e-6 && (got.1 - expected.1).abs() < 1e-6, "invert = input flip at width {width}"); + } + } + + #[test] + fn swap_keeps_each_channels_polarity_flag() { + let mut matrix = Matrix::default(); + update_matrix(&mut matrix, &full(1.0, 0.0, 0.0, true, false, true), Mixing::Linear); + // invert L then swap: out L = R, out R = -L. + assert_eq!(apply(&matrix, 0.4, 0.3), (0.3, -0.4)); + } + + #[test] + fn pan_law_numbers() { + let [l, r] = panning_to_gains(0.5, Mixing::Linear); + assert_eq!([l, r], [0.5, 1.0], "linear balance: +50% = far side -6.02 dB, near side 0 dB"); + let [l, r] = panning_to_gains(0.0, Mixing::EqualPower); + assert!((20.0 * libm::log10f(l) + 3.0103).abs() < 1e-3 && (l - r).abs() < 1e-7, "equal power centre -3.01 dB"); + let [l, r] = panning_to_gains(0.5, Mixing::EqualPower); + assert!((l * l + r * r - 1.0).abs() < 1e-6); + } } diff --git a/crates/dsp/src/stereo_scope.rs b/crates/dsp/src/stereo_scope.rs new file mode 100644 index 000000000..039aa00f3 --- /dev/null +++ b/crates/dsp/src/stereo_scope.rs @@ -0,0 +1,164 @@ +//! A live stereo-image analyser for device editors: a phase-correlation meter (Pearson correlation of L and R +//! over an exponential ~300 ms window, +1 mono, 0 wide / decorrelated, -1 out of phase), the side-to-mid RMS +//! ratio (0 mono, 1 equal mid and side, larger = wider), the L / R RMS, and a decimated ring of mid / side +//! points for a goniometer (vectorscope). No allocation; a zeroed instance is valid after `init`. +//! +//! Broadcast layout (`SLOT_LEN` f32s): `[0]` correlation, `[1]` side/mid ratio, `[2]` RMS L, `[3]` RMS R, +//! `[4..]` `POINTS` interleaved `(side, mid)` pairs (the goniometer x / y, linear amplitude). + +pub const POINTS: usize = 256; +pub const HEADER: usize = 4; +pub const SLOT_LEN: usize = HEADER + POINTS * 2; +const WINDOW_SECONDS: f32 = 0.3; +const DECIMATE: u32 = 2; + +#[derive(Clone, Copy)] +pub struct StereoScope { + decay: f32, + sum_lr: f32, + sum_ll: f32, + sum_rr: f32, + sum_mm: f32, + sum_ss: f32, + weight: f32, + points: [f32; POINTS * 2], + write: usize, + phase: u32 +} + +impl StereoScope { + pub const fn new() -> Self { + Self {decay: 0.0, sum_lr: 0.0, sum_ll: 0.0, sum_rr: 0.0, sum_mm: 0.0, sum_ss: 0.0, weight: 0.0, + points: [0.0; POINTS * 2], write: 0, phase: 0} + } + + pub fn init(&mut self, sample_rate: f32) { + *self = Self::new(); + self.decay = libm::expf(-1.0 / (sample_rate * WINDOW_SECONDS)); + } + + pub fn clear(&mut self) { + let decay = self.decay; + *self = Self::new(); + self.decay = decay; + } + + pub fn process(&mut self, left: &[f32], right: &[f32]) { + let decay = self.decay; + for (&l, &r) in left.iter().zip(right.iter()) { + let (l, r) = (if l.is_finite() {l} else {0.0}, if r.is_finite() {r} else {0.0}); + let mid = (l + r) * 0.5; + let side = (l - r) * 0.5; + self.sum_lr = self.sum_lr * decay + l * r; + self.sum_ll = self.sum_ll * decay + l * l; + self.sum_rr = self.sum_rr * decay + r * r; + self.sum_mm = self.sum_mm * decay + mid * mid; + self.sum_ss = self.sum_ss * decay + side * side; + self.weight = self.weight * decay + 1.0; + self.phase += 1; + if self.phase >= DECIMATE { + self.phase = 0; + self.points[self.write * 2] = side; + self.points[self.write * 2 + 1] = mid; + self.write = (self.write + 1) % POINTS; + } + } + } + + /// Phase correlation in `-1..1`; 0 while silent (no meaningful phase). + pub fn correlation(&self) -> f32 { + let energy = self.sum_ll * self.sum_rr; + if energy <= 1.0e-20 { + 0.0 + } else { + (self.sum_lr / libm::sqrtf(energy)).clamp(-1.0, 1.0) + } + } + + /// Side RMS over mid RMS: 0 mono, 1 at equal mid / side energy (e.g. one channel only), large when mostly side. + pub fn side_to_mid(&self) -> f32 { + if self.sum_ss <= 1.0e-20 { + 0.0 + } else if self.sum_mm <= 1.0e-20 { + f32::MAX + } else { + libm::sqrtf(self.sum_ss / self.sum_mm) + } + } + + pub fn rms(&self) -> [f32; 2] { + if self.weight <= 0.0 { + [0.0, 0.0] + } else { + [libm::sqrtf(self.sum_ll / self.weight), libm::sqrtf(self.sum_rr / self.weight)] + } + } + + /// Write the broadcast layout into `slot` (at least `SLOT_LEN` floats). + pub fn write_slot(&self, slot: &mut [f32]) { + let [rms_l, rms_r] = self.rms(); + slot[0] = self.correlation(); + slot[1] = self.side_to_mid().min(1.0e6); + slot[2] = rms_l; + slot[3] = rms_r; + slot[HEADER..SLOT_LEN].copy_from_slice(&self.points); + } +} + +impl Default for StereoScope { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::{StereoScope, SLOT_LEN}; + + fn scope_of(left: &[f32], right: &[f32]) -> StereoScope { + let mut scope = StereoScope::new(); + scope.init(48_000.0); + scope.process(left, right); + scope + } + + fn sine(n: usize) -> Vec { + (0..n).map(|i| (i as f32 * 0.05).sin() * 0.5).collect() + } + + #[test] + fn mono_reads_plus_one_and_no_side() { + let x = sine(48_000); + let scope = scope_of(&x, &x); + assert!((scope.correlation() - 1.0).abs() < 1e-5); + assert_eq!(scope.side_to_mid(), 0.0); + } + + #[test] + fn inverted_reads_minus_one() { + let x = sine(48_000); + let y: Vec = x.iter().map(|v| -v).collect(); + let scope = scope_of(&x, &y); + assert!((scope.correlation() + 1.0).abs() < 1e-5); + } + + #[test] + fn one_channel_only_reads_zero_correlation_and_unit_ratio() { + let x = sine(48_000); + let z = vec![0.0f32; x.len()]; + let scope = scope_of(&x, &z); + assert!(scope.correlation().abs() < 1e-6); + assert!((scope.side_to_mid() - 1.0).abs() < 1e-4); + let mut slot = vec![0.0f32; SLOT_LEN]; + scope.write_slot(&mut slot); + assert!(slot.iter().all(|v| v.is_finite())); + } + + #[test] + fn silence_is_neutral() { + let z = vec![0.0f32; 1024]; + let scope = scope_of(&z, &z); + assert_eq!(scope.correlation(), 0.0); + assert_eq!(scope.side_to_mid(), 0.0); + } +} diff --git a/crates/engine-env/src/audio_bus_processor.rs b/crates/engine-env/src/audio_bus_processor.rs index 7d862bca9..4ba93c1ad 100644 --- a/crates/engine-env/src/audio_bus_processor.rs +++ b/crates/engine-env/src/audio_bus_processor.rs @@ -16,6 +16,7 @@ use crate::processor::Processor; use crate::meter::Meter; use crate::telemetry::BroadcastSlot; use crate::RENDER_QUANTUM; +use crate::output_gain::{OutputGainSource, OutputGainStage}; struct BusSource { buffer: SharedAudioBuffer, @@ -29,12 +30,15 @@ pub struct AudioBusProcessor { delayed: AudioBuffer, enabled: bool, events: EventBuffer, - meter: Option + meter: Option, + // ESC: a COMPOSITE instrument's output gain (its box field 6) over the summed children; `None` for a + // plain bus / the output unit (untouched) + output_gain: Option } impl AudioBusProcessor { pub fn new(output: SharedAudioBuffer) -> Self { - Self {output, sources: Vec::new(), delayed: AudioBuffer::new(), enabled: true, events: EventBuffer::new(), meter: None} + Self {output, sources: Vec::new(), delayed: AudioBuffer::new(), enabled: true, events: EventBuffer::new(), meter: None, output_gain: None} } /// Enable metering of this bus's RAW SUM (pre-fx, pre-strip) and return its broadcast slot. Lazy: the meter @@ -50,6 +54,11 @@ impl AudioBusProcessor { self.enabled = enabled; } + /// ESC: apply a composite instrument's output gain to the sum (before the meter). Set once at build. + pub fn set_output_gain(&mut self, source: Rc, sample_rate: f32) { + self.output_gain = Some(OutputGainStage::new(source, sample_rate)); + } + /// Add a source whose output is summed into this bus (TS `addAudioSource`). pub fn add_audio_source(&mut self, source: SharedAudioBuffer, node: NodeId) { self.sources.push(BusSource {buffer: source, node, delay: None}); @@ -101,7 +110,7 @@ impl Processor for AudioBusProcessor { } } - fn process(&mut self, _info: &ProcessInfo) { + fn process(&mut self, info: &ProcessInfo) { let mut output = self.output.borrow_mut(); output.clear_range(0, RENDER_QUANTUM); if self.enabled { @@ -116,6 +125,10 @@ impl Processor for AudioBusProcessor { } } } + if let Some(stage) = self.output_gain.as_mut() { + let output = &mut *output; + stage.process(info, &mut output.left[..RENDER_QUANTUM], &mut output.right[..RENDER_QUANTUM]); + } // A disabled (bypassed) bus meters its silence; the peak decays to zero. if let Some(meter) = self.meter.as_mut() { meter.process(&output.left, &output.right); diff --git a/crates/engine-env/src/channel_strip.rs b/crates/engine-env/src/channel_strip.rs index 4fdc0ff11..7c39bb76f 100644 --- a/crates/engine-env/src/channel_strip.rs +++ b/crates/engine-env/src/channel_strip.rs @@ -82,6 +82,11 @@ pub struct ChannelStripProcessor { input: Option, gain_left: LinearRamp, gain_right: LinearRamp, + // STEREO PAN mode only (`set_stereo_pan`): the cross-feeds right->left and left->right. Stay at 0 in the + // default BALANCE mode, so a track strip is untouched. + cross_to_left: LinearRamp, + cross_to_right: LinearRamp, + stereo_pan: bool, mute_gain: LinearRamp, meter: crate::meter::Meter, // peaks/RMS of the strip output (a broadcast slot) sample_rate: f32, @@ -99,6 +104,9 @@ impl ChannelStripProcessor { input: None, gain_left: LinearRamp::linear(sample_rate), gain_right: LinearRamp::linear(sample_rate), + cross_to_left: LinearRamp::linear(sample_rate), + cross_to_right: LinearRamp::linear(sample_rate), + stereo_pan: false, mute_gain: LinearRamp::linear(sample_rate), meter: crate::meter::Meter::new(sample_rate), sample_rate, @@ -113,8 +121,16 @@ impl ChannelStripProcessor { // mixer's solo gating) is OR-ed on top here so it always wins. fn retarget(&mut self, volume_db: f32, panning: f32, muted: bool) { let gain = db_to_gain(volume_db); - self.gain_left.set((1.0 - panning.max(0.0)) * gain, self.processing); - self.gain_right.set((1.0 + panning.min(0.0)) * gain, self.processing); + if self.stereo_pan { + let [left_to_left, right_to_left, left_to_right, right_to_right] = stereo_pan_gains(panning); + self.gain_left.set(left_to_left * gain, self.processing); + self.gain_right.set(right_to_right * gain, self.processing); + self.cross_to_left.set(right_to_left * gain, self.processing); + self.cross_to_right.set(left_to_right * gain, self.processing); + } else { + self.gain_left.set((1.0 - panning.max(0.0)) * gain, self.processing); + self.gain_right.set((1.0 + panning.min(0.0)) * gain, self.processing); + } let silent = muted || self.params.forced_silent.get(); self.mute_gain.set(if silent {0.0} else {1.0}, self.processing); } @@ -150,6 +166,21 @@ impl ChannelStripProcessor { // the loop auto-vectorizable; the ramped branch keeps the per-sample de-click. Two multiplies in BOTH // branches (float multiplication does not re-associate, the branches must produce identical samples). fn apply(&mut self, source: &AudioBuffer, output: &mut AudioBuffer, from: usize, to: usize) { + let crossing = self.cross_to_left.is_interpolating() || self.cross_to_right.is_interpolating() + || self.cross_to_left.get() != 0.0 || self.cross_to_right.get() != 0.0; + if crossing { + // STEREO PAN with a cross-feed in play (an off-centre pan): the full 2x2 matrix, per-sample ramped. + for index in from..to { + let mute = self.mute_gain.move_and_get(); + let (left, right) = (source.left[index], source.right[index]); + let (left_to_left, right_to_right) = (self.gain_left.move_and_get(), self.gain_right.move_and_get()); + let (right_to_left, left_to_right) = (self.cross_to_left.move_and_get(), self.cross_to_right.move_and_get()); + output.left[index] = (left * left_to_left + right * right_to_left) * mute; + output.right[index] = (left * left_to_right + right * right_to_right) * mute; + } + self.processing = true; + return; + } if self.gain_left.is_interpolating() || self.gain_right.is_interpolating() || self.mute_gain.is_interpolating() { for index in from..to { let mute = self.mute_gain.move_and_get(); @@ -168,6 +199,14 @@ impl ChannelStripProcessor { self.processing = true; // TS sets it per processed sub-block, so a mid-quantum retarget already ramps } + /// Switch the pan law from the track-style BALANCE (pan right only turns the left channel down, so a hard + /// pan silences one side of a stereo signal) to STEREO PAN (the far channel is MOVED to the near side with + /// an equal-power split, nothing is thrown away). Used by Frequency Split bands. Centre is identical in both + /// laws (the cross-feeds are 0 and the old two-multiply path runs), so a centred band is bit-exact. + pub fn set_stereo_pan(&mut self, stereo_pan: bool) { + self.stereo_pan = stereo_pan; + } + /// The peak/RMS broadcast slot of this strip's output. pub fn meter_slot(&self) -> crate::telemetry::BroadcastSlot { self.meter.slot() @@ -179,6 +218,21 @@ impl ChannelStripProcessor { } } +/// The STEREO PAN law, `[left->left, right->left, left->right, right->right]` for `panning` in -1..1. Panning +/// right keeps the right channel at unity and splits the left channel between left and right with an +/// equal-power (sine) law; panning left mirrors it. Every gain is in 0..1 (no polarity flip, no boost per path), +/// exactly `[1, 0, 0, 1]` at centre and `[0, 0, 1, 1]` / `[1, 1, 0, 0]` at the hard sides. +pub fn stereo_pan_gains(panning: f32) -> [f32; 4] { + let x = math::clamp(panning, -1.0, 1.0); + if x == 0.0 { + return [1.0, 0.0, 0.0, 1.0]; + } + let half_pi = core::f32::consts::FRAC_PI_2; + let amount = x.abs(); + let (stay, moved) = if amount >= 1.0 {(0.0, 1.0)} else {(math::sin((1.0 - amount) * half_pi), math::sin(amount * half_pi))}; + if x > 0.0 {[stay, 0.0, moved, 1.0]} else {[1.0, moved, 0.0, stay]} +} + impl EventReceiver for ChannelStripProcessor { fn event_input(&mut self) -> &mut EventBuffer { &mut self.events diff --git a/crates/engine-env/src/lib.rs b/crates/engine-env/src/lib.rs index 076d6bb59..27e4fe637 100644 --- a/crates/engine-env/src/lib.rs +++ b/crates/engine-env/src/lib.rs @@ -58,5 +58,6 @@ pub mod ppqn; pub mod process_info; pub mod process_phase; pub mod processor; +pub mod output_gain; pub mod topological_sort; pub mod update_clock; diff --git a/crates/engine-env/src/output_gain.rs b/crates/engine-env/src/output_gain.rs new file mode 100644 index 000000000..a09f307e2 --- /dev/null +++ b/crates/engine-env/src/output_gain.rs @@ -0,0 +1,363 @@ +//! The uniform instrument OUTPUT GAIN (instrument box field 6, `output-gain`, dB): a last-stage trim on an +//! instrument's own output, BEFORE the unit's audio-fx chain and strip. Applied engine-side by the unit's +//! source node (a plugin instrument, the tape player, a composite's sum), so every instrument kind gets the +//! same behaviour with no device-ABI change. `OutputGainSource` is the unit-level binding (the field's live +//! cell + its optional automation resolver, rebound by the engine); `OutputGainStage` is the per-node DSP. +//! 0 dB settled is a bit-exact pass-through (no multiply), so a project saved before the field existed +//! renders identically. Moves are de-clicked by a 5 ms `LinearRamp`, like the strip. At or below +//! [`OUTPUT_GAIN_FLOOR_DB`] the output is silent (the bottom of the range reads as -inf). + +use alloc::rc::Rc; +use core::cell::{Cell, RefCell}; +use math::value_mapping::Decibel; +use crate::block::Block; +use crate::channel_strip::StripValueSource; +use crate::process_info::ProcessInfo; +use crate::ramp::LinearRamp; + +/// The field key of the output gain on every instrument box (`InstrumentDeviceAttributes` field 6). +pub const OUTPUT_GAIN_KEY: u16 = 6; +/// The bottom of the range, treated as -inf (silence). +pub const OUTPUT_GAIN_FLOOR_DB: f32 = -60.0; +/// TS `ValueMapping.decibel(-60, 0, 12)`: 0 dB sits at the knob centre (unit 0.5). +pub const OUTPUT_GAIN_MAPPING: Decibel = Decibel::new(OUTPUT_GAIN_FLOOR_DB, 0.0, 12.0); + +/// dB to the linear factor, with the floor (and anything below it, -inf included) mapped to 0. +#[inline] +pub fn output_gain_linear(db: f32) -> f32 { + if db.is_nan() || db <= OUTPUT_GAIN_FLOOR_DB {0.0} else {math::db_to_gain(db)} +} + +/// The unit's output-gain binding, shared (`Rc`) between the engine (which rebinds it to the unit's current +/// instrument box) and the unit's source node. `field` is the box field's live real value (dB); `automation` +/// is `Some` while a Value track or modulation drives it. Both `None`: unity. +pub struct OutputGainSource { + pub field: RefCell>>>, + pub automation: RefCell> +} + +impl OutputGainSource { + pub fn new() -> Self { + Self {field: RefCell::new(None), automation: RefCell::new(None)} + } + + pub fn clear(&self) { + *self.field.borrow_mut() = None; + *self.automation.borrow_mut() = None; + } + + pub fn is_automated(&self) -> bool { + self.automation.borrow().is_some() + } + + pub fn static_db(&self) -> f32 { + self.field.borrow().as_ref().map_or(0.0, |cell| cell.get()) + } + + fn db_at(&self, position: f64, transporting: bool, lag: u32) -> f32 { + match self.automation.borrow().as_ref() { + Some(source) => source(position, transporting, lag), + None => self.static_db() + } + } +} + +impl Default for OutputGainSource { + fn default() -> Self { + Self::new() + } +} + +/// The per-node DSP: resolves the source at the update clock (like the strip) and applies a de-clicked gain +/// in place over the node's stereo output. +pub struct OutputGainStage { + source: Rc, + ramp: LinearRamp, + sample_rate: f32, + lag: u32, + processing: bool +} + +impl OutputGainStage { + pub fn new(source: Rc, sample_rate: f32) -> Self { + let mut ramp = LinearRamp::linear(sample_rate); + ramp.set(1.0, false); + Self {source, ramp, sample_rate, lag: 0, processing: false} + } + + pub fn source(&self) -> &Rc { + &self.source + } + + pub fn set_automation_delay(&mut self, frames: u32) { + self.lag = frames; + } + + fn retarget(&mut self, db: f32) { + self.ramp.set(output_gain_linear(db), self.processing); + } + + fn apply_range(&mut self, left: &mut [f32], right: &mut [f32], from: usize, to: usize) { + if self.ramp.is_interpolating() { + for index in from..to { + let gain = self.ramp.move_and_get(); + left[index] *= gain; + right[index] *= gain; + } + } else { + let gain = self.ramp.get(); + if gain != 1.0 { + for index in from..to { + left[index] *= gain; + right[index] *= gain; + } + } + } + self.processing = true; + } + + /// Apply over one quantum of the node's output (`left` / `right` hold at least `s1` of every block). + pub fn process(&mut self, info: &ProcessInfo, left: &mut [f32], right: &mut [f32]) { + let frames = left.len().min(right.len()); + if !self.source.is_automated() { + self.retarget(self.source.static_db()); + self.apply_range(left, right, 0, frames); + return; + } + for block in info.blocks { + let (s0, s1) = ((block.s0 as usize).min(frames), (block.s1 as usize).min(frames)); + if !block.flags.transporting() { + let db = self.source.db_at(block.p0, false, self.lag); + self.retarget(db); + self.apply_range(left, right, s0, s1); + continue; + } + let mut cursor = s0; + let db = self.source.db_at(block.p0, true, self.lag); + self.retarget(db); + let mut position = crate::update_clock::first_update(block.p0, self.lag); + while position < block.p1 { + let offset = self.offset_of(position, block).clamp(cursor, s1); + if offset > cursor { + self.apply_range(left, right, cursor, offset); + cursor = offset; + } + let db = self.source.db_at(position, true, self.lag); + self.retarget(db); + position = crate::update_clock::next_update(position, self.lag); + } + if cursor < s1 { + self.apply_range(left, right, cursor, s1); + } + } + } + + fn offset_of(&self, position: f64, block: &Block) -> usize { + crate::block::frame_of(block, position, self.sample_rate) + } +} + +#[cfg(test)] +mod tests { + use alloc::rc::Rc; + use alloc::vec::Vec; + use core::cell::Cell; + use math::value_mapping::ValueMapping; + use crate::block::Block; + use crate::block_flags::BlockFlags; + use crate::process_info::ProcessInfo; + use crate::RENDER_QUANTUM; + use super::{output_gain_linear, OutputGainSource, OutputGainStage, OUTPUT_GAIN_MAPPING}; + + const SR: f32 = 48_000.0; + + fn block(transporting: bool, p0: f64) -> Block { + Block {index: 0, flags: BlockFlags::create(transporting, false, transporting, false), + p0, p1: p0 + 5.12, anchor: p0, anchor_frame: 0, s0: 0, s1: RENDER_QUANTUM as u32, bpm: 120.0} + } + + fn signal(quantum: usize) -> ([f32; RENDER_QUANTUM], [f32; RENDER_QUANTUM]) { + let mut left = [0.0f32; RENDER_QUANTUM]; + let mut right = [0.0f32; RENDER_QUANTUM]; + for index in 0..RENDER_QUANTUM { + let n = (quantum * RENDER_QUANTUM + index) as f32; + left[index] = (n * 0.031).sin() * 0.7 + 1.0e-7 * n; + right[index] = (n * 0.017).cos() * -0.4; + } + (left, right) + } + + fn with_field(db: f32) -> (Rc, Rc>) { + let source = Rc::new(OutputGainSource::new()); + let cell = Rc::new(Cell::new(db)); + *source.field.borrow_mut() = Some(cell.clone()); + (source, cell) + } + + fn run(stage: &mut OutputGainStage, quanta: usize) -> Vec { + let blocks = [block(true, 0.0)]; + let info = ProcessInfo {blocks: &blocks}; + let mut out = Vec::new(); + for quantum in 0..quanta { + let (mut left, mut right) = signal(quantum); + stage.process(&info, &mut left, &mut right); + out.extend_from_slice(&left); + out.extend_from_slice(&right); + } + out + } + + fn reference(quanta: usize) -> Vec { + let mut out = Vec::new(); + for quantum in 0..quanta { + let (left, right) = signal(quantum); + out.extend_from_slice(&left); + out.extend_from_slice(&right); + } + out + } + + fn bits(samples: &[f32]) -> Vec { + samples.iter().map(|sample| sample.to_bits()).collect() + } + + #[test] + fn zero_db_is_bit_identical() { + let (source, _) = with_field(0.0); + let mut stage = OutputGainStage::new(source, SR); + assert_eq!(bits(&run(&mut stage, 32)), bits(&reference(32))); + let mut unbound = OutputGainStage::new(Rc::new(OutputGainSource::new()), SR); + assert_eq!(bits(&run(&mut unbound, 32)), bits(&reference(32)), "no field (old project / no instrument): untouched"); + } + + #[test] + fn zero_db_sits_at_the_knob_centre() { + assert!(OUTPUT_GAIN_MAPPING.y(0.5).abs() < 1.0e-4, "{}", OUTPUT_GAIN_MAPPING.y(0.5)); + assert_eq!(OUTPUT_GAIN_MAPPING.y(1.0), 12.0); + assert_eq!(OUTPUT_GAIN_MAPPING.y(0.0), f32::NEG_INFINITY); + } + + #[test] + fn minus_six_db_halves() { + let (source, _) = with_field(-6.0); + let mut stage = OutputGainStage::new(source, SR); + let out = run(&mut stage, 8); + let input = reference(8); + let gain = output_gain_linear(-6.0); + assert!((gain - 0.501).abs() < 0.0005, "-6 dB = {gain}, expected 0.501"); + for (sample, dry) in out.iter().zip(input.iter()) { + assert!((sample - dry * gain).abs() <= 1.0e-6); + } + } + + #[test] + fn plus_twelve_db_boosts() { + let (source, _) = with_field(12.0); + let mut stage = OutputGainStage::new(source, SR); + let out = run(&mut stage, 1); + let input = reference(1); + assert!((out[5] - input[5] * 3.981_071_7).abs() < 1.0e-5); + } + + #[test] + fn floor_is_silence() { + for db in [-60.0f32, -72.0, f32::NEG_INFINITY] { + let (source, _) = with_field(db); + let mut stage = OutputGainStage::new(source, SR); + assert!(run(&mut stage, 4).iter().all(|sample| *sample == 0.0), "{db} dB"); + } + assert!(output_gain_linear(-59.9) > 0.0); + } + + #[test] + fn a_jump_is_smoothed_and_settles_exactly() { + let (source, cell) = with_field(-60.0); + let mut stage = OutputGainStage::new(source, SR); + let blocks = [block(true, 0.0)]; + let info = ProcessInfo {blocks: &blocks}; + let mut trace = Vec::new(); + for quantum in 0..8 { + if quantum == 2 { + cell.set(12.0); + } + let mut left = [1.0f32; RENDER_QUANTUM]; + let mut right = [1.0f32; RENDER_QUANTUM]; + stage.process(&info, &mut left, &mut right); + trace.extend_from_slice(&left); + } + assert!(trace[..2 * RENDER_QUANTUM].iter().all(|sample| *sample == 0.0), "the first value snaps (no fade-in on load)"); + let jump = output_gain_linear(12.0); + let max_step = trace.windows(2).map(|pair| (pair[1] - pair[0]).abs()).fold(0.0f32, f32::max); + let ramp = (SR * 0.005) as f32; + assert!(max_step <= jump / ramp * 1.001, "largest per-sample step {max_step}, a hard jump would be {jump}"); + assert!(max_step < 0.02); + assert_eq!(*trace.last().unwrap_or(&0.0), jump, "settles exactly on the target"); + } + + #[test] + fn automation_overrides_the_field_at_the_update_clock() { + let (source, _) = with_field(0.0); + *source.automation.borrow_mut() = Some(Rc::new(|_position: f64, _transporting: bool, _lag: u32| -6.0)); + let mut stage = OutputGainStage::new(source.clone(), SR); + let out = run(&mut stage, 1); + let input = reference(1); + assert!((out[3] - input[3] * output_gain_linear(-6.0)).abs() <= 1.0e-6, "the first resolve snaps"); + source.clear(); + let mut cleared = OutputGainStage::new(source, SR); + assert_eq!(bits(&run(&mut cleared, 2)), bits(&reference(2))); + } + + #[test] + fn a_composite_sum_applies_its_gain_and_is_untouched_without_one() { + use crate::audio_buffer::shared_audio_buffer; + use crate::audio_bus_processor::AudioBusProcessor; + use crate::processor::Processor; + let blocks = [block(true, 0.0)]; + let info = ProcessInfo {blocks: &blocks}; + let child = shared_audio_buffer(); + { + let mut child = child.borrow_mut(); + let (left, right) = signal(0); + child.left[..RENDER_QUANTUM].copy_from_slice(&left); + child.right[..RENDER_QUANTUM].copy_from_slice(&right); + } + let plain_out = shared_audio_buffer(); + let mut plain = AudioBusProcessor::new(plain_out.clone()); + plain.add_audio_source(child.clone(), 1); + plain.process(&info); + let (dry_left, _) = signal(0); + assert_eq!(bits(&plain_out.borrow().left[..RENDER_QUANTUM]), bits(&dry_left), "a bus with no gain: bit-exact sum"); + let gained_out = shared_audio_buffer(); + let mut gained = AudioBusProcessor::new(gained_out.clone()); + gained.add_audio_source(child, 1); + let (source, _) = with_field(-6.0); + gained.set_output_gain(source, SR); + gained.process(&info); + let gain = output_gain_linear(-6.0); + for (wet, dry) in gained_out.borrow().left[..RENDER_QUANTUM].iter().zip(dry_left.iter()) { + assert!((wet - dry * gain).abs() <= 1.0e-6); + } + } + + #[test] + fn a_downward_move_mid_play_ramps_without_a_click() { + let (source, cell) = with_field(0.0); + let mut stage = OutputGainStage::new(source, SR); + let blocks = [block(true, 0.0)]; + let info = ProcessInfo {blocks: &blocks}; + let mut trace = Vec::new(); + for quantum in 0..6 { + if quantum == 1 { + cell.set(-60.0); + } + let mut left = [0.8f32; RENDER_QUANTUM]; + let mut right = [0.8f32; RENDER_QUANTUM]; + stage.process(&info, &mut left, &mut right); + trace.extend_from_slice(&left); + } + assert!(trace[..RENDER_QUANTUM].iter().all(|sample| *sample == 0.8), "0 dB first: untouched"); + let max_step = trace.windows(2).map(|pair| (pair[1] - pair[0]).abs()).fold(0.0f32, f32::max); + assert!(max_step < 0.8 / 200.0, "spread over the 5 ms ramp, largest step {max_step}"); + assert_eq!(*trace.last().unwrap_or(&1.0), 0.0, "ends silent"); + } +} diff --git a/crates/engine-env/tests/frequency_split_pan.rs b/crates/engine-env/tests/frequency_split_pan.rs new file mode 100644 index 000000000..7d8a1c2ab --- /dev/null +++ b/crates/engine-env/tests/frequency_split_pan.rs @@ -0,0 +1,224 @@ +//! Frequency Split band PAN on stereo material: each band runs through its own `ChannelStripProcessor` in the +//! STEREO PAN mode the engine gives effect-composite entries, then the bands are summed (the composite's wet sum). +//! Compared against the old track-style BALANCE law. Run with `--nocapture` to see the measured numbers. + +use alloc::rc::Rc; +extern crate alloc; + +use engine_env::audio_buffer::{shared_audio_buffer, SharedAudioBuffer}; +use engine_env::audio_generator::AudioGenerator; +use engine_env::audio_input::AudioInput; +use engine_env::channel_strip::{stereo_pan_gains, ChannelStripProcessor, StripAutomation, StripParams}; +use engine_env::frequency_split::FrequencySplitter; +use engine_env::process_info::ProcessInfo; +use engine_env::processor::Processor; +use engine_env::RENDER_QUANTUM; + +const SR: f32 = 48_000.0; +const ROUNDS: usize = 400; +const SKIP: usize = 40; + +#[derive(Clone, Copy)] +enum Source {Wide, HardLeft, Mono, Partial} + +struct Rig { + splitter: FrequencySplitter, + strips: Vec<(ChannelStripProcessor, Rc)>, + input: SharedAudioBuffer, + seed: u32, + phase: f32 +} + +impl Rig { + fn new(stereo_pan: bool) -> Self { + let splitter = FrequencySplitter::new(SR as f64, 3, &[300.0, 3_000.0]); + let strips = (0..3).map(|band| { + let params = Rc::new(StripParams::new()); + let mut strip = ChannelStripProcessor::new(params.clone(), Rc::new(StripAutomation::new()), SR); + strip.set_stereo_pan(stereo_pan); + strip.set_audio_source(splitter.band(band)); + (strip, params) + }).collect(); + Self {splitter, strips, input: shared_audio_buffer(), seed: 0x1357_9bdf, phase: 0.0} + } + + fn noise(&mut self) -> f32 { + self.seed = self.seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + (self.seed >> 8) as f32 / 8_388_608.0 - 1.0 + } + + fn fill(&mut self, source: Source) { + let input = self.input.clone(); + let mut buffer = input.borrow_mut(); + for index in 0..RENDER_QUANTUM { + let (a, b) = (self.noise() * 0.3, self.noise() * 0.3); + self.phase += 0.02; + let tone = self.phase.sin() * 0.2; + let (left, right) = match source { + Source::Wide => (a, b), + Source::HardLeft => (a + tone, 0.0), + Source::Mono => (a + tone, a + tone), + Source::Partial => (a + tone, 0.6 * a + b * 0.5 + tone) + }; + buffer.left[index] = left; + buffer.right[index] = right; + } + } + + // One quantum: split, strip every band, sum. Returns (input, per-band outputs, sum). + fn step(&mut self, source: Source) -> (Vec<[f32; 2]>, Vec>, Vec<[f32; 2]>) { + self.fill(source); + self.splitter.process(&self.input); + let info = ProcessInfo {blocks: &[]}; + let mut bands = Vec::new(); + let mut sum = vec![[0.0f32; 2]; RENDER_QUANTUM]; + for (strip, _) in self.strips.iter_mut() { + strip.process(&info); + let out = strip.audio_output(); + let out = out.borrow(); + let frames: Vec<[f32; 2]> = (0..RENDER_QUANTUM).map(|i| [out.left[i], out.right[i]]).collect(); + for (i, frame) in frames.iter().enumerate() {sum[i][0] += frame[0]; sum[i][1] += frame[1];} + bands.push(frames); + } + let input = self.input.borrow(); + let frames = (0..RENDER_QUANTUM).map(|i| [input.left[i], input.right[i]]).collect(); + (frames, bands, sum) + } + + fn pan(&mut self, band: usize, panning: f32) { + self.strips[band].1.panning.set(panning); + } +} + +#[derive(Default)] +struct Stats {ll: f64, rr: f64, lr: f64} + +impl Stats { + fn add(&mut self, frames: &[[f32; 2]]) { + for f in frames { + let (l, r) = (f[0] as f64, f[1] as f64); + self.ll += l * l; + self.rr += r * r; + self.lr += l * r; + } + } + fn corr(&self) -> f64 {if self.ll * self.rr == 0.0 {0.0} else {self.lr / (self.ll * self.rr).sqrt()}} + fn energy_db(&self) -> f64 {10.0 * (self.ll + self.rr).log10()} + fn right_share(&self) -> f64 {self.rr / (self.ll + self.rr)} +} + +// Measure band `band` (and the full sum) with `pan` applied to that band, after the pan glide has settled. +fn measure(stereo_pan: bool, source: Source, band: usize, pan: f32) -> (Stats, Stats) { + let mut rig = Rig::new(stereo_pan); + rig.pan(band, pan); + let (mut band_stats, mut sum_stats) = (Stats::default(), Stats::default()); + for round in 0..ROUNDS { + let (_, bands, sum) = rig.step(source); + if round >= SKIP { + band_stats.add(&bands[band]); + sum_stats.add(&sum); + } + } + (band_stats, sum_stats) +} + +#[test] +fn the_law_is_identity_at_centre_and_never_negative() { + assert_eq!(stereo_pan_gains(0.0), [1.0, 0.0, 0.0, 1.0]); + assert_eq!(stereo_pan_gains(1.0), [0.0, 0.0, 1.0, 1.0]); + assert_eq!(stereo_pan_gains(-1.0), [1.0, 1.0, 0.0, 0.0]); + for step in -100..=100 { + let gains = stereo_pan_gains(step as f32 / 100.0); + assert!(gains.iter().all(|g| *g >= 0.0 && *g <= 1.0), "no polarity flip, no boost per path"); + let far_power = if step >= 0 {gains[0] * gains[0] + gains[2] * gains[2]} else {gains[3] * gains[3] + gains[1] * gains[1]}; + assert!((far_power - 1.0).abs() < 1e-6, "the far channel's energy is kept (equal-power move)"); + } +} + +#[test] +fn centre_is_a_bit_exact_pass_through_and_the_band_sum_is_flat() { + let mut new_rig = Rig::new(true); + let mut old_rig = Rig::new(false); + let (mut worst, mut err, mut ref_energy) = (0u32, 0.0f64, 0.0f64); + for round in 0..ROUNDS { + let (input, new_bands, new_sum) = new_rig.step(Source::Partial); + let (_, old_bands, _) = old_rig.step(Source::Partial); + for (a, b) in new_bands.iter().flatten().zip(old_bands.iter().flatten()) { + if a[0].to_bits() != b[0].to_bits() || a[1].to_bits() != b[1].to_bits() {worst += 1;} + } + if round >= SKIP { + for (s, i) in new_sum.iter().zip(&input) { + err += ((s[0] - i[0]) as f64).powi(2) + ((s[1] - i[1]) as f64).powi(2); + ref_energy += (i[0] as f64).powi(2) + (i[1] as f64).powi(2); + } + } + } + let flat = 10.0 * (err / ref_energy).log10(); + println!("centre: {worst} samples differ from the old strip; band sum vs input residual {flat:.1} dB"); + assert_eq!(worst, 0, "centre pan is bit-identical to the old strip path"); + assert!(flat < -100.0, "the three bands still sum back to the input (got {flat} dB)"); +} + +#[test] +fn hard_panned_input_moves_to_the_panned_side() { + // Material only on the LEFT; pan the mid band hard RIGHT. Balance can only silence it. + let (in_stats, _) = measure(true, Source::HardLeft, 1, 0.0); + let (new_stats, _) = measure(true, Source::HardLeft, 1, 1.0); + let (old_stats, _) = measure(false, Source::HardLeft, 1, 1.0); + println!("hard-left band panned hard right: input {:.1} dB (right share {:.2})", in_stats.energy_db(), in_stats.right_share()); + println!(" BEFORE (balance): {:.1} dB, right share {:.2}", old_stats.energy_db(), old_stats.right_share()); + println!(" AFTER (stereo pan): {:.1} dB, right share {:.2}", new_stats.energy_db(), new_stats.right_share()); + assert!((new_stats.energy_db() - in_stats.energy_db()).abs() < 0.01, "the band keeps its level"); + assert!(new_stats.right_share() > 0.999, "and now sits on the right"); + let (half, _) = measure(true, Source::HardLeft, 1, 0.5); + println!(" AFTER at +50%: {:.1} dB, right share {:.2}", half.energy_db(), half.right_share()); + assert!((half.right_share() - 0.5).abs() < 0.01 && (half.energy_db() - in_stats.energy_db()).abs() < 0.01); +} + +#[test] +fn wide_stereo_moves_to_the_side_without_losing_energy_or_inverting() { + let (in_stats, _) = measure(true, Source::Wide, 1, 0.0); + println!("wide (decorrelated) band: input {:.2} dB, corr {:.3}, right share {:.3}", in_stats.energy_db(), in_stats.corr(), in_stats.right_share()); + let mut last_toward = 0.5f64; + for &pan in &[0.25f32, 0.5, 0.75, 1.0, -0.5] { + let (old_stats, _) = measure(false, Source::Wide, 1, pan); + let (new_stats, _) = measure(true, Source::Wide, 1, pan); + println!(" pan {pan:+.2}: BEFORE {:+.2} dB, right share {:.3}, corr {:.3} | AFTER {:+.2} dB, right share {:.3}, corr {:.3}", + old_stats.energy_db() - in_stats.energy_db(), old_stats.right_share(), old_stats.corr(), + new_stats.energy_db() - in_stats.energy_db(), new_stats.right_share(), new_stats.corr()); + assert!((new_stats.energy_db() - in_stats.energy_db()).abs() < 0.05, "a wide band keeps its energy"); + assert!(new_stats.corr() >= -1e-3, "never pushed out of phase"); + let toward = if pan > 0.0 {new_stats.right_share()} else {1.0 - new_stats.right_share()}; + let old_toward = if pan > 0.0 {old_stats.right_share()} else {1.0 - old_stats.right_share()}; + // The balance law LOOKS further out at small pans only because it throws the far channel away (it + // loses level); the stereo pan moves energy instead. Required: it moves steadily toward the side and + // reaches it fully at 100%. + let _ = old_toward; + if pan > 0.0 { + assert!(toward > last_toward, "moves further to the side as the pan grows"); + last_toward = toward; + } + if pan == 1.0 {assert!(toward > 0.999, "hard pan is fully on the side");} + if pan == -0.5 {assert!((toward - 0.75).abs() < 0.01, "pan -50% mirrors +50% (got {toward})");} + } +} + +#[test] +fn a_mono_band_pans_like_a_mono_source() { + let (in_stats, _) = measure(true, Source::Mono, 0, 0.0); + let (new_stats, _) = measure(true, Source::Mono, 0, 1.0); + let (old_stats, _) = measure(false, Source::Mono, 0, 1.0); + println!("mono band hard right: BEFORE {:+.2} dB right share {:.2} | AFTER {:+.2} dB right share {:.2}, corr in {:.3}", + old_stats.energy_db() - in_stats.energy_db(), old_stats.right_share(), + new_stats.energy_db() - in_stats.energy_db(), new_stats.right_share(), in_stats.corr()); + assert!(new_stats.right_share() > 0.999); +} + +#[test] +fn panning_one_band_leaves_the_others_untouched_and_the_mix_balanced() { + let (_, centre_sum) = measure(true, Source::Partial, 2, 0.0); + let (_, panned_sum) = measure(true, Source::Partial, 2, -1.0); + println!("mix with the high band hard left: right share {:.3} -> {:.3}, mix level {:+.2} dB", + centre_sum.right_share(), panned_sum.right_share(), panned_sum.energy_db() - centre_sum.energy_db()); + assert!(panned_sum.right_share() < centre_sum.right_share(), "the mix leans to the panned side"); +} diff --git a/crates/engine/src/audio_region_player.rs b/crates/engine/src/audio_region_player.rs index 9f4314ef7..a41544c9e 100644 --- a/crates/engine/src/audio_region_player.rs +++ b/crates/engine/src/audio_region_player.rs @@ -26,7 +26,7 @@ use engine_env::event_buffer::EventBuffer; use engine_env::event_receiver::EventReceiver; use engine_env::process_info::ProcessInfo; use engine_env::processor::Processor; -use math::curve::normalized_at; +use math::curve::{fade_in_gain, fade_out_gain}; use math::db_to_gain; use alloc::vec::Vec; use value::region::locate_loops; @@ -42,6 +42,8 @@ use crate::tempo_map::{SharedTempoMap, TempoMap}; /// The boundary declick window in seconds (matches the TS tape `VOICE_FADE_DURATION`): a region edge with no /// authored fade gets this short anti-click ramp so an adjacent-region seam does not hard-cut into a click. const VOICE_FADE_DURATION: f64 = 0.020; +/// The opt-in anti-click fade at a frame-0 clip start (AudioRegionBox `fading.anti-click`, set on new clips). +const ANTI_CLICK_DURATION: f64 = 0.003; /// The crossfade in seconds at a transport jump: only long enough to remove the hard click. const JUMP_FADE_DURATION: f64 = 0.005; @@ -70,6 +72,7 @@ pub(crate) struct AudioRegionPlayer { // IS the source. `None` when the unit is not monitoring. Re-set on rebuild (the map change rewires). monitor: Option<(i32, i32)>, meter: engine_env::meter::Meter, // peaks/RMS of the tape output (a broadcast slot) + output_gain: Option, // ESC: TapeDeviceBox field 6 // Recycled sequencers: a pruned region's sequencer parks here (voices cleared, capacity kept) and the // next stretch region reuses it. `prepare` (reconcile) pre-warms the pool for every BOUND stretch region, // so the render-path `pop()` never misses; without the pre-warm each new concurrency high-water would @@ -194,7 +197,7 @@ impl AudioRegionPlayer { release_tails: Vec::with_capacity(16), jump_tails: Vec::with_capacity(16), granular_releases: Vec::with_capacity(8), granular_files: Vec::with_capacity(8), signalsmith_releases: Vec::with_capacity(4), signalsmith_templates: Vec::with_capacity(4), enabled: true, - monitor: None, meter: engine_env::meter::Meter::new(sample_rate), clips} + monitor: None, meter: engine_env::meter::Meter::new(sample_rate), clips, output_gain: None} } /// Set the staged live-input channels summed into the output (EFFECTS monitoring), or `None`. The @@ -242,6 +245,11 @@ impl AudioRegionPlayer { } /// The peak/RMS broadcast slot of the tape output. + /// ESC: attach the TapeDeviceBox's output-gain source (field 6), once at build. + pub(crate) fn set_output_gain(&mut self, source: Rc) { + self.output_gain = Some(engine_env::output_gain::OutputGainStage::new(source, self.sample_rate)); + } + pub(crate) fn meter_slot(&self) -> engine_env::telemetry::BroadcastSlot { self.meter.slot() } @@ -269,7 +277,7 @@ impl Processor for AudioRegionPlayer { let AudioRegionPlayer { tracks, sample_rate, tempo_map, output, sequencers, native_cursors, visited, sequencer_pool, signalsmith_players, signalsmith_pool, release_tails, jump_tails, granular_releases, granular_files, - signalsmith_releases, signalsmith_templates, clips, enabled, monitor, meter, .. + signalsmith_releases, signalsmith_templates, clips, enabled, monitor, meter, output_gain, .. } = self; let mut output = output.borrow_mut(); output.clear(); // the player is a source: it fills its own output each quantum (silence when not playing) @@ -373,6 +381,10 @@ impl Processor for AudioRegionPlayer { } } } + if let Some(stage) = output_gain.as_mut() { + let output = &mut *output; + stage.process(info, &mut output.left[..engine_env::RENDER_QUANTUM], &mut output.right[..engine_env::RENDER_QUANTUM]); + } meter.process(&output.left, &output.right); // Prune per-region state for regions that stopped playing: cursors are plain Copy structs (retain frees // nothing), sequencers park in the pool for reuse instead of dropping their voice buffers mid-render. @@ -521,12 +533,11 @@ fn play_signalsmith(player: &mut SignalsmithStretch, region: &AudioRegion, confi let pitch = math::pow(2.0, config.transpose as f64 / 12.0) as f32; let resample = source_rate as f64 / engine_rate as f64; // actual source samples per engine-rate sample let source_frames = left.len(); - let declick_pulses = seconds_to_pulses(VOICE_FADE_DURATION, block.bpm) as f64; + let (declick_in, declick_pulses) = start_declick(region, block.bpm); // The region starts partway INTO its source (so a fade-in is needed to avoid an onset click) when the read // begins past frame 0. A CUT expresses that mid-file start via loop_offset (RegionEditing.cut moves // loop_offset, never waveform_offset), so both must be consulted — guarding on waveform_offset alone left a // cut's second region with no fade-in, hard-starting at full gain against the first region's declicked end. - let declick_in = region.waveform_offset > 0.0 || region.loop_offset > 0.0; let mut scratch_l = [0.0f32; engine_env::RENDER_QUANTUM]; let mut scratch_r = [0.0f32; engine_env::RENDER_QUANTUM]; // A re-entry plays fresh (re-primes below), so drop any release seeded at a previous end. @@ -635,8 +646,7 @@ fn fill_fading_gain(buffer: &mut [f32], region: &AudioRegion, result_start: f64, // sequencers, so their grains do not stitch across the seam: without this fade region A hard-cuts into region // B = a loud high-pitch click. The fade-in guard consults loop_offset because a cut expresses the second // region's mid-file start there (mirrors render_region / play_signalsmith). - let declick_pulses = seconds_to_pulses(VOICE_FADE_DURATION, block.bpm) as f64; - let declick_in = region.waveform_offset > 0.0 || region.loop_offset > 0.0; + let (declick_in, declick_pulses) = start_declick(region, block.bpm); for (index, slot) in buffer.iter_mut().enumerate().take(count) { let ppqn = start_ppqn + if count > 0 { index as f64 / count as f64 * span_ppqn } else { 0.0 }; // declick_out = false: the granular path fades OUT by ringing its voices out PAST the region end (the @@ -839,12 +849,11 @@ fn render_region(output: &mut AudioBuffer, region: &AudioRegion, left: &[f32], r // Boundary declick window in pulses (~20 ms at the block tempo): a short fade applied at a region edge that // has no authored fade, so an adjacent-region seam does not click. The start edge is declicked only when the // read cuts into the file (waveform offset > 0); a frame-0 onset (song start / loop start) is left alone. - let declick_pulses = seconds_to_pulses(VOICE_FADE_DURATION, block.bpm) as f64; + let (declick_in, declick_pulses) = start_declick(region, block.bpm); // The region starts partway INTO its source (so a fade-in is needed to avoid an onset click) when the read // begins past frame 0. A CUT expresses that mid-file start via loop_offset (RegionEditing.cut moves // loop_offset, never waveform_offset), so both must be consulted — guarding on waveform_offset alone left a // cut's second region with no fade-in, hard-starting at full gain against the first region's declicked end. - let declick_in = region.waveform_offset > 0.0 || region.loop_offset > 0.0; let window = (VOICE_FADE_DURATION * engine_rate as f64).max(1.0); let jump_window = (JUMP_FADE_DURATION * engine_rate as f64).max(1.0); if block.flags.discontinuous() && from > region.position { @@ -976,12 +985,24 @@ fn interpolate(buffer: &[f32], index: usize, frac: f32) -> f32 { /// fade-product doubling): an authored fade replaces the declick on its edge. `declick_in` gates the START /// declick to reads that CUT into the file (a frame-0 onset, e.g. the song start, is left untouched); the END /// declick is always applied (the outgoing hard cut is the click TS removes). +/// Whether a region's start gets the linear boundary declick, and its length in pulses: a read that starts +/// mid-file always does (20 ms, upstream); a frame-0 start only with the per-clip anti-click flag (3 ms). +fn start_declick(region: &AudioRegion, bpm: f32) -> (bool, f64) { + if region.waveform_offset > 0.0 || region.loop_offset > 0.0 { + (true, seconds_to_pulses(VOICE_FADE_DURATION, bpm) as f64) + } else if region.anti_click { + (true, seconds_to_pulses(ANTI_CLICK_DURATION, bpm) as f64) + } else { + (false, seconds_to_pulses(VOICE_FADE_DURATION, bpm) as f64) + } +} + fn fade_gain(position: f64, duration: f64, region: &AudioRegion, declick_pulses: f64, declick_in: bool, declick_out: bool) -> f32 { let mut fade_in = 1.0f32; let mut fade_out = 1.0f32; if region.fade_in > 0.0 { if position < region.fade_in { - fade_in = normalized_at((position / region.fade_in) as f32, region.fade_in_slope); + fade_in = fade_in_gain((position / region.fade_in) as f32, region.fade_in_slope, region.fade_in_curve); } } else if declick_in && declick_pulses > 0.0 && position < declick_pulses { fade_in = (position / declick_pulses).clamp(0.0, 1.0) as f32; @@ -990,7 +1011,7 @@ fn fade_gain(position: f64, duration: f64, region: &AudioRegion, declick_pulses: let fade_out_start = duration - region.fade_out; if position > fade_out_start { let progress = ((position - fade_out_start) / region.fade_out) as f32; - fade_out = 1.0 - normalized_at(progress, region.fade_out_slope); + fade_out = fade_out_gain(progress, region.fade_out_slope, region.fade_out_curve); } } else if declick_out && declick_pulses > 0.0 && position > duration - declick_pulses { fade_out = ((duration - position) / declick_pulses).clamp(0.0, 1.0) as f32; @@ -1038,7 +1059,7 @@ mod tests { AudioRegion { region_uuid: [1u8; 16], position: 0.0, duration: 96_000.0, loop_offset: 0.0, loop_duration: 96_000.0, file: [9u8; 16], gain_db, mute: false, waveform_offset: 0.0, fade_in, fade_out, - fade_in_slope: 0.5, fade_out_slope: 0.5, warp: Vec::new(), time_stretch: None, signalsmith: None, transients: Vec::new() + fade_in_slope: 0.5, fade_out_slope: 0.5, fade_in_curve: 0, fade_out_curve: 0, crossfade_out: false, anti_click: false, warp: Vec::new(), time_stretch: None, signalsmith: None, transients: Vec::new() } } @@ -1699,3 +1720,225 @@ mod tests { } } + +#[cfg(test)] +mod crossfade_tests { + use super::*; + use alloc::vec::Vec; + use engine_env::block_flags::BlockFlags; + use math::curve::{normalized_at, s_curve_at, FADE_BEND, FADE_EQUAL_POWER, FADE_S_CURVE}; + use value::region::RegionCollection; + + const SPP: f64 = 25.0; // samples per pulse at 120 bpm / 48 kHz + + fn noise(frames: usize, seed: u32) -> Vec { + let mut state = seed.wrapping_mul(2_654_435_761).max(1); + (0..frames).map(|_| { + state ^= state << 13; + state ^= state >> 17; + state ^= state << 5; + (state as f32 / u32::MAX as f32) * 2.0 - 1.0 + }).collect() + } + + fn plain(id: u8, position: f64, duration: f64, loop_offset: f64, loop_duration: f64) -> AudioRegion { + AudioRegion { + region_uuid: [id; 16], position, duration, loop_offset, loop_duration, + file: [id; 16], gain_db: 0.0, mute: false, waveform_offset: 0.0, fade_in: 0.0, fade_out: 0.0, + fade_in_slope: 0.75, fade_out_slope: 0.25, fade_in_curve: 0, fade_out_curve: 0, crossfade_out: false, anti_click: false, + warp: Vec::new(), time_stretch: None, signalsmith: None, transients: Vec::new() + } + } + + // What the timeline writes for a crossfade: A's tail and B's head overlap by `length` pulses, matched curves. + fn link(a: &mut AudioRegion, b: &mut AudioRegion, curve: i32, slope: f32) { + let length = a.position + a.duration - b.position; + a.fade_out = length; + b.fade_in = length; + a.fade_out_curve = curve; + b.fade_in_curve = curve; + a.fade_out_slope = slope; + b.fade_in_slope = slope; + a.crossfade_out = true; + } + + // The player's per-track path: a RegionCollection range query per quantum (so the crossfade predecessor + // visit is exercised), the native read head, and its seam tails. `sources[i]` plays for `regions[i]`. + fn render(regions: &[AudioRegion], sources: &[&[f32]], frames: usize, quantum: usize) -> Vec { + let tempo = TempoMap::fixed(120.0); + let mut collection = RegionCollection::new(); + for region in regions { collection.add(region.clone()); } + let source_of = |uuid: &Uuid| regions.iter().position(|r| r.region_uuid == *uuid).map(|i| sources[i]).unwrap_or(sources[0]); + let mut cursors: Vec<(Uuid, NativeCursor)> = regions.iter().map(|r| (r.region_uuid, NativeCursor::new())).collect(); + let mut tails: Vec = Vec::new(); + let mut out = Vec::with_capacity(frames); + let mut n = 0usize; + let mut index = 0u32; + while n < frames { + let q = quantum.min(frames - n); + let (p0, p1) = (n as f64 / SPP, (n + q) as f64 / SPP); + let block = Block {index, flags: BlockFlags::create(true, index == 0, true, false), p0, p1, anchor: p0, anchor_frame: 0, s0: 0, s1: q as u32, bpm: 120.0}; + let mut output = AudioBuffer::new(); + tails.retain_mut(|tail| { let src = source_of(&tail.region_uuid); render_one_tail(tail, src, src, &mut output, &block) }); + for region in collection.iterate_range(p0, p1) { + let src = source_of(®ion.region_uuid); + let cursor = &mut cursors.iter_mut().find(|(uuid, _)| *uuid == region.region_uuid).expect("cursor").1; + tails.retain(|tail| tail.region_uuid != region.region_uuid); + if let Some(tail) = render_region(&mut output, region, src, src, 48_000.0, p0, p1, &block, 48_000.0, &tempo, cursor) { + tails.push(tail); + } + } + out.extend_from_slice(&output.left[..q]); + n += q; + index += 1; + } + out + } + + fn db(value: f64) -> f64 { 10.0 * value.max(1e-30).log10() } + + fn mean_square(signal: &[f32]) -> f64 { signal.iter().map(|v| (*v as f64) * (*v as f64)).sum::() / signal.len() as f64 } + + // Two unrelated sources, A [0, 4800) ppqn, B [3840, 8640): a 960 ppqn (0.5 s) overlap. + fn unrelated_pair(curve: i32, slope: f32) -> Vec { + let mut a = plain(1, 0.0, 4800.0, 0.0, 9600.0); + let mut b = plain(2, 3840.0, 4800.0, 0.0, 9600.0); + link(&mut a, &mut b, curve, slope); + alloc::vec![a, b] + } + + #[test] + fn equal_power_on_uncorrelated_noise_keeps_loudness() { + let (x, y) = (noise(240_000, 7), noise(240_000, 99)); + let level = db(mean_square(&x[..48_000])); + let report = |curve: i32, slope: f32| { + let out = render(&unrelated_pair(curve, slope), &[&x, &y], 216_000, 128); + let mut worst = 0.0f64; + let mut centre = 0.0f64; + for start in (96_000 - 4_800..120_000 + 4_800).step_by(1_200) { + let deviation = db(mean_square(&out[start..start + 4_800])) - level; + if deviation.abs() > worst.abs() { worst = deviation; } + if start == 108_000 - 2_400 { centre = deviation; } + } + (worst, centre) + }; + let (worst, _) = report(FADE_EQUAL_POWER, 0.5); + std::println!("equal power, uncorrelated noise: worst 0.1 s window level deviation through the overlap {worst:.3} dB"); + assert!(worst.abs() < 0.5, "equal power keeps loudness: {worst}"); + let (_, centre) = report(FADE_BEND, 0.5); + std::println!("linear on the same uncorrelated noise: {centre:.3} dB at the centre (why equal power is the default)"); + assert!(centre < -2.5); + } + + #[test] + fn linear_on_correlated_material_keeps_level() { + let source = noise(240_000, 11); + let level = db(mean_square(&source[..48_000])); + let mut a = plain(1, 0.0, 3360.0, 0.0, 9600.0); + let mut b = plain(2, 2400.0, 2400.0, 2400.0, 9600.0); + link(&mut a, &mut b, FADE_BEND, 0.5); + let out = render(&[a, b], &[&source, &source], 120_000, 128); + let worst = (1_000..118_000).map(|i| (out[i] - source[i]).abs()).fold(0.0f32, f32::max); + let zone = db(mean_square(&out[60_000..84_000])) - db(mean_square(&source[60_000..84_000])); + std::println!("linear crossfade over a cut of the same material: max sample error vs the uncut source {worst:.2e}, zone level {zone:.4} dB (source {level:.2} dB)"); + assert!(zone.abs() < 0.5 && worst < 1e-3); + } + + #[test] + fn s_curve_endpoints_and_monotonic() { + for slope in [0.0f32, 0.25, 0.5, 0.75, 1.0] { + assert!(s_curve_at(0.0, slope).abs() < 1e-7 && (s_curve_at(1.0, slope) - 1.0).abs() < 1e-7); + assert!((s_curve_at(0.5, slope) - 0.5).abs() < 1e-6); + let mut last = -1.0f32; + for k in 0..=10_000 { + let value = s_curve_at(k as f32 / 10_000.0, slope); + assert!(value >= last, "monotonic at slope {slope}"); + last = value; + } + } + std::println!("s-curve: f(0)=0, f(1)=1, f(0.5)=0.5, monotonic over 10001 points at 5 slopes"); + } + + #[test] + fn no_clicks_at_edit_points() { + let sine: Vec = (0..240_000).map(|i| (i as f32 * 440.0 * core::f32::consts::TAU / 48_000.0).sin() * 0.5).collect(); + let other: Vec = (0..240_000).map(|i| (i as f32 * 440.0 * core::f32::consts::TAU / 48_000.0 + 2.0).sin() * 0.5).collect(); + let pure = (1..48_000).map(|i| (sine[i] - sine[i - 1]).abs()).fold(0.0f32, f32::max); + for curve in [FADE_BEND, FADE_S_CURVE, FADE_EQUAL_POWER] { + let out = render(&unrelated_pair(curve, 0.5), &[&sine, &other], 216_000, 128); + let step = (1..216_000).map(|i| (out[i] - out[i - 1]).abs()).fold(0.0f32, f32::max); + let mut clip = plain(1, 960.0, 4800.0, 0.0, 9600.0); + clip.fade_in = 4.8; + clip.fade_out = 4.8; + clip.fade_in_curve = curve; + clip.fade_out_curve = curve; + let faded = render(&[clip], &[&sine], 168_000, 128); + let edge = (1..168_000).map(|i| (faded[i] - faded[i - 1]).abs()).fold(0.0f32, f32::max); + std::println!("curve {curve}: crossfade max sample step {step:.4}, 2.5 ms clip fades max step {edge:.4} (pure sine {pure:.4}, a hard cut at the peak would step 0.5)"); + assert!(step <= pure * 1.3 && edge <= pure * 1.3, "no click: {step} {edge} vs {pure}"); + } + } + + #[test] + fn anti_click_fades_a_frame_zero_start_only_when_flagged() { + let dc: Vec = alloc::vec![0.5; 96_000]; + let mut clip = plain(1, 480.0, 960.0, 0.0, 9600.0); + let upstream = render(&[clip.clone()], &[&dc], 48_000, 128); + clip.anti_click = true; + let declicked = render(&[clip], &[&dc], 48_000, 128); + let onset = 12_000usize; + let ramp = (onset..onset + 400).take_while(|i| declicked[*i] < 0.4999).count(); + std::println!("frame-0 start: upstream first sample {:.3} (hard onset), anti-click first sample {:.4}, ramp {ramp} samples (3 ms = 144)", upstream[onset], declicked[onset]); + assert_eq!(upstream[onset], 0.5); + assert!(declicked[onset] < 0.01 && (140..=146).contains(&ramp)); + assert!(upstream[onset + 200..].iter().zip(declicked[onset + 200..].iter()).all(|(a, b)| a.to_bits() == b.to_bits())); + } + + #[test] + fn render_is_identical_for_any_block_partition() { + let (x, y) = (noise(240_000, 3), noise(240_000, 5)); + let regions = unrelated_pair(FADE_EQUAL_POWER, 0.5); + let reference = render(®ions, &[&x, &y], 216_000, 128); + let again = render(®ions, &[&x, &y], 216_000, 128); + assert!(reference.iter().zip(again.iter()).all(|(p, q)| p.to_bits() == q.to_bits()), "deterministic"); + for quantum in [64usize, 32, 100] { + let other = render(®ions, &[&x, &y], 216_000, quantum); + let worst = reference.iter().zip(other.iter()).map(|(p, q)| (p - q).abs()).fold(0.0f32, f32::max); + std::println!("block size {quantum} vs 128: max difference {worst:.2e}"); + assert!(worst < 1e-4, "partitions agree to float rounding: {worst}"); + } + } + + #[test] + fn old_projects_without_crossfades_are_bit_identical() { + let upstream = |position: f64, duration: f64, region: &AudioRegion| -> f32 { + let mut fade_in = 1.0f32; + let mut fade_out = 1.0f32; + if region.fade_in > 0.0 && position < region.fade_in { fade_in = normalized_at((position / region.fade_in) as f32, region.fade_in_slope); } + if region.fade_out > 0.0 { + let start = duration - region.fade_out; + if position > start { fade_out = 1.0 - normalized_at(((position - start) / region.fade_out) as f32, region.fade_out_slope); } + } + fade_in.min(fade_out) + }; + let mut count = 0usize; + for (fade_in, fade_out, in_slope, out_slope) in [(480.0, 960.0, 0.75, 0.25), (100.0, 100.0, 0.5, 0.5), (2000.0, 1500.0, 0.1, 0.9)] { + let mut region = plain(1, 0.0, 4800.0, 0.0, 9600.0); + region.fade_in = fade_in; + region.fade_out = fade_out; + region.fade_in_slope = in_slope; + region.fade_out_slope = out_slope; + for k in 0..=48_000 { + let position = k as f64 * 0.1; + assert_eq!(fade_gain(position, 4800.0, ®ion, 0.0, false, false).to_bits(), upstream(position, 4800.0, ®ion).to_bits()); + count += 1; + } + } + let source = noise(240_000, 13); + let plain_overlap = alloc::vec![plain(1, 0.0, 2880.0, 0.0, 9600.0), plain(2, 1920.0, 2880.0, 1920.0, 9600.0)]; + let out = render(&plain_overlap, &[&source, &source], 120_000, 128); + let region_a_silent_after_b = (49_500..72_000).all(|i| (out[i] - source[i]).abs() < 1e-6); + std::println!("upstream fade gain reproduced bit for bit at {count} points; an unflagged overlap still plays only the later region ({region_a_silent_after_b})"); + assert!(region_a_silent_after_b); + } +} diff --git a/crates/engine/src/audio_unit/mod.rs b/crates/engine/src/audio_unit/mod.rs index ccce8dad4..75a3e413b 100644 --- a/crates/engine/src/audio_unit/mod.rs +++ b/crates/engine/src/audio_unit/mod.rs @@ -325,7 +325,8 @@ pub(crate) struct TapeWired { pub(crate) pre_strip_node: NodeId, pub(crate) strip_id: NodeId, pub(crate) strip_output: SharedAudioBuffer, - pub(crate) edges: Vec<(NodeId, NodeId)> // player -> fx0 -> ... -> strip + pub(crate) edges: Vec<(NodeId, NodeId)>, // player -> fx0 -> ... -> strip + pub(crate) output_gain: Option // ESC: TapeDeviceBox field 6, applied in the player } /// A held device processor, kept alive across rewires so its DSP state (voices, delay tails, filter history) @@ -570,7 +571,10 @@ pub(crate) struct DeviceParams { // the table entry alive (Weak-swept on drop); teardown zeroes the registry ptr + frees the id. pub(crate) broadcast_slots: Vec<(u32, engine_env::telemetry::BroadcastSlot)>, // the groove slots the device observed (`observe_groove`), freed on teardown - pub(crate) groove_slots: Vec + pub(crate) groove_slots: Vec, + // ESC: an INSTRUMENT's output-gain observation (field 6), host-only (never delivered to the device); + // re-observed with the device's params in `rebind_one`, released with them on teardown + pub(crate) output_gain: Option } /// A device node that takes resolved sidechain buffers: an audio effect, or an instrument with an audio input. @@ -1152,6 +1156,9 @@ impl Engine { } Wired::Tape(tape) => { self.graph.unsubscribe(tape.enabled_sub); + if let Some(gain) = tape.output_gain { + self.release_output_gain(gain); + } self.output_registry.remove(&Address::of(tape.instrument_uuid, vec![])); for (source, target) in &tape.edges { self.context.remove_edge(*source, *target); diff --git a/crates/engine/src/audio_unit/params.rs b/crates/engine/src/audio_unit/params.rs index a653b1b1b..6df30d1f6 100644 --- a/crates/engine/src/audio_unit/params.rs +++ b/crates/engine/src/audio_unit/params.rs @@ -431,7 +431,7 @@ impl Engine { None }; sink.set_params(handles.clone(), armed); - DeviceParams {device_uuid, reg, state_ptr, sink, paths, handles, field_subs, collections, observe_subs, pointer_field_subs, sidechain_paths, param_hub_sub, sample_hub_sub, sample_subs, records, broadcast_slots, groove_slots} + DeviceParams {device_uuid, reg, state_ptr, sink, paths, handles, field_subs, collections, observe_subs, pointer_field_subs, sidechain_paths, param_hub_sub, sample_hub_sub, sample_subs, records, broadcast_slots, groove_slots, output_gain: None} } /// Wire each field a device asked to observe. A PLAIN observation (`observe_field`, `target_key == 0`): @@ -774,6 +774,9 @@ impl Engine { collection.terminate(&mut self.graph); } params.groove_slots.into_iter().for_each(crate::release_groove_slot); + if let Some(gain) = params.output_gain { + self.release_output_gain(gain); + } for (id, slot) in params.broadcast_slots { if let Some(entry) = unsafe { DEVICE_BROADCASTS.get() }.get_mut(id as usize) { *entry = (0, false); @@ -858,11 +861,15 @@ impl Engine { composite.binding.for_each_params(&mut |params| self.rebind_one(params, &invalidate, position)); composite.binding.for_each_midi_out(&mut |device| self.rebind_midi_out_cc(device, &invalidate)); self.rebind_composite_strips(&mut composite.binding, &invalidate); + self.rebind_composite_output_gains(&mut composite.binding, &invalidate); for member in &mut composite.audio { self.rebind_member(member, &invalidate, position); } } Wired::Tape(tape) => { + if let Some(gain) = tape.output_gain.as_mut() { + self.rebind_output_gain(gain, &invalidate); + } for member in &mut tape.audio { self.rebind_member(member, &invalidate, position); } @@ -957,6 +964,10 @@ impl Engine { params.handles = handles; params.field_subs = field_subs; params.collections = collections; + if let Some(mut gain) = params.output_gain.take() { + self.rebind_output_gain(&mut gain, invalidate); + params.output_gain = Some(gain); + } } /// Inject a live note signal into the unit identified by its `AudioUnitBox` uuid. Called OFF-render diff --git a/crates/engine/src/audio_unit/tests.rs b/crates/engine/src/audio_unit/tests.rs index e65034eb0..843bc1276 100644 --- a/crates/engine/src/audio_unit/tests.rs +++ b/crates/engine/src/audio_unit/tests.rs @@ -664,7 +664,7 @@ fn a_parameter_write_in_the_same_transaction_as_an_enabled_toggle_reaches_the_de device_uuid: INSTR, reg, state_ptr: 0, sink: ParamNode::Audio(Rc::new(RefCell::new(StubSink))), paths: alloc::vec![alloc::vec![PATH]], handles, field_subs, collections, observe_subs: Vec::new(), pointer_field_subs: Vec::new(), sidechain_paths: Vec::new(), sample_subs: Vec::new(), - param_hub_sub: None, sample_hub_sub: None, records: None, broadcast_slots: Vec::new(), groove_slots: Vec::new() + param_hub_sub: None, sample_hub_sub: None, records: None, broadcast_slots: Vec::new(), groove_slots: Vec::new(), output_gain: None }), _ => panic!("expected a leaf chain") } @@ -741,7 +741,7 @@ fn read_audio_region_reads_span_file_gain_and_fades() { (7, FieldValue::Float32(0.5)), // waveform offset (seconds) (14, FieldValue::Boolean(false)), // mute (17, FieldValue::Float32(-6.0)), // gain (dB) - (AUDIO_REGION_FADING_KEY, FieldValue::Object(fading)) + (AUDIO_REGION_FADING_KEY, FieldValue::Object(fading.clone())) ]) ]); let region = read_audio_region(&graph, REGION, &TempoMap::fixed(120.0)).expect("a region with a file resolves"); @@ -756,6 +756,23 @@ fn read_audio_region_reads_span_file_gain_and_fades() { assert_eq!(region.fade_out, 240.0); assert_eq!(region.fade_in_slope, 0.75); assert_eq!(region.fade_out_slope, 0.25); + assert_eq!((region.fade_in_curve, region.fade_out_curve, region.crossfade_out), (0, 0, false), "an old project reads the upstream curves"); + let mut crossfading = fading.clone(); + crossfading.insert(5u16, FieldValue::Int32(2)); + crossfading.insert(6u16, FieldValue::Int32(1)); + crossfading.insert(7u16, FieldValue::Boolean(true)); + let graph = BoxGraph::from_boxes(vec![ + graph_box(FILE, "AudioFileBox", &[]), + graph_box(REGION, "AudioRegionBox", &[ + (2, FieldValue::Pointer(Some(Address::box_of(FILE)))), + (10, FieldValue::Int32(0)), + (11, FieldValue::Float32(3840.0)), + (13, FieldValue::Float32(3840.0)), + (AUDIO_REGION_FADING_KEY, FieldValue::Object(crossfading)) + ]) + ]); + let region = read_audio_region(&graph, REGION, &TempoMap::fixed(120.0)).expect("a region with a file resolves"); + assert_eq!((region.fade_in_curve, region.fade_out_curve, region.crossfade_out), (2, 1, true)); // A region with no file pointer is skipped (never played), not a panic. let orphan = BoxGraph::from_boxes(vec![graph_box(REGION, "AudioRegionBox", &[(10, FieldValue::Int32(0))])]); assert!(read_audio_region(&orphan, REGION, &TempoMap::fixed(120.0)).is_none()); @@ -2625,7 +2642,7 @@ fn a_device_param_automation_rebind_keeps_its_ui_broadcast_alive() { sink: ParamNode::Audio(Rc::new(RefCell::new(StubSink))), paths: alloc::vec![alloc::vec![PATH]], handles, field_subs, collections, observe_subs: Vec::new(), pointer_field_subs: Vec::new(), sidechain_paths: Vec::new(), - param_hub_sub: None, sample_hub_sub: None, sample_subs: Vec::new(), records: None, broadcast_slots: Vec::new(), groove_slots: Vec::new() + param_hub_sub: None, sample_hub_sub: None, sample_subs: Vec::new(), records: None, broadcast_slots: Vec::new(), groove_slots: Vec::new(), output_gain: None }; let entry_ptr = |engine: &Engine| (0..engine.broadcasts.len()).find_map(|index| { let entry = engine.broadcasts.entry(index).expect("entry"); @@ -5793,3 +5810,111 @@ fn a_bytes_field_is_delivered_as_bytes_with_its_length() { assert_eq!(delivered.len(), 1); assert_eq!((delivered[0].0, delivered[0].1, delivered[0].3), (4, abi::FIELD_KIND_BYTES, 0), "an empty field is an empty slice"); } + +// ---- ESC: the uniform instrument OUTPUT GAIN (field 6) ---------------------------------------------------- + +const OG_KEY: u16 = engine_env::output_gain::OUTPUT_GAIN_KEY; + +fn og_unit_graph(instrument_fields: &[(u16, FieldValue)]) -> BoxGraph { + let mut fields = vec![(HOST_KEY, FieldValue::Pointer(Some(Address::of(UNIT, vec![UNIT_INPUT_KEY]))))]; + fields.extend_from_slice(instrument_fields); + BoxGraph::from_boxes(vec![ + graph_box(UNIT, "AudioUnitBox", &[ + (UNIT_TRACKS_KEY, FieldValue::Hook), (UNIT_MIDI_KEY, FieldValue::Hook), + (UNIT_INPUT_KEY, FieldValue::Hook), (UNIT_AUDIO_KEY, FieldValue::Hook) + ]), + graph_box(INSTR, "TestInstrument", &fields) + ]) +} + +fn leaf_output_gain(unit: &AudioUnitBinding) -> Option> { + match unit.wired.as_ref().expect("wired") { + Wired::Leaf(chain) => chain.instrument.params.as_ref().and_then(|params| params.output_gain.as_ref()) + .map(|gain| gain.source.clone()), + _ => panic!("expected a leaf chain") + } +} + +#[test] +fn an_instrument_binds_its_output_gain_field_and_follows_edits() { + let mut engine = engine_with_devices(); + engine.graph = og_unit_graph(&[(OG_KEY, FieldValue::Float32(-6.0))]); + let mut unit = engine.build_unit(UNIT); + engine.reconcile_one(&mut unit); + let source = leaf_output_gain(&unit).expect("field 6 present: bound"); + assert_eq!(source.static_db(), -6.0); + assert!(!source.is_automated(), "no Value track: the static field rules"); + engine.graph.transaction(&[Update::Primitive {address: Address::of(INSTR, vec![OG_KEY]), + old: FieldValue::Float32(-6.0), new: FieldValue::Float32(3.5)}], &engine.registry).expect("edit"); + engine.reconcile_one(&mut unit); + assert_eq!(source.static_db(), 3.5, "a knob edit reaches the node's source live"); + // an automation re-bind keeps the SAME source (the node needs no re-wire) and re-reads the field + engine.rebind_automation(&mut unit); + let rebound = leaf_output_gain(&unit).expect("still bound"); + assert!(Rc::ptr_eq(&source, &rebound)); + assert_eq!(rebound.static_db(), 3.5); + engine.teardown_unit(unit); + assert_eq!(source.static_db(), 0.0, "released on teardown (field cleared -> unity)"); +} + +#[test] +fn an_old_graph_without_field_6_binds_nothing() { + let mut engine = engine_with_devices(); + engine.graph = og_unit_graph(&[]); + let mut unit = engine.build_unit(UNIT); + engine.reconcile_one(&mut unit); + assert!(leaf_output_gain(&unit).is_none(), "no field: the node stays a pure pass-through"); +} + +#[test] +fn a_composite_binds_its_own_output_gain_on_the_sum() { + let mut engine = composite_engine(); + // field 6 on the composite box by rebuilding the graph with it + engine.graph = BoxGraph::from_boxes(vec![ + graph_box(UNIT, "AudioUnitBox", &[ + (UNIT_TRACKS_KEY, FieldValue::Hook), (UNIT_MIDI_KEY, FieldValue::Hook), (UNIT_INPUT_KEY, FieldValue::Hook), (UNIT_AUDIO_KEY, FieldValue::Hook) + ]), + graph_box(COMPOSITE, "TestComposite", &[ + (HOST_KEY, FieldValue::Pointer(Some(Address::of(UNIT, vec![UNIT_INPUT_KEY])))), + (CHILDREN_FIELD, FieldValue::Hook), (OG_KEY, FieldValue::Float32(-12.0)) + ]), + graph_box(CHILD_A, "TestInstrument", &[ + (HOST_KEY, FieldValue::Pointer(Some(Address::of(COMPOSITE, vec![CHILDREN_FIELD])))), + (CHILD_ENABLED_KEY, FieldValue::Boolean(true)) + ]) + ]); + let mut unit = engine.build_unit(UNIT); + engine.reconcile_one(&mut unit); + let source = match unit.wired.as_ref().expect("wired") { + Wired::Composite(composite) => Engine::output_gain_source(&composite.binding).expect("composite field 6 bound"), + _ => panic!("expected a composite") + }; + assert_eq!(source.static_db(), -12.0); + engine.rebind_automation(&mut unit); + assert_eq!(source.static_db(), -12.0, "survives an automation re-bind"); + engine.teardown_unit(unit); + assert_eq!(source.static_db(), 0.0, "released with the composite"); +} + +#[test] +fn a_tape_binds_its_output_gain_in_the_player() { + const TAPE: Uuid = [65u8; 16]; + let mut engine = engine_with_devices(); + engine.graph = BoxGraph::from_boxes(vec![ + graph_box(UNIT, "AudioUnitBox", &[ + (UNIT_TRACKS_KEY, FieldValue::Hook), (UNIT_MIDI_KEY, FieldValue::Hook), + (UNIT_INPUT_KEY, FieldValue::Hook), (UNIT_AUDIO_KEY, FieldValue::Hook) + ]), + graph_box(TAPE, "TapeDeviceBox", &[(HOST_KEY, FieldValue::Pointer(Some(Address::of(UNIT, vec![UNIT_INPUT_KEY])))), + (OG_KEY, FieldValue::Float32(6.0))]) + ]); + let mut unit = engine.build_unit(UNIT); + engine.reconcile_one(&mut unit); + let source = match unit.wired.as_ref().expect("wired") { + Wired::Tape(tape) => tape.output_gain.as_ref().expect("tape field 6 bound").source.clone(), + _ => panic!("expected a tape chain") + }; + assert_eq!(source.static_db(), 6.0); + engine.teardown_unit(unit); + assert_eq!(source.static_db(), 0.0); +} diff --git a/crates/engine/src/audio_unit/tracks/audio.rs b/crates/engine/src/audio_unit/tracks/audio.rs index 7ce9d9098..561011ff6 100644 --- a/crates/engine/src/audio_unit/tracks/audio.rs +++ b/crates/engine/src/audio_unit/tracks/audio.rs @@ -89,7 +89,7 @@ pub(crate) const AUDIO_REGION_TIMEBASE_KEY: u16 = 4; // "musical" (ppqn) pub(crate) const AUDIO_REGION_WAVEFORM_OFFSET_KEY: u16 = 7; // seconds into the source where playback reads pub(crate) const AUDIO_REGION_MUTE_KEY: u16 = 14; pub(crate) const AUDIO_REGION_GAIN_KEY: u16 = 17; // decibels -pub(crate) const AUDIO_REGION_FADING_KEY: u16 = 18; // object: 1 in, 2 out (ppqn), 3 in-slope, 4 out-slope (ratio) +pub(crate) const AUDIO_REGION_FADING_KEY: u16 = 18; // object: 1 in, 2 out (ppqn), 3 in-slope, 4 out-slope (ratio), 5/6 in/out-curve (enum), 7 crossfade-out, 8 anti-click pub(crate) const AUDIO_REGION_PLAYMODE_KEY: u16 = 8; // -> an AudioPitchStretchBox / AudioTimeStretchBox, or unset (native) pub(crate) const PITCH_STRETCH_WARP_HUB_KEY: u16 = 1; // AudioPitchStretchBox.warp-markers hub // Every play-mode box (Pitch / TimeStretch / Signalsmith) exposes its warp-markers hub at field 1, so the @@ -129,6 +129,10 @@ pub(crate) struct AudioRegion { pub(crate) fade_out: f64, // ppqn pub(crate) fade_in_slope: f32, // 0..1 ratio pub(crate) fade_out_slope: f32, // 0..1 ratio + pub(crate) fade_in_curve: i32, // math::curve FADE_* kind (0 = upstream bend) + pub(crate) fade_out_curve: i32, + pub(crate) crossfade_out: bool, // the tail overlaps the next region as a crossfade: keep visiting it + pub(crate) anti_click: bool, // a frame-0 start gets a 3 ms declick (new clips only; old projects read false) // PitchStretch play-mode warp markers (content ppqn -> source seconds), sorted by ppqn. EMPTY = no // PitchStretch play-mode (native, or a TimeStretch play-mode — see `time_stretch`). pub(crate) warp: Vec<(f64, f64)>, @@ -146,6 +150,11 @@ pub(crate) struct AudioRegion { impl Span for AudioRegion { fn position(&self) -> f64 { self.position } fn duration(&self) -> f64 { self.duration } + fn overlaps_successor(&self) -> bool { self.crossfade_out } +} + +pub(crate) fn region_int(graph: &BoxGraph, uuid: Uuid, path: &[u16]) -> i32 { + graph.field_value(&Address::of(uuid, path.to_vec())).and_then(|value| value.as_int32()).unwrap_or(0) } pub(crate) fn region_float(graph: &BoxGraph, uuid: Uuid, path: &[u16]) -> f32 { @@ -185,6 +194,10 @@ pub(crate) fn read_audio_region(graph: &BoxGraph, region_uuid: Uuid, tempo_map: fade_out: region_float(graph, region_uuid, &[AUDIO_REGION_FADING_KEY, 2]) as f64, fade_in_slope: region_float(graph, region_uuid, &[AUDIO_REGION_FADING_KEY, 3]), fade_out_slope: region_float(graph, region_uuid, &[AUDIO_REGION_FADING_KEY, 4]), + fade_in_curve: region_int(graph, region_uuid, &[AUDIO_REGION_FADING_KEY, 5]), + fade_out_curve: region_int(graph, region_uuid, &[AUDIO_REGION_FADING_KEY, 6]), + crossfade_out: graph.field_value(&Address::of(region_uuid, vec![AUDIO_REGION_FADING_KEY, 7])).and_then(|value| value.as_bool()).unwrap_or(false), + anti_click: graph.field_value(&Address::of(region_uuid, vec![AUDIO_REGION_FADING_KEY, 8])).and_then(|value| value.as_bool()).unwrap_or(false), warp: read_warp_markers(graph, region_uuid), time_stretch, signalsmith: read_signalsmith(graph, region_uuid), @@ -394,6 +407,10 @@ pub(crate) fn read_audio_clip(graph: &BoxGraph, clip_uuid: Uuid, tempo_map: &Tem fade_out: 0.0, fade_in_slope: 0.0, fade_out_slope: 0.0, + fade_in_curve: 0, + fade_out_curve: 0, + crossfade_out: false, + anti_click: false, warp: read_warp_markers(graph, clip_uuid), time_stretch, signalsmith: read_signalsmith(graph, clip_uuid), diff --git a/crates/engine/src/audio_unit/wiring.rs b/crates/engine/src/audio_unit/wiring.rs index 640be3b07..9c02ce597 100644 --- a/crates/engine/src/audio_unit/wiring.rs +++ b/crates/engine/src/audio_unit/wiring.rs @@ -241,6 +241,9 @@ impl Engine { // A live old meter slot then dedup-SKIPS the fresh meter registration (see `Broadcasts::register`), // and the UI keeps reading the old, no-longer-processed slot — the tape meter freezes. self.graph.unsubscribe(tape.enabled_sub); + if let Some(gain) = tape.output_gain { + self.release_output_gain(gain); + } for (source, target) in &tape.edges { self.context.remove_edge(*source, *target); } @@ -259,6 +262,11 @@ impl Engine { // EFFECTS monitoring: the armed tape sums its staged live input into its OWN output, so the tape // device (meter + a side-chain tapping it) carries the live signal and it flows through the fx chain. player.borrow_mut().set_monitor(self.monitor_channels_of(&unit.unit)); + // ESC: the TapeDeviceBox's OUTPUT GAIN (field 6), applied inside the player (pre fx / strip / sidechain tap) + let output_gain = self.bind_output_gain(instrument_uuid, invalidate); + if let Some(gain) = &output_gain { + player.borrow_mut().set_output_gain(gain.source.clone()); + } let player_output = player.borrow().audio_output(); let player_id = self.context.register_processor(player.clone()); let (stretch_regions, total_regions) = tape_region_counts(&unit.audio_track_sets); @@ -328,7 +336,7 @@ impl Engine { edges.push((output_node, strip_id)); // The strip's output is routed to the unit's OUTPUT bus by `resolve_outputs` (not wired to master here). self.output_registry.register(Address::of(unit.unit, vec![]), strip_output.clone(), strip_id); - unit.wired = Some(Wired::Tape(TapeWired {player, enabled_sub, player_id, instrument_uuid, audio: audio_members, pre_strip: output, pre_strip_node: output_node, strip_id, strip_output, edges})); + unit.wired = Some(Wired::Tape(TapeWired {player, enabled_sub, player_id, instrument_uuid, audio: audio_members, pre_strip: output, pre_strip_node: output_node, strip_id, strip_output, edges, output_gain})); } /// The MIDI-OUTPUT instrument path (TS `MIDIOutputDeviceProcessor`, engine-side like the tape): the @@ -885,8 +893,13 @@ impl Engine { let instrument = Rc::new(RefCell::new(PluginInstrument::new(self.sample_rate, device, uuid, self.box_type_name(uuid)))); let state_ptr = instrument.borrow().state_ptr(); let sink: Rc> = instrument.clone(); - let params = self.bind_device(uuid, device, state_ptr, ParamNode::Audio(sink), invalidate); + let mut params = self.bind_device(uuid, device, state_ptr, ParamNode::Audio(sink), invalidate); refresh_params(¶ms.handles, params.reg, params.state_ptr, self.transport.position()); // joiner only + // ESC: the instrument box's OUTPUT GAIN (field 6), applied inside the node (pre the unit's fx) + params.output_gain = self.bind_output_gain(uuid, invalidate); + if let Some(gain) = ¶ms.output_gain { + instrument.borrow_mut().set_output_gain(gain.source.clone()); + } let output = instrument.borrow().audio_output(); let node_id = self.context.register_processor(instrument.clone()); self.context.set_label(node_id, device_label(&self.graph, &uuid)); diff --git a/crates/engine/src/composite.rs b/crates/engine/src/composite.rs index 974582b3f..c63a29cca 100644 --- a/crates/engine/src/composite.rs +++ b/crates/engine/src/composite.rs @@ -63,7 +63,9 @@ pub(crate) struct CompositeBinding { // EMPTY cells: no child is built, but the hosted-instrument observation stays so an arrival enqueues the unit. pending: Vec, // A silent parent layer: this composite's MIDI Output layers drop their starts too. - inherited_silent: bool + inherited_silent: bool, + // ESC: the composite box's OUTPUT GAIN (field 6), applied on `sum`; None when the box has no such field + output_gain: Option } impl CompositeBinding { @@ -416,7 +418,11 @@ impl Engine { let sum_meter = sum.borrow_mut().meter_slot(self.sample_rate); self.broadcasts.register(composite_uuid, &[], crate::broadcast::PACKAGE_FLOAT_ARRAY, &sum_meter); } - let mut binding = CompositeBinding {spec: spec.clone(), composite_uuid, children, sum, sum_id, sum_buffer, members: Vec::new(), unit_midi_members, unit_midi, pending: Vec::new(), inherited_silent: false}; + let mut binding = CompositeBinding {spec: spec.clone(), composite_uuid, children, sum, sum_id, sum_buffer, members: Vec::new(), unit_midi_members, unit_midi, pending: Vec::new(), inherited_silent: false, output_gain: None}; + binding.output_gain = self.bind_output_gain(composite_uuid, invalidate); + if let Some(gain) = &binding.output_gain { + binding.sum.borrow_mut().set_output_gain(gain.source.clone(), self.sample_rate); + } self.reconcile_composite_children(&mut binding, track_sets, signal, invalidate); binding } @@ -985,6 +991,28 @@ impl Engine { observation.terminate(&mut self.graph); } binding.children.terminate(&mut self.graph); + if let Some(gain) = binding.output_gain { + self.release_output_gain(gain); + } + } + + /// ESC (tests): the composite's bound output-gain source, if its box carries field 6. + #[cfg(test)] + pub(crate) fn output_gain_source(binding: &CompositeBinding) -> Option> { + binding.output_gain.as_ref().map(|gain| gain.source.clone()) + } + + /// ESC: re-observe the output gain of this composite and every nested one (an automation attach / detach). + pub(crate) fn rebind_composite_output_gains(&mut self, binding: &mut CompositeBinding, invalidate: &Rc) { + if let Some(mut gain) = binding.output_gain.take() { + self.rebind_output_gain(&mut gain, invalidate); + binding.output_gain = Some(gain); + } + for child in binding.members.iter_mut() { + if let ChildBody::Nested {binding: nested} | ChildBody::NestedCell {binding: nested, ..} = &mut child.body { + self.rebind_composite_output_gains(nested, invalidate); + } + } } /// What a CELL currently hosts (the first member of its instrument host): a registered composite, or a plugin diff --git a/crates/engine/src/effect_composite.rs b/crates/engine/src/effect_composite.rs index e8a496282..68c286cca 100644 --- a/crates/engine/src/effect_composite.rs +++ b/crates/engine/src/effect_composite.rs @@ -555,6 +555,11 @@ impl Engine { let strip_automation = Rc::new(StripAutomation::new()); let strip = Rc::new(RefCell::new(ChannelStripProcessor::new(strip_params.clone(), strip_automation.clone(), self.sample_rate))); + // A FREQUENCY split's band pan is a STEREO pan (the band is moved, not half of it silenced, the ESC fix + // for bands collapsing / losing a side). FX stacks and the L/R split keep the track-style balance. + if matches!(spec.distributor, Distributor::Frequency) { + strip.borrow_mut().set_stereo_pan(true); + } let strip_output = strip.borrow().audio_output(); let strip_id = self.context.register_processor(strip.clone()); // Label the node with the entry's own label ("A", "L", ...) so a graph dump names the branch. diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 02c374a5c..20b2ce8e7 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -422,6 +422,7 @@ mod tempo_map; mod script_device; use audio_unit::{AudioUnitBinding, Members}; mod composite; +mod output_gain; mod effect_composite; mod param_automation; diff --git a/crates/engine/src/output_gain.rs b/crates/engine/src/output_gain.rs new file mode 100644 index 000000000..c2a3660c0 --- /dev/null +++ b/crates/engine/src/output_gain.rs @@ -0,0 +1,82 @@ +//! ESC: the uniform instrument OUTPUT GAIN binding (instrument box field 6, `output-gain`, dB). The DSP lives in +//! `engine_env::output_gain` (`OutputGainStage`); this file is the engine half: observe field 6 (+ its +//! automation / modulation) on an instrument box and keep an `OutputGainSource` the source node reads. +//! +//! Who applies it (always the node that produces the instrument's own output, before the unit's audio-fx): +//! - a plugin instrument (every built-in synth / sampler, Apparat, Soundfont, WebCLAP): `PluginInstrument`; +//! - a composite (Playfield, Instrument Composite, nested ones): its child-sum `AudioBusProcessor`; +//! - the Tape device: the `AudioRegionPlayer`. +//! +//! A box WITHOUT a Float32 field 6 (a Playfield sample slot, an effect, an old graph that never carried the +//! field) gets no binding at all, so its node stays a pure pass-through. + +use alloc::rc::Rc; +use alloc::vec::Vec; +use bindings::value_collection::ValueCollection; +use boxgraph::address::{Address, Uuid}; +use boxgraph::subscription::SubscriptionId; +use engine_env::output_gain::{OutputGainSource, OUTPUT_GAIN_KEY, OUTPUT_GAIN_MAPPING}; +use crate::audio_unit::params::host_float; +use crate::Engine; + +/// The parameter id the output-gain observation is tagged with. Never delivered to a device (the handle is +/// host-only), it only labels the handle for the UI broadcast. +const OUTPUT_GAIN_PARAM_ID: u32 = 0x4f47; // "OG" + +/// One instrument box's live output-gain observation: the shared source the node reads + the subscriptions / +/// curve collections that keep it current. Released through `Engine::release_output_gain`. +pub(crate) struct OutputGainBinding { + pub(crate) box_uuid: Uuid, + pub(crate) source: Rc, + subs: Vec, + collections: Vec +} + +impl Engine { + /// Whether `box_uuid` carries the instrument output-gain field (Float32 at key 6). + pub(crate) fn has_output_gain(&self, box_uuid: Uuid) -> bool { + self.graph.field_value(&Address::of(box_uuid, alloc::vec![OUTPUT_GAIN_KEY])) + .and_then(|value| value.as_float32()).is_some() + } + + /// Observe field 6 of `box_uuid` (static value + automation + modulation). `None` when the box has no such + /// field (then the caller leaves its node untouched). + pub(crate) fn bind_output_gain(&mut self, box_uuid: Uuid, invalidate: &Rc) -> Option { + if !self.has_output_gain(box_uuid) { + return None; + } + let mut binding = OutputGainBinding {box_uuid, source: Rc::new(OutputGainSource::new()), subs: Vec::new(), collections: Vec::new()}; + self.observe_output_gain(&mut binding, invalidate); + Some(binding) + } + + /// Re-observe after an automation attach / detach / curve edit (the source `Rc` is kept, so the node needs + /// no re-wiring). + pub(crate) fn rebind_output_gain(&mut self, binding: &mut OutputGainBinding, invalidate: &Rc) { + self.drop_output_gain_observers(binding); + self.observe_output_gain(binding, invalidate); + } + + /// Drop the observation (teardown). The node keeps its last value, but it is being removed with it. + pub(crate) fn release_output_gain(&mut self, mut binding: OutputGainBinding) { + self.drop_output_gain_observers(&mut binding); + binding.source.clear(); + } + + fn drop_output_gain_observers(&mut self, binding: &mut OutputGainBinding) { + for sub in core::mem::take(&mut binding.subs) { + self.graph.unsubscribe(sub); + } + for collection in core::mem::take(&mut binding.collections) { + collection.terminate(&mut self.graph); + } + } + + fn observe_output_gain(&mut self, binding: &mut OutputGainBinding, invalidate: &Rc) { + let (handle, resolver) = self.observe_field_automation(binding.box_uuid, &[OUTPUT_GAIN_KEY], OUTPUT_GAIN_PARAM_ID, + &mut binding.subs, &mut binding.collections, invalidate, + |value, kind, modulation| host_float(value, kind, modulation, &OUTPUT_GAIN_MAPPING)); + *binding.source.field.borrow_mut() = Some(handle.field.clone()); + *binding.source.automation.borrow_mut() = resolver; + } +} diff --git a/crates/engine/src/plugin_instrument.rs b/crates/engine/src/plugin_instrument.rs index e3a2e6f85..1ef9992e4 100644 --- a/crates/engine/src/plugin_instrument.rs +++ b/crates/engine/src/plugin_instrument.rs @@ -21,6 +21,7 @@ use engine_env::event_buffer::EventBuffer; use engine_env::event_receiver::EventReceiver; use engine_env::process_info::ProcessInfo; use engine_env::processor::Processor; +use engine_env::output_gain::{OutputGainSource, OutputGainStage}; use transport::transport::RENDER_QUANTUM; use crate::param_automation::{ParamHandle, ParamSink}; use crate::audio_unit::SidechainSink; @@ -73,7 +74,10 @@ pub(crate) struct PluginInstrument { #[allow(dead_code)] out_offsets: Box<[u32]>, descriptor: Box<[u32]>, - latency: Rc> + latency: Rc>, + // ESC: the instrument box's OUTPUT GAIN (field 6), applied to the device output before it leaves the node. + // `None` = no field (untouched); 0 dB settled is a bit-exact pass-through either way. + output_gain: Option } impl PluginInstrument { @@ -129,7 +133,8 @@ impl PluginInstrument { device_state, out_offsets, descriptor, - latency + latency, + output_gain: None } } @@ -141,6 +146,11 @@ impl PluginInstrument { if changed { self.reset(); } } + /// ESC: attach the box's output-gain source (bound by the engine from field 6), once at build. + pub(crate) fn set_output_gain(&mut self, source: Rc) { + self.output_gain = Some(OutputGainStage::new(source, self.sample_rate)); + } + /// The peak/RMS broadcast slot of this instrument's output. pub(crate) fn meter_slot(&self) -> engine_env::telemetry::BroadcastSlot { self.meter.slot() @@ -274,6 +284,10 @@ impl Processor for PluginInstrument { self.type_name, boxgraph::address::uuid_to_string(&self.uuid), channel, frame)); } } + if let Some(stage) = self.output_gain.as_mut() { + let [left, right] = &mut self.device_output; + stage.process(info, left, right); + } { let mut output = self.output.borrow_mut(); for index in 0..RENDER_QUANTUM { diff --git a/crates/math/src/curve.rs b/crates/math/src/curve.rs index e7c04e83d..ca6f18548 100644 --- a/crates/math/src/curve.rs +++ b/crates/math/src/curve.rs @@ -3,7 +3,7 @@ //! `powf`/`logf` keep it `no_std` and identical across host and wasm. Inputs are segment-relative //! (small), so f32 is precise enough; absolute positions stay f64 in the transport. -use libm::{fabsf, logf, powf}; +use libm::{cosf, fabsf, logf, powf, sinf}; use crate::clamp; const EPSILON: f32 = 1.0e-7; @@ -47,3 +47,37 @@ pub fn slope_by_half(y0: f32, ym: f32, y1: f32) -> f32 { (ym - y0) / (y1 - y0) } } + +/// Fade curve kinds stored per fade (AudioRegionBox `fading.in-curve` / `out-curve`). `FADE_BEND` is the +/// upstream slope-shaped curve (Linear / Exponential / Logarithmic are bend presets), the default. +pub const FADE_BEND: i32 = 0; +pub const FADE_S_CURVE: i32 = 1; +pub const FADE_EQUAL_POWER: i32 = 2; + +/// Symmetric S-curve on [0,1]: `x^a / (x^a + (1-x)^a)` with `a = 1 + 4 * slope` (slope 0 is linear). +pub fn s_curve_at(x: f32, slope: f32) -> f32 { + let x = clamp(x, 0.0, 1.0); + let a = 1.0 + 4.0 * clamp(slope, 0.0, 1.0); + let rise = powf(x, a); + let fall = powf(1.0 - x, a); + if rise + fall <= 0.0 { 0.5 } else { rise / (rise + fall) } +} + +/// Fade-in gain at progress `x` in [0,1] for `curve`. `FADE_BEND` is bit-identical to `normalized_at`. +pub fn fade_in_gain(x: f32, slope: f32, curve: i32) -> f32 { + match curve { + FADE_S_CURVE => s_curve_at(x, slope), + FADE_EQUAL_POWER => sinf(clamp(x, 0.0, 1.0) * core::f32::consts::FRAC_PI_2), + _ => normalized_at(x, slope) + } +} + +/// Fade-out gain at progress `x` in [0,1] (1 at the fade start, 0 at the end). `FADE_BEND` is the upstream +/// `1 - normalized_at`; equal power is `cos`, so a matched in/out pair keeps `in^2 + out^2 = 1`. +pub fn fade_out_gain(x: f32, slope: f32, curve: i32) -> f32 { + match curve { + FADE_S_CURVE => 1.0 - s_curve_at(x, slope), + FADE_EQUAL_POWER => cosf(clamp(x, 0.0, 1.0) * core::f32::consts::FRAC_PI_2), + _ => 1.0 - normalized_at(x, slope) + } +} diff --git a/crates/stock-devices/device-crusher/src/lib.rs b/crates/stock-devices/device-crusher/src/lib.rs index 03d7a9225..16b802726 100644 --- a/crates/stock-devices/device-crusher/src/lib.rs +++ b/crates/stock-devices/device-crusher/src/lib.rs @@ -176,4 +176,66 @@ mod tests { assert!(nearest < 1e-4, "1-bit routing snaps to a step, got {sample}"); } } + + // ESC (item 7): the Crush knob now READS OUT a rate in Hz/kHz (CrusherRates.rateOf in the adapters). This + // pins the DSP to that readout: over one second the held value must change at the displayed rate. + // The display formula is restated here in f64 on purpose, independent of the DSP's f32 Exponential. + fn displayed_rate(crush: f64, sample_rate: f64) -> f64 { + let max = sample_rate * 0.5; + 20.0 * (max / 20.0).powf((1.0 - crush).clamp(0.0, 1.0)) + } + + fn crush_of(rate: f64, sample_rate: f64) -> f64 { + let max = sample_rate * 0.5; + if rate >= max {return 0.0} + 1.0 - (rate / 20.0).ln() / (max / 20.0).ln() + } + + fn counted_rate(crush: f64, sample_rate: f32) -> f64 { + let mut state: CrusherState = unsafe { core::mem::zeroed() }; + CrusherDevice::init(&mut state, sample_rate); + state.crush_id = 1; + state.bits_id = 2; + state.boost_id = 3; + state.mix_id = 4; + CrusherDevice::parameter_changed(&mut state, 1, ParamValue::Float(crush as f32)); + CrusherDevice::parameter_changed(&mut state, 2, ParamValue::Int(16)); + CrusherDevice::parameter_changed(&mut state, 3, ParamValue::Float(0.0)); + CrusherDevice::parameter_changed(&mut state, 4, ParamValue::Float(1.0)); + let frames = sample_rate as usize; // one second + let block = 128usize; + let (mut changes, mut last) = (0usize, f32::NAN); + let mut position = 0usize; + while position < frames { + let input: Vec = (0..block) + .map(|i| -0.9 + 1.8 * ((position + i) as f32 / frames as f32)).collect(); + let (mut left, mut right) = (vec![0.0f32; block], vec![0.0f32; block]); + state.dsp.process(&input, &input, &mut left, &mut right, 0, block); + for (i, sample) in left.iter().enumerate() { + if position + i >= frames {break} + if last.is_finite() && *sample != last {changes += 1} + last = *sample; + } + position += block; + } + changes as f64 + } + + #[test] + fn hold_rate_matches_the_displayed_rate() { + for sample_rate in [44_100.0f32, 48_000.0] { + let sr = sample_rate as f64; + let mut cases: Vec = vec![0.0, 0.25, 0.5, 0.75, 0.9, 1.0]; + for rate in [32_000.0, 22_050.0, 11_025.0, 8_000.0, 4_000.0] { + if rate < sr * 0.5 {cases.push(crush_of(rate, sr))} + } + for crush in cases { + let want = displayed_rate(crush, sr); + let got = counted_rate(crush, sample_rate); + let tolerance = (want * 0.002).max(1.5); + assert!((got - want).abs() <= tolerance, + "sr {sample_rate} crush {crush:.4}: displayed {want:.2} Hz, held value changed {got} times"); + } + } + } } diff --git a/crates/stock-devices/device-revamp/Cargo.toml b/crates/stock-devices/device-revamp/Cargo.toml index e19c53296..d0a348533 100644 --- a/crates/stock-devices/device-revamp/Cargo.toml +++ b/crates/stock-devices/device-revamp/Cargo.toml @@ -11,3 +11,6 @@ crate-type = ["cdylib", "lib"] abi = {path = "../../abi"} math = {path = "../../math"} dsp = {path = "../../dsp"} + +[dev-dependencies] +libm = "0.2" # ESC slope tests: the verbatim old shelf formula diff --git a/crates/stock-devices/device-revamp/src/lib.rs b/crates/stock-devices/device-revamp/src/lib.rs index 8f207d438..49791d394 100644 --- a/crates/stock-devices/device-revamp/src/lib.rs +++ b/crates/stock-devices/device-revamp/src/lib.rs @@ -7,7 +7,8 @@ //! //! Parameters per band (`RevampDeviceBox` sub-objects HP=10, LSh=11, LB=12, MB=13, HB=14, HSh=15, LP=16): each //! has enabled `[b,1]` (bool) + frequency `[b,10]` (exp 20..20000 Hz); Pass bands add order `[b,11]` (int 0..3 -> -//! 1..4 sections) + q `[b,12]` (exp 0.01..10); Shelf bands add gain `[b,11]` (linear -24..24 dB); Bell bands add +//! 1..4 sections) + q `[b,12]` (exp 0.01..10); Shelf bands add gain `[b,11]` (linear -24..24 dB) and +//! (ESC) slope `[b,12]` (exp 0.1..1.5, RBJ shelf slope S, default 1 = the old fixed shelf); Bell bands add //! gain `[b,11]` + q `[b,12]`. Coefficients are recomputed on any change; the host is mapping-agnostic. //! //! Exports: `kind()` (effect), `state_size()`, `process(desc_ptr)`, `init(...)`, `parameter_changed(...)`, @@ -53,6 +54,9 @@ const Q_HIGH_BELL: [u16; 2] = [14, 12]; const Q_LP: [u16; 2] = [16, 12]; const ORDER_HP: [u16; 2] = [10, 11]; const ORDER_LP: [u16; 2] = [16, 11]; +// ESC: shelf slope (RBJ S) on the two shelves, field 12 (the slot Pass/Bell use for q). Default 1 = old shelf. +const SLOPE_LOW_SHELF: [u16; 2] = [11, 12]; +const SLOPE_HIGH_SHELF: [u16; 2] = [15, 12]; const SPECTRUM_FIELD: [u16; 1] = [0xFFF]; // TS RevampDeviceBoxAdapter.spectrum const SPECTRUM_LOW_FIELD: [u16; 1] = [0xFFE]; // TS RevampDeviceBoxAdapter.spectrumLow @@ -62,6 +66,7 @@ const FREQ_MAPPING: Exponential = Exponential {min: 20.0, max: 20_000.0}; const Q_MAPPING: Exponential = Exponential {min: 0.01, max: 10.0}; const GAIN_MAPPING: Linear = Linear {min: -24.0, max: 24.0}; const ORDER_MAPPING: LinearInteger = LinearInteger {min: 0, max: 3}; +const SLOPE_MAPPING: Exponential = Exponential {min: 0.1, max: 1.5}; const NONE: u32 = u32::MAX; @@ -88,6 +93,7 @@ pub struct RevampState { q_id: [u32; 7], order_hp_id: u32, order_lp_id: u32, + slope_id: [u32; 2], // The editor's output spectrum (TS `adapter.spectrum` at `[0xFFF]`): the analyser runs (and the bins are // copied) only while the UI subscribes (`broadcast_active`), mirroring TS `#needsSpectrum`. analyser: AudioAnalyser, @@ -110,9 +116,10 @@ impl Revamp { let q = state.q[band] as f64; match band { HP => state.coeff[HP].set_highpass_params(frequency, q), - LOW_SHELF => state.coeff[LOW_SHELF].set_low_shelf_params(frequency, gain), + // the shelves keep their slope in the q slot (0 until delivered = the old fixed slope) + LOW_SHELF => state.coeff[LOW_SHELF].set_low_shelf_params_slope(frequency, gain, q), LOW_BELL | MID_BELL | HIGH_BELL => state.coeff[band].set_peaking_params(frequency, q, gain), - HIGH_SHELF => state.coeff[HIGH_SHELF].set_high_shelf_params(frequency, gain), + HIGH_SHELF => state.coeff[HIGH_SHELF].set_high_shelf_params_slope(frequency, gain, q), LP => state.coeff[LP].set_lowpass_params(frequency, q), _ => {} } @@ -165,6 +172,9 @@ impl AudioEffect for Revamp { state.q_id[LP] = abi::bind_parameter(&Q_LP); state.order_hp_id = abi::bind_parameter(&ORDER_HP); state.order_lp_id = abi::bind_parameter(&ORDER_LP); + state.slope_id = [abi::bind_parameter(&SLOPE_LOW_SHELF), abi::bind_parameter(&SLOPE_HIGH_SHELF)]; + state.q[LOW_SHELF] = 1.0; + state.q[HIGH_SHELF] = 1.0; state.analyser.init(0.0); state.spectrum_id = abi::bind_broadcast(&SPECTRUM_FIELD, NUM_BINS as u32); state.spectrum_ptr = 0; @@ -186,6 +196,13 @@ impl AudioEffect for Revamp { state.lp[1].set_order(order as usize); return; } + for (index, band) in [LOW_SHELF, HIGH_SHELF].into_iter().enumerate() { + if id == state.slope_id[index] { + state.q[band] = float_value(value, &SLOPE_MAPPING); + Revamp::recompute(state, band); + return; + } + } for band in 0..7 { if id == state.enabled_id[band] { state.enabled[band] = bool_value(value); @@ -302,7 +319,8 @@ pub extern "C" fn parameter_changed(state_ptr: u32, id: u32, kind: u32, value: f } /// Parity probe: the REAL value stored for a UNIT automation value, ids in `init` bind order: per-band -/// enabled/freq pairs (0..13), then the five gains (14..18), the five Qs (19..23), the two orders (24, 25). +/// enabled/freq pairs (0..13), then the five gains (14..18), the five Qs (19..23), the two orders (24, 25), the +/// two ESC shelf slopes (26 low, 27 high). #[no_mangle] pub extern "C" fn map_parameter(id: u32, unit: f32) -> f32 { let value = ParamValue::Unit(unit); @@ -315,6 +333,7 @@ pub extern "C" fn map_parameter(id: u32, unit: f32) -> f32 { 14..=18 => float_value(value, &GAIN_MAPPING), 19..=23 => float_value(value, &Q_MAPPING), 24 | 25 => int_value(value, &ORDER_MAPPING) as f32, + 26 | 27 => float_value(value, &SLOPE_MAPPING), _ => f32::NAN } } @@ -434,3 +453,135 @@ mod tests { assert!(out != input, "the chain alters the signal"); } } + +#[cfg(test)] +mod esc_slope_tests { + //! ESC: adjustable shelf slope. Default slope 1 must give the pre-slope coefficients bit for bit. + use super::{Revamp, RevampState, HIGH_SHELF, LOW_SHELF}; + use dsp::biquad::BiquadCoeff; + + const SR: f32 = 48_000.0; + const TAU: f64 = core::f64::consts::TAU; + + // The readybase low/high shelf code, copied verbatim (fixed S = 1 via the sqrt(2) constant). + fn old_shelf(low: bool, frequency: f64, db_gain: f64) -> [f64; 5] { + let a = libm::pow(10.0, db_gain / 40.0); + let w0 = TAU * frequency; + let alpha = 0.5 * libm::sin(w0) * core::f64::consts::SQRT_2; + let k = libm::cos(w0); + let k2 = 2.0 * libm::sqrt(a) * alpha; + let (ap, am) = (a + 1.0, a - 1.0); + let mut coeff = BiquadCoeff::default(); + if low { + coeff.set_normalized_coefficients(a * (ap - am * k + k2), 2.0 * a * (am - ap * k), a * (ap - am * k - k2), + ap + am * k + k2, -2.0 * (am + ap * k), ap + am * k - k2); + } else { + coeff.set_normalized_coefficients(a * (ap + am * k + k2), -2.0 * a * (am + ap * k), a * (ap + am * k - k2), + ap - am * k + k2, 2.0 * (am - ap * k), ap - am * k - k2); + } + [coeff.b0, coeff.b1, coeff.b2, coeff.a1, coeff.a2] + } + + fn bits(c: &BiquadCoeff) -> [u64; 5] { + [c.b0.to_bits(), c.b1.to_bits(), c.b2.to_bits(), c.a1.to_bits(), c.a2.to_bits()] + } + + fn state() -> RevampState { + let mut state: RevampState = unsafe { core::mem::zeroed() }; + state.sample_rate = SR; + state + } + + // |H(e^jw)| in dB at `hz`. + fn response_db(c: &BiquadCoeff, hz: f64) -> f64 { + let w = TAU * hz / SR as f64; + let (c1, s1, c2, s2) = (libm::cos(w), libm::sin(w), libm::cos(2.0 * w), libm::sin(2.0 * w)); + let (nr, ni) = (c.b0 + c.b1 * c1 + c.b2 * c2, -(c.b1 * s1 + c.b2 * s2)); + let (dr, di) = (1.0 + c.a1 * c1 + c.a2 * c2, -(c.a1 * s1 + c.a2 * s2)); + 10.0 * libm::log10((nr * nr + ni * ni) / (dr * dr + di * di)) + } + + // Octaves between the 10% and 90% points of the shelf's dB transition (wider = gentler slope). + fn transition_octaves(c: &BiquadCoeff, gain: f64) -> f64 { + let (mut f10, mut f90) = (0.0, 0.0); + let mut hz = 10.0f64; + let mut prev = response_db(c, hz); + while hz < 23_000.0 { + let next_hz = hz * 1.002; + let next = response_db(c, next_hz); + // low shelf: gain at DC falling to 0; record crossings of 90% then 10% + if prev >= 0.9 * gain && next < 0.9 * gain {f90 = next_hz;} + if prev >= 0.1 * gain && next < 0.1 * gain {f10 = next_hz;} + hz = next_hz; + prev = next; + } + libm::log2(f10 / f90) + } + + #[test] + fn default_slope_is_bit_identical_to_the_old_shelf() { + for &hz in &[20.0f32, 80.0, 440.0, 3220.0, 10_000.0, 19_000.0] { + for &gain in &[-24.0f32, -6.0, 0.0, 4.5, 6.0, 24.0] { + let mut state = state(); + state.freq[LOW_SHELF] = hz; + state.gain[LOW_SHELF] = gain; + state.freq[HIGH_SHELF] = hz; + state.gain[HIGH_SHELF] = gain; + for &slope in &[0.0f32, 1.0] { // 0 = never delivered (zeroed state), 1 = the schema default + state.q[LOW_SHELF] = slope; + state.q[HIGH_SHELF] = slope; + Revamp::recompute(&mut state, LOW_SHELF); + Revamp::recompute(&mut state, HIGH_SHELF); + let f = (hz / SR) as f64; + let old_low = old_shelf(true, f, gain as f64).map(f64::to_bits); + let old_high = old_shelf(false, f, gain as f64).map(f64::to_bits); + assert_eq!(bits(&state.coeff[LOW_SHELF]), old_low, "low shelf {hz} Hz {gain} dB slope {slope}"); + assert_eq!(bits(&state.coeff[HIGH_SHELF]), old_high, "high shelf {hz} Hz {gain} dB slope {slope}"); + } + } + } + } + + #[test] + fn slope_changes_the_transition_width_and_keeps_the_end_gains() { + let mut widths = Vec::new(); + for &slope in &[0.1f64, 0.3, 0.6, 1.0, 1.5] { + let mut c = BiquadCoeff::default(); + c.set_low_shelf_params_slope(500.0 / SR as f64, 12.0, slope); + let dc = response_db(&c, 5.0); + let top = response_db(&c, 20_000.0); + assert!((dc - 12.0).abs() < 0.05, "slope {slope}: low end keeps +12 dB ({dc})"); + assert!(top.abs() < 0.05, "slope {slope}: top stays at 0 dB ({top})"); + let mid = response_db(&c, 500.0); + assert!((mid - 6.0).abs() < 0.01, "slope {slope}: half gain at the corner frequency ({mid})"); + let width = transition_octaves(&c, 12.0); + println!("low shelf +12 dB @500 Hz, slope {slope}: 10-90% transition {width:.2} octaves"); + widths.push(width); + } + assert!(widths.windows(2).all(|w| w[0] > w[1]), "lower slope = wider transition: {widths:?}"); + // the high shelf mirrors it + let mut c = BiquadCoeff::default(); + c.set_high_shelf_params_slope(2000.0 / SR as f64, -9.0, 0.3); + assert!(response_db(&c, 5.0).abs() < 0.05); + assert!((response_db(&c, 23_000.0) + 9.0).abs() < 0.1); + } + + #[test] + fn every_slope_and_gain_is_stable_and_finite() { + for step in 0..=40 { + let slope = 0.1 * libm::pow(15.0, step as f64 / 40.0); // the knob range 0.1..1.5 + for &gain in &[-24.0f64, -12.0, -1.0, 1.0, 12.0, 24.0] { + for &hz in &[20.0f64, 200.0, 2000.0, 20_000.0] { + for low in [true, false] { + let mut c = BiquadCoeff::default(); + if low {c.set_low_shelf_params_slope(hz / SR as f64, gain, slope)} + else {c.set_high_shelf_params_slope(hz / SR as f64, gain, slope)} + // poles inside the unit circle: |a2| < 1 and |a1| < 1 + a2 + assert!(c.a2.abs() < 1.0 && c.a1.abs() < 1.0 + c.a2, "unstable: slope {slope} {gain} dB {hz} Hz"); + assert!([c.b0, c.b1, c.b2, c.a1, c.a2].iter().all(|v| v.is_finite())); + } + } + } + } + } +} diff --git a/crates/stock-devices/device-reverb/src/lib.rs b/crates/stock-devices/device-reverb/src/lib.rs index 78043c9bb..69ce4af0d 100644 --- a/crates/stock-devices/device-reverb/src/lib.rs +++ b/crates/stock-devices/device-reverb/src/lib.rs @@ -4,7 +4,8 @@ //! //! Parameters (`ReverbDeviceBox`): decay `[10]` (unipolar -> room size), pre-delay `[11]` (exp 0.001..0.5 s), //! damp `[12]` (unipolar), filter `[13]` (bipolar; bound but UNUSED, mirroring the TS which never applies it), -//! wet `[14]` / dry `[15]` (decibel). The device owns the mappings; the host is mapping-agnostic. +//! wet `[14]` / dry `[15]` (decibel). ESC: extend `[16]` (unipolar, "Long"), freeze `[17]` (bool), mix `[18]` +//! (unipolar, 50% = old), wet-boost `[19]` (linear 0..12 dB). The device owns the mappings; the host is mapping-agnostic. //! //! Exports: `kind()` (audio effect), `state_size()`, `process(desc_ptr)`, `init(...)`, `parameter_changed(...)`. @@ -12,7 +13,7 @@ #[cfg(target_family = "wasm")] use core::panic::PanicInfo; -use abi::{float_value, AudioEffect, Block, ParamValue, Ports}; +use abi::{bool_value, float_value, AudioEffect, Block, ParamValue, Ports}; use dsp::db_to_gain; use dsp::freeverb::FreeVerb; use math::value_mapping::{Decibel, Exponential, Linear}; @@ -29,12 +30,27 @@ const DAMP_FIELD: [u16; 1] = [12]; const FILTER_FIELD: [u16; 1] = [13]; const WET_FIELD: [u16; 1] = [14]; const DRY_FIELD: [u16; 1] = [15]; +// ESC upgrade (Nov 2026): every default reproduces the old sound bit for bit. +const EXTEND_FIELD: [u16; 1] = [16]; +const FREEZE_FIELD: [u16; 1] = [17]; +const MIX_FIELD: [u16; 1] = [18]; +const WET_BOOST_FIELD: [u16; 1] = [19]; const DECAY_MAPPING: Linear = Linear::unipolar(); const PRE_DELAY_MAPPING: Exponential = Exponential {min: 0.001, max: 0.500}; const DAMP_MAPPING: Linear = Linear::unipolar(); const FILTER_MAPPING: Linear = Linear::bipolar(); const GAIN_MAPPING: Decibel = Decibel::default_volume(); // ValueMapping.DefaultDecibel = decibel(-72, -12, 0) +const EXTEND_MAPPING: Linear = Linear::unipolar(); +const MIX_MAPPING: Linear = Linear::unipolar(); +const WET_BOOST_MAPPING: Linear = Linear {min: 0.0, max: 12.0}; + +/// The Mix balance: 50% keeps both Dry and Wet at their dB values (the pre-upgrade sound), below 50% the wet +/// fades out (0% = dry only), above it the dry fades out (100% = wet only). Linear, so 50% is exactly `[1, 1]`. +pub fn mix_gains(mix: f32) -> [f32; 2] { + let mix = math::clamp(mix, 0.0, 1.0); + [(2.0 * (1.0 - mix)).min(1.0), (2.0 * mix).min(1.0)] +} /// The reverb's per-instance state (engine-allocated, zeroed): the FreeVerb DSP (built in `init`), the sample /// rate (for the pre-delay in samples), and the parameter ids. `filter_id` is bound but never applied (TS parity). @@ -46,7 +62,25 @@ pub struct ReverbState { damp_id: u32, filter_id: u32, wet_id: u32, - dry_id: u32 + dry_id: u32, + extend_id: u32, + freeze_id: u32, + mix_id: u32, + wet_boost_id: u32, + dry_db: f32, + wet_db: f32, + wet_boost_db: f32, + mix: f32 +} + +impl ReverbState { + // Dry / Wet dB trims x the Mix balance (+ the Wet Boost). At Mix 50% / Boost 0 this is exactly the old + // `db_to_gain(dry)` / `db_to_gain(wet)`. + fn update_gains(&mut self) { + let [dry_mix, wet_mix] = mix_gains(self.mix); + self.reverb.dry_gain = db_to_gain(self.dry_db) * dry_mix; + self.reverb.wet_gain = db_to_gain(self.wet_db + self.wet_boost_db) * wet_mix; + } } /// The DSP, plugged into the SDK's `AudioEffect` template ([`abi::render_effect`]). @@ -64,6 +98,15 @@ impl AudioEffect for Reverb { state.filter_id = abi::bind_parameter(&FILTER_FIELD); state.wet_id = abi::bind_parameter(&WET_FIELD); state.dry_id = abi::bind_parameter(&DRY_FIELD); + state.extend_id = abi::bind_parameter(&EXTEND_FIELD); + state.freeze_id = abi::bind_parameter(&FREEZE_FIELD); + state.mix_id = abi::bind_parameter(&MIX_FIELD); + state.wet_boost_id = abi::bind_parameter(&WET_BOOST_FIELD); + // Box defaults (the host delivers every parameter after init; these only cover the gap). + state.dry_db = 0.0; + state.wet_db = -3.0; + state.wet_boost_db = 0.0; + state.mix = 0.5; } fn parameter_changed(state: &mut ReverbState, id: u32, value: ParamValue) { @@ -75,9 +118,21 @@ impl AudioEffect for Reverb { } else if id == state.damp_id { state.reverb.damp = float_value(value, &DAMP_MAPPING); } else if id == state.wet_id { - state.reverb.wet_gain = db_to_gain(float_value(value, &GAIN_MAPPING)); + state.wet_db = float_value(value, &GAIN_MAPPING); + state.update_gains(); } else if id == state.dry_id { - state.reverb.dry_gain = db_to_gain(float_value(value, &GAIN_MAPPING)); + state.dry_db = float_value(value, &GAIN_MAPPING); + state.update_gains(); + } else if id == state.mix_id { + state.mix = float_value(value, &MIX_MAPPING); + state.update_gains(); + } else if id == state.wet_boost_id { + state.wet_boost_db = float_value(value, &WET_BOOST_MAPPING); + state.update_gains(); + } else if id == state.extend_id { + state.reverb.extend = float_value(value, &EXTEND_MAPPING); + } else if id == state.freeze_id { + state.reverb.freeze = bool_value(value); } else if id == state.filter_id { let _ = float_value(value, &FILTER_MAPPING); // bound for automation parity, but the TS DSP ignores it } @@ -131,6 +186,10 @@ pub extern "C" fn map_parameter(id: u32, unit: f32) -> f32 { 2 => float_value(value, &DAMP_MAPPING), 3 => float_value(value, &FILTER_MAPPING), 4 | 5 => float_value(value, &GAIN_MAPPING), + 6 => float_value(value, &EXTEND_MAPPING), + 7 => if bool_value(value) {1.0} else {0.0}, + 8 => float_value(value, &MIX_MAPPING), + 9 => float_value(value, &WET_BOOST_MAPPING), _ => f32::NAN } } @@ -140,3 +199,29 @@ pub extern "C" fn map_parameter(id: u32, unit: f32) -> f32 { pub extern "C" fn reset(state_ptr: u32) { unsafe { abi::with_state(state_ptr, ::reset) } } + +#[cfg(test)] +mod tests { + use super::mix_gains; + + #[test] + fn mix_fifty_percent_is_exactly_the_old_balance() { + assert_eq!(mix_gains(0.5), [1.0, 1.0], "the default leaves Dry and Wet at their dB values"); + assert_eq!(mix_gains(0.0), [1.0, 0.0], "0% = dry only"); + assert_eq!(mix_gains(1.0), [0.0, 1.0], "100% = wet only"); + assert_eq!(mix_gains(0.25), [1.0, 0.5]); + assert_eq!(mix_gains(0.75), [0.5, 1.0]); + } + + #[test] + fn default_gains_match_the_old_mapping_bit_for_bit() { + // Old: wet_gain = db_to_gain(wet), dry_gain = db_to_gain(dry). New: x mix 1.0 and + boost 0.0. + for &db in &[-72.0f32, -24.0, -3.0, -0.0, 0.0] { + let old = dsp::db_to_gain(db); + let new = dsp::db_to_gain(db + 0.0) * mix_gains(0.5)[1]; + assert_eq!(old.to_bits(), new.to_bits(), "{db} dB"); + } + // Wet Boost lifts the ceiling: Wet 0 dB + Boost 12 dB = +12 dB. + assert!((dsp::db_to_gain(0.0 + 12.0) - 3.981).abs() < 1e-3); + } +} diff --git a/crates/stock-devices/device-stereo-tool/src/lib.rs b/crates/stock-devices/device-stereo-tool/src/lib.rs index f748b84f4..77ee9533b 100644 --- a/crates/stock-devices/device-stereo-tool/src/lib.rs +++ b/crates/stock-devices/device-stereo-tool/src/lib.rs @@ -18,6 +18,7 @@ use abi::{bool_value, float_value, AudioEffect, Block, FieldValue, ParamValue, P use dsp::db_to_gain; use dsp::panning::{Mixing, StereoParams}; use dsp::ramp::StereoMatrixRamp; +use dsp::stereo_scope::{StereoScope, SLOT_LEN as SCOPE_LEN}; use math::value_mapping::{Decibel, Linear}; #[cfg(target_family = "wasm")] @@ -33,6 +34,7 @@ const INVERT_L_FIELD: [u16; 1] = [13]; const INVERT_R_FIELD: [u16; 1] = [14]; const SWAP_FIELD: [u16; 1] = [15]; const PANNING_MIXING_FIELD: [u16; 1] = [20]; +const SCOPE_BROADCAST: [u16; 1] = [0]; // ESC: the output stereo scope (correlation, side/mid, RMS, goniometer) at address.append(0) const VOLUME_MAPPING: Decibel = Decibel::new(-72.0, 0.0, 12.0); const PANNING_MAPPING: Linear = Linear::bipolar(); @@ -55,7 +57,10 @@ pub struct StereoToolState { invert_l_id: u32, invert_r_id: u32, swap_id: u32, - panning_mixing_field_id: u32 + panning_mixing_field_id: u32, + scope: StereoScope, + scope_id: u32, + scope_ptr: u32 } /// The DSP, plugged into the SDK's `AudioEffect` template ([`abi::render_effect`]). @@ -77,6 +82,9 @@ impl AudioEffect for StereoTool { state.invert_r_id = abi::bind_parameter(&INVERT_R_FIELD); state.swap_id = abi::bind_parameter(&SWAP_FIELD); state.panning_mixing_field_id = abi::observe_field(&PANNING_MIXING_FIELD); + state.scope.init(sample_rate); + state.scope_id = abi::bind_broadcast(&SCOPE_BROADCAST, SCOPE_LEN as u32); + state.scope_ptr = 0; } fn parameter_changed(state: &mut StereoToolState, id: u32, value: ParamValue) { @@ -100,6 +108,7 @@ impl AudioEffect for StereoTool { fn reset(state: &mut StereoToolState) { state.processed = false; + state.scope.clear(); } fn process_audio(state: &mut StereoToolState, output: [&mut [f32]; 2], block: &Block) { @@ -110,8 +119,20 @@ impl AudioEffect for StereoTool { state.matrix.update(&state.params, state.mixing, state.processed); state.needs_update = false; } - state.matrix.process_frames(in_left, in_right, out_left, out_right, block.s0 as usize, block.s1 as usize); + let (s0, s1) = (block.s0 as usize, block.s1 as usize); + state.matrix.process_frames(in_left, in_right, out_left, out_right, s0, s1); state.processed = true; + // The scope reads the OUTPUT, and only while an editor is subscribed (read-only: never touches the audio). + if abi::broadcast_active(state.scope_id) { + if state.scope_ptr == 0 { + state.scope_ptr = abi::broadcast_ptr(state.scope_id); + } + if state.scope_ptr != 0 { + state.scope.process(&out_left[s0..s1], &out_right[s0..s1]); + let slot = unsafe { core::slice::from_raw_parts_mut(state.scope_ptr as *mut f32, SCOPE_LEN) }; + state.scope.write_slot(slot); + } + } } } @@ -238,4 +259,105 @@ mod tests { let (left, _right) = run(&mut state, &[0.4], &[0.0]); assert!((left[0] + 0.4).abs() < 1e-6, "left channel is inverted"); } + + fn noise(seed: &mut u32, n: usize, scale: f32) -> Vec { + (0..n).map(|_| { + *seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + ((*seed >> 8) as f32 / 8_388_608.0 - 1.0) * scale + }).collect() + } + + fn db(x: f64) -> f64 {if x <= 0.0 {f64::NEG_INFINITY} else {10.0 * x.log10()}} + + #[test] + fn null_test_at_defaults_is_exact() { + // Defaults: Volume 0 dB, Panning 0, Width 0, no channel ops. Output minus input over 1 s of wide noise. + let mut state = state(); + state.params.gain = dsp::db_to_gain(0.0); // the box stores the real default 0.0 dB + assert_eq!(state.params.gain, 1.0, "the 0 dB default is exactly unity gain"); + let mut seed = 7; + let (mut err, mut sig) = (0.0f64, 0.0f64); + for _ in 0..375 { + let (l, r) = (noise(&mut seed, 128, 0.9), noise(&mut seed, 128, 0.9)); + let (out_l, out_r) = run(&mut state, &l, &r); + for i in 0..128 { + err += ((out_l[i] - l[i]) as f64).powi(2) + ((out_r[i] - r[i]) as f64).powi(2); + sig += (l[i] as f64).powi(2) + (r[i] as f64).powi(2); + } + } + let level = db(err / sig); + println!("null test at defaults: residual {level} dB"); + assert!(level < -120.0, "null residual {level} dB"); + assert_eq!(err, 0.0, "bit-exact"); + } + + #[test] + fn full_width_is_mono_compatible() { + let mut state = state(); + state.params.gain = 1.0; + state.params.stereo = 1.0; + let mut seed = 11; + let (mut mono_err, mut mono_in) = (0.0f64, 0.0f64); + let (mut corr, mut ll, mut rr) = (0.0f64, 0.0f64, 0.0f64); + for _ in 0..100 { + let common = noise(&mut seed, 128, 0.5); + let l: Vec = common.iter().zip(noise(&mut seed, 128, 0.2)).map(|(c, x)| c + x).collect(); + let r: Vec = common.iter().zip(noise(&mut seed, 128, 0.2)).map(|(c, x)| c + x).collect(); + let (out_l, out_r) = run(&mut state, &l, &r); + for i in 0..128 { + mono_err += (((out_l[i] + out_r[i]) - (l[i] + r[i])) as f64).powi(2); + mono_in += ((l[i] + r[i]) as f64).powi(2); + corr += (out_l[i] * out_r[i]) as f64; + ll += (out_l[i] as f64).powi(2); + rr += (out_r[i] as f64).powi(2); + } + } + let corr = corr / (ll * rr).sqrt(); + println!("width +100%: mono-sum residual {} dB, output correlation {corr:.3}", db(mono_err / mono_in)); + assert!(db(mono_err / mono_in) < -120.0, "the mono sum is untouched by the width"); + assert!(corr > 0.0, "a mostly-centred source stays positively correlated (the port gave exactly -1)"); + } + + #[test] + fn hard_pan_on_a_mono_fold_lands_on_that_side() { + let mut state = state(); + state.params.gain = 1.0; + state.params.stereo = -1.0; + state.params.panning = -1.0; + let (left, right) = run(&mut state, &[0.2], &[0.6]); + assert!((left[0] - 0.4).abs() < 1e-6 && right[0].abs() < 1e-6, "mono fold hard left: {left:?} {right:?}"); + } + + #[test] + fn pan_law_at_plus_50_percent() { + let mut state = state(); + state.params.gain = 1.0; + state.params.panning = 0.5; + let (left, right) = run(&mut state, &[1.0], &[1.0]); + println!("linear +50%: left {:.2} dB, right {:.2} dB", 20.0 * left[0].log10(), 20.0 * right[0].log10()); + assert!((left[0] - 0.5).abs() < 1e-6 && (right[0] - 1.0).abs() < 1e-6); + let mut state = super::tests::state(); + state.mixing = Mixing::EqualPower; + state.params.gain = 1.0; + state.params.panning = 0.5; + let (left, right) = run(&mut state, &[1.0], &[1.0]); + println!("equal power +50%: left {:.2} dB, right {:.2} dB", 20.0 * left[0].log10(), 20.0 * right[0].log10()); + assert!((left[0] * left[0] + right[0] * right[0] - 1.0).abs() < 1e-5); + } + + #[test] + fn a_change_glides_without_a_jump() { + // Flipping invert L mid-stream ramps the matrix over 5 ms (240 samples at 48 kHz): no step larger than one + // ramp increment on a DC input. + let mut state = state(); + state.params.gain = 1.0; + let dc = vec![0.5f32; 512]; + run(&mut state, &dc, &dc); + state.params.invert_l = true; + state.needs_update = true; + let (left, _) = run(&mut state, &dc, &dc); + let max_step = left.windows(2).map(|w| (w[1] - w[0]).abs()).fold(0.0f32, f32::max); + println!("invert toggle: max sample step {max_step}"); + assert!(max_step < 0.005 && (left[511] + 0.5).abs() < 1e-6); + } } diff --git a/crates/stock-devices/device-tidal/src/lib.rs b/crates/stock-devices/device-tidal/src/lib.rs index 004592adc..998dca492 100644 --- a/crates/stock-devices/device-tidal/src/lib.rs +++ b/crates/stock-devices/device-tidal/src/lib.rs @@ -19,7 +19,7 @@ use abi::{float_value, AudioEffect, Block, ParamValue, Ports}; use dsp::ppqn; use dsp::smooth::Smooth; use dsp::tidal::TidalComputer; -use math::value_mapping::{Linear, LinearInteger, ValueMapping}; +use math::value_mapping::{Linear, ValueMapping, Values}; #[cfg(target_family = "wasm")] #[panic_handler] @@ -42,17 +42,21 @@ const SYMMETRY_MAPPING: Linear = Linear::unipolar(); const DEPTH_MAPPING: Linear = Linear::unipolar(); const OFFSET_MAPPING: Linear = Linear {min: -180.0, max: 180.0}; const CHANNEL_OFFSET_MAPPING: Linear = Linear {min: -180.0, max: 180.0}; -// The rate parameter selects a fraction by INDEX. The TS uses `values(RateFractions.map((_, i) => i))`; over -// contiguous indices that is exactly a linear-integer map onto `0 ..= len - 1`. -const RATE_MAPPING: LinearInteger = LinearInteger {min: 0, max: RATE_FRACTIONS.len() as i32 - 1}; +// The rate parameter selects a fraction by INDEX. The knob runs over RATE_ORDER (slowest to fastest), the TS +// `ValueMapping.values(TidalRates.Order)`, so a normalized value maps to the index at that knob position. +const RATE_MAPPING: Values<'static, i32> = Values::new(&RATE_ORDER); // The rate fractions (numerator, denominator), `TidalDeviceBoxAdapter.RateFractions` in DESCENDING value // order (which is also the order they are declared, since they decrease monotonically). The rate parameter // indexes this table. A fraction's period in pulses is `floor(BAR / denominator) * numerator` (PPQN.fromSignature). -const RATE_FRACTIONS: [(i32, i32); 17] = [ +// ESC: 4/1, 2/1 and 3/8 (1/4.) are APPENDED at 17..19 so a stored index keeps its meaning (TidalRates.Fractions). +const RATE_FRACTIONS: [(i32, i32); 20] = [ (1, 1), (1, 2), (1, 3), (1, 4), (3, 16), (1, 6), (1, 8), (3, 32), (1, 12), - (1, 16), (3, 64), (1, 24), (1, 32), (1, 48), (1, 64), (1, 96), (1, 128) + (1, 16), (3, 64), (1, 24), (1, 32), (1, 48), (1, 64), (1, 96), (1, 128), + (4, 1), (2, 1), (3, 8) ]; +// The indices from the slowest to the fastest rate: the knob's order (TidalRates.Order). +const RATE_ORDER: [i32; 20] = [17, 18, 0, 1, 19, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; const SMOOTH_TIME_SECONDS: f64 = 0.003; // the TS gain smoother's time constant /// Resolve the rate-fraction index (a `linearInteger` on a Float32 field, so `int_value` cannot serve it): @@ -302,18 +306,34 @@ mod tests { #[test] fn the_rate_index_takes_every_wire_the_engine_sends() { let last = RATE_FRACTIONS.len() as i32 - 1; - assert_eq!(rate_index(ParamValue::Unit(0.0)), 0); - assert_eq!(rate_index(ParamValue::Unit(1.0)), last); + assert_eq!(rate_index(ParamValue::Unit(0.0)), 17, "the knob's low end is 4/1, the slowest"); + assert_eq!(rate_index(ParamValue::Unit(1.0)), 16, "the knob's high end is 1/128, the fastest"); assert_eq!(rate_index(ParamValue::Float(4.0)), 4); assert_eq!(rate_index(ParamValue::Int(4)), 4); // A modulated STORAGE value folds in normalized space, around the stored index. let base = RATE_MAPPING.x(4); assert_eq!(rate_index(ParamValue::Modulated {base: 4.0, kind: PARAM_KIND_FLOAT, sum: 0.0}), 4); assert!(rate_index(ParamValue::Modulated {base: 4.0, kind: PARAM_KIND_FLOAT, sum: 0.25}) > 4); - assert_eq!(rate_index(ParamValue::Modulated {base: 4.0, kind: PARAM_KIND_FLOAT, sum: 5.0}), last, + assert_eq!(rate_index(ParamValue::Modulated {base: 4.0, kind: PARAM_KIND_FLOAT, sum: 5.0}), 16, "an over-driven sum clamps at the fastest fraction"); - assert_eq!(rate_index(ParamValue::Modulated {base: 4.0, kind: PARAM_KIND_FLOAT, sum: -5.0}), 0); + assert_eq!(rate_index(ParamValue::Modulated {base: 4.0, kind: PARAM_KIND_FLOAT, sum: -5.0}), 17); + assert_eq!(rate_index(ParamValue::Float(last as f32)), last); // A modulated AUTOMATION value: the base is already normalized. assert_eq!(rate_index(ParamValue::Modulated {base, kind: PARAM_KIND_UNIT, sum: 0.0}), 4); } + + #[test] + fn the_knob_order_runs_from_the_slowest_to_the_fastest_rate() { + use super::RATE_ORDER; + let mut seen = [false; 20]; + let value = |index: i32| { let (n, d) = RATE_FRACTIONS[index as usize]; n as f64 / d as f64 }; + for window in RATE_ORDER.windows(2) { + assert!(value(window[0]) > value(window[1]), "{:?} is not strictly decreasing", window); + } + for index in RATE_ORDER { seen[index as usize] = true; } + assert!(seen.iter().all(|flag| *flag), "every fraction is on the knob exactly once"); + for (position, index) in RATE_ORDER.iter().enumerate() { + assert_eq!(RATE_MAPPING.y(position as f32 / 19.0), *index, "knob position {position}"); + } + } } diff --git a/crates/studio-boxes/src/registry.rs b/crates/studio-boxes/src/registry.rs index 9c923a0d1..395e89c3f 100644 --- a/crates/studio-boxes/src/registry.rs +++ b/crates/studio-boxes/src/registry.rs @@ -32,7 +32,7 @@ pub fn registry() -> Registry { ("ValueRegionBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Pointer), (10u16, FieldType::Int32), (11u16, FieldType::Int32), (12u16, FieldType::Int32), (13u16, FieldType::Int32), (14u16, FieldType::Boolean), (15u16, FieldType::String), (16u16, FieldType::Int32), (17u16, FieldType::Pointer)])), ("ValueClipBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Pointer), (3u16, FieldType::Int32), (4u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (2u16, FieldType::Boolean), (4u16, FieldType::Int32), (5u16, FieldType::Int32), (6u16, FieldType::Int32)]))), (10u16, FieldType::Int32), (11u16, FieldType::Boolean), (12u16, FieldType::String), (13u16, FieldType::Int32), (14u16, FieldType::Pointer)])), ("SignatureEventBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (9u16, FieldType::Int32), (10u16, FieldType::Int32), (21u16, FieldType::Int32), (22u16, FieldType::Int32)])), - ("AudioRegionBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Pointer), (4u16, FieldType::String), (5u16, FieldType::Pointer), (7u16, FieldType::Float32), (8u16, FieldType::Pointer), (10u16, FieldType::Int32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Boolean), (15u16, FieldType::String), (16u16, FieldType::Int32), (17u16, FieldType::Float32), (18u16, FieldType::Object(Schema::from([(1u16, FieldType::Float32), (2u16, FieldType::Float32), (3u16, FieldType::Float32), (4u16, FieldType::Float32)])))])), + ("AudioRegionBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Pointer), (4u16, FieldType::String), (5u16, FieldType::Pointer), (7u16, FieldType::Float32), (8u16, FieldType::Pointer), (10u16, FieldType::Int32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Boolean), (15u16, FieldType::String), (16u16, FieldType::Int32), (17u16, FieldType::Float32), (18u16, FieldType::Object(Schema::from([(1u16, FieldType::Float32), (2u16, FieldType::Float32), (3u16, FieldType::Float32), (4u16, FieldType::Float32), (5u16, FieldType::Int32), (6u16, FieldType::Int32), (7u16, FieldType::Boolean), (8u16, FieldType::Boolean)])))])), ("AudioClipBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Pointer), (3u16, FieldType::Int32), (4u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (2u16, FieldType::Boolean), (4u16, FieldType::Int32), (5u16, FieldType::Int32), (6u16, FieldType::Int32)]))), (5u16, FieldType::Pointer), (7u16, FieldType::Float32), (8u16, FieldType::Pointer), (10u16, FieldType::Float32), (11u16, FieldType::Boolean), (12u16, FieldType::String), (13u16, FieldType::Int32), (14u16, FieldType::Float32), (21u16, FieldType::String)])), ("AudioPitchStretchBox".to_string(), Schema::from([(1u16, FieldType::Hook)])), ("AudioTimeStretchBox".to_string(), Schema::from([(1u16, FieldType::Hook), (2u16, FieldType::Int32), (3u16, FieldType::Float32)])), @@ -67,7 +67,7 @@ pub fn registry() -> Registry { ("ModularDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Pointer), (11u16, FieldType::Object(Schema::from([(1u16, FieldType::Hook)])))])), ("StereoToolDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Boolean), (14u16, FieldType::Boolean), (15u16, FieldType::Boolean), (20u16, FieldType::Int32)])), ("MaximizerDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Boolean), (11u16, FieldType::Float32)])), - ("MasteringDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (4u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Boolean), (20u16, FieldType::Float32), (21u16, FieldType::Float32), (22u16, FieldType::Bytes), (23u16, FieldType::Bytes), (24u16, FieldType::String), (25u16, FieldType::Bytes), (26u16, FieldType::String)])), + ("MasteringDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (4u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Boolean), (20u16, FieldType::Float32), (21u16, FieldType::Float32), (22u16, FieldType::Bytes), (23u16, FieldType::Bytes), (24u16, FieldType::String), (25u16, FieldType::Bytes), (26u16, FieldType::String), (27u16, FieldType::Float32), (28u16, FieldType::Float32), (29u16, FieldType::Boolean)])), ("CompressorDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Boolean), (11u16, FieldType::Boolean), (12u16, FieldType::Boolean), (13u16, FieldType::Boolean), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Float32), (18u16, FieldType::Float32), (19u16, FieldType::Float32), (20u16, FieldType::Float32), (21u16, FieldType::Float32), (30u16, FieldType::Pointer)])), ("GateDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Boolean), (30u16, FieldType::Pointer)])), ("DelayDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Float32), (19u16, FieldType::Float32), (20u16, FieldType::Float32), (22u16, FieldType::Float32), (23u16, FieldType::Float32), (24u16, FieldType::Float32), (99u16, FieldType::Int32)])), @@ -77,21 +77,21 @@ pub fn registry() -> Registry { ("ConvolverDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Pointer), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Boolean), (15u16, FieldType::Boolean)])), ("VelocityDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Int32), (13u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32)])), ("FoldDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Int32), (12u16, FieldType::Float32)])), - ("TidalDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (20u16, FieldType::Float32), (21u16, FieldType::Float32), (22u16, FieldType::Float32), (23u16, FieldType::Float32)])), - ("RevampDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Int32), (12u16, FieldType::Float32)]))), (11u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32)]))), (12u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (13u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (14u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (15u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32)]))), (16u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Int32), (12u16, FieldType::Float32)])))])), - ("ReverbDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32)])), - ("VaporisateurDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Float32), (18u16, FieldType::Float32), (19u16, FieldType::Float32), (20u16, FieldType::Float32), (21u16, FieldType::Float32), (22u16, FieldType::Int32), (23u16, FieldType::Int32), (24u16, FieldType::Float32), (25u16, FieldType::Float32), (26u16, FieldType::Int32), (27u16, FieldType::Float32), (30u16, FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Float32), (3u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (40u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Float32), (3u16, FieldType::Int32), (4u16, FieldType::Float32)]))), length: 2}), (50u16, FieldType::Object(Schema::from([(1u16, FieldType::Float32), (2u16, FieldType::Float32), (3u16, FieldType::Float32), (4u16, FieldType::Float32)]))), (99u16, FieldType::Int32)])), - ("MIDIOutputDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (11u16, FieldType::Int32), (13u16, FieldType::Hook), (14u16, FieldType::Pointer)])), + ("TidalDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (20u16, FieldType::Float32), (21u16, FieldType::Float32), (22u16, FieldType::Float32), (23u16, FieldType::Float32), (99u16, FieldType::Int32)])), + ("RevampDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Int32), (12u16, FieldType::Float32)]))), (11u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (12u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (13u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (14u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (15u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (16u16, FieldType::Object(Schema::from([(1u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Int32), (12u16, FieldType::Float32)])))])), + ("ReverbDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Boolean), (18u16, FieldType::Float32), (19u16, FieldType::Float32)])), + ("VaporisateurDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Float32), (18u16, FieldType::Float32), (19u16, FieldType::Float32), (20u16, FieldType::Float32), (21u16, FieldType::Float32), (22u16, FieldType::Int32), (23u16, FieldType::Int32), (24u16, FieldType::Float32), (25u16, FieldType::Float32), (26u16, FieldType::Int32), (27u16, FieldType::Float32), (30u16, FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Float32), (3u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32)]))), (40u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Float32), (3u16, FieldType::Int32), (4u16, FieldType::Float32)]))), length: 2}), (50u16, FieldType::Object(Schema::from([(1u16, FieldType::Float32), (2u16, FieldType::Float32), (3u16, FieldType::Float32), (4u16, FieldType::Float32)]))), (99u16, FieldType::Int32)])), + ("MIDIOutputDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (11u16, FieldType::Int32), (13u16, FieldType::Hook), (14u16, FieldType::Pointer)])), ("MIDIOutputBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Hook), (3u16, FieldType::String), (4u16, FieldType::String), (5u16, FieldType::Int32), (6u16, FieldType::Boolean)])), ("MIDIOutputParameterBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::Int32), (4u16, FieldType::Float32)])), - ("SoundfontDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Pointer), (11u16, FieldType::Int32)])), - ("NeonDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Int32), (11u16, FieldType::Int32), (12u16, FieldType::Int32), (13u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Int32), (20u16, FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Float32), (3u16, FieldType::Float32), (4u16, FieldType::Float32)]))), (30u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Int32), (3u16, FieldType::Float32), (4u16, FieldType::Float32)]))), length: 2}), (40u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Float32), (2u16, FieldType::Float32), (3u16, FieldType::Float32), (4u16, FieldType::Float32), (5u16, FieldType::Float32), (6u16, FieldType::Float32), (7u16, FieldType::Float32), (8u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Float32), (18u16, FieldType::Float32), (20u16, FieldType::Int32), (21u16, FieldType::Int32)]))), length: 6})])), - ("WclapInstrumentBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::String), (11u16, FieldType::String), (12u16, FieldType::String), (13u16, FieldType::Hook), (14u16, FieldType::Bytes), (15u16, FieldType::Array {element: Box::new(FieldType::Pointer), length: 8}), (16u16, FieldType::Hook)])), - ("TubularDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Int32), (14u16, FieldType::Float32), (15u16, FieldType::Int32), (16u16, FieldType::Int32), (17u16, FieldType::Int32), (20u16, FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Int32), (3u16, FieldType::Int32), (4u16, FieldType::Int32), (5u16, FieldType::Int32), (6u16, FieldType::Int32)]))), (21u16, FieldType::Int32), (22u16, FieldType::Int32), (30u16, FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Int32), (3u16, FieldType::Int32), (4u16, FieldType::Int32), (11u16, FieldType::Int32), (12u16, FieldType::Int32), (13u16, FieldType::Int32), (14u16, FieldType::Int32)]))), (40u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Int32), (3u16, FieldType::Int32), (4u16, FieldType::Int32), (5u16, FieldType::Int32), (6u16, FieldType::Int32), (7u16, FieldType::Int32), (8u16, FieldType::Int32), (9u16, FieldType::Int32), (10u16, FieldType::Int32), (11u16, FieldType::Int32), (12u16, FieldType::Int32), (13u16, FieldType::Int32), (14u16, FieldType::Int32), (15u16, FieldType::Int32), (16u16, FieldType::Int32), (17u16, FieldType::Int32), (18u16, FieldType::Int32), (19u16, FieldType::Int32), (20u16, FieldType::Int32), (21u16, FieldType::Int32), (22u16, FieldType::Int32)]))), length: 6}), (50u16, FieldType::Int32)])), - ("NanoDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Int32), (13u16, FieldType::Float32), (14u16, FieldType::Int32), (15u16, FieldType::Pointer), (20u16, FieldType::Float32), (21u16, FieldType::Float32), (22u16, FieldType::Float32), (23u16, FieldType::Float32), (24u16, FieldType::Boolean), (25u16, FieldType::Float32), (26u16, FieldType::Float32), (27u16, FieldType::Float32), (28u16, FieldType::Float32), (29u16, FieldType::Boolean), (30u16, FieldType::Boolean), (31u16, FieldType::Boolean)])), - ("PlayfieldDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Hook)])), + ("SoundfontDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::Pointer), (11u16, FieldType::Int32)])), + ("NeonDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::Int32), (11u16, FieldType::Int32), (12u16, FieldType::Int32), (13u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Int32), (20u16, FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Float32), (3u16, FieldType::Float32), (4u16, FieldType::Float32)]))), (30u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Int32), (3u16, FieldType::Float32), (4u16, FieldType::Float32)]))), length: 2}), (40u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Float32), (2u16, FieldType::Float32), (3u16, FieldType::Float32), (4u16, FieldType::Float32), (5u16, FieldType::Float32), (6u16, FieldType::Float32), (7u16, FieldType::Float32), (8u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Float32), (18u16, FieldType::Float32), (20u16, FieldType::Int32), (21u16, FieldType::Int32)]))), length: 6})])), + ("WclapInstrumentBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::String), (11u16, FieldType::String), (12u16, FieldType::String), (13u16, FieldType::Hook), (14u16, FieldType::Bytes), (15u16, FieldType::Array {element: Box::new(FieldType::Pointer), length: 8}), (16u16, FieldType::Hook)])), + ("TubularDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Int32), (14u16, FieldType::Float32), (15u16, FieldType::Int32), (16u16, FieldType::Int32), (17u16, FieldType::Int32), (20u16, FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Int32), (3u16, FieldType::Int32), (4u16, FieldType::Int32), (5u16, FieldType::Int32), (6u16, FieldType::Int32)]))), (21u16, FieldType::Int32), (22u16, FieldType::Int32), (30u16, FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Int32), (3u16, FieldType::Int32), (4u16, FieldType::Int32), (11u16, FieldType::Int32), (12u16, FieldType::Int32), (13u16, FieldType::Int32), (14u16, FieldType::Int32)]))), (40u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Int32), (3u16, FieldType::Int32), (4u16, FieldType::Int32), (5u16, FieldType::Int32), (6u16, FieldType::Int32), (7u16, FieldType::Int32), (8u16, FieldType::Int32), (9u16, FieldType::Int32), (10u16, FieldType::Int32), (11u16, FieldType::Int32), (12u16, FieldType::Int32), (13u16, FieldType::Int32), (14u16, FieldType::Int32), (15u16, FieldType::Int32), (16u16, FieldType::Int32), (17u16, FieldType::Int32), (18u16, FieldType::Int32), (19u16, FieldType::Int32), (20u16, FieldType::Int32), (21u16, FieldType::Int32), (22u16, FieldType::Int32)]))), length: 6}), (50u16, FieldType::Int32)])), + ("NanoDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::Float32), (11u16, FieldType::Int32), (13u16, FieldType::Float32), (14u16, FieldType::Int32), (15u16, FieldType::Pointer), (20u16, FieldType::Float32), (21u16, FieldType::Float32), (22u16, FieldType::Float32), (23u16, FieldType::Float32), (24u16, FieldType::Boolean), (25u16, FieldType::Float32), (26u16, FieldType::Float32), (27u16, FieldType::Float32), (28u16, FieldType::Float32), (29u16, FieldType::Boolean), (30u16, FieldType::Boolean), (31u16, FieldType::Boolean)])), + ("PlayfieldDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::Hook)])), ("PlayfieldSampleBox".to_string(), Schema::from([(10u16, FieldType::Pointer), (11u16, FieldType::Pointer), (12u16, FieldType::Hook), (13u16, FieldType::Hook), (15u16, FieldType::Int32), (21u16, FieldType::String), (22u16, FieldType::Boolean), (23u16, FieldType::Boolean), (40u16, FieldType::Boolean), (41u16, FieldType::Boolean), (42u16, FieldType::Boolean), (43u16, FieldType::Boolean), (44u16, FieldType::Int32), (45u16, FieldType::Float32), (46u16, FieldType::Float32), (47u16, FieldType::Float32), (48u16, FieldType::Float32), (49u16, FieldType::Float32), (50u16, FieldType::Float32), (51u16, FieldType::Float32)])), - ("TapeDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32)])), + ("TapeDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32)])), ("ArpeggioDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Int32), (11u16, FieldType::Int32), (12u16, FieldType::Int32), (13u16, FieldType::Float32), (14u16, FieldType::Int32), (15u16, FieldType::Float32), (16u16, FieldType::Int32), (17u16, FieldType::Boolean), (18u16, FieldType::Int32), (19u16, FieldType::Int32), (20u16, FieldType::Int32), (21u16, FieldType::Boolean), (30u16, FieldType::Array {element: Box::new(FieldType::Int32), length: 64}), (31u16, FieldType::Array {element: Box::new(FieldType::Int32), length: 64}), (32u16, FieldType::Array {element: Box::new(FieldType::Int32), length: 64}), (33u16, FieldType::Array {element: Box::new(FieldType::Int32), length: 64}), (34u16, FieldType::Array {element: Box::new(FieldType::Int32), length: 64}), (35u16, FieldType::Array {element: Box::new(FieldType::Int32), length: 64}), (36u16, FieldType::Array {element: Box::new(FieldType::Int32), length: 64}), (37u16, FieldType::Int32), (99u16, FieldType::Int32)])), ("PitchDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Int32), (11u16, FieldType::Float32), (12u16, FieldType::Int32)])), ("NoteFilterDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Int32), (11u16, FieldType::Int32), (12u16, FieldType::Boolean), (13u16, FieldType::Int32), (14u16, FieldType::Int32)])), @@ -113,11 +113,11 @@ pub fn registry() -> Registry { ("ScriptBufferBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::Int32), (4u16, FieldType::Bytes), (5u16, FieldType::Hook)])), ("ScriptBufferChunkBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::Bytes)])), ("SpielwerkDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::String), (11u16, FieldType::Hook), (12u16, FieldType::Hook), (14u16, FieldType::Hook), (15u16, FieldType::Hook)])), - ("ApparatDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::String), (11u16, FieldType::Hook), (12u16, FieldType::Hook), (13u16, FieldType::Hook), (14u16, FieldType::Hook), (15u16, FieldType::Hook)])), - ("CubedDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Int32), (20u16, FieldType::Int32), (30u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Array {element: Box::new(FieldType::Int32), length: 64})]))), length: 16}), (40u16, FieldType::Pointer), (99u16, FieldType::Int32)])), - ("KorpusDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Int32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Int32), (15u16, FieldType::Float32), (16u16, FieldType::Int32), (17u16, FieldType::Float32), (18u16, FieldType::Int32), (19u16, FieldType::Float32), (22u16, FieldType::Float32), (23u16, FieldType::Float32), (24u16, FieldType::Int32), (25u16, FieldType::Float32), (26u16, FieldType::Float32), (28u16, FieldType::Float32), (29u16, FieldType::Float32), (30u16, FieldType::Float32), (31u16, FieldType::Float32)])), - ("NoopInstrumentBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean)])), - ("InstrumentCompositeBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Hook)])), + ("ApparatDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::String), (11u16, FieldType::Hook), (12u16, FieldType::Hook), (13u16, FieldType::Hook), (14u16, FieldType::Hook), (15u16, FieldType::Hook)])), + ("CubedDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::Float32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Float32), (15u16, FieldType::Float32), (16u16, FieldType::Float32), (17u16, FieldType::Int32), (20u16, FieldType::Int32), (30u16, FieldType::Array {element: Box::new(FieldType::Object(Schema::from([(1u16, FieldType::Int32), (2u16, FieldType::Array {element: Box::new(FieldType::Int32), length: 64})]))), length: 16}), (40u16, FieldType::Pointer), (99u16, FieldType::Int32)])), + ("KorpusDeviceBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::Int32), (11u16, FieldType::Float32), (12u16, FieldType::Float32), (13u16, FieldType::Float32), (14u16, FieldType::Int32), (15u16, FieldType::Float32), (16u16, FieldType::Int32), (17u16, FieldType::Float32), (18u16, FieldType::Int32), (19u16, FieldType::Float32), (22u16, FieldType::Float32), (23u16, FieldType::Float32), (24u16, FieldType::Int32), (25u16, FieldType::Float32), (26u16, FieldType::Float32), (28u16, FieldType::Float32), (29u16, FieldType::Float32), (30u16, FieldType::Float32), (31u16, FieldType::Float32)])), + ("NoopInstrumentBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32)])), + ("InstrumentCompositeBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::String), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (6u16, FieldType::Float32), (10u16, FieldType::Hook)])), ("InstrumentCompositeCellBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Hook), (3u16, FieldType::Hook), (4u16, FieldType::Hook), (5u16, FieldType::Int32), (7u16, FieldType::Boolean), (40u16, FieldType::Float32), (41u16, FieldType::Boolean), (42u16, FieldType::Boolean), (43u16, FieldType::Float32)])), ("AudioEffectCompositeBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Int32), (3u16, FieldType::String), (4u16, FieldType::Boolean), (5u16, FieldType::Boolean), (10u16, FieldType::Hook), (11u16, FieldType::Hook), (12u16, FieldType::Float32), (13u16, FieldType::Float32)])), ("AudioEffectCompositeCellBox".to_string(), Schema::from([(1u16, FieldType::Pointer), (2u16, FieldType::Hook), (3u16, FieldType::Int32), (5u16, FieldType::Boolean), (40u16, FieldType::Float32), (41u16, FieldType::Boolean), (42u16, FieldType::Boolean), (43u16, FieldType::Float32)])), diff --git a/crates/value/src/region.rs b/crates/value/src/region.rs index a445fc181..4a8ca045f 100644 --- a/crates/value/src/region.rs +++ b/crates/value/src/region.rs @@ -96,6 +96,9 @@ impl Iterator for LoopCycles { pub trait Span { fn position(&self) -> f64; fn duration(&self) -> f64; + /// Whether this span deliberately sounds past its successor's start (an audio crossfade), so a range query + /// must still visit it after the successor began. Default false: regions on a track do not overlap. + fn overlaps_successor(&self) -> bool { false } } /// A collection of spans kept sorted by `position`, range-queried span-aware (the Rust mirror of lib-dsp @@ -168,6 +171,12 @@ impl RegionCollection { start += 1; } } + // An audio crossfade extends a region past its successor's start (only ever the adjacent one), so step + // back over such a predecessor still sounding at `from`. Plain overlaps keep the upstream rule. + while start > 0 && start <= self.regions.len() && self.regions[start - 1].overlaps_successor() + && self.regions[start - 1].position() + self.regions[start - 1].duration() > from { + start -= 1; + } self.regions.get(start..).unwrap_or(&[]).iter().take_while(move |region| region.position() < to) } } diff --git a/crates/value/tests/region.rs b/crates/value/tests/region.rs index 5d2432d06..7345e1b09 100644 --- a/crates/value/tests/region.rs +++ b/crates/value/tests/region.rs @@ -183,3 +183,26 @@ fn a_long_region_under_later_regions_is_found_only_until_the_next_one_starts() { assert_eq!(ids(&collection, 220.0, 280.0), Vec::::new(), "and it does not come back in the gap"); assert_eq!(ids(&collection, 500.0, 600.0), Vec::::new(), "nor after the last region"); } + +#[derive(Clone, Copy)] +struct CrossfadeStub(u32, f64, f64, bool); + +impl Span for CrossfadeStub { + fn position(&self) -> f64 { self.1 } + fn duration(&self) -> f64 { self.2 } + fn overlaps_successor(&self) -> bool { self.3 } +} + +#[test] +fn iterate_range_visits_a_crossfading_predecessor_only_when_flagged() { + let query = |flag: bool, from: f64, to: f64| { + let mut collection = RegionCollection::new(); + collection.add(CrossfadeStub(0, 0.0, 2880.0, flag)); + collection.add(CrossfadeStub(1, 1920.0, 2880.0, false)); + collection.iterate_range(from, to).map(|span| span.0).collect::>() + }; + assert_eq!(query(false, 2500.0, 2600.0), vec![1], "a plain overlap keeps the upstream behaviour"); + assert_eq!(query(true, 2500.0, 2600.0), vec![0, 1]); + assert_eq!(query(true, 1800.0, 1900.0), vec![0]); + assert_eq!(query(true, 2900.0, 3000.0), vec![1]); +} diff --git a/packages/app/manual/public/devices/audio/tidal.md b/packages/app/manual/public/devices/audio/tidal.md index 74c7f832f..970176968 100644 --- a/packages/app/manual/public/devices/audio/tidal.md +++ b/packages/app/manual/public/devices/audio/tidal.md @@ -1,4 +1,4 @@ -# Tidal +# ESC LFO TOOL A tempo-synced tremolo effect with customizable waveshape and stereo phase control. @@ -10,7 +10,7 @@ A tempo-synced tremolo effect with customizable waveshape and stereo phase contr ## 0. Overview -_Tidal_ modulates amplitude in sync with the project tempo. The waveshape can be precisely sculpted using slope and symmetry controls, from smooth sine-like curves to sharp rhythmic gates. +_ESC LFO TOOL_ (formerly Tidal) modulates amplitude in sync with the project tempo. The waveshape can be precisely sculpted using slope and symmetry controls, from smooth sine-like curves to sharp rhythmic gates. Example uses: diff --git a/packages/app/manual/public/devices/instruments/neon.md b/packages/app/manual/public/devices/instruments/neon.md index 40bcfaf54..e715fa1a0 100644 --- a/packages/app/manual/public/devices/instruments/neon.md +++ b/packages/app/manual/public/devices/instruments/neon.md @@ -93,9 +93,9 @@ Time before the vibrato fades in after note-on. Vibrato speed. -### 2.4 Depth +### 2.4 Amount -Vibrato depth. Detuned pairs keep their beat rate — the vibrato bends both lines together. +Vibrato amount. Detuned pairs keep their beat rate, the vibrato bends both lines together. --- diff --git a/packages/app/manual/public/devices/instruments/tubular.md b/packages/app/manual/public/devices/instruments/tubular.md index a6b87c239..4266586eb 100644 --- a/packages/app/manual/public/devices/instruments/tubular.md +++ b/packages/app/manual/public/devices/instruments/tubular.md @@ -52,7 +52,7 @@ One LFO modulates pitch and amplitude of all operators. - **Wave**: triangle, saw down, saw up, square, sine, sample and hold. - **Speed** and **Delay** (fade-in time after key-on). The display shows the shape and the delay ramp. -- **PM Depth** and **AM Depth**: how much pitch and amplitude modulation the LFO applies. Amplitude +- **PM Amount** and **AM Amount**: how much pitch and amplitude modulation the LFO applies. Amplitude modulation only reaches operators whose **AMS** (see section 5) is above zero. - **PM Sens**: the pitch modulation sensitivity 0 to 7, scaling both LFO pitch modulation and the modulation wheel. @@ -85,9 +85,9 @@ the pitch envelope. or loudest). - **SENS**: **AMS** (amplitude modulation sensitivity 0 to 3 for the LFO), **Velocity** (0 to 7), **Rate Scl** (rate scaling 0 to 7, higher notes get shorter envelopes). -- **SCALE**: keyboard level scaling. The level falls or rises away from **Break Pt** with **L Depth** and - **R Depth** along **L Curve** and **R Curve** (-LIN, -EXP, +EXP, +LIN). The display shows the resulting - level across the keyboard, its three handles drag the break point and the two depths. Dragging a depth +- **SCALE**: keyboard level scaling. The level falls or rises away from **Break Pt** with **L Amount** and + **R Amount** along **L Curve** and **R Curve** (-LIN, -EXP, +EXP, +LIN). The display shows the resulting + level across the keyboard, its three handles drag the break point and the two amounts. Dragging an amount through the centre line flips its curve between - and +. ## Credits diff --git a/packages/app/manual/public/modulation.md b/packages/app/manual/public/modulation.md index 46d8bafa0..4c781e691 100644 --- a/packages/app/manual/public/modulation.md +++ b/packages/app/manual/public/modulation.md @@ -1,7 +1,7 @@ # Modulation Modulators are project-wide sources that move device parameters on their own. They sit on the **Modulation** -screen (⇧3), and one modulator can drive as many parameters as you like, each with its own depth. +screen (⇧3), and one modulator can drive as many parameters as you like, each with its own amount. --- @@ -36,25 +36,25 @@ checked entry removes that assignment. Each editor carries the list of parameters it drives, on its right. Every row has: - the parameter's owner and name, click the owner to jump to that device -- a **depth**, dragged like any other control, signed so a negative depth inverts the modulator +- an **amount**, dragged like any other control, signed so a negative amount inverts the modulator - an **enable** toggle for that one assignment -The depth is itself a parameter, so right-clicking it offers the same menu a device control does: **Create -Automation**, **Learn Midi Control** and **Modulate**. It lights up with the same ring, so a depth that is +The amount is itself a parameter, so right-clicking it offers the same menu a device control does: **Create +Automation**, **Learn Midi Control** and **Modulate**. It lights up with the same ring, so an amount that is being driven is visible at a glance. --- -## 3. Modulating a Depth +## 3. Modulating an Amount -Because a depth takes **Modulate**, one modulator can set how far another reaches. An LFO on a filter with its -depth driven by a slow second LFO opens and closes the amount of movement over time, rather than the filter +Because an amount takes **Modulate**, one modulator can set how far another reaches. An LFO on a filter with its +amount driven by a slow second LFO opens and closes the amount of movement over time, rather than the filter itself. The assignment shows up in the driving modulator's target list like any other row, named after the whole -chain, so `LFO → Vaporisateur Cutoff` tells you it is the depth of that assignment being moved, not the cutoff. +chain, so `LFO → Vaporisateur Cutoff` tells you it is the amount of that assignment being moved, not the cutoff. -Depth is the only modulator-side parameter that takes modulation. A modulator's own controls take automation +Amount is the only modulator-side parameter that takes modulation. A modulator's own controls take automation and MIDI, but not another modulator. --- @@ -144,9 +144,9 @@ want several parameters under a single hand, and it is the natural target for MI ## 10. Modulator Automation -A modulator's own controls are parameters too, and so is every depth in its target list. Right-click one and +A modulator's own controls are parameters too, and so is every amount in its target list. Right-click one and choose **Create Automation** to give it a lane, and the lane appears in the timeline in its own group, after -every audio unit. A depth's lane belongs to the modulator that drives it, so it sits with that modulator's own +every audio unit. An amount's lane belongs to the modulator that drives it, so it sits with that modulator's own lanes and moves with it when the modulator is replaced. The controls that shape a pattern rather than ride a curve are ordinary fields and carry no lane: the Steps @@ -158,7 +158,7 @@ count and mode, and the Random loop, seed and levels. The **⋮** on each editor: -- **Replace with** turns the modulator into another kind in place. The targets, their depths and their depth +- **Replace with** turns the modulator into another kind in place. The targets, their amounts and their amount automation all survive. The old kind's own settings do not - **Duplicate** makes a copy with the same settings and no targets - **Delete** removes the modulator and every assignment on it diff --git a/packages/app/manual/src/Manuals.ts b/packages/app/manual/src/Manuals.ts index 5c8985024..4a6d9048a 100644 --- a/packages/app/manual/src/Manuals.ts +++ b/packages/app/manual/src/Manuals.ts @@ -91,7 +91,7 @@ export const Manuals: ReadonlyArray = [ {type: "page", label: "Revamp", path: "/manuals/devices/audio/revamp", icon: IconSymbol.EQ}, {type: "page", label: "Stereo Split", path: "/manuals/devices/audio/stereo-composite", icon: IconSymbol.StereoSplit}, {type: "page", label: "Stereo Tool", path: "/manuals/devices/audio/stereotool", icon: IconSymbol.Stereo}, - {type: "page", label: "Tidal", path: "/manuals/devices/audio/tidal", icon: IconSymbol.Tidal}, + {type: "page", label: "ESC LFO TOOL", path: "/manuals/devices/audio/tidal", icon: IconSymbol.Tidal}, {type: "page", label: "Tone3000", path: "/manuals/devices/audio/neural-amp", icon: IconSymbol.Tone3000}, {type: "page", label: "Vocoder", path: "/manuals/devices/audio/vocoder", icon: IconSymbol.Vocoder}, {type: "page", label: "Waveshaper", path: "/manuals/devices/audio/waveshaper", icon: IconSymbol.Curve}, diff --git a/packages/app/studio/package.json b/packages/app/studio/package.json index c4e62b1d6..39608d323 100644 --- a/packages/app/studio/package.json +++ b/packages/app/studio/package.json @@ -12,6 +12,7 @@ "test": "vitest run --config vitest.config.ts" }, "dependencies": { + "@coderline/alphatab": "1.9.0", "@opendaw/lib-box": "^0.0.104", "@opendaw/lib-dom": "^0.0.96", "@opendaw/lib-dsp": "^0.0.102", @@ -40,6 +41,7 @@ "monaco-editor": "^0.54.0" }, "devDependencies": { + "@coderline/alphatab-vite": "1.9.0", "@opendaw/eslint-config": "^0.0.28", "@opendaw/typescript-config": "^0.0.32", "@types/node": "^22.15.18", diff --git a/packages/app/studio/src/AppDialogs.tsx b/packages/app/studio/src/AppDialogs.tsx index ef63d2987..21b08c3fe 100644 --- a/packages/app/studio/src/AppDialogs.tsx +++ b/packages/app/studio/src/AppDialogs.tsx @@ -28,7 +28,7 @@ export const showStoragePersistDialog = (): Promise => {

Preferences - Privacy & Security - Cookies & Site Data - Manage Exceptions...

-

and add opendaw.studio to the list. You will never be bothered again.

+

and add {location.hostname} to the list. You will never be bothered again.

) diff --git a/packages/app/studio/src/boot.ts b/packages/app/studio/src/boot.ts index a0191d0ad..b7a54acf7 100644 --- a/packages/app/studio/src/boot.ts +++ b/packages/app/studio/src/boot.ts @@ -1,4 +1,6 @@ import "./main.sass" +import {EscDeepLink} from "@/service/EscDeepLink" +import {ImportProgressPanel} from "@/ui/esc/ImportProgressPanel" // Escape hatch for AUTOMATED browser sessions (set `localStorage["opendaw-suppress-unload-guard"]`): // swallow every beforeunload prompt so scripted reloads never hang on the native dialog. @@ -8,7 +10,7 @@ if (localStorage.getItem("opendaw-suppress-unload-guard") !== null) { import {App} from "@/ui/App.tsx" import {isDefined, panic, Progress, RuntimeNotification, RuntimeNotifier, UUID} from "@opendaw/lib-std" import {StudioService} from "@/service/StudioService" -import {SampleMetaData, SoundfontMetaData} from "@opendaw/studio-adapters" +import {CrusherRates, SampleMetaData, SoundfontMetaData} from "@opendaw/studio-adapters" import {Dialogs} from "@/ui/components/dialogs.tsx" import {installCursors} from "@/ui/Cursors.ts" import {BuildInfo} from "./BuildInfo" @@ -40,7 +42,8 @@ import {ErrorHandler} from "@/errors/ErrorHandler.ts" import {ReportNames} from "@/errors/ReportNames" import {AudioData} from "@opendaw/lib-dsp" import {ChainedSampleProvider, ChainedSoundfontProvider, ChainedWclapProvider} from "@opendaw/studio-p2p" -import {IconSymbol, initializeColors, setColorScheme} from "@opendaw/studio-enums" +import {IconSymbol, initializeColors, setColorScheme, setCustomTheme} from "@opendaw/studio-enums" +import type {EscThemeCustom} from "@opendaw/studio-enums" import {SampleGarbage, StudioPreferences, WclapStorage} from "@opendaw/studio-core" import {StudioShortcutManager} from "@/service/StudioShortcutManager" import {Tour} from "@/ui/tour/Tour" @@ -82,11 +85,13 @@ export const boot = async ({workersUrl, workletsUrl, wasmProcessorUrl, wasmOffli console.debug("requesting custom sampleRate", sampleRate ?? "'No (Firefox)'") const context = new AudioContext({sampleRate, latencyHint: 0}) console.debug(`AudioContext state: ${context.state}, sampleRate: ${context.sampleRate}`) + CrusherRates.setEngineSampleRate(context.sampleRate) // ESC: Crusher shows its real hold rate in Hz console.debug(`Error.stackTraceLimit: ${Error.stackTraceLimit ?? "N/A"}`) // purge identifiers written by the old client-side counting (§25 TDDDG) localStorage.removeItem("__id__") localStorage.removeItem("reported-latencies") - reportVisitor() + // ESC: do not ping api.opendaw.studio from the ESC host unless explicitly enabled + if ((globalThis as any).ESC_DAW_CONFIG?.reportVisitor === true) {reportVisitor()} const audioWorklets = await Promises.tryCatch(AudioWorklets.createFor(context)) if (audioWorklets.status === "rejected") { return panic(audioWorklets.error) @@ -166,8 +171,41 @@ export const boot = async ({workersUrl, workletsUrl, wasmProcessorUrl, wasmOffli TouchContextMenu.install(surface.owner)) }, errorHandler) Surface.subscribeKeyboard("keydown", event => ShortcutManager.get().handleEvent(event), Number.MAX_SAFE_INTEGER) + // ESC STUDIO theme system: apply the saved custom theme (Theme Studio panel) + // before the appearance subscription initialises colours, and keep it live + // afterwards. The shell (overlay/esc/esc-theme.js) dispatches esc-theme-apply + // / esc-theme-clear window events; the theme is persisted under the + // "esc-studio-theme" localStorage key. The custom theme only applies while + // the "esc" scheme is active — picking a built-in scheme shows that scheme + // as-is, and re-picking "esc" restores the saved theme. + const applyStoredEscTheme = (): void => { + try { + const stored = JSON.parse(localStorage.getItem("esc-studio-theme") ?? "null") as (EscThemeCustom & { enabled?: boolean, palette?: EscThemeCustom }) | null + // ESC: newer shells store the exact palette payload (incl. MIDI note colours) under `palette`; + // older stored themes have none and keep the previous behaviour. + if (stored && stored.enabled !== false) { setCustomTheme(stored.palette ?? stored) } + } catch { /* corrupted stored theme — fall back to the built-in scheme */ } + } + applyStoredEscTheme() + window.addEventListener("esc-theme-apply", (event: Event) => { + setCustomTheme((event as CustomEvent).detail) + Surface.forEach(surface => initializeColors(surface.owner.document.documentElement)) + // ESC build 4: picking a theme in Theme Studio is an app-wide choice. The custom theme only loads at start-up + // while the colour scheme preference is "esc", so a profile still set to another scheme (an older build's + // Classic/Modern/Contrast) used to lose the theme on the next project or reload. Pin the scheme to "esc". + if (StudioPreferences.settings.appearance["color-scheme"] !== "esc") { + StudioPreferences.settings.appearance["color-scheme"] = "esc" + } + }) + window.addEventListener("esc-theme-clear", () => { + setCustomTheme(null) + Surface.forEach(surface => initializeColors(surface.owner.document.documentElement)) + }) + // ESC: the shell may have applied its theme before these listeners existed; ask it to re-send. + window.dispatchEvent(new CustomEvent("esc-theme-request")) StudioPreferences.catchupAndSubscribe(({"color-scheme": scheme}) => { setColorScheme(scheme) + if (scheme === "esc") { applyStoredEscTheme() } else { setCustomTheme(null) } Surface.forEach(surface => initializeColors(surface.owner.document.documentElement)) }, "appearance") document.querySelector("#preloader")?.remove() @@ -180,6 +218,8 @@ export const boot = async ({workersUrl, workletsUrl, wasmProcessorUrl, wasmOffli }) replaceChildren(surface.ground, App(service)) Tour.install(service) + EscDeepLink.apply(service) + ImportProgressPanel.install() AnimationFrame.start(window) installCursors() const opfsProbe = await Promises.tryCatch(Workers.Opfs.isAvailable()) diff --git a/packages/app/studio/src/errors/ErrorHandler.ts b/packages/app/studio/src/errors/ErrorHandler.ts index 837c2c15a..37722f0d5 100644 --- a/packages/app/studio/src/errors/ErrorHandler.ts +++ b/packages/app/studio/src/errors/ErrorHandler.ts @@ -105,7 +105,7 @@ export class ErrorHandler { this.#chunkLoadDialogOpen = true Dialogs.info({ headline: "Couldn't Load Component", - message: "A part of openDAW failed to load, usually a temporary network issue. Your project is unaffected — please try again." + message: "A part of openDAW failed to load, usually a temporary network issue. Your project is unaffected - please try again." }).finally(() => {this.#chunkLoadDialogOpen = false}) } @@ -335,7 +335,10 @@ export class ErrorHandler { : {name: error.name, stack: error.stack?.slice(0, maxStackSize)}, logs: isDefined(scrub) ? logs.map(entry => ({...entry, args: entry.args.map(scrub)})) : [] } satisfies ErrorLog) - return fetch("https://logs.opendaw.studio/log.php", { + // ESC: never send ESC users' error logs to openDAW's log server; only to an ESC endpoint if one is configured + const escConfig = (globalThis as any).ESC_DAW_CONFIG + if (isDefined(escConfig) && !isDefined(escConfig.errorLogUrl)) {return Promise.resolve(new Response("error log disabled on this host"))} + return fetch(escConfig?.errorLogUrl ?? "https://logs.opendaw.studio/log.php", { method: "POST", headers: {"Content-Type": "application/json"}, body diff --git a/packages/app/studio/src/opendaw-api/CommunityAPI.ts b/packages/app/studio/src/opendaw-api/CommunityAPI.ts index 9d247add5..8642ff3b4 100644 --- a/packages/app/studio/src/opendaw-api/CommunityAPI.ts +++ b/packages/app/studio/src/opendaw-api/CommunityAPI.ts @@ -8,7 +8,11 @@ import {WclapIndex} from "./WclapIndex" export class CommunityAPI { static readonly FileRoot = "https://assets.opendaw.studio/community" - static readonly ScriptsIndexFile = `${CommunityAPI.FileRoot}/scripts/index.json` + // ESC: the community SCRIPT library is the ESC pack, shipped with this build + // and served same-origin under /community. The WebCLAP catalogue stays on + // openDAW's host (FileRoot) so its behaviour is unchanged. + static readonly ScriptsFileRoot = "/community" + static readonly ScriptsIndexFile = `${CommunityAPI.ScriptsFileRoot}/scripts/index.json` static readonly WclapsIndexFile = `${CommunityAPI.FileRoot}/wclaps/index.json` @Lazy @@ -45,11 +49,11 @@ export class CommunityAPI { } screenshotOf(entry: CommunityScript): Option { - return Option.wrap(entry.screenshot).map(name => `${CommunityAPI.FileRoot}/scripts/${name}`) + return Option.wrap(entry.screenshot).map(name => `${CommunityAPI.ScriptsFileRoot}/scripts/${name}`) } loadSource(entry: CommunityScript): Promise { - return retryTransient(() => fetch(`${CommunityAPI.FileRoot}/scripts/${entry.url}`, OpenDAWHeaders) + return retryTransient(() => fetch(`${CommunityAPI.ScriptsFileRoot}/scripts/${entry.url}`, OpenDAWHeaders) .then(response => response.ok ? response.text() : Promise.reject(new HttpStatus(response.status))), 4) .then(source => entry.target === "project" ? ScriptPreamble.strip(source) : source) } diff --git a/packages/app/studio/src/opendaw-api/FreesoundAPI.ts b/packages/app/studio/src/opendaw-api/FreesoundAPI.ts new file mode 100644 index 000000000..6fe969c70 --- /dev/null +++ b/packages/app/studio/src/opendaw-api/FreesoundAPI.ts @@ -0,0 +1,177 @@ +// ESC: live Freesound search for the Samples tab. +// +// The browser talks ONLY to the same-origin proxy (/api/freesound/search) — +// the Freesound API key never reaches the client. Search goes through the +// proxy; preview audio is streamed through /api/freesound/preview so it +// plays under the studio's Cross-Origin-Embedder-Policy. +// +// Freesound APIv2 docs: https://freesound.org/docs/api/ +// Search: GET https://freesound.org/apiv2/search/ +// Previews need no OAuth2; downloading a sound in its original quality +// (GET /apiv2/sounds//download/) DOES — see INTEGRATION.md for the +// "Log in with Freesound" upgrade path. This build ships previews only. + +import {Lazy} from "@opendaw/lib-std" +import {z} from "zod" +import {HttpStatus, retryTransient} from "./HttpRetry" + +// Freesound's own licence strings, as returned by the API. +export const FreesoundLicense = z.enum(["Attribution", "Attribution NonCommercial", "Creative Commons 0"]) +export type FreesoundLicense = z.infer + +// Freesound returns `license` as a URL, not a display name — e.g. +// http://creativecommons.org/publicdomain/zero/1.0/ for CC0, +// https://creativecommons.org/licenses/by/3.0/ for CC-BY, +// https://creativecommons.org/licenses/by-nc/4.0/ for CC-BY-NC. +// Map the URL to the display name before validation. Display names are +// accepted too (defensive — the docs show them). Anything unrecognized +// returns undefined and the row is dropped by the response schema, so one +// bad row never fails the whole search. +const licenseFromValue = (value: unknown): FreesoundLicense | undefined => { + if (typeof value !== "string") {return undefined} + const normalized = value.trim().toLowerCase().replace(/^https?:\/\//, "").replace(/\/+$/, "") + switch (normalized) { + case "creativecommons.org/publicdomain/zero/1.0": + case "creative commons 0": + return "Creative Commons 0" + case "creativecommons.org/licenses/by/3.0": + case "creativecommons.org/licenses/by/4.0": + case "attribution": + return "Attribution" + case "creativecommons.org/licenses/by-nc/3.0": + case "creativecommons.org/licenses/by-nc/4.0": + case "attribution noncommercial": + return "Attribution NonCommercial" + default: + return undefined + } +} + +// Short labels shown on every result. CC-BY-NC is called out plainly: +// those sounds may not be used in releases that are sold. +export const licenseShort = (license: FreesoundLicense): string => { + switch (license) { + case "Creative Commons 0": return "CC0" + case "Attribution": return "CC-BY" + case "Attribution NonCommercial": return "CC-BY-NC" + } +} + +export const licenseIsNonCommercialOnly = (license: FreesoundLicense): boolean => + license === "Attribution NonCommercial" + +const FreesoundPreviews = z.object({ + "preview-hq-mp3": z.string().optional(), + "preview-lq-mp3": z.string().optional(), + "preview-hq-ogg": z.string().optional(), + "preview-lq-ogg": z.string().optional() +}) + +export const FreesoundSound = z.object({ + id: z.number(), + name: z.string(), + tags: z.array(z.string()).default([]), + username: z.string(), + license: FreesoundLicense, + duration: z.number(), + type: z.string().optional(), + previews: FreesoundPreviews, + url: z.string(), + num_downloads: z.number().optional(), + avg_rating: z.number().optional(), + created: z.string().optional() +}) +export type FreesoundSound = z.infer + +const FreesoundSearchResponse = z.object({ + count: z.number(), + next: z.string().nullable(), + previous: z.string().nullable(), + // Map each result's licence URL to its display name first; rows whose + // licence is unrecognized are dropped, never failed as a whole search. + results: z.preprocess((value) => { + if (!Array.isArray(value)) {return value} + return value + .map((raw) => { + if (raw === null || typeof raw !== "object") {return raw} + const license = licenseFromValue((raw as {license?: unknown}).license) + return license === undefined ? undefined : {...raw, license} + }) + .filter((raw) => raw !== undefined) + }, z.array(FreesoundSound)) +}) +export type FreesoundSearchResponse = z.infer + +// The licence filter the UI offers. Default is CC0 + CC-BY: CC-BY-NC sounds +// are opt-in because they may not be used in commercial releases. +export type FreesoundLicenseFilter = "cc0-ccby" | "cc0" | "cc-by" | "cc-by-nc" | "all" +export type FreesoundSort = "score" | "created_desc" | "downloads_desc" | "rating_desc" + +export type FreesoundSearchParams = { + query: string + license: FreesoundLicenseFilter + sort: FreesoundSort + page: number + pageSize: number +} + +// The one-line credit the licence requires for CC-BY / CC-BY-NC sounds. +// Shown on every result and again when a sound is added to a project. +export const attributionLine = (sound: FreesoundSound): string => + `"${sound.name}" by ${sound.username} (${licenseShort(sound.license)}) - https://freesound.org/s/${sound.id}/` + +// Best preview for playback: high-quality mp3 first, then ogg, then low. +const previewSource = (sound: FreesoundSound): string | undefined => + sound.previews["preview-hq-mp3"] + ?? sound.previews["preview-hq-ogg"] + ?? sound.previews["preview-lq-mp3"] + ?? sound.previews["preview-lq-ogg"] + +// Same-origin proxy URL for a preview. The proxy only serves Freesound's +// own preview host, so a tampered URL cannot turn it into an open proxy. +export const previewUrl = (sound: FreesoundSound): string | undefined => { + const source = previewSource(sound) + return source === undefined + ? undefined + : `/api/freesound/preview?url=${encodeURIComponent(source)}` +} + +export class FreesoundAPI { + @Lazy + static get(): FreesoundAPI {return new FreesoundAPI()} + + private constructor() {} + + async search({query, license, sort, page, pageSize}: FreesoundSearchParams): Promise { + const url = new URL("/api/freesound/search", window.location.origin) + url.searchParams.set("q", query) + url.searchParams.set("license", license) + url.searchParams.set("sort", sort) + url.searchParams.set("page", String(page)) + url.searchParams.set("page_size", String(pageSize)) + const response = await retryTransient(() => fetch(url.toString(), {headers: {Accept: "application/json"}}) + .then(async response => { + if (response.ok) {return response} + // The proxy answers errors as {error, detail} — surface its + // words in the thrown error instead of the generic string. + let detail: string | undefined + try { + const errorJson = await response.json() as {detail?: unknown} + if (typeof errorJson.detail === "string" && errorJson.detail.length > 0) { + detail = errorJson.detail + } + } catch { /* keep the status-only error */ } + return Promise.reject(new HttpStatus(response.status, detail)) + }), 2) + const json = await response.json() + const parsed = FreesoundSearchResponse.safeParse(json) + if (!parsed.success) { + // The proxy answers errors as {error, detail} — surface the words. + const detail = (json as {detail?: unknown})?.detail + throw new Error(typeof detail === "string" && detail.length > 0 + ? detail + : "Freesound search failed - try again.") + } + return parsed.data + } +} diff --git a/packages/app/studio/src/opendaw-api/HttpRetry.ts b/packages/app/studio/src/opendaw-api/HttpRetry.ts index c2ba48841..9be88df23 100644 --- a/packages/app/studio/src/opendaw-api/HttpRetry.ts +++ b/packages/app/studio/src/opendaw-api/HttpRetry.ts @@ -2,7 +2,7 @@ import {Errors} from "@opendaw/lib-std" import {Promises} from "@opendaw/lib-runtime" export class HttpStatus extends Error { - constructor(readonly status: number) {super(`HTTP ${status}`)} + constructor(readonly status: number, message?: string) {super(message ?? `HTTP ${status}`)} } // network failures and 5xx are worth another attempt, a 4xx answer will not change diff --git a/packages/app/studio/src/opendaw-api/IndexFetch.ts b/packages/app/studio/src/opendaw-api/IndexFetch.ts index 9c9cb0575..34efbf59f 100644 --- a/packages/app/studio/src/opendaw-api/IndexFetch.ts +++ b/packages/app/studio/src/opendaw-api/IndexFetch.ts @@ -21,3 +21,35 @@ export const fetchIndex = (url: string, headers: RequestInit): Promise }) .then(response => response.ok ? response.json() : panic(`${response.status} ${response.statusText}`)), IndefiniteRetry(url)) + +// ESC: the openDAW catalogues (assets.opendaw.studio) send no Access-Control-Allow-Origin for the ESC host, so +// fetchIndex above retries forever and never settles. Anything that awaits it (the Samples/Soundfonts tab's +// Online AND Local views, project-load missing-file checks) then hangs on its loader for good. Callers that only +// need the catalogue as an optional extra race it against this grace period and fall back to an empty index; +// the indefinite retry keeps running in the background and later calls pick it up once it resolves. +export const CatalogGrace = TimeSpan.seconds(6) +// +// The outcome is remembered per catalogue promise: once it resolved, callers get it at once; once the grace period +// ran out, later callers get the fallback at once instead of each waiting another 6 s (that per-call wait made the +// Local view take 6 s on every reload and let overlapping list loads race - 2026-11-19 browser check). +type CatalogState = {resolved: boolean, value: unknown, timedOut: boolean} +const catalogStates = new WeakMap, CatalogState>() +export const withCatalogFallback = (pending: Promise, fallback: T): Promise => { + let state = catalogStates.get(pending) + if (state === undefined) { + const fresh: CatalogState = {resolved: false, value: undefined, timedOut: false} + catalogStates.set(pending, fresh) + pending.then(value => {fresh.resolved = true; fresh.value = value}, () => {}) + state = fresh + } + if (state.resolved) {return Promise.resolve(state.value as T)} + if (state.timedOut) {return Promise.resolve(fallback)} + const current = state + return Promise.race([pending, new Promise(resolve => setTimeout(() => { + if (!current.resolved && !current.timedOut) { + current.timedOut = true + console.warn("[ESC] openDAW catalogue unreachable, continuing without it") + } + resolve(fallback) + }, CatalogGrace.millis()))]) +} diff --git a/packages/app/studio/src/opendaw-api/OpenPresetAPI.ts b/packages/app/studio/src/opendaw-api/OpenPresetAPI.ts index a04a35ade..2bf321007 100644 --- a/packages/app/studio/src/opendaw-api/OpenPresetAPI.ts +++ b/packages/app/studio/src/opendaw-api/OpenPresetAPI.ts @@ -3,23 +3,33 @@ import { } from "@opendaw/lib-std" import {network, Promises} from "@opendaw/lib-runtime" import {AccessKey} from "./AccessKey" -import {base64Credentials, OpenDAWHeaders} from "./OpenDAWHeaders" +import {base64Credentials} from "./OpenDAWHeaders" import {PresetMeta} from "@opendaw/studio-core" +import {withCatalogFallback} from "./IndexFetch" export class OpenPresetAPI { static readonly ApiRoot = "https://api.opendaw.studio/presets" - static readonly FileRoot = "https://assets.opendaw.studio/presets" + // ESC: assets.opendaw.studio sends no Access-Control-Allow-Origin for the ESC host, so the stock preset index and + // every .odp failed and the preset library was empty. The ESC preset library (esc-studio-presets, CC0) is served + // same-origin from dist/presets/ by postbuild (same pattern as CommunityAPI.ScriptsFileRoot). Uploads stay on ApiRoot. + static readonly FileRoot = "/presets" + // "OPRE", the PresetHeader magic: a static host answers a missing file with index.html (HTTP 200), so a load is + // only accepted when the bytes really are a preset. + static readonly #Magic = [0x4F, 0x50, 0x52, 0x45] @Lazy static get(): OpenPresetAPI {return new OpenPresetAPI()} private constructor() {} + // ESC: never block the preset browser on the index - after the shared 6 s grace it continues with no stock presets + list(): Promise> {return withCatalogFallback(this.fetchList(), [])} + @Lazy - async list(): Promise> { + private async fetchList(): Promise> { const url = `${OpenPresetAPI.FileRoot}/index.json?t=${Date.now()}` const result = await Promises.tryCatch(Promises.retry(() => - network.defaultFetch(url, OpenDAWHeaders).then(response => response.json()))) + network.defaultFetch(url, {}).then(response => response.ok ? response.json() : panic(`${response.status}`)))) if (result.status === "rejected") { console.warn("OpenPresetAPI.list fetch failed", url, result.error) return [] @@ -28,19 +38,32 @@ export class OpenPresetAPI { console.warn("OpenPresetAPI.list unexpected payload", result.value) return [] } - console.info(`OpenPresetAPI.list loaded ${result.value.length} cloud preset(s)`) - return result.value as ReadonlyArray + const entries = result.value.filter((entry: any) => isDefined(entry) && typeof entry.uuid === "string" + && typeof entry.name === "string" && typeof entry.category === "string") + console.info(`OpenPresetAPI.list loaded ${entries.length} stock preset(s)`) + return entries as ReadonlyArray } async load(uuid: UUID.Bytes, progress?: Procedure, signal?: AbortSignal): Promise { + return this.#download(uuid, progress, signal).then(buffer => { + const head = new Uint8Array(buffer, 0, Math.min(4, buffer.byteLength)) + return head.length === 4 && OpenPresetAPI.#Magic.every((byte, index) => head[index] === byte) + ? buffer + : panic(`Preset ${UUID.toString(uuid)} is not available (no preset data at ${OpenPresetAPI.FileRoot})`) + }) + } + + async #download(uuid: UUID.Bytes, + progress?: Procedure, + signal?: AbortSignal): Promise { const url = `${OpenPresetAPI.FileRoot}/${UUID.toString(uuid)}.odp` // Cancellable fetches skip the auto-retry: a user-cancelled download // must not silently retry after the dialog has already closed. const response = isDefined(signal) - ? await network.limitFetch(url, {...OpenDAWHeaders, signal}) - : await Promises.retry(() => network.limitFetch(url, OpenDAWHeaders)) + ? await network.limitFetch(url, {signal}) + : await Promises.retry(() => network.limitFetch(url, {})) if (!response.ok) { return panic(`Failed to fetch preset ${UUID.toString(uuid)}: ${response.status} ${response.statusText}`) } diff --git a/packages/app/studio/src/opendaw-api/OpenSampleAPI.ts b/packages/app/studio/src/opendaw-api/OpenSampleAPI.ts index f93462ce5..9d23b825e 100644 --- a/packages/app/studio/src/opendaw-api/OpenSampleAPI.ts +++ b/packages/app/studio/src/opendaw-api/OpenSampleAPI.ts @@ -16,7 +16,7 @@ import {SampleAPI} from "@opendaw/studio-core" import {AccessKey} from "./AccessKey" import {base64Credentials, OpenDAWHeaders} from "./OpenDAWHeaders" import {SampleIndex} from "./SampleIndex" -import {fetchIndex} from "./IndexFetch" +import {fetchIndex, withCatalogFallback} from "./IndexFetch" import {z} from "zod" import {AudioData, WavFile} from "@opendaw/lib-dsp" @@ -36,9 +36,11 @@ export class OpenSampleAPI implements SampleAPI { private constructor() {} - async tree(): Promise {return this.#memoized()} + // ESC: never block on the (unreachable) openDAW catalogue, see withCatalogFallback + readonly #available = (): Promise => withCatalogFallback(this.#memoized(), {version: 1, folders: []}) - async all(): Promise> {return SampleIndex.flatten(await this.#memoized())} + async tree(): Promise {return this.#available()} + async all(): Promise> {return SampleIndex.flatten(await this.#available())} async get(uuid: UUID.Bytes): Promise { const uuidAsString = UUID.toString(uuid) diff --git a/packages/app/studio/src/opendaw-api/OpenSoundfontAPI.ts b/packages/app/studio/src/opendaw-api/OpenSoundfontAPI.ts index 22162de38..87e355db7 100644 --- a/packages/app/studio/src/opendaw-api/OpenSoundfontAPI.ts +++ b/packages/app/studio/src/opendaw-api/OpenSoundfontAPI.ts @@ -2,7 +2,7 @@ import {asDefined, Lazy, Procedure, unitValue, UUID} from "@opendaw/lib-std" import {Soundfont, SoundfontMetaData} from "@opendaw/studio-adapters" import {OpenDAWHeaders} from "./OpenDAWHeaders" import {SoundfontIndex} from "./SoundfontIndex" -import {fetchIndex} from "./IndexFetch" +import {fetchIndex, withCatalogFallback} from "./IndexFetch" import {Promises} from "@opendaw/lib-runtime" export class OpenSoundfontAPI { @@ -18,9 +18,11 @@ export class OpenSoundfontAPI { private constructor() {} - async tree(): Promise {return this.#memoized()} + // ESC: never block on the (unreachable) openDAW catalogue, see withCatalogFallback + readonly #available = (): Promise => withCatalogFallback(this.#memoized(), {version: 1, folders: []}) - async all(): Promise> {return SoundfontIndex.flatten(await this.#memoized())} + async tree(): Promise {return this.#available()} + async all(): Promise> {return SoundfontIndex.flatten(await this.#available())} async get(uuid: UUID.Bytes): Promise { const uuidAsString = UUID.toString(uuid) diff --git a/packages/app/studio/src/opendaw-api/index.ts b/packages/app/studio/src/opendaw-api/index.ts index 32ff18fa1..3f43ed0b5 100644 --- a/packages/app/studio/src/opendaw-api/index.ts +++ b/packages/app/studio/src/opendaw-api/index.ts @@ -1,5 +1,6 @@ export * from "./OpenDAWHeaders" export * from "./OpenSampleAPI" +export * from "./FreesoundAPI" export * from "./OpenSoundfontAPI" export * from "./OpenPresetAPI" export * from "./OpenWclapAPI" diff --git a/packages/app/studio/src/perf/DeviceBenchmark.ts b/packages/app/studio/src/perf/DeviceBenchmark.ts index 86c65d59d..227730fae 100644 --- a/packages/app/studio/src/perf/DeviceBenchmark.ts +++ b/packages/app/studio/src/perf/DeviceBenchmark.ts @@ -390,7 +390,7 @@ export const runAllBenchmarks = async ( const failure = (result: RenderResult | string, expectAudio: boolean): string | undefined => { if (typeof result === "string") {return result} if (expectAudio && result.peak < SILENCE_THRESHOLD) { - return `silent — no audio produced (peak ${result.peak.toExponential(2)})` + return `silent - no audio produced (peak ${result.peak.toExponential(2)})` } return undefined } diff --git a/packages/app/studio/src/service/EscDeepLink.ts b/packages/app/studio/src/service/EscDeepLink.ts new file mode 100644 index 000000000..4503751df --- /dev/null +++ b/packages/app/studio/src/service/EscDeepLink.ts @@ -0,0 +1,100 @@ +// ESC: deep links into the studio, for the globe's Studio tab. +// +// / -> the studio dashboard (default, unchanged) +// /?view=studio -> same as / +// /?view=score -> opens a project (the recovered one, or a new empty +// project if none) and shows the Score view expanded to +// fill the window +// /?view=dj -> RESERVED for ESC DJ. Until DJ exists it falls back to +// the dashboard with a short "coming soon" note. +// +// The view is read once at boot. Unknown values fall back to the dashboard. + +import {RuntimeNotifier} from "@opendaw/lib-std" +import {StudioService} from "@/service/StudioService" +import {BrowseScope} from "@/ui/browse/BrowseScope" +import {PanelType} from "@/ui/workspace/PanelType" + +export namespace EscDeepLink { + export type View = "studio" | "score" | "dj" + + let expandScoreOnOpen = false + + export const read = (): View => { + const value = new URLSearchParams(location.search).get("view")?.trim().toLowerCase() + return value === "score" || value === "dj" ? value : "studio" + } + + // The Score view asks this once when it mounts: true when the studio was + // opened through ?view=score, so the view starts expanded. + export const consumeExpandScore = (): boolean => { + const value = expandScoreOnOpen + expandScoreOnOpen = false + return value + } + + export const apply = (service: StudioService): void => { + const view = read() + if (view === "score") { + expandScoreOnOpen = true + let done = false + const showScore = (): void => { + if (done) {return} + done = true + setTimeout(() => { + service.layout.browseScope.setValue(BrowseScope.Score) + service.panelLayout.showIfAvailable(PanelType.BrowserPanel) + }, 250) + } + const subscription = service.projectProfileService.catchupAndSubscribe(option => { + if (option.nonEmpty()) { + showScore() + queueMicrotask(() => subscription.terminate()) + } + }) + // Give crash recovery a moment to restore a project first; open a + // new empty project only when nothing was restored. + setTimeout(() => { + if (!service.hasProfile) {service.newProject().catch(console.warn)} + }, 1200) + } else if (view === "dj") { + // The toast alone was easy to miss (it fades after a few seconds and boot dialogs can cover it), so the + // note also shows as a banner at the top of the window until closed or for 20 seconds. + const message = "ESC DJ is coming soon. Opening the studio instead." + const show = (): void => { + RuntimeNotifier.notify({message, icon: "Info"}) + showBanner(message) + } + if (document.readyState === "complete") {setTimeout(show, 1500)} else { + window.addEventListener("load", () => setTimeout(show, 1500), {once: true}) + } + } + } + + const showBanner = (message: string): void => { + if (document.getElementById("esc-deeplink-banner") !== null) {return} + const banner = document.createElement("div") + banner.id = "esc-deeplink-banner" + banner.setAttribute("role", "status") + banner.style.cssText = [ + "position:fixed", "top:12px", "left:50%", "transform:translateX(-50%)", "z-index:2147483000", + "display:flex", "align-items:center", "gap:12px", "max-width:calc(100vw - 24px)", "box-sizing:border-box", + "padding:10px 14px", "background:#12100a", "color:#f5f0e6", "border:1px solid #d4af37", + "border-radius:6px", "box-shadow:0 6px 24px rgba(0,0,0,0.6)", + "font:600 13px Oswald, 'Arial Narrow', sans-serif", "letter-spacing:0.06em", "text-transform:uppercase" + ].join(";") + const text = document.createElement("span") + text.textContent = message + const close = document.createElement("button") + close.type = "button" + close.textContent = "Close" + close.setAttribute("aria-label", "Close") + close.style.cssText = "background:#d4af37;color:#12100a;border:0;border-radius:4px;padding:4px 10px;" + + "font:inherit;cursor:pointer" + const remove = (): void => banner.remove() + close.addEventListener("click", remove) + banner.append(text, close) + document.body.appendChild(banner) + setTimeout(remove, 20_000) + } +} diff --git a/packages/app/studio/src/service/Mixdowns.ts b/packages/app/studio/src/service/Mixdowns.ts index 7def62190..ed4c41ac0 100644 --- a/packages/app/studio/src/service/Mixdowns.ts +++ b/packages/app/studio/src/service/Mixdowns.ts @@ -11,6 +11,7 @@ import { import {Files} from "@opendaw/lib-dom" import {Promises} from "@opendaw/lib-runtime" import {ExportConfiguration} from "@opendaw/studio-adapters" +import {MasteringDeviceBox} from "@opendaw/studio-boxes" import {Dialogs} from "@/ui/components/dialogs" import {AudioFormatSelector} from "@/ui/components/AudioFormatSelector" import {MixdownSectionSelector} from "@/ui/components/MixdownSectionSelector" @@ -83,6 +84,10 @@ export namespace Mixdowns { config: ExportConfiguration, section: MixdownSection): Promise => { const project = source.copy() + // Auto Gain belongs to the mixdown only: stems render without the mastering output trim (the copy only) + project.editing.modify(() => project.boxGraph.boxes().forEach(box => { + if (box instanceof MasteringDeviceBox) {box.outputTrim.setValue(1.0)} + }), false) const abortController = new AbortController() const progress = new DefaultObservableValue(0.0) const dialog = RuntimeNotifier.progress({ diff --git a/packages/app/studio/src/service/NeuralDemux.tsx b/packages/app/studio/src/service/NeuralDemux.tsx index 51089e870..e2314a23e 100644 --- a/packages/app/studio/src/service/NeuralDemux.tsx +++ b/packages/app/studio/src/service/NeuralDemux.tsx @@ -32,7 +32,7 @@ const MODELS: ReadonlyArray = [ { key: "stem-separation", label: "htdemucs v4 (smank, MIT)", - description: "Hybrid Transformer Demucs v4 — drums / bass / other / vocals.\nONNX export: smank/htdemucs-onnx. License: MIT." + description: "Hybrid Transformer Demucs v4 - drums / bass / other / vocals.\nONNX export: smank/htdemucs-onnx. License: MIT." }, { key: "stem-separation-alt", @@ -90,7 +90,7 @@ const pickSelection = async (Inference: typeof InferenceNamespace, const providerSelect: HTMLSelectElement = ( diff --git a/packages/app/studio/src/service/ProjectProfileService.ts b/packages/app/studio/src/service/ProjectProfileService.ts index 6dfea1a74..25da89e10 100644 --- a/packages/app/studio/src/service/ProjectProfileService.ts +++ b/packages/app/studio/src/service/ProjectProfileService.ts @@ -83,12 +83,22 @@ export class ProjectProfileService { }) } + // ESC build 4: a never-saved project with any edit counts as unsaved too, and the dialog defaults to keeping the + // project (Enter or Escape keeps working; only the explicit button discards). Autosave is not part of this. + hasUnsavedWork(): boolean { + return this.#profile.mapOr(profile => profile.hasUnsavedChanges() + || (!profile.saved() && !profile.project.editing.hasNoChanges()), false) + } + async approveLosingChanges(): Promise { - const hasChanges = this.#profile.mapOr(profile => profile.hasUnsavedChanges(), false) - if (!hasChanges) {return true} - return RuntimeNotifier.approve({ - headline: "Closing Project?", - message: "You will lose all progress!" + if (!this.hasUnsavedWork()) {return true} + const name = this.#profile.mapOr(profile => profile.meta.name, "") + return Dialogs.approve({ + headline: "Unsaved changes", + message: `${name.length > 0 ? `'${name}'` : "This project"} has changes that are not saved. Leaving now throws them away. Choose Keep working, then save with Ctrl+S (Cmd+S on a Mac).`, + approveText: "Leave without saving", + cancelText: "Keep working", + reverse: false }) } diff --git a/packages/app/studio/src/service/SamplePlayback.test.ts b/packages/app/studio/src/service/SamplePlayback.test.ts new file mode 100644 index 000000000..4bfcb75ff --- /dev/null +++ b/packages/app/studio/src/service/SamplePlayback.test.ts @@ -0,0 +1,276 @@ +// @vitest-environment jsdom +import {afterEach, beforeEach, describe, expect, it, vi} from "vitest" +import {WavFile} from "@opendaw/lib-dsp" +import {PlaybackEvent, PreviewContext, SamplePlayback} from "./SamplePlayback" + +// studio-core pulls in the audio engine (AudioWorkletNode), which jsdom does not have; the preview only needs +// these names from it, and the loaders are injected by each test. +vi.mock("@opendaw/studio-core", () => ({ + ExternalLib: {SoundFont2: async () => ({status: "rejected", error: new Error("not in tests")})}, + SampleStorage: {Folder: "samples/v2"}, + SoundfontStorage: {get: () => ({load: async () => {throw new Error("none")}})}, + Workers: { + Opfs: { + read: async (path: string) => { + const bytes = (globalThis as any).__opfs?.get(path) + if (bytes === undefined) {throw new Error(`no file ${path}`)} + return bytes + } + } + } +})) +vi.mock("@/opendaw-api", () => ({ + OpenDAWHeaders: {}, OpenSampleAPI: {FileRoot: "https://example.invalid/samples"}, + OpenSoundfontAPI: {FileRoot: "https://example.invalid/soundfonts"} +})) + +// A tiny fake Web Audio graph. The analyser reports what really arrives through source -> gain -> analyser, and +// only when the analyser is connected to the destination and the source was started: a preview that is not routed +// to the output reads as silence, exactly what the level check must catch. +type FakeNode = { connected: Array, connect: (node: unknown) => void, disconnect: () => void } +const makeNode = (extra: T): T & FakeNode => { + const node = { + connected: [] as Array, + connect(target: unknown) {node.connected.push(target)}, + disconnect() {node.connected.length = 0}, + ...extra + } + return node as T & FakeNode +} +const fakeBuffer = (channels: number, length: number, sampleRate: number) => { + const data = Array.from({length: channels}, () => new Float32Array(length)) + return { + numberOfChannels: channels, length, sampleRate, duration: length / sampleRate, + getChannelData: (channel: number) => data[channel], + copyToChannel: (source: Float32Array, channel: number) => data[channel].set(source) + } as unknown as AudioBuffer +} +const makeContext = (options: { routeBroken?: boolean, staySuspended?: boolean } = {}) => { + const destination = {} + const sources: Array = [] + const ctx = { + state: "suspended" as AudioContextState, + currentTime: 0, + sampleRate: 48000, + destination, + resumeCalls: 0, + resume() { + ctx.resumeCalls++ + if (!options.staySuspended) {ctx.state = "running"} + return Promise.resolve() + }, + createBuffer: (channels: number, length: number, rate: number) => fakeBuffer(channels, length, rate), + decodeAudioData: vi.fn(async () => { + const buffer = fakeBuffer(1, 4800, 48000) + buffer.getChannelData(0).fill(0.5) + return buffer + }), + createBufferSource() { + const source = makeNode({ + buffer: null as AudioBuffer | null, playbackRate: {value: 1}, started: false, + onended: null as null | (() => void), + start() {source.started = true}, + stop() {source.started = false} + }) + sources.push(source) + return source + }, + createGain: () => makeNode({gain: {value: 1}}), + createAnalyser() { + const analyser: any = makeNode({ + fftSize: 2048, + getFloatTimeDomainData(out: Float32Array) { + out.fill(0) + if (options.routeBroken || !analyser.connected.includes(destination)) {return} + const source = sources.find(s => s.started && s.connected[0]?.connected?.includes(analyser)) + if (source === undefined || source.buffer === null) {return} + const gain = source.connected[0].gain.value + const data = source.buffer.getChannelData(0) + for (let i = 0; i < out.length && i < data.length; i++) {out[i] = data[i] * gain} + } + }) + return analyser + }, + sources + } + return ctx +} +const sineWav = (amplitude: number): ArrayBuffer => { + const n = 4800 + const frames = new Float32Array(n) + for (let i = 0; i < n; i++) {frames[i] = amplitude * Math.sin(2 * Math.PI * 440 * i / 48000)} + return WavFile.encodeFloats({frames: [frames], sampleRate: 48000, numberOfFrames: n, numberOfChannels: 1}) +} +const events = (playback: SamplePlayback, key: string): Array => { + const list: Array = [] + playback.subscribe(key, event => list.push(event)) + return list +} +const settle = async () => { + for (let i = 0; i < 5; i++) {await Promise.resolve()} + await vi.advanceTimersByTimeAsync(0) +} + +describe("SamplePlayback (sample previews through the studio output)", () => { + beforeEach(() => vi.useFakeTimers()) + afterEach(() => vi.useRealTimers()) + + it("resumes a suspended context inside the click and produces real level at the output", async () => { + const ctx = makeContext() + const playback = new SamplePlayback(ctx as unknown as PreviewContext, {loadLocal: async () => sineWav(0.8)}) + const seen = events(playback, "pack-kick") + playback.toggle("pack-kick") + expect(ctx.resumeCalls).toBe(1) // synchronously, still inside the user gesture + await settle() + expect(ctx.sources).toHaveLength(1) + expect(ctx.sources[0].started).toBe(true) + await vi.advanceTimersByTimeAsync(120) + const levels = seen.filter(e => e.type === "playing").map(e => (e as { level: number }).level) + expect(Math.max(...levels)).toBeGreaterThan(0.5) + ctx.currentTime = 1.0 + await vi.advanceTimersByTimeAsync(120) + expect(seen.some(e => e.type === "error")).toBe(false) + }) + + it("reports when a non-silent sample produces no sound at the output", async () => { + const ctx = makeContext({routeBroken: true}) + const playback = new SamplePlayback(ctx as unknown as PreviewContext, {loadLocal: async () => sineWav(0.8)}) + const seen = events(playback, "a") + playback.toggle("a") + await settle() + ctx.currentTime = 1.0 + await vi.advanceTimersByTimeAsync(120) + const error = seen.find(e => e.type === "error") as { reason: string } | undefined + expect(error?.reason).toContain("No sound reached the studio output") + expect(playback.isActive).toBe(false) + }) + + it("says a silent sample is silent instead of pretending to play", async () => { + const ctx = makeContext() + const playback = new SamplePlayback(ctx as unknown as PreviewContext, {loadLocal: async () => sineWav(0)}) + const seen = events(playback, "s") + playback.toggle("s") + await settle() + ctx.currentTime = 1.0 + await vi.advanceTimersByTimeAsync(120) + expect((seen.find(e => e.type === "error") as { reason: string }).reason).toContain("silent") + }) + + it("tells the user when the browser keeps audio off", async () => { + const ctx = makeContext({staySuspended: true}) + const playback = new SamplePlayback(ctx as unknown as PreviewContext, {loadLocal: async () => sineWav(0.8)}) + const seen = events(playback, "x") + playback.toggle("x") + await settle() + expect((seen.find(e => e.type === "error") as { reason: string }).reason).toContain("Click the play button again") + expect(ctx.sources).toHaveLength(0) + }) + + it("falls back to the catalogue only when the sample is not in local storage", async () => { + const ctx = makeContext() + const fetchRemote = vi.fn(async () => new ArrayBuffer(8)) + const playback = new SamplePlayback(ctx as unknown as PreviewContext, { + loadLocal: async () => {throw new Error("not stored")}, fetchRemote + }) + playback.toggle("factory") + await settle() + expect(fetchRemote).toHaveBeenCalledWith("factory") + expect(ctx.decodeAudioData).toHaveBeenCalled() + expect(ctx.sources[0].started).toBe(true) + }) + + it("plays one preview at a time, toggles off, and Escape stops", async () => { + const ctx = makeContext() + const playback = new SamplePlayback(ctx as unknown as PreviewContext, {loadLocal: async () => sineWav(0.5)}) + playback.toggle("a") + await settle() + playback.toggle("b") + await settle() + expect(ctx.sources[0].started).toBe(false) + expect(ctx.sources[1].started).toBe(true) + expect(playback.currentKey).toBe("b") + playback.toggle("b") + expect(playback.isActive).toBe(false) + playback.toggle("a") + await settle() + document.dispatchEvent(new KeyboardEvent("keydown", {key: "Escape"})) + expect(playback.isActive).toBe(false) + }) + + it("sync to song tempo sets the playback rate from the stored BPM", async () => { + const ctx = makeContext() + const playback = new SamplePlayback(ctx as unknown as PreviewContext, { + loadLocal: async () => sineWav(0.5), projectBpm: () => 140 + }) + playback.tempoSync.setValue(true) + playback.play("loop", {bpm: 70}) + await settle() + expect(ctx.sources[0].playbackRate.value).toBe(2) + expect(SamplePlayback.syncRate(0, 120)).toBe(1) + playback.tempoSync.setValue(false) + }) + + it("renders a soundfont note at the requested pitch", () => { + const data = new Int16Array(44100) + for (let i = 0; i < data.length; i++) {data[i] = Math.round(16000 * Math.sin(2 * Math.PI * 220 * i / 44100))} + const ctx = makeContext() + const buffer = SamplePlayback.renderNote(ctx as unknown as PreviewContext, { + sample: { + header: {startLoop: 1000, endLoop: 40000, sampleRate: 44100, originalPitch: 57, pitchCorrection: 0}, + data + } + }, 69, 0.5) + expect(buffer.length).toBe(24000) + expect(SamplePlayback.peakOf(buffer)).toBeGreaterThan(0.3) + }) + + it("uploaded pack rows (nested folders) play from local storage, also after a reload", async () => { + // A folder pack lands as samples/v2//audio.wav regardless of the library folder it is shown in. + const pack = ["drums-kick", "drums-snare", "fx-riser"] + const opfs = new Map() + pack.forEach(uuid => opfs.set(`samples/v2/${uuid}/audio.wav`, new Uint8Array(sineWav(0.6)))) + ;(globalThis as any).__opfs = opfs + for (const session of ["after import", "after reload"]) { + const ctx = makeContext() // a reload starts with a fresh, suspended context and an empty cache + const playback = new SamplePlayback(ctx as unknown as PreviewContext) // default loaders + for (const uuid of pack) { + const seen = events(playback, uuid) + playback.toggle(uuid) + await settle() + await vi.advanceTimersByTimeAsync(60) + const level = Math.max(0, ...seen.filter(e => e.type === "playing") + .map(e => (e as { level: number }).level)) + expect(level, `${session}: ${uuid}`).toBeGreaterThan(0.3) + expect(seen.some(e => e.type === "error"), `${session}: ${uuid}`).toBe(false) + } + expect(ctx.resumeCalls).toBe(1) + playback.eject() + } + delete (globalThis as any).__opfs + }) +}) + +describe("SamplePlayback shared-memory guards (ESC 2026-11-19)", () => { + it("copies SharedArrayBuffer-backed frames into a private Float32Array for copyToChannel", () => { + const shared = new Float32Array(new SharedArrayBuffer(4 * 8)) + shared.set([0.1, -0.2, 0.3, -0.4, 0.5, -0.6, 0.7, -0.8]) + const copy = SamplePlayback.unshared(shared) + expect(copy.buffer instanceof SharedArrayBuffer).toBe(false) + expect(copy).not.toBe(shared) + expect(Array.from(copy)).toEqual(Array.from(shared)) + const strictCopyToChannel = (source: Float32Array) => { + if (source.buffer instanceof SharedArrayBuffer) {throw new TypeError("The provided Float32Array value must not be shared.")} + } + expect(() => strictCopyToChannel(copy)).not.toThrow() + expect(() => strictCopyToChannel(shared)).toThrow() + }) + it("leaves ordinary Float32Arrays alone and copies shared bytes for decodeAudioData", () => { + const plain = new Float32Array([1, 2, 3]) + expect(SamplePlayback.unshared(plain)).toBe(plain) + const sharedBytes = new SharedArrayBuffer(16) + new Uint8Array(sharedBytes).set([1, 2, 3, 4]) + const bytes = SamplePlayback.privateBytes(sharedBytes) + expect(bytes instanceof SharedArrayBuffer).toBe(false) + expect(Array.from(new Uint8Array(bytes)).slice(0, 4)).toEqual([1, 2, 3, 4]) + }) +}) diff --git a/packages/app/studio/src/service/SamplePlayback.ts b/packages/app/studio/src/service/SamplePlayback.ts index 2d6d1487d..e069893ec 100644 --- a/packages/app/studio/src/service/SamplePlayback.ts +++ b/packages/app/studio/src/service/SamplePlayback.ts @@ -1,8 +1,15 @@ +// ESC: sample preview (audition) through the STUDIO's audio output. +// openDAW previewed with a hidden