# Midi scheduler in ruby

**URL:** <https://www.ruby-forum.com/t/midi-scheduler-in-ruby/115465>\
**Category:** Ruby\
**Created:** [September 25, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/midi-scheduler-in-ruby/115465 "2007-09-25T16:45:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [September 25, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/midi-scheduler-in-ruby/115465/1 "2007-09-25T16:45:48Z")

</div>

I am writing ruby wrappers around rtMidi  
([The RtMidi Tutorial](http://www.music.mcgill.ca/~gary/rtmidi/)) 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]

1. What is the best way to implement such a scheduler ?

2. Should I create a thread in C that checks for events in a buffer ?

3. 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
