I am writing ruby wrappers around rtMidi
(The RtMidi Tutorial) and need to trigger “note
off”.
This is the api I wish to implement:
@midiout = RtMidi.new(‘port name’)
note = 62
velocity = 100
duration = 0.25 # 1/4
@midiout.play(note, velocity, duration)
When a note is played, a first midi event is created for the velocity,
then, a little later a second for the note off:
[note on ]
[wait ]
[note off]
-
What is the best way to implement such a scheduler ?
-
Should I create a thread in C that checks for events in a buffer ?
-
Should this scheduler be implemented in Ruby ?
class Scheduler
def initialize
@events = []
Thread.new
while(true) do
sleep 0.01 # ?
if @events[0] && @events[0].time >= Time.now
@events.shift.trigger
end
end
end
end
def <<(e)
i = 0
while(@events[i] && e.time <= @events[i])
i += 1
end
if @events[i]
@events[i…i] << [e, i]
else
@events << e
end
end
end
Many thanks for your insight.
Gaspard