From: Macario Ortega Date: 2009-08-08T14:40:11+09:00 Subject: Soft realtime with EventMachine and timer resolution 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 -- Posted via http://www.ruby-forum.com/.