Sonic Pi as USB MIDI Keyboard Host

Stop me when you’ve heard it, but now I’ve got all these synths playing happily from SPi, I wanted to be able to optionally play them from a keyboard too. Problem is, some of them only have 5-Pin DIN input
and I only have USB MIDI keyboards d’Oh.

But Spi to the rescue with a few lines of code. I’m using one of the midi CC knobs to switch between the 4 ports on my setup. 1=Volca, 2=VCV Rack, 3=Crave. The only little issue is that I have to remember not to switch port while holding a key down, else the synth never gets a note_off. I tried midi_all_notes_off but they don’t respond to that. A minor thing.

Seems like it could a be neat solution - I can keep a keyboard hooked up, put the code it’s own buffer and run it if needs be. Then in the moment, I can either have SPi playing the synth or take over. This seems more flexible than the hardware-based alternative of a USB-MIDI host box, where I’d have to unplug/plug cables to switch.

Sorry about the vid quality, there’s a fan heater involved and I’m not set up properly for video.

#USB Keyboard MIDI host
set :midiport,"1"

live_loop :midihoston do
  n,v=sync "/midi:mpk_mini_midi_1:1:1/note_on"
  midi_note_on n,100,channel: 1, port: "midi4x4_midi_"+get[:midiport]

live_loop :midihostoff do
  n,v=sync "/midi:mpk_mini_midi_1:1:1/note_off"
  midi_note_off n,0,channel: 1, port: "midi4x4_midi_"+get[:midiport]

live_loop :midihostchannel do
  i,j=sync "/midi:mpk_mini_midi_1:1:1/control_change"
  set :midiport, (1+(4*j/128).round).to_s

…and combined with a sequencer also running in a SPi loop, I can play call-response on the one synth. Live and programmed combined, which I like