lib/xi/clock.rb in xi-lang-0.1.0 vs lib/xi/clock.rb in xi-lang-0.1.2
- old
+ new
@@ -5,11 +5,11 @@
Thread.abort_on_exception = true
module Xi
class Clock
DEFAULT_CPS = 1.0
- INTERVAL_SEC = 10 / 1000.0
+ INTERVAL_SEC = 20 / 1000.0
def initialize(cps: DEFAULT_CPS)
@mutex = Mutex.new
@cps = cps
@playing = true
@@ -39,10 +39,14 @@
def stopped?
!playing?
end
+ def now
+ Time.now.to_f * cps
+ end
+
def play
@mutex.synchronize { @playing = true }
self
end
alias_method :start, :play
@@ -50,9 +54,17 @@
def stop
@mutex.synchronize { @playing = false }
self
end
alias_method :pause, :play
+
+ def seconds_per_cycle
+ @mutex.synchronize { 1.0 / @cps }
+ end
+
+ def at(cycle_pos)
+ Time.at(cycle_pos * seconds_per_cycle)
+ end
def inspect
"#<#{self.class.name}:#{"0x%014x" % object_id} cps=#{cps.inspect} #{playing? ? :playing : :stopped}>"
end