Split MIDI Files with Python

In my previous post I showed how to use Raspberry Pi to automatically record MIDI files from digital piano whenever you turn it on. However, if you sit down and play for an hour, and get one big MIDI file, it is not very useful. So what to do?

Pianoteq has a feature that splits your playing session based on breaks you take between playing notes. So I decided to mimick this feature with a simple Python script that:

  1. Keeps tab on keys and pedals pressed
  2. Whenever X seconds elapse without any keys or pedals down, a new MIDI file is started
  3. Additionally, if user presses and releases sustain pedal several times in the end, filename for that MIDI is altered to “highlight” that file

I first thought to learn enough of MIDI file format to do everything from scratch, but there’s quite a bit of small details to handle, so in the end I decided to use an external toolkit from Craig Stuart Sapp called midifile to do the heavy lifting. You should be able to just clone the Git repo and make it with Raspberry Pi:

pi@raspberrypi:~ $ git clone https://github.com/craigsapp/midifile
pi@raspberrypi:~ $ cd midifile
pi@raspberrypi:~/midifile $ make

You should now have two useful commands in ~/midifile/bin: toascii to read a MIDI file and dump an ASCII (text) version of it, and tobinary to do the reverse. With Python’s Popen and smart piping, we can read and write the binary MIDI files as they were in this text format. You can check out how the format looks like with some MIDI file you have:

~/midifile/bin/toascii somemidi.mid | less

Here’s a sample of a MIDI file recorded by arecordmidi (I added the note in square brackets):

"MThd"
4'6
2'0
2'1
2'384

;;; TRACK 0 ----------------------------------
"MTrk"
4'50044
v0      ff 51 v3 t120
v0      ff 58 v4 '4 '2 '24 '8
v7147   90 '58 '29
v88     b0 '64 '5
v1      b0 '64 '7

[LOTS OF MIDI EVENTS]

v0      ff 2f v0

There’s some header data in the beginning, and each MIDI event is comprised of a deltatime field starting with ‘v’, and then fairly standard MIDI events in straightforward syntax. I hardcoded my program to expect single track which starts with tempo and speed data (ff 51 and ff 58 lines) which I use to calculate how many deltatime units is one second. I copy this header part to start of every MIDI file, and append the final “ff 2f v0” (END TRACK) event to the end. Here’s the full code:
Continue reading Split MIDI Files with Python

Using Raspberry Pi as an automatic MIDI logger

During my summer holidays I got an interesting idea: Pianoteq has a very nice feature of “always on MIDI logging” that saves everything you play on your keyboard while Pianoteq was on. I’ve previously made some MIDI projects and had a great idea:

How about building a small device that records everything I play on my piano, and save it as MIDI files?

This would enable me to later grab a good performance, and eliminate the “recording anxiety” I get if I know I’m recording and should definitely not do any mistakes during the next 1000+ notes. Furthermore, even with easy MIDI recording to USB stick, it’s still several manual steps plugging the memory stick in, starting recording, stopping it, lugging it to a computer, etc.

My first idea was to use some WLAN-enabled embedded device, but MIDI IN would require optoisolators and some custom electronics, and more modern digital pianos often come with only USB MIDI, so it could easily become an exercise in communication protocols. Fast forward a couple of minutes to my next revelation:

Raspberry Pi Model 0 W already has USB and WLAN, and it’s small. Why not use that?

Turns out using a RaspPi as fully automated MIDI logger is really easy. Read on for instructions!

Update: Also check out my follow-up post to split the recorded MIDI files automatically!

Recording MIDI with Raspbian

Turns out recording MIDI from a USB MIDI enabled device is really easy. When I plug in my Kawai CS-11 (sorry for the unsolicited link, I love my CS11 :) to the Pi (or just turn it on when it’s plugged in), dmesg shows that the Pi automatically notices the new MIDI device:

