Skip to content

Instantly share code, notes, and snippets.

@kill9zombie
Created March 28, 2016 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kill9zombie/8b24389239891a5dbe3e to your computer and use it in GitHub Desktop.
Save kill9zombie/8b24389239891a5dbe3e to your computer and use it in GitHub Desktop.
MIDI control connection for Sonic Pi
#!/usr/bin/env ruby
# encoding: utf-8
require 'drb/drb'
require 'micromidi'
# The URI we're using for RDb, we use unix sockets just because they're quicker.
DRB_URI="drbunix:/var/tmp/sonic-pi-midiconnector"
class SPIMidiConnector
def initialize
@input = UniMIDI::Input.use(0)
@midithread = listen()
end
# Return the value of the pot.
#
# If we haven't moved a pot yet then just return 0.
def get(key)
@midithread[key] || 0
end
# Some convenience methods (they're shorter to type while livecoding).
# These should return the midi value (0 to 127) of the potentiometer on the LPD8.
def k1; get(:k1); end
def k2; get(:k2); end
def k3; get(:k3); end
def k4; get(:k4); end
def k5; get(:k5); end
def k6; get(:k6); end
def k7; get(:k7); end
def k8; get(:k8); end
def thread
@midithread
end
private
def listen
midithread = Thread.new(@pads_map) do |pad_map|
MIDI.using(@input) do
# When we get a control change message, update a
# thread attribute with the value.
thru_except :control_change do |msg|
key = "k#{msg.data[0]}".to_sym
puts "key: #{key} value: #{msg.value}"
midithread[key] = msg.value
end
join
end
end
midithread
end
end
# Then we start the DRb server, as per the DRb docs.
FRONT_OBJECT = SPIMidiConnector.new
$SAFE = 1
DRb.start_service(DRB_URI, FRONT_OBJECT)
DRb.thread.join
require 'drb/drb'
DRB_URI="drbunix:/var/tmp/sonic-pi-midiconnector"
@lpd = DRbObject.new_with_uri(DRB_URI)
use_bpm 120
live_loop :kick do
sample :bd_haus
sleep 1
end
with_fx :bitcrusher do
live_loop :saw do
sync :kick
use_synth :dsaw
play chord(:c, :major, num_octaves: 4).choose, cutoff: @lpd.k1, release: 2, attack: 1
sleep 0.5
end
end
@kill9zombie
Copy link
Author

The aim of all this is to try and use the LPD8 with Sonic Pi. I want to keep track of the eight pots on the right hand side (labelled up as K1 to K8).

Currently using dRuby because it's part of the Ruby std-lib and I didn't want to mess with Sonic Pi too much. You could start caching on the client side too, but it doesn't seem to need it yet.

The Sonic Pi example just lets you change the cutoff by turning the K1 pot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment