Skip to content

Instantly share code, notes, and snippets.

@kylekyle
Last active December 11, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylekyle/4657952 to your computer and use it in GitHub Desktop.
Save kylekyle/4657952 to your computer and use it in GitHub Desktop.
Reads keypress information and translates to midi notes. See comments for details.
# This reads from key presses from a serial device (such
# as an arduino) in the following form:
# +4-2+11
# this means that the keys 4 and 11 are engaged
# and key 2 is not engaged. Use the keys map below
# to map keys to midi notes
require 'unimidi'
require 'serialport'
keys = {
'2' => 60,
'3' => 62,
'5' => 64,
'6' => 65,
'8' => 67,
'9' => 69,
'11' => 71,
'12' => 72
}
pressed = Hash.new false
midi = UniMIDI::Output.first
arduino = SerialPort.new '/dev/tty.usbmodem1411'
arduino.baud = 9600
arduino.data_bits = 8
arduino.stop_bits = 1
loop do
arduino.readline.split.each do |info|
state, key = info[0], info[1..-1]
next unless keys.include? key
if state == '+' and not pressed[key]
midi.puts(0x90, keys[key], 100)
pressed[key] = true
end
if state == '-' and pressed[key]
midi.puts(0x80, keys[key], 100)
pressed[key] = false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment