Skip to main content

MidiDecoder

Struct MidiDecoder 

Source
pub struct MidiDecoder { /* private fields */ }
Expand description

Soft-synth decoder: SMF in, interleaved S16 stereo PCM out.

Stateful — accepts exactly one SMF blob via send_packet and then streams audio frames out of receive_frame until both the event scheduler and the voice pool have run dry, at which point Error::Eof is returned. Calling send_packet again replaces the scheduler with a fresh one (re-priming for a new file).

State that survives across receive_frame calls:

  • the merged event list + cursor + sample clock (in Scheduler)
  • the voice pool + per-channel CC state (in Mixer)
  • a small carry-over flag that lets the decoder render a few extra trailing chunks after the last event so release tails don’t get cut off mid-envelope.

Implementations§

Source§

impl MidiDecoder

Source

pub const TAIL_CHUNK_CAP: usize = 32

Hard cap on how many extra audio chunks we’ll emit after the last SMF event has fired. Voice release tails (50–100 ms with the round-2/3 envelopes) live inside this budget; without it, a malformed or never-releasing voice could keep the decoder emitting forever.

Source

pub fn new(instrument: Arc<dyn Instrument>, sample_rate: u32) -> Self

Build a decoder bound to a specific instrument and sample rate. Use this directly when you have a SoundFont 2 bank loaded and want to drive the synth with it; the [make_decoder] factory (called by the codec registry) builds one with the pure-tone fallback because there’s no instrument-discovery plumbing in the factory signature yet.

Source

pub fn with_instrument(instrument: Arc<dyn Instrument>) -> Self

Convenience constructor: same as new but takes a concrete Instrument by value and wraps it in an Arc.

Source

pub fn with_instrument_source(source: InstrumentSource) -> Result<Self>

Build a decoder bound to an instrument loaded from a path on disk. The format is dispatched by InstrumentSource so the caller picks SFZ / SF2 / DLS explicitly (file extensions are not always reliable indicators).

Source§

impl MidiDecoder

Source

pub fn sample_rate(&self) -> u32

Sample rate the decoder is rendering at. Equal to whatever was passed to new (default OUTPUT_SAMPLE_RATE when constructed via the registry).

Source

pub fn scheduler(&self) -> Option<&Scheduler>

Borrow the active scheduler — None until the first send_packet. Useful for diagnostics + tests.

Trait Implementations§

Source§

impl Decoder for MidiDecoder

Source§

fn codec_id(&self) -> &CodecId

Source§

fn send_packet(&mut self, packet: &Packet) -> Result<()>

Feed one compressed packet. May or may not produce a frame immediately — call receive_frame in a loop afterwards.
Source§

fn receive_frame(&mut self) -> Result<Frame>

Pull the next decoded frame, if any. Returns Error::NeedMore when the decoder needs another packet.
Source§

fn flush(&mut self) -> Result<()>

Signal end-of-stream. After this, receive_frame will drain buffered frames and eventually return Error::Eof.
Source§

fn reset(&mut self) -> Result<()>

Discard all carry-over state so the decoder can resume from a new bitstream position without producing stale output. Called by the player after a container seek. Read more
Source§

fn receive_arena_frame(&mut self) -> Result<Arc<FrameInner>, Error>

Pull the next decoded frame as an arena-backed arena::sync::Frame. Read more
Source§

fn set_execution_context(&mut self, _ctx: &ExecutionContext)

Advisory: announce the runtime environment (today: a thread budget for codec-internal parallelism). Called at most once, before the first send_packet. Default no-op; codecs that want to run slice-/GOP-/tile-parallel override this to capture the budget. Ignoring the hint is always safe — callers must still work with a decoder that runs serial.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.