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: u32Sample offset within the current buffer (0 = start of buffer).
event: MidiEventKindThe MIDI event data.
Implementations§
Source§impl MidiEvent
impl MidiEvent
Sourcepub const fn note_on(
sample_offset: u32,
channel: MidiChannel,
pitch: MidiNote,
velocity: f32,
note_id: NoteId,
tuning: f32,
length: i32,
) -> Self
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.
Sourcepub const fn note_off(
sample_offset: u32,
channel: MidiChannel,
pitch: MidiNote,
velocity: f32,
note_id: NoteId,
tuning: f32,
) -> Self
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.
Sourcepub const fn poly_pressure(
sample_offset: u32,
channel: MidiChannel,
pitch: MidiNote,
pressure: f32,
note_id: NoteId,
) -> Self
pub const fn poly_pressure( sample_offset: u32, channel: MidiChannel, pitch: MidiNote, pressure: f32, note_id: NoteId, ) -> Self
Create a polyphonic pressure event.
Sourcepub const fn control_change(
sample_offset: u32,
channel: MidiChannel,
controller: u8,
value: f32,
) -> Self
pub const fn control_change( sample_offset: u32, channel: MidiChannel, controller: u8, value: f32, ) -> Self
Create a control change event.
Sourcepub const fn pitch_bend(
sample_offset: u32,
channel: MidiChannel,
value: f32,
) -> Self
pub const fn pitch_bend( sample_offset: u32, channel: MidiChannel, value: f32, ) -> Self
Create a pitch bend event.
Sourcepub const fn channel_pressure(
sample_offset: u32,
channel: MidiChannel,
pressure: f32,
) -> Self
pub const fn channel_pressure( sample_offset: u32, channel: MidiChannel, pressure: f32, ) -> Self
Create a channel pressure event.
Sourcepub const fn program_change(
sample_offset: u32,
channel: MidiChannel,
program: u8,
) -> Self
pub const fn program_change( sample_offset: u32, channel: MidiChannel, program: u8, ) -> Self
Create a program change event.
Sourcepub fn from_midi1_bytes(
sample_offset: u32,
status: u8,
channel: MidiChannel,
data1: u8,
data2: u8,
) -> Option<Self>
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
plugin hosts or hardware MIDI) into beamer’s MidiEvent format.
§Arguments
sample_offset- Sample position within the current bufferstatus- 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
| Status | Message Type | data1 | data2 |
|---|---|---|---|
| 0x80 | Note Off | note number | velocity |
| 0x90 | Note On | note number | velocity |
| 0xA0 | Poly Pressure | note number | pressure |
| 0xB0 | Control Change | CC number | value |
| 0xC0 | Program Change | program | (ignored) |
| 0xD0 | Channel Pressure | pressure | (ignored) |
| 0xE0 | Pitch Bend | LSB | MSB |
§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_idis set to the pitch value for basic voice allocationtuningandlengthdefault 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());Sourcepub fn sysex(sample_offset: u32, data: &[u8]) -> Self
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.
Sourcepub const fn note_expression_value(
sample_offset: u32,
note_id: NoteId,
expression_type: u32,
value: f64,
) -> Self
pub const fn note_expression_value( sample_offset: u32, note_id: NoteId, expression_type: u32, value: f64, ) -> Self
Create a Note Expression value event.
Sourcepub const fn note_expression_int(
sample_offset: u32,
note_id: NoteId,
expression_type: u32,
value: u64,
) -> Self
pub const fn note_expression_int( sample_offset: u32, note_id: NoteId, expression_type: u32, value: u64, ) -> Self
Create a Note Expression integer event.
Sourcepub fn note_expression_text(
sample_offset: u32,
note_id: NoteId,
expression_type: u32,
text: &str,
) -> Self
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.
Sourcepub fn chord_info(
sample_offset: u32,
root: i8,
bass_note: i8,
mask: u16,
name: &str,
) -> Self
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.
Sourcepub fn scale_info(sample_offset: u32, root: i8, mask: u16, name: &str) -> Self
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.
Sourcepub fn with(self, kind: MidiEventKind) -> Self
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
})));
}