Soft realtime with EventMachine and timer resolution

M

Macario Ortega

Hi, I trying to do music livecoding with Ruby but I am having problems
with event scheduling, I shold have known before.

My question is how to increse timer resolution for EventMachine timer.

Thing is I have a more or less precise timer (apparently a jitter of
10ms in rithmic patterns is not percieivable) but is kind of expensive
in terms of computation.

It looks somewhat like this:

class Ticker
attr_reader :tick

def initialize tempo
@tempo = tempo
@interval = 60.0 / @tempo
@sleep_time = @interval / 100
@tick = 0
end

def run
@thread = Thread.new do
@next = Time.now.to_f
loop do
@next += @interval
@tick += 1
Thread.new do
tick
end
sleep @sleep_time until Time.now.to_f >= @next
end
end
end

def tick
end
end

I've never used EventMachine and I don't understand much of it but I
made a timer. First thing I was surprised on how light on the processor
it is, but no matter how low is the number I pass to add_periodic_timer
I get jitter of average 50ms and If I create too much timers the jitter
regulary increases eventually reaching seconds.

Is there any way to increase timer quatum from Ruby?

I found this C line somewhere:
evma_set_timer_quantum(16/*msec*/); // roughly 60Hz

Any advice?

Thanks
Macario
btw, heres my code:



require 'rubygems'
require 'eventmachine'

class Ticker
attr_reader :tick

def initialize tempo
@tempo = tempo
@interval = 60.0 / @tempo
@tick = 0
end

def bang
puts Time.now.to_f - @expected
@expected = @next
end

def run
@expected = Time.now.to_f
@next = Time.now.to_f
@thread = Thread.new do
EventMachine.run do
EM.add_periodic_timer do
if Time.now.to_f >= @next
@next += @interval
@tick += 1
bang
end
end
end
end
end
end

10.times do
Ticker.new(120*4).run
end

sleep 10
 
B

Bill Kelly

From: "Macario Ortega said:
I've never used EventMachine and I don't understand much of it but I
made a timer. First thing I was surprised on how light on the processor
it is, but no matter how low is the number I pass to add_periodic_timer
I get jitter of average 50ms and If I create too much timers the jitter
regulary increases eventually reaching seconds.

Is there any way to increase timer quatum from Ruby?

I found this C line somewhere:
evma_set_timer_quantum(16/*msec*/); // roughly 60Hz

Hehe, i thought that looked familiar... it appears to be from an email
I posted on the eventmachine-talk mailing list in 2006. :)

But yes: these days the set_timer_quantum API is available from ruby.

EventMachine.set_timer_quantum(10);

Note: Internally, EventMachine places limits on the min/max allowed
values for quantum: (in em.cpp)

/* We get a timer-quantum expressed in milliseconds.
* Don't set a quantum smaller than 5 or larger than 2500.
*/

if ((interval < 5) || (interval > 2500))
throw std::runtime_error ("invalid timer-quantum");


Hope this helps,

Bill
 
M

Macario Ortega

Bill said:
Hehe, i thought that looked familiar... it appears to be from an email
I posted on the eventmachine-talk mailing list in 2006. :)

But yes: these days the set_timer_quantum API is available from ruby.

EventMachine.set_timer_quantum(10);

Note: Internally, EventMachine places limits on the min/max allowed
values for quantum: (in em.cpp)

/* We get a timer-quantum expressed in milliseconds.
* Don't set a quantum smaller than 5 or larger than 2500.
*/

if ((interval < 5) || (interval > 2500))
throw std::runtime_error ("invalid timer-quantum");


Hope this helps,

Bill


Thanks for your reply, it was useful!

I found the cheapest is to exclusivelly use add_periodic_timer with my
desired interval but it increasingly drifted.

This is my timer loop:

def run
@next = Time.now.to_f
@thread = Thread.new do
EventMachine.run do
@start = Time.now.to_f
EM.set_quantum 5
EM.add_periodic_timer @interval / 30 do
if Time.now.to_f >= @next
bang
@tick += 1
@next = @tick * @interval + @start
end
end
end
end
self
end

EventMachine doesn't seem to warranty the accuracy of the periodic
timers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top