[  587.887059] usb 1-1.5: new full-speed USB device number 4 using dwc_otg
[  588.022788] usb 1-1.5: New USB device found, idVendor=0f54, idProduct=0101
[  588.022800] usb 1-1.5: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[  588.022807] usb 1-1.5: Product: USB-MIDI
[  588.074579] usbcore: registered new interface driver snd-usb-audio

Once the USB MIDI device is found, you can use arecordmidi -l to list available MIDI ports:

pi@raspberrypi:~ $ arecordmidi -l
 Port    Client name                      Port name
 14:0    Midi Through                     Midi Through Port-0
 20:0    USB-MIDI                         USB-MIDI MIDI 1

Continue reading Using Raspberry Pi as an automatic MIDI logger

MIDI to USB Adapter with Teensy LC

Among my recent electronics purchase spree was the amazing Teensy LC from PJRC. It has a nice ARM Cortex-M0+ processor, real hardware USB, and what’s the nicest part, an Arduino add-on called Teensyduino which enables easy programming with Arduino, but with support for many of the hardware features.

Now I started playing piano a while ago, and just a few weeks ago bought a Pianoteq license to send notes via MIDI to my computer, and render high quality piano sound to speakers. However, it turns out my $8 USB-MIDI adapter from DealExtreme had a less than perfect implementation, essentially changing pedal events into “note on” events!

Thankfully, I had a MIDI connector and a high-speed optocoupler at hand, and with these I could implement a MIDI in rather easily. After some investigation with Arduino Uno, it seemed quite simple to receive the serial MIDI bytes and dump them over Arduino serial (I’ll write another post about this later).

However, Arduino cannot become a USB MIDI device very easily, so here comes the really nice part: Teensy LC can, and the Teensyduino add-on included a working USB MIDI and also serial MIDI libraries!

The Hardware

The Wikipedia page for MIDI essentially shows the required circuitry for receiving MIDI data – wire the DIN cable pins 4 and 5 through the receiving side of optocoupler and put a 200 ohm resistor in series with it. A diode is also suggested for reverse current (ESD) protection, but I skipped that. You can start with a LED instead of optocoupler to see it lights up if you’re unsure you have pins 4 and 5 the right way. Or just put the LED (with a resistor) on the other side of the optocoupler first.

The HCPL-2531 I had at hand requires an additional VCC connection on the sending side. After some experimentation, a 4k7 ohm pullup resistor between VCC and VO1 (NPN-transistor base?) gave the cleanest signal out. The wiring diagram (thanks Diptrace!) is shown below:

Teensy LC MIDI schematic
Continue reading MIDI to USB Adapter with Teensy LC

Adafruit Trinket USB keyboard without Arduino

Happy New Year 2016! After a long hiatus in electronics, I have been quite busy in the last month or so. I have a bigger (USB MIDI related) project posting coming up, but just wanted share a small nugget already.

I ordered a big chunk of things recently from Adafruit shop and Sparkfun. Among them was the lovely Adafruit Trinket (which actually came as a free bonus because I spent way too much on black friday :).

Now I am planning a project which involves transforming the very compact, but already USB-enabled Trinket into a USB MIDI device. However, there are two problems:

  1. Adafruit examples for USB come in Arduino form
  2. There are no USB MIDI examples

I somewhat dislike the high-level Arduino environment in cases where low-level performance is needed (and the V-USB implementation is one of those places), and for my later MIDI part, I will need fine-grained control to juggle serial communication and USB. Also, all USB MIDI examples are on “bare metal”, so the Adafruit example Arduino code would require deeper knowledge of Arduino inner workings than I have.

Time to do some chopping!

Slimming Down the TrinketKeyboard Example

I decided to adapt the excellent base code in Adafruit Trinket USB GitHub repository, but trim the keyboard example to bare essentials (my next step will be to transform it into a USB MIDI device, so the less code I have to adapt, the better). Turns out this was quite simple to do:
Continue reading Adafruit Trinket USB keyboard without Arduino