Skip to main content

Crate oxideav_midi

Crate oxideav_midi 

Source
Expand description

MIDI — Standard MIDI File (SMF) parser + transport metadata + soft-synth.

  • smf — pure-Rust parser for the Standard MIDI File format (Type 0 / 1 / 2). Header (MThd) + tracks (MTrk) + every common channel-voice message, sysex (F0 / F7), and meta event (tempo, time signature, key signature, text, marker, end-of-track, SMPTE offset, sequencer-specific). Running status is honoured; VLQs are bounded to 4 bytes per spec; chunk lengths are validated against remaining bytes; total events per file are capped at smf::MAX_EVENTS_PER_FILE.
  • paths — per-OS SoundFont/SFZ/DLS search paths plus the OXIDEAV_SOUNDFONT_PATH environment override. find_soundfonts walks them and returns every instrument-bank file present.
  • instrumentsinstruments::Instrument trait. Three adapters:
    • instruments::sf2 — full SoundFont 2 RIFF reader + voice generator. Walks the sfbk form, cross-resolves the preset → instrument → zone → sample chain, and renders sm24-aware 24-bit PCM at the requested pitch via linear interpolation. Honours the volume + modulation DAHDSR envelopes, the initial low-pass biquad filter, mod-env → pitch / filter routing, exclusive-class drum cuts, and native stereo zones.
    • instruments::sfz — text patch reader plus voice generator. Strips comments, walks <control> / <global> / <master> / <group> / <region> sections, flattens inheritance into one fully-resolved opcode map per region, and (via SfzInstrument::open) reads every referenced sample off disk. Voice generation decodes the WAV sample bytes, picks the matching region by (key, velocity), shifts pitch off pitch_keycenter + tune + transpose, and runs a DAHDSR amplitude envelope (ampeg_*) + vibrato LFO (lfo01_*).
    • instruments::dls — DLS (Downloadable Sounds) Level 1 + Level 2 RIFF reader plus voice generator. Walks the DLS form, parses the colh / vers / ptbl pool table / lins-list instrument table / wvpl-list wave pool, and surfaces a fully-cross-resolved DlsBank of instruments → regions → wave-pool samples with their wsmp loops, wlnk cue references, and art1 / art2 articulation connection blocks. make_voice resolves the wlnk → ptbl → wave-pool entry, decodes the PCM, and plays the sample through SamplePlayer. art1/art2 connection-block evaluation is round 2.
    • instruments::sample_voice — shared sample-playback voice (mono in, mono out) used by both SFZ and DLS. Covers DAHDSR amplitude envelope, four loop modes, pitch bend, and a vibrato LFO.
    • instruments::wav_pcm — minimal RIFF/WAVE PCM decoder used by the SFZ and DLS sample loaders.
    • instruments::tone — sine/triangle/saw/square fallback so the synth produces something even when no on-disk bank is present.
  • mixer — polyphonic voice pool (32 voices) with stereo mixdown, per-channel volume / pan / sustain pedal handling, and oldest-voice preemption when the pool is full. Round 75 adds the full RPN 1 / RPN 2 / RPN 5 control surface (channel fine + coarse tune + modulation-depth range), CC 1 (mod wheel) → per-voice depth, CC 74 (MPE “third dimension”) → per-voice timbre, the MpeZone / MpeRole topology built from MCM messages, and universal-SysEx-driven master volume / master fine / master coarse tuning that sum with per-channel tuning into the effective pitch each voice receives.
  • scheduler — SMF event scheduler. Merges every track into a single time-ordered stream, converts ticks → samples against the current tempo + division, and dispatches each event into the mixer at the right audio sample. Round 75 wires the Universal Real-Time / Non-Real-Time SysEx routing: GM 1 / GM 2 / GM Off reset, CA-25 Master Fine / Master Coarse Tuning, Master Volume, plus the CC 1 / CC 74 / MPE-MCM channel-CC paths.
  • downloader — stub that names a planned default bank (TimGM6mb) but currently returns Error::Unsupported.

The decoder factory ([make_decoder]) is registered under codec id CODEC_ID_STR = "midi". Round-3 wires SMF events end-to-end: send_packet parses the SMF and primes the scheduler; receive_frame pulls one chunk of stereo PCM (FRAME_SAMPLES samples per channel at OUTPUT_SAMPLE_RATE) until both the event stream and the voice pool have run dry, then returns Error::Eof.

Without an instrument bank the decoder uses instruments::tone::ToneInstrument — the pure-tone fallback — so a .mid file plays back as audible-but-not-musical sine / triangle / square waves. To use a real bank, build the decoder by hand and pass an Sf2Instrument to MidiDecoder::with_instrument; the decoder factory wired into the registry today does not yet plumb a bank-discovery hook.

Modules§

downloader
Default-SoundFont downloader.
instruments
External-instrument trait + per-format adapters + pure-tone fallback.
mixer
Polyphonic voice pool + mixdown into a stereo PCM buffer.
paths
Cross-platform search paths for soft-synth instrument banks.
scheduler
SMF event scheduler — converts a parsed SmfFile into an absolute-tick, time-ordered event stream and walks it sample-by-sample against the audio output rate.
smf
Standard MIDI File (SMF) parser.
tuning
MIDI Tuning Standard (MTS) — microtuning state + Universal SysEx data-format decoders.

Structs§

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

Enums§

InstrumentSource
Source descriptor for an external instrument bank. Paired with MidiDecoder::with_instrument_source so a caller that only knows “I have an SF2 file at this path” doesn’t have to type the full Arc::new(Sf2Instrument::open(...)) chain.

Constants§

CODEC_ID_STR
Public codec id string. Matches the aggregator feature name midi.
FRAME_SAMPLES
Number of per-channel samples emitted per Decoder::receive_frame call. ~23 ms at 44.1 kHz — small enough for low playback latency, big enough that the per-call overhead is dwarfed by the inner mix loop.
OUTPUT_CHANNELS
Channel count of the PCM output bus. Stereo. Same fixed assumption as OUTPUT_SAMPLE_RATE.
OUTPUT_SAMPLE_RATE
Round-3 audio output sample rate. Hard-coded to 44 100 Hz so the decoder doesn’t need a parameter from the caller (the SMF container itself doesn’t carry one). Round-4 may wire this through CodecParameters::sample_rate.

Functions§

register_codecs
Register the MIDI codec. Round-3 produces interleaved S16 stereo PCM at OUTPUT_SAMPLE_RATE — the registry-built decoder uses the pure-tone fallback because we don’t yet have a bank-discovery hook in the factory signature. Callers who want SoundFont 2 playback should build the decoder by hand via MidiDecoder::with_instrument.