SPB Git

Repository Public

mod.rs 3.8 KB 107 lines rust

1//! ORIX Sonata2//! File: crates/sonata-instruments/tests/common/mod.rs3//! Description: Helpers shared by the instrument integration tests.4//!5//! Author: Simon-Pierre Boucher6//! Contact: contact@spboucher.ai7#![allow(dead_code)]89use sonata_dsp::core::midi_to_hz;10use sonata_instruments::{create_instrument, Instrument, NoteOn};11use sonata_ir::{Articulation, EngineDesc, InstrumentSpec, ParamValue, Params};1213pub fn spec(model: &str, preset: Option<&str>, overrides: &[(&str, f64)]) -> InstrumentSpec {14    let mut params = Params::new();15    if let Some(p) = preset {16        params.insert("preset".into(), ParamValue::Text(p.into()));17    }18    for (k, v) in overrides {19        params.insert(k.to_string(), ParamValue::Num(*v));20    }21    InstrumentSpec { name: "Test".into(), model: model.into(), params, patch: None }22}2324pub fn make(spec: &InstrumentSpec, sr: f32, voices: usize) -> Box<dyn Instrument> {25    let mut inst = create_instrument(spec, 440.0, 7).unwrap_or_else(|e| panic!("{e}"));26    inst.prepare(sr, 512, voices);27    inst28}2930pub fn note(id: u64, pitch: f32, velocity: f32) -> NoteOn {31    NoteOn { id, pitch, freq: midi_to_hz(pitch, 440.0), velocity, articulation: Articulation::Normal }32}3334/// Renders `seconds` in blocks of 512 (the last block shorter) and returns (left, right).35pub fn render(inst: &mut dyn Instrument, sr: f32, seconds: f32) -> (Vec<f32>, Vec<f32>) {36    let n = (seconds * sr) as usize;37    let (mut l, mut r) = (vec![0.0f32; n], vec![0.0f32; n]);38    for (a, b) in l.chunks_mut(512).zip(r.chunks_mut(512)) {39        inst.process(a, b);40    }41    (l, r)42}4344pub fn peak(l: &[f32], r: &[f32]) -> f32 {45    l.iter().chain(r.iter()).fold(0.0f32, |m, x| m.max(x.abs()))46}4748pub fn all_finite(l: &[f32], r: &[f32]) -> bool {49    l.iter().chain(r.iter()).all(|x| x.is_finite())50}5152/// A representative pitch inside the engine's range.53pub fn centre(desc: &EngineDesc) -> f32 {54    if desc.family == "bass" {55        40.056    } else {57        ((desc.range.0 as u32 + desc.range.1 as u32) / 2).clamp(40, 72) as f3258    }59}6061/// Overrides that remove deliberate pitch modulation and detuning, for tuning measurements.62pub fn steady(desc: &EngineDesc) -> Vec<(&'static str, f64)> {63    let mut o = Vec::new();64    // The organ's 16' and 5 1/3' footages sound below the written pitch: measured on the 8' alone.65    for (name, value) in [66        ("vibrato.depth", 0.0),67        ("osc2.level", 0.0),68        ("unison_detune", 0.0),69        ("mod.lfo_pitch", 0.0),70        ("players.detune", 0.0),71        ("drawbar.f16", 0.0),72        ("drawbar.f5_1_3", 0.0),73    ] {74        if desc.params.iter().any(|p| p.name == name) {75            o.push((name, value));76        }77    }78    o79}8081/// Frequency of the strongest spectral component within +-40 cents of `expected` (1-cent scan82/// with parabolic refinement). Used where partials are inharmonic and a periodicity estimate83/// would be biased (piano, bars, bells).84pub fn spectral_peak(signal: &[f32], sr: f32, expected: f32) -> f32 {85    let amp = |c: f32| sonata_dsp::core::goertzel_amplitude(signal, expected * (c / 1200.0).exp2(), sr);86    let vals: Vec<f32> = (-40..=40).map(|c| amp(c as f32)).collect();87    let mut k = 1;88    for i in 1..vals.len() - 1 {89        if vals[i] > vals[k] {90            k = i;91        }92    }93    let (a, b, c) = (vals[k - 1], vals[k], vals[k + 1]);94    let d = a - 2.0 * b + c;95    let shift = if d.abs() > 1e-12 { 0.5 * (a - c) / d } else { 0.0 };96    expected * ((k as f32 - 40.0 + shift) / 1200.0).exp2()97}9899/// Measured fundamental: spectral peak for inharmonic engines, autocorrelation otherwise.100pub fn measure_f0(desc: &EngineDesc, signal: &[f32], sr: f32, expected: f32) -> Option<f32> {101    if desc.model == "piano" || desc.model == "modal_percussion" {102        Some(spectral_peak(signal, sr, expected))103    } else {104        sonata_dsp::core::estimate_f0(signal, sr, expected * 0.94, expected * 1.06)105    }106}107
Files main
Menu

Theme