#midi #vst-plugin #web #gui #standalone #processor #lock-free #real-time #manual-memory


Stand-alone MIDI hosting for a VST host or an audio-processor-traits implementor

19 releases (8 stable)

1.9.0 May 22, 2023
1.5.0 Mar 22, 2023
1.3.0 Oct 22, 2022
1.0.0-alpha.10 May 25, 2022
0.1.0 Jul 21, 2021

#472 in Audio

Download history 12/week @ 2023-06-11 89/week @ 2023-06-18 30/week @ 2023-06-25 86/week @ 2023-07-02 24/week @ 2023-07-09 38/week @ 2023-07-16 43/week @ 2023-07-23 14/week @ 2023-07-30 31/week @ 2023-08-06 33/week @ 2023-08-13 22/week @ 2023-08-20 42/week @ 2023-08-27 47/week @ 2023-09-03 21/week @ 2023-09-10 21/week @ 2023-09-17 22/week @ 2023-09-24

112 downloads per month
Used in audio-processor-standalon…

MIT license



crates.io docs.rs

This crate provides conversion into the VST types, which is to allow a VST host to use it. This is provided by the MidiVSTConverter.

Wraps midir to provide MIDI input handling. The host may be started with MidiHost.

When MIDI messages are received, they're pushed onto a lock-free atomic_queue::Queue. The messages are picked-up in the audio-thread by MidiAudioThreadHandler.

It provides easy stand-alone MIDI integration with:

  • audio-processor-traits - MidiEventHandler trait
  • rust-vst - PluginInstance

Currently, MIDI messages over 3 bytes are dropped by this host. In addition, the queue is bounded & a size must be provided. Default implementations will use a MIDI queue capacity of 100. This is a stand-alone MIDI host with helpers for doing real-time handling of MIDI messages in the audio-thread and to integrate with VST plugins.

This is part of https://github.com/yamadapc/augmented-audio.

Memory Safety

In order to integrate with the VST C API, this crate does manual memory allocation and handling. This is due to VST event types being unrepresentable as safe Rust constructs (and due to real-time safety being required as well).

Real-time Safety

This crate provides the host side, which is the MIDI host. This host allocates when it receives messages from midir.

The events are forwarded onto a lock-free queue (atomic_queue).

On the audio_thread and vst modules, past construction methods that should be called on the audio-thread will not (de)-allocate. This is tested using the assert_no_alloc crate.

In addition, basedrop / audio_garbage_collector are used to prevent de-allocation from happening on the audio-thread.

Test coverage

The crate has unit-tests (though it should be considered experimental as all of this repository).

Test coverage is at 80%.

Actix & Actors

The MidiHost exposes an actix API, to be used with the actix actor system. This is to ease communicating with the midi handler from multiple threads.


To use this crate:

fn example() {
    use audio_processor_standalone_midi::audio_thread::MidiAudioThreadHandler;
    use audio_processor_standalone_midi::host::MidiHost;
    use audio_processor_standalone_midi::vst::MidiVSTConverter;
    use basedrop::Collector;

    // GC ======================================================================================
    // See `audio-garbage-collector` for an easier to use wrapper on top of this.
    // `Collector` will let us use reference counted values on the audio-thread, which will be
    // pushed to a queue for de-allocation. You must set-up a background thread that forces it
    // to actually collect garbage from the queue.
    let gc = Collector::new();
    let handle = gc.handle();

    // Host ====================================================================================
    // A host may be created with `new` or `default_with_handle`.
    let mut host = MidiHost::default_with_handle(&handle);
    // It'll connect to all MIDI input ports when started
    // host.start().expect("Failed to connect");
    // The host will push messages onto a lock-free queue. This is a reference counted value.
    let midi_messages_queue = host.messages().clone();

    // Audio-thread ============================================================================
    // ...
    // Within your audio-thread
    // ...
    // You'll want to share a `MidiAudioThreadHandler` between process calls as it pre-allocates
    // buffers.
    let mut midi_audio_thread_handler = MidiAudioThreadHandler::default();

    // On each tick you'll call collect
    // You'll get the MIDI message buffer
    let midi_messages = midi_audio_thread_handler.buffer();
    // ^ This is a `&Vec<MidiMessageEntry>`. If you're using `audio-processor-traits`, you can
    //   pass this into any `MidiEventHandler` implementor.

    // VST interop =============================================================================
    // If you want to interface with a VST plugin, you'll need to convert messages into the
    // C-api.
    // ...
    // You'll want to share this between process calls as it pre-allocates buffers.
    let mut midi_vst_converter = MidiVSTConverter::default();
    // This can be passed into VST plugins.
    let events: &vst::api::Events = midi_vst_converter.events();

License: MIT


~618K SLoC