Skip to content

Instantly share code, notes, and snippets.

@eric-wood
Created December 20, 2014 23:20
Show Gist options
  • Save eric-wood/8ef56a782e05034b6117 to your computer and use it in GitHub Desktop.
Save eric-wood/8ef56a782e05034b6117 to your computer and use it in GitHub Desktop.
Read the first 5 MIDI notes presented, play 'em back
require 'unimidi'
input = UniMIDI::Input.use(:first)
output = UniMIDI::Output.use(:first)
puts "-" * 100
puts "Start playing some notes plz"
puts "-" * 100
data = []
NUM_NOTES = 5
loop do
break if data.size >= NUM_NOTES
note = input.gets.first
# only add "note on" data
data << note if note[:data][0] == 0x90
end
puts "cool, thanks for the notes"
# strip out the other details
notes = data.map { |i| i[:data] }
sequencer = Thread.new do
Thread.current[:delay] = 0.1
Thread.current[:notes] ||= []
loop do
Thread.current[:notes].each do |note|
output.puts(0x90, note[1], note[2])
sleep(Thread.current[:delay])
output.puts(0x80, note[1], note[2])
end
end
end
sequencer[:notes] = notes
gets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment