Table of Contents

Class LiveMidiInstrument

Namespace
NAudio.Midi
Assembly
NAudio.Midi.dll

Plays an IMidiInstrument from a live MIDI source (a hardware keyboard, an on-screen keyboard, etc.). Unlike SequencedMidiPlayer, which reads events from a timeline on the audio thread, events here arrive asynchronously from other threads (a MIDI input callback, the UI thread, …).

public sealed class LiveMidiInstrument : IMidiInstrument, ISampleProvider
Inheritance
LiveMidiInstrument
Implements
Inherited Members
Extension Methods

Remarks

Incoming events are placed on a lock-free queue by Send(MidiEvent) and drained on the audio thread at the start of each Read(Span<float>), so the (single-threaded) wrapped instrument is only ever touched from one thread. Events therefore take effect at the next block boundary; the resulting latency is just the output buffer size, which is the expected behaviour for a live, pull-driven instrument.

The wrapper is itself an IMidiInstrument whose ProcessMidiEvent(MidiEvent) is the thread-safe, deferred Send(MidiEvent), so hosts can treat live-bridged and directly-driven instruments uniformly.

Constructors

LiveMidiInstrument(IMidiInstrument)

Wraps the given instrument so it can be driven from live MIDI input.

public LiveMidiInstrument(IMidiInstrument instrument)

Parameters

instrument IMidiInstrument

Properties

Instrument

The wrapped instrument.

public IMidiInstrument Instrument { get; }

Property Value

IMidiInstrument

WaveFormat

Gets the WaveFormat of this Sample Provider.

public WaveFormat WaveFormat { get; }

Property Value

WaveFormat

The wave format.

Methods

AllSoundOff()

Queues an "all sound off" (CC120) for every channel — the panic button. Safe to call from any thread.

public void AllSoundOff()

NoteOff(int, int)

Queues a note-off. channel is zero-based.

public void NoteOff(int channel, int note)

Parameters

channel int
note int

NoteOn(int, int, int)

Queues a note-on. channel is zero-based.

public void NoteOn(int channel, int note, int velocity)

Parameters

channel int
note int
velocity int

ProcessMidiEvent(MidiEvent)

Dispatches a MIDI event (note on/off, control change, pitch-bend, aftertouch, program change). Unsupported messages are ignored.

public void ProcessMidiEvent(MidiEvent midiEvent)

Parameters

midiEvent MidiEvent

Read(Span<float>)

Fill the specified buffer with 32 bit floating point samples

public int Read(Span<float> buffer)

Parameters

buffer Span<float>

The buffer to fill with samples.

Returns

int

The number of samples written. Return 0 to signal end of stream.

Send(MidiEvent)

Queues a MIDI event for the instrument. Safe to call from any thread; the event is applied on the audio thread at the start of the next block. Null events are ignored.

public void Send(MidiEvent midiEvent)

Parameters

midiEvent MidiEvent