SPB Git

Repository Public

instrument.rs 3.4 KB 75 lines rust

1//! ORIX Sonata2//! File: crates/sonata-instruments/src/instrument.rs3//! Description: The Instrument contract between the engines and the runtime.4//!5//! Author: Simon-Pierre Boucher6//! Contact: contact@spboucher.ai78use sonata_ir::{Articulation, EngineDesc, InstrumentSpec, PatchNodeDesc};910/// A note start delivered to an instrument at the first sample of a `process` call.11#[derive(Clone, Copy, Debug, PartialEq)]12pub struct NoteOn {13    /// Identifier of the note (`NoteEvent::id`, or a live MIDI note id).14    pub id: u64,15    /// MIDI pitch, fractional for microtones.16    pub pitch: f32,17    /// Fundamental in Hz under the program tuning.18    pub freq: f32,19    /// Linear velocity in [0, 1].20    pub velocity: f32,21    pub articulation: Articulation,22}2324/// A polyphonic instrument: it owns its voices and renders them to stereo.25///26/// Contract with the runtime:27/// * `prepare` runs off the audio thread and preallocates `max_voices` voices and all buffers;28///   nothing else may allocate;29/// * the runtime splits audio blocks at event boundaries: `note_on`, `note_off`, `pitch_bend`30///   and `set_param` take effect at the first sample of the next `process` call (sample accurate);31/// * `process` **adds** the voices into `left`/`right` (the caller cleared them),32///   `left.len() == right.len() <= max_block`; no allocation, lock, log or panic; finite output;33/// * when every voice is busy, the instrument steals deterministically: the voice in release34///   with the lowest level first, otherwise the oldest note; a stolen voice fades out over 5 ms;35/// * a single note at velocity 1 must peak below 0 dBFS (engines are level-matched around -12 dBFS);36/// * `set_param` takes the index of the parameter in the engine's [`EngineDesc::params`];37/// * given the same calls in the same order, the output is bit-identical (engines seed their38///   noise from `seed` passed to [`create_instrument`] and the note id, never from time).39pub trait Instrument: Send {40    fn prepare(&mut self, sample_rate: f32, max_block: usize, max_voices: usize);41    fn note_on(&mut self, note: &NoteOn);42    fn note_off(&mut self, id: u64);43    fn pitch_bend(&mut self, id: u64, semitones: f32);44    fn set_param(&mut self, index: usize, value: f32);45    /// Sustain pedal: while down, note-offs are deferred.46    fn sustain(&mut self, _down: bool) {}47    fn process(&mut self, left: &mut [f32], right: &mut [f32]);48    /// Voices currently producing sound, release tails included.49    fn active_voices(&self) -> usize;50    /// Longest time the instrument keeps sounding after `active_voices()` has reached 051    /// (shared body, soundboard and sympathetic resonators), in seconds. Renderers add it to the tail.52    fn tail_seconds(&self) -> f32 {53        0.054    }55    /// Releases every voice (transport stop). Tails still ring out.56    fn all_notes_off(&mut self);57    /// Silences everything at once (seek, program swap).58    fn reset(&mut self);59}6061/// Builds an instrument from a compiled spec. Runs off the audio thread.62pub fn create_instrument(spec: &InstrumentSpec, tuning_hz: f64, seed: u64) -> Result<Box<dyn Instrument>, String> {63    crate::engines::create(spec, tuning_hz, seed)64}6566/// Descriptors of every engine, in documentation order.67pub fn engine_registry() -> Vec<EngineDesc> {68    crate::engines::descriptors()69}7071/// Descriptors of every patch node kind.72pub fn patch_node_registry() -> Vec<PatchNodeDesc> {73    crate::engines::patch_nodes()74}75
Files main
Menu

Theme