Skip to main content

MidiEvent

Struct MidiEvent 

Source
pub struct MidiEvent {
    pub sample_offset: u32,
    pub event: MidiEventKind,
}
Expand description

A sample-accurate MIDI event.

The sample_offset field specifies when within the current audio buffer this event should be processed, enabling sample-accurate MIDI timing.

Fields§

§sample_offset: u32

Sample offset within the current buffer (0 = start of buffer).

§event: MidiEventKind

The MIDI event data.

Implementations§

Source§

impl MidiEvent

Source

pub const fn note_on( sample_offset: u32, channel: MidiChannel, pitch: MidiNote, velocity: f32, note_id: NoteId, tuning: f32, length: i32, ) -> Self

Create a new note-on event.

Source

pub const fn note_off( sample_offset: u32, channel: MidiChannel, pitch: MidiNote, velocity: f32, note_id: NoteId, tuning: f32, ) -> Self

Create a new note-off event.

Source

pub const fn poly_pressure( sample_offset: u32, channel: MidiChannel, pitch: MidiNote, pressure: f32, note_id: NoteId, ) -> Self

Create a polyphonic pressure event.

Source

pub const fn control_change( sample_offset: u32, channel: MidiChannel, controller: u8, value: f32, ) -> Self

Create a control change event.

Source

pub const fn pitch_bend( sample_offset: u32, channel: MidiChannel, value: f32, ) -> Self

Create a pitch bend event.

Source

pub const fn channel_pressure( sample_offset: u32, channel: MidiChannel, pressure: f32, ) -> Self

Create a channel pressure event.

Source

pub const fn program_change( sample_offset: u32, channel: MidiChannel, program: u8, ) -> Self

Create a program change event.

Source

pub fn from_midi1_bytes( sample_offset: u32, status: u8, channel: MidiChannel, data1: u8, data2: u8, ) -> Option<Self>

Parse a MIDI 1.0 channel voice message from raw bytes.

This is the standard way to convert raw MIDI bytes (as received from Audio Units, CLAP, LV2, or hardware MIDI) into beamer’s MidiEvent format.

§Arguments
  • sample_offset - Sample position within the current buffer
  • status - MIDI status byte with channel masked out (0x80-0xF0)
  • channel - MIDI channel (0-15)
  • data1 - First data byte (note number, CC number, etc.)
  • data2 - Second data byte (velocity, CC value, etc.)
§Returns

Some(MidiEvent) for supported channel voice messages, None for unsupported message types (system messages, etc.)

§Supported Messages
StatusMessage Typedata1data2
0x80Note Offnote numbervelocity
0x90Note Onnote numbervelocity
0xA0Poly Pressurenote numberpressure
0xB0Control ChangeCC numbervalue
0xC0Program Changeprogram(ignored)
0xD0Channel Pressurepressure(ignored)
0xE0Pitch BendLSBMSB
§Notes
  • Note On with velocity 0 is converted to Note Off (per MIDI spec)
  • Velocities and CC values are normalized: 0-127 → 0.0-1.0
  • Pitch bend is normalized: 0-16383 → -1.0 to 1.0 (center at 8192)
  • note_id is set to the pitch value for basic voice allocation
  • tuning and length default to 0
§Example
use beamer_core::MidiEvent;

// Parse a Note On: channel 0, note 60 (middle C), velocity 100
let event = MidiEvent::from_midi1_bytes(0, 0x90, 0, 60, 100);
assert!(event.is_some());
Source

pub fn sysex(sample_offset: u32, data: &[u8]) -> Self

Create a SysEx event.

Note: This allocates the SysEx data on the heap. SysEx messages are relatively rare, so the allocation is acceptable.

Source

pub const fn note_expression_value( sample_offset: u32, note_id: NoteId, expression_type: u32, value: f64, ) -> Self

Create a Note Expression value event.

Source

pub const fn note_expression_int( sample_offset: u32, note_id: NoteId, expression_type: u32, value: u64, ) -> Self

Create a Note Expression integer event.

Source

pub fn note_expression_text( sample_offset: u32, note_id: NoteId, expression_type: u32, text: &str, ) -> Self

Create a Note Expression text event.

Note: This is not const because it initializes the fixed-size buffer.

Source

pub fn chord_info( sample_offset: u32, root: i8, bass_note: i8, mask: u16, name: &str, ) -> Self

Create a Chord info event.

Note: This is not const because it initializes the fixed-size buffer.

Source

pub fn scale_info(sample_offset: u32, root: i8, mask: u16, name: &str) -> Self

Create a Scale info event.

Note: This is not const because it initializes the fixed-size buffer.

Source

pub fn with(self, kind: MidiEventKind) -> Self

Create a new event with the same timing but different event data.

This preserves the sample_offset while replacing the MidiEventKind. Useful when transforming MIDI events where you’ve already matched on the event type and want to create a modified version.

§Arguments
  • kind - The new event data
§Returns

A new MidiEvent with the same sample_offset but new event data.

§Example
MidiEventKind::NoteOn(note_on) => {
    output.push(event.clone().with(MidiEventKind::NoteOn(NoteOn {
        pitch: new_pitch,
        velocity: new_velocity,
        ..*note_on  // Copy channel, note_id, tuning, length
    })));
}

Trait Implementations§

Source§

impl Clone for MidiEvent

Source§

fn clone(&self) -> MidiEvent

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MidiEvent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MidiEvent

Source§

fn default() -> Self

Creates a default MidiEvent (NoteOff with all fields zeroed).

Used for buffer initialization. Does not allocate.

Source§

impl PartialEq for MidiEvent

Source§

fn eq(&self, other: &MidiEvent) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for MidiEvent

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.