Created
March 28, 2016 19:31
-
-
Save kill9zombie/8b24389239891a5dbe3e to your computer and use it in GitHub Desktop.
MIDI control connection for Sonic Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.