Class: MIDIator::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/midiator/timer.rb

Overview

A timer that allows you to schedule notes to be played in the future.

Authors

Copyright © 2008 Ben Bleything

This code released under the terms of the MIT license.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolution) ⇒ Timer

Create a new Timer object that ticks every resolution seconds.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/midiator/timer.rb', line 21

def initialize( resolution )
  @resolution = resolution
  @queue = []

  @thread = Thread.new do
    loop do
      dispatch
      sleep @resolution
    end
  end
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



18
19
20
# File 'lib/midiator/timer.rb', line 18

def queue
  @queue
end

#resolutionObject (readonly)

Returns the value of attribute resolution.



18
19
20
# File 'lib/midiator/timer.rb', line 18

def resolution
  @resolution
end

#threadObject (readonly)

Returns the value of attribute thread.



18
19
20
# File 'lib/midiator/timer.rb', line 18

def thread
  @thread
end

Instance Method Details

#at(time, &block) ⇒ Object

Add a new job to be performed at time.



41
42
43
44
# File 'lib/midiator/timer.rb', line 41

def at( time, &block )
  time = time.to_f if time.kind_of? Time
  @queue.push [ time, block ]
end

#flushObject

Empty the queue



35
36
37
# File 'lib/midiator/timer.rb', line 35

def flush
  @queue.clear
end