Enum nih_plug::midi::NoteEvent

source ·
#[non_exhaustive]
pub enum NoteEvent<S> {
Show 18 variants NoteOn { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, velocity: f32, }, NoteOff { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, velocity: f32, }, Choke { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, }, VoiceTerminated { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, }, PolyModulation { timing: u32, voice_id: i32, poly_modulation_id: u32, normalized_offset: f32, }, MonoAutomation { timing: u32, poly_modulation_id: u32, normalized_value: f32, }, PolyPressure { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, pressure: f32, }, PolyVolume { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, gain: f32, }, PolyPan { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, pan: f32, }, PolyTuning { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, tuning: f32, }, PolyVibrato { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, vibrato: f32, }, PolyExpression { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, expression: f32, }, PolyBrightness { timing: u32, voice_id: Option<i32>, channel: u8, note: u8, brightness: f32, }, MidiChannelPressure { timing: u32, channel: u8, pressure: f32, }, MidiPitchBend { timing: u32, channel: u8, value: f32, }, MidiCC { timing: u32, channel: u8, cc: u8, value: f32, }, MidiProgramChange { timing: u32, channel: u8, program: u8, }, MidiSysEx { timing: u32, message: S, },
}
Expand description

Event for (incoming) notes. The set of supported note events depends on the value of Plugin::MIDI_INPUT. Also check out the util module for convenient conversion functions.

S is a MIDI SysEx message type that needs to implement SysExMessage to allow converting this NoteEvent to and from raw MIDI data. () is provided as a default implementing for plugins that don’t use SysEx.

All of the timings are sample offsets within the current buffer. Out of bound timings are clamped to the current buffer’s length. All sample, channel and note numbers are zero-indexed.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

NoteOn

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

§velocity: f32

The note’s velocity, in [0, 1]. Some plugin APIs may allow higher precision than the 128 levels available in MIDI.

A note on event, available on MidiConfig::Basic and up.

§

NoteOff

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

§velocity: f32

The note’s velocity, in [0, 1]. Some plugin APIs may allow higher precision than the 128 levels available in MIDI.

A note off event, available on MidiConfig::Basic and up. Bitwig Studio does not provide a voice ID for this event.

§

Choke

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

A note choke event, available on MidiConfig::Basic and up. When the host sends this to the plugin, it indicates that a voice or all sound associated with a note should immediately stop playing.

§

VoiceTerminated

Fields

§timing: u32
§voice_id: Option<i32>

The voice’s unique identifier. Setting this allows a single voice to be terminated if the plugin allows multiple overlapping voices for a single key.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

Sent by the plugin to the host to indicate that a voice has ended. This needs to be sent when a voice terminates when using polyphonic modulation. Otherwise you can ignore this event.

§

PolyModulation

Fields

§timing: u32
§voice_id: i32

The identifier of the voice this polyphonic modulation event should affect. This voice should use the values from this and subsequent polyphonic modulation events instead of the global value.

§poly_modulation_id: u32

The ID that was set for the modulated parameter using the .with_poly_modulation_id() method.

§normalized_offset: f32

The normalized offset value. See the event’s docstring for more information.

A polyphonic modulation event, available on MidiConfig::Basic and up. This will only be sent for parameters that were decorated with the .with_poly_modulation_id() modifier, and only by supported hosts. This event contains a normalized offset value for the parameter’s current, unmodulated value. That is, an offset for the current value before monophonic modulation is applied, as polyphonic modulation overrides monophonic modulation. There are multiple ways to incorporate this polyphonic modulation into a synthesizer, but a simple way to incorporate this would work as follows:

  • By default, a voice uses the parameter’s global value, which may or may not include monophonic modulation. This is parameter.value for unsmoothed parameters, and smoothed parameters should use block smoothing so the smoothed values can be reused by multiple voices.
  • If a PolyModulation event is emitted for the voice, that voice should use the the normalized offset contained within the event to compute the voice’s modulated value and use that in place of the global value.
    • This value can be obtained by calling param.preview_plain(param.normalized_value() + event.normalized_offset). These functions automatically clamp the values as necessary.
    • If the parameter uses smoothing, then the parameter’s smoother can be copied to the voice. Smoother::set_target() can then be used to have the smoother use the modulated value.
    • One caveat with smoothing is that copying the smoother like this only works correctly if it last produced a value during the sample before the PolyModulation event. Otherwise there may still be an audible jump in parameter values. A solution for this would be to first call the Smoother::reset() with the current sample’s global value before calling set_target().
    • Finally, if the polyphonic modulation happens on the same sample as the NoteOn event, then the smoothing should not start at the current global value. In this case, reset() should be called with the voice’s modulated value.
  • If a MonoAutomation event is emitted for a parameter, then the values or target values (if the parameter uses smoothing) for all voices must be updated. The normalized value from the MonoAutomation and the voice’s normalized modulation offset must be added and converted back to a plain value. This value can be used directly for unsmoothed parameters, or passed to set_target() for smoothed parameters. The global value will have already been updated, so this event only serves as a notification to update polyphonic modulation.
  • When a voice ends, either because the amplitude envelope has hit zero or because the voice was stolen, the plugin must send a VoiceTerminated to the host to let it know that it can reuse the resources it used to modulate the value.
§

MonoAutomation

Fields

§timing: u32
§poly_modulation_id: u32

The ID that was set for the modulated parameter using the .with_poly_modulation_id() method.

§normalized_value: f32

The parameter’s new normalized value. This needs to be added to a voice’s normalized offset to get that voice’s modulated normalized value. See the PolyModulation event’s docstring for more information.

A notification to inform the plugin that a polyphonically modulated parameter has received a new automation value. This is used in conjunction with the PolyModulation event. See that event’s documentation for more details. The parameter’s global value has already been updated when this event is emitted.

