SPB Git

Repository Public

bench.rs 3.2 KB 79 lines rust

1//! ORIX Sonata2//! File: crates/sonata-instruments/examples/bench.rs3//! Description: Measures the cost of each engine: seconds of audio rendered per second of CPU4//! for a given number of held voices, in blocks of 128 frames at 48 kHz (single thread).5//!6//! Author: Simon-Pierre Boucher7//! Contact: contact@spboucher.ai89use sonata_dsp::core::{midi_to_hz, DenormalGuard};10use sonata_instruments::{create_instrument, engine_registry, NoteOn};11use sonata_ir::{Articulation, InstrumentSpec, ParamValue, Params};12use std::time::Instant;1314fn main() {15    let voices: usize = std::env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(16);16    let sr = 48_000.0f32;17    let block = 128;18    let seconds = 4.0;19    println!("{voices} held voices, {block}-frame blocks, {sr} Hz, {seconds} s of audio, release build");20    println!("{:<18} {:<12} {:>12} {:>16} {:>14}", "model", "preset", "x real time", "us per block", "% of a core");21    for desc in engine_registry() {22        if desc.model == "patch" {23            continue;24        }25        // The default preset and the heaviest known one.26        let heavy = match desc.model.as_str() {27            "bowed_string" => Some("section"),28            "synth" => Some("pad"),29            "organ" => Some("full"),30            _ => None,31        };32        for preset in [None, heavy] {33            let mut params = Params::new();34            if let Some(p) = preset {35                params.insert("preset".into(), ParamValue::Text(p.into()));36            }37            let spec = InstrumentSpec { name: "B".into(), model: desc.model.clone(), params, patch: None };38            let mut inst = create_instrument(&spec, 440.0, 1).expect("create");39            inst.prepare(sr, block, voices);40            let lo = desc.range.0.max(36) as f32;41            let span = (desc.range.1.min(96) as f32 - lo).max(1.0);42            let _guard = DenormalGuard::new();43            let (mut l, mut r) = (vec![0.0f32; block], vec![0.0f32; block]);44            let blocks = (seconds * sr) as usize / block;45            let start = Instant::now();46            for b in 0..blocks {47                // Re-trigger all voices every half second so decaying engines stay busy.48                if b % ((0.5 * sr) as usize / block) == 0 {49                    for k in 0..voices {50                        let pitch = lo + span * k as f32 / voices as f32;51                        inst.note_on(&NoteOn {52                            id: (b * 1000 + k) as u64,53                            pitch,54                            freq: midi_to_hz(pitch, 440.0),55                            velocity: 0.8,56                            articulation: Articulation::Normal,57                        });58                    }59                }60                l.fill(0.0);61                r.fill(0.0);62                inst.process(&mut l, &mut r);63            }64            let elapsed = start.elapsed().as_secs_f64();65            println!(66                "{:<18} {:<12} {:>12.0} {:>16.1} {:>14.2}",67                desc.model,68                preset.unwrap_or("default"),69                seconds as f64 / elapsed,70                elapsed * 1e6 / blocks as f64,71                100.0 * elapsed / seconds as f6472            );73            if heavy.is_none() {74                break;75            }76        }77    }78}79
Files main
Menu

Theme