SPB Git

Repository Public

calibrate.rs 2.8 KB 65 lines rust

1//! ORIX Sonata2//! File: crates/sonata-instruments/examples/calibrate.rs3//! Description: Measures the tuning error of the waveguide engines across their range, at4//! several velocities and sample rates (vibrato off). Used to state the tuning tolerances.5//!6//! Author: Simon-Pierre Boucher7//! Contact: contact@spboucher.ai89use sonata_dsp::core::{cents, estimate_f0, midi_to_hz};10use sonata_instruments::{create_instrument, engine_registry, NoteOn};11use sonata_ir::{Articulation, InstrumentSpec, ParamValue, Params};1213/// Measured error in cents (wide search: reports octave or mode jumps as large values), or None when silent.14pub fn measure(model: &str, preset: Option<&str>, pitch: f32, velocity: f32, sr: u32) -> Option<f32> {15    let mut params = Params::new();16    if let Some(p) = preset {17        params.insert("preset".into(), ParamValue::Text(p.into()));18    }19    params.insert("vibrato.depth".into(), ParamValue::Num(0.0));20    let spec = InstrumentSpec { name: "Cal".into(), model: model.into(), params, patch: None };21    let mut inst = create_instrument(&spec, 440.0, 1).ok()?;22    inst.prepare(sr as f32, 512, 4);23    let f = midi_to_hz(pitch, 440.0);24    inst.note_on(&NoteOn { id: 1, pitch, freq: f, velocity, articulation: Articulation::Normal });25    let n = sr as usize * 3 / 2;26    let (mut l, mut r) = (vec![0.0f32; n], vec![0.0f32; n]);27    for (cl, cr) in l.chunks_mut(512).zip(r.chunks_mut(512)) {28        inst.process(cl, cr);29    }30    let seg = &l[n / 2..];31    let peak = seg.iter().fold(0.0f32, |m, x| m.max(x.abs()));32    if peak < 1e-3 {33        return None;34    }35    estimate_f0(seg, sr as f32, f * 0.45, f * 2.2).map(|e| cents(e, f))36}3738fn main() {39    let models: Vec<String> = std::env::args().skip(1).collect();40    for desc in engine_registry() {41        if !models.is_empty() && !models.contains(&desc.model) {42            continue;43        }44        if models.is_empty() && !["bowed_string", "flute", "clarinet", "brass"].contains(&desc.model.as_str()) {45            continue;46        }47        println!("== {} (range {}..{})", desc.model, desc.range.0, desc.range.1);48        println!("{:>5} {:>9} {:>9} {:>9} {:>9} {:>9}", "midi", "48k v1.0", "48k v0.5", "44.1k", "96k", "192k");49        let mut p = desc.range.0 as f32;50        while p <= desc.range.1 as f32 {51            let show = |v: Option<f32>| v.map(|c| format!("{c:+.1}")).unwrap_or_else(|| "silent".into());52            println!(53                "{:>5} {:>9} {:>9} {:>9} {:>9} {:>9}",54                p,55                show(measure(&desc.model, None, p, 1.0, 48_000)),56                show(measure(&desc.model, None, p, 0.5, 48_000)),57                show(measure(&desc.model, None, p, 1.0, 44_100)),58                show(measure(&desc.model, None, p, 1.0, 96_000)),59                show(measure(&desc.model, None, p, 1.0, 192_000))60            );61            p += 4.0;62        }63    }64}65
Files main
Menu

Theme