Simple Midi Controller (2/2)

In this post I will finish the build of the MIDI controller from the part 1, focusing on the PC software side that forwards UART MIDI messages to a MIDI port.

In the physical world, MIDI actually IS run over UART. If you have a MIDI device with a DIN-5 connector, it is pretty straightforward to communicate with it via UART. You just need to set the correct UART communication settings (baud rate, parity, etc) and put an optocoupler between both devices. But in software (windows at least) you need a virtual MIDI device, which is not trivial to write. So, this seems to be an unusual case where the same thing is harder in software than in hardware.

For my luck, someone already made the hard work and make it publicly available. Here you can download Tobias Erichsen´s loopMIDI, a virtual MIDI loopback device. With loopMIDI you can, on one side, connected to a virtual MIDI interface and send MIDI messages and, on the other side, connect your DAW to the same interface to receive those messages.

Since I already dealt with MIDI with Go in the past, I was able to just grab some pieces of code to make it work. If you want to try it, it is available here in my github. In the releases page, you can download a pre-compiled binary for windows.

The code is actually very self-explanatory. The MIDI protocol is simple, but in order to make a robust forwarder it is appropriate to implement all expected MIDI messages one by one. This is needed because the midi driver does not work with a stream of bytes, like the UART, it expects the whole message frame. Therefore, the sender must know the protocol format.

As a future improvement, the forwarder must be resilient to UART transmission error, that are not that uncommon. A viable approach would be to use status bytes as messages delimiter and validate each message. An additional measure would be to implement a timeout interval between data bytes. If it stopped receiving data for more than X amount of time, it assumes that the midi message ended and then validates and forwards the packet. A usable “X” value would be something greater than 320 us, because the MIDI standard uses a baud rate of 31250 with 8 data bits + 1 start bit + 1 stop bit. Therefore, the byte period is 320 us

For my case, I only used MIDI CC messages, so that is the message type that I really tested. Even Tho, I implemented the whole MIDI spec (which is very succinct) and is available here. As for UART transmission errors, in practice, I did not find any yet.

The most painful part was actually, to compile the rtmidi library with CGO on windows. Some people on reddit even recommend to cross compile for windows on a Linux environment 😐. (Edit: I wrote a post about compiling rtmidi wih CGO in Windows)

Anyways, this is the final result, the MIDI controller, controlling some volume levels:


Publicado

em

por

Comentários

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *