Skip to content

Instantly share code, notes, and snippets.

@lucasluitjes
Last active February 3, 2021 00:08
Show Gist options
  • Save lucasluitjes/527cd9ac52e67139e5a99ac1c6440026 to your computer and use it in GitHub Desktop.
Save lucasluitjes/527cd9ac52e67139e5a99ac1c6440026 to your computer and use it in GitHub Desktop.
# demo: https://www.youtube.com/watch?v=MZ3M0ePA5U4&feature=youtu.be
# this depends on the imitone software to convert whistling into midi
require "rtmidi"
midiin = RtMidi::In.new
MATCHERS = {
[0..2, 3..5] => "Page_Up",
[3..5, 0..2] => "Page_Down",
[0..2, 3..5, 6..9] => "Up",
[6..9, 3..5, 0..2] => "Down"
}
def match_buffer b
b = b.map { |n| n - b.min }
MATCHERS.to_a.select { |m| m[0].size == b.size }.each do |matcher, cmd|
next if matcher.zip(b).find { |r,n| !r.include? n}
`DISPLAY=':0' xdotool key #{cmd}`
end
end
last_note = 0
buffer = []
Thread.new do
loop do
sleep 0.1
if !buffer.empty? && (Time.now.to_f - last_note) > 1
match_buffer(buffer)
buffer = []
end
end
end
midiin.receive_channel_message do |byte1, byte2, byte3|
buffer << byte2 if byte1 == 144 && (buffer.empty? || (buffer.last - byte2).abs > 2)
last_note = Time.now.to_f
end
midiin.open_port(0)
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment