From a43ffece021d3dc56e8b1ca06a3c72dea0e67697 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 18:36:32 -0800 Subject: [PATCH] audio-dsp: document the event, transport, parameter and biquad items EventInput::note_on and note_off state the MIDI 1.0 note and velocity ranges, and the trait states that callers keep offset below the next block's frame count. The Transport fields, the ParamSpec range fields, the Coeffs fields, FilterType::from_index and the Biquad methods gain doc comments. --- crates/audio-dsp/src/biquad.rs | 11 +++++++++++ crates/audio-dsp/src/lib.rs | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/audio-dsp/src/biquad.rs b/crates/audio-dsp/src/biquad.rs index cc108220a9..668af6bb58 100644 --- a/crates/audio-dsp/src/biquad.rs +++ b/crates/audio-dsp/src/biquad.rs @@ -31,6 +31,7 @@ impl FilterType { FilterType::Notch, FilterType::BandPass, ]; + /// The entry of [`FilterType::ALL`] at `i`; an index past the end gives the last entry. pub fn from_index(i: usize) -> FilterType { Self::ALL[i.min(Self::ALL.len() - 1)] } @@ -39,10 +40,15 @@ impl FilterType { /// Normalized biquad coefficients (`a0 == 1`). #[derive(Clone, Copy, Debug, PartialEq)] pub struct Coeffs { + /// Feed-forward coefficient of the input sample. pub b0: f64, + /// Feed-forward coefficient of the input one sample back. pub b1: f64, + /// Feed-forward coefficient of the input two samples back. pub b2: f64, + /// Feedback coefficient of the output one sample back. pub a1: f64, + /// Feedback coefficient of the output two samples back. pub a2: f64, } @@ -128,9 +134,13 @@ pub struct Biquad { } impl Biquad { + /// A section with zero state. pub const fn new() -> Self { Biquad { s1: 0.0, s2: 0.0 } } + /// Filters one sample `x` through `c` and returns the output sample. A state value whose + /// magnitude is below 1e-30 or that is not finite is stored as zero, so a NaN or infinite + /// input sample makes that one output non-finite and leaves the state at zero. #[inline(always)] pub fn tick(&mut self, c: &Coeffs, x: f64) -> f64 { let y = c.b0 * x + self.s1; @@ -138,6 +148,7 @@ impl Biquad { self.s2 = flush(c.b2 * x - c.a2 * y); y } + /// Sets the state back to zero. pub fn reset(&mut self) { *self = Biquad::new(); } diff --git a/crates/audio-dsp/src/lib.rs b/crates/audio-dsp/src/lib.rs index 2e3a743275..2c6e4c733f 100644 --- a/crates/audio-dsp/src/lib.rs +++ b/crates/audio-dsp/src/lib.rs @@ -105,9 +105,13 @@ pub struct ParamSpec { pub id: &'static str, /// Human-readable label. pub name: &'static str, + /// Lowest value [`ParamSpec::sanitize`] keeps. pub min: f32, + /// Highest value [`ParamSpec::sanitize`] keeps. pub max: f32, + /// Value a new effect starts with, and the value NaN is read as. pub default: f32, + /// Unit the value is in. pub unit: Unit, /// Hint that the control should be logarithmic (frequencies, times). pub log_scale: bool, @@ -248,15 +252,21 @@ pub trait AudioEffect: Send { /// The host's transport at the start of a block, for tempo-synced effects and instruments. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct Transport { + /// Whether the timeline is playing. pub playing: bool, + /// Whether the host is recording. pub recording: bool, /// Quarter notes per minute. pub tempo: f64, + /// Beats per bar, the upper number of the time signature. pub numerator: u32, + /// Note value of one beat, the lower number of the time signature (4 for a quarter note). pub denominator: u32, - /// Timeline position. + /// Timeline position of the block's first frame, in samples. pub samples: i64, + /// The same position in seconds. pub seconds: f64, + /// The same position in quarter notes. pub quarters: f64, /// Start of the current bar, in quarter notes, and its number (0 for the first bar). pub bar_start: f64, @@ -265,13 +275,20 @@ pub struct Transport { /// Note and transport input of an instrument or tempo-synced effect. Every call refers to the /// next processed block; `offset` is a frame within it. Calls never allocate. +/// +/// Callers keep `offset` below the frame count of the next block. The trait does not define what +/// an implementation does with a larger offset. pub trait EventInput { /// The transport for the next block. fn set_transport(&mut self, transport: &Transport); + /// Starts `note`, a MIDI 1.0 key number from 0 to 127 (60 is middle C), at `velocity` from 1 + /// to 127. Callers send a MIDI note-on with velocity 0 as [`EventInput::note_off`]. fn note_on(&mut self, offset: usize, note: u8, velocity: u8); + /// Releases `note`, a MIDI 1.0 key number from 0 to 127. fn note_off(&mut self, offset: usize, note: u8); /// Any other MIDI 1.0 channel message (control change, pitch bend, pressure, program change). fn midi(&mut self, offset: usize, message: [u8; 3]); + /// Releases every sounding note at the start of the next block. fn all_notes_off(&mut self); }