Skip to content

Instantly share code, notes, and snippets.

@eric-wood
Last active August 29, 2015 14:14
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 eric-wood/efe7d13d3a91e5d02f7f to your computer and use it in GitHub Desktop.
Save eric-wood/efe7d13d3a91e5d02f7f to your computer and use it in GitHub Desktop.
Midi threading puzzle...
# NOTE:
# the following code works GREAT, since UniMIDI::Input#gets blocks (just like stdin's gets)
input = UniMIDI::Input.use(:first)
Thread.new do
loop { p input.gets }
end
# ...but the following code only returns once and no subsequent notes are printed...
# test of Sequencer::Reader
# also note that in real life this is called via a program that doesn't immediately exit! (the UI event loop)
reader = Sequencer::Reader.new
reader.start
# if you bang on a midi device you'll only see gets return ONCE and never return again. Weird.
# I would LOVE to hear some theories about this...
class Sequencer
class Reader
attr_reader :notes
def initialize(input=nil)
@input ||= UniMIDI::Input.use(:first)
@notes = []
end
def start
@notes = []
@thread = create_thread
end
def stop
@notes = @thread[:notes]
Thread.kill(@thread)
return @notes
end
private
def is_key_on?(note)
note[:data][0] == 0x90
end
def create_thread
Thread.new do
Thread.current[:notes] = []
loop do
puts "searching!"
note = @input.gets
p note
if is_key_on?(note)
Thread.current[:notes] << note
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment