Skip to main content

Module midi

Module midi 

Source
Expand description

MIDI value-domain helpers: normalize / denormalize between wire-native integers and f32 ranges.

truce’s EventBody carries MIDI events as wire-native integers (7-bit u8, 14-bit u16, 16-bit u16, 32-bit u32) so the framework’s representation round-trips exactly with the wire. Plugin code that wants to multiply by a parameter, accumulate into a phase, or otherwise use the value as a float reaches for the helpers below.

Each pair (norm_* / denorm_*) round-trips for every representable wire input. See the per-helper docs for endpoint semantics - pitch-bend is asymmetric on both MIDI 1.0 and MIDI 2.0 because the spec’s center value sits one code closer to the negative end than the positive.

Lints: the helpers do as-casts at well-defined widening or lossless points (u8 → f32, u16 → f32, f64 → f32 after a clamped multiply), so the cast_* lints are allowed at the module level rather than per call.

Functions§

denorm_7bit
f32 ∈ [0.0, 1.0] → MIDI 1.0 7-bit unsigned (0..=127).
denorm_pitch_bend
f32 ∈ [-1.0, 1.0] → MIDI 1.0 14-bit pitch-bend code (0..=16383).
norm_7bit
MIDI 1.0 7-bit unsigned (0..=127) → f32 ∈ [0.0, 1.0].
norm_pitch_bend
MIDI 1.0 14-bit pitch-bend code (0..=16383) → f32 ∈ [-1.0, ~0.99987].
pitch_bend_from_bytes
Combine two MIDI bytes (LSB first) into a 14-bit pitch-bend code. Each input byte’s high bit is masked off before combining.
pitch_bend_to_bytes
Split a 14-bit pitch-bend code into the (LSB, MSB) byte pair the wire format carries. Each output byte has the high bit clear.