Live Recording of a Midi Controller via Mido Inport

motivation

I got the idea to import live data of a midi controller through this blog post:

mido controller input via mido

To run this on your machine, you need to install the R package reticulate and the python package mido.

import mido

mido <- reticulate::import("mido")

connect to inport

You need to connect your midi controller to the computer. In case you don’t have one and want to try this out you can also run a virtual midi controller, for example VMPK. In this example I connected a Nektar Impact LX 61.

# In order to obtain all the inports available, just type
# mido$get_input_names()
# in your console
inport  <- mido$open_input("Impact LX61+:Impact LX61+ MIDI 1 20:0")

live record midi controller

Now we are all set up to record the midi messages coming in via the input port.

We just create empty vectors and append the midi messages and the times when they arrive in an endless loop until we send a keyboard interrupt:

msgs <- c()
times <- c()
while (TRUE) {
  t <- Sys.time()
  msg <- inport$poll()
  # only record the midi message when it's not empty:
  if (!is.null(msg)) {
    msgs <<- append(msgs, as.character(msg))
    times <<- append(times, t)
  }
}

Instead of the poll() method mido also provides a receive() method which should be more appropriate for this action. However, I wasn’t able to get this working without making my PC stuck and having to restart RStudio.

Urs Wilke
Urs Wilke
data scientist
physicist
PhD in environmental engineering (EPFL)