Midiex

DocumentationPackage

Midiex overview

Midiex is a cross-platform, real-time MIDI processing library in Elixir.

midir Rust library

Using Rustler, Midiex wraps the excellent midir Rust library.

Midir support a range of platforms and backends, such as:

Using WinRT or Jack requires special feature flags enabled. See the midir GitHub and create docs for more details.

The hot-plug support of MIDI devices on MacOS is made possible with with the Rust coremidi library.

Status

This library is currently under active development and it’s API is likely to change. It's been tested on MacOS only.

API

At it's most basic level, the core functions of Midiex are for:

Feature support

Not all midir features have been wrapped and some features are backend specific:

MIDI messages

MIDI messages are in binary format. They're usually in the format of one status byte followed by one or two data bytes.

For example, the status byte for 'Note On' is 0x90 in HEX format. The data byte representing the note Middle C is 60. The data byte representing velocity (i.e. how hard the key was struck when the note was played) is an integer in the range 0 - 127 where 127 is the loudest.

Putting that together, the message to play Middle C at a velocity of 127 is: <<0x90, 60, 127>> You can stop the same note from playing by sending the 'Note Off' status byte 0x80, which would make the message: <<0x80, 60, 127>>.

For more information on MIDI messages, see the offical MIDI Assocations Specifications, Expanded MIDI 1.0 message list or the various articles online such as this one.

Example

# List MIDI ports
Midiex.ports()

# Create a virtual output connection
piano = Midiex.create_virtual_output("piano")

# Returns an output connection:
# %Midiex.OutConn{
#   conn_ref: #Reference<0.1633267383.3718381569.210768>,
#   name: "piano",
#   port_num: 0
# }

# Send to MIDI messages to a connection
note_on = <<0x90, 60, 127>>
note_off = <<0x80, 60, 127>>

Midiex.send_msg(piano, note_on)
:timer.sleep(3000) # wait three seconds
Midiex.send_msg(piano, note_off)

Getting started

Adding it to your Elixir project

The package can be installed by adding midiex to your list of dependencies in mix.exs:

def deps do
  [
    {:midiex, "~> 0.5.1"}
  ]
End

Using within LiveBook and IEx

Mix.install([{:midiex, "~> 0.5.1"}])

LiveBook tour

Also see the introductory tour in LiveBook at /livebook/midiex_notebook.livemd.

Run in Livebook

Documentation

The docs can be found at https://hexdocs.pm/midiex.