Last active
August 29, 2015 14:14
-
-
Save eric-wood/efe7d13d3a91e5d02f7f to your computer and use it in GitHub Desktop.
Midi threading puzzle...
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
# 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... |
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
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