§

PolyPressure

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

§pressure: f32

The note’s pressure, in [0, 1].

A polyphonic note pressure/aftertouch event, available on MidiConfig::Basic and up. Not all hosts may support polyphonic aftertouch.

Note

When implementing MPE support you should use MIDI channel pressure instead as polyphonic key pressure + MPE is undefined as per the MPE specification. Or as a more generic catch all, you may manually combine the polyphonic key pressure and MPE channel pressure.

§

PolyVolume

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

§gain: f32

The note’s voltage gain ratio, where 1.0 is unity gain.

A volume expression event, available on MidiConfig::Basic and up. Not all hosts may support these expressions.

§

PolyPan

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

§pan: f32

The note’s panning from, in [-1, 1], with -1 being panned hard left, and 1 being panned hard right.

A panning expression event, available on MidiConfig::Basic and up. Not all hosts may support these expressions.

§

PolyTuning

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

§tuning: f32

The note’s tuning in semitones, in [-128, 128].

A tuning expression event, available on MidiConfig::Basic and up. Not all hosts may support these expressions.

§

PolyVibrato

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

§vibrato: f32

The note’s vibrato amount, in [0, 1].

A vibrato expression event, available on MidiConfig::Basic and up. Not all hosts may support these expressions.

§

PolyExpression

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

§expression: f32

The note’s expression amount, in [0, 1].

A expression expression (yes, expression expression) event, available on MidiConfig::Basic and up. Not all hosts may support these expressions.

§

PolyBrightness

Fields

§timing: u32
§voice_id: Option<i32>

A unique identifier for this note, if available. Using this to refer to a note is required when allowing overlapping voices for CLAP plugins.

§channel: u8

The note’s channel, in 0..16.

§note: u8

The note’s MIDI key number, in 0..128.

§brightness: f32

The note’s brightness amount, in [0, 1].

A brightness expression event, available on MidiConfig::Basic and up. Not all hosts may support these expressions.

§

MidiChannelPressure

Fields

§timing: u32
§channel: u8

The affected channel, in 0..16.

§pressure: f32

The pressure, normalized to [0, 1] to match the poly pressure event.

A MIDI channel pressure event, available on MidiConfig::MidiCCs and up.

§

MidiPitchBend

Fields

§timing: u32
§channel: u8

The affected channel, in 0..16.

§value: f32

The pressure, normalized to [0, 1]. 0.5 means no pitch bend.

A MIDI pitch bend, available on MidiConfig::MidiCCs and up.

§

MidiCC

Fields

§timing: u32
§channel: u8

The affected channel, in 0..16.

§cc: u8

The control change number. See control_change for a list of CC numbers.

§value: f32

The CC’s value, normalized to [0, 1]. Multiply by 127 to get the original raw value.

A MIDI control change event, available on MidiConfig::MidiCCs and up.

Note

The wrapper does not perform any special handling for two message 14-bit CCs (where the CC number is in 0..32, and the next CC is that number plus 32) or for four message RPN messages. For now you will need to handle these CCs yourself.

§

MidiProgramChange

Fields

§timing: u32
§channel: u8

The affected channel, in 0..16.

§program: u8

The program number, in 0..128.

A MIDI program change event, available on MidiConfig::MidiCCs and up. VST3 plugins cannot receive these events.

§

MidiSysEx

Fields

§timing: u32
§message: S

A MIDI SysEx message supported by the plugin’s SysExMessage type, available on MidiConfig::Basic and up. If the conversion from the raw byte array fails (e.g. the plugin doesn’t support this kind of message), then this will be logged during debug builds of the plugin, and no event is emitted.

Implementations§

source§

impl<S> NoteEvent<S>

source

pub fn timing(&self) -> u32

Returns the sample within the current buffer this event belongs to.

source

pub fn voice_id(&self) -> Option<i32>

Returns the event’s voice ID, if it has any.

source

pub fn channel(&self) -> Option<u8>

Returns the event’s channel, if it has any.

source§

impl<S: SysExMessage> NoteEvent<S>

source

pub fn from_midi(timing: u32, midi_data: &[u8]) -> Result<Self, u8>

Parse MIDI into a NoteEvent. Supports both basic three bytes messages as well as SysEx. Will return Err(event_type) if the parsing failed.

source

pub fn as_midi(self) -> Option<MidiResult<S>>

Create a MIDI message from this note event. Returns None if this even does not have a direct MIDI equivalent. PolyPressure will be converted to polyphonic key pressure, but the other polyphonic note expression types will not be converted to MIDI CC messages.

Trait Implementations§

source§

impl<S: Clone> Clone for NoteEvent<S>

source§

fn clone(&self) -> NoteEvent<S>

Returns a copy 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<S: Debug> Debug for NoteEvent<S>

source§

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

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

impl<S: PartialEq> PartialEq<NoteEvent<S>> for NoteEvent<S>

source§

fn eq(&self, other: &NoteEvent<S>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<S: Copy> Copy for NoteEvent<S>

source§

impl<S> StructuralPartialEq for NoteEvent<S>

Auto Trait Implementations§

§

impl<S> RefUnwindSafe for NoteEvent<S>where S: RefUnwindSafe,

§

impl<S> Send for NoteEvent<S>where S: Send,

§

impl<S> Sync for NoteEvent<S>where S: Sync,

§

impl<S> Unpin for NoteEvent<S>where S: Unpin,

§

impl<S> UnwindSafe for NoteEvent<S>where S: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

impl<T, U> Into<U> for Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

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
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<T> Any for Twhere T: Any,

§

impl<T> CloneAny for Twhere T: Any + Clone,

§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,