class Xi::Clock

Constants

DEFAULT_CPS
INTERVAL_SEC

Attributes

init_ts[R]
latency[R]

Public Class Methods

new(cps: DEFAULT_CPS) click to toggle source
# File lib/xi/clock.rb, line 13
def initialize(cps: DEFAULT_CPS)
  @mutex = Mutex.new
  @cps = cps
  @playing = true
  @streams = [].to_set
  @init_ts = Time.now.to_i.to_f
  @latency = 0.0
  @play_thread = Thread.new { thread_routine }
end

Public Instance Methods

bpm() click to toggle source
# File lib/xi/clock.rb, line 47
def bpm
  @mutex.synchronize { @cps * 120 }
end
bpm=(new_bpm) click to toggle source
# File lib/xi/clock.rb, line 51
def bpm=(new_bpm)
  @mutex.synchronize { @cps = new_bpm / 120.0 }
end
bps() click to toggle source
# File lib/xi/clock.rb, line 39
def bps
  @mutex.synchronize { @cps * 2 }
end
bps=(new_bps) click to toggle source
# File lib/xi/clock.rb, line 43
def bps=(new_bps)
  @mutex.synchronize { @cps = new_bps / 2.0 }
end
cps() click to toggle source
# File lib/xi/clock.rb, line 31
def cps
  @mutex.synchronize { @cps }
end
cps=(new_cps) click to toggle source
# File lib/xi/clock.rb, line 35
def cps=(new_cps)
  @mutex.synchronize { @cps = new_cps.to_f }
end
current_cycle() click to toggle source
# File lib/xi/clock.rb, line 87
def current_cycle
  current_time * cps
end
current_time() click to toggle source
# File lib/xi/clock.rb, line 83
def current_time
  Time.now.to_f - @init_ts + @latency
end
inspect() click to toggle source
# File lib/xi/clock.rb, line 91
def inspect
  "#<#{self.class.name}:#{"0x%014x" % object_id} " \
    "cps=#{cps.inspect} #{playing? ? :playing : :stopped}>"
end
latency=(new_latency) click to toggle source
# File lib/xi/clock.rb, line 55
def latency=(new_latency)
  @latency = new_latency.to_f
end
pause()
Alias for: play
play() click to toggle source
# File lib/xi/clock.rb, line 67
def play
  @mutex.synchronize { @playing = true }
  self
end
Also aliased as: start, pause
playing?() click to toggle source
# File lib/xi/clock.rb, line 59
def playing?
  @mutex.synchronize { @playing }
end
seconds_per_cycle() click to toggle source
# File lib/xi/clock.rb, line 79
def seconds_per_cycle
  @mutex.synchronize { 1.0 / @cps }
end
start()
Alias for: play
stop() click to toggle source
# File lib/xi/clock.rb, line 73
def stop
  @mutex.synchronize { @playing = false }
  self
end
stopped?() click to toggle source
# File lib/xi/clock.rb, line 63
def stopped?
  !playing?
end
subscribe(stream) click to toggle source
# File lib/xi/clock.rb, line 23
def subscribe(stream)
  @mutex.synchronize { @streams << stream }
end
unsubscribe(stream) click to toggle source
# File lib/xi/clock.rb, line 27
def unsubscribe(stream)
  @mutex.synchronize { @streams.delete(stream) }
end

Private Instance Methods

do_tick() click to toggle source
# File lib/xi/clock.rb, line 105
def do_tick
  return unless playing?
  now  = self.current_time
  cps = self.cps
  @streams.each { |s| s.notify(now, cps) }
rescue => err
  error(err)
end
thread_routine() click to toggle source
# File lib/xi/clock.rb, line 98
def thread_routine
  loop do
    do_tick
    sleep INTERVAL_SEC
  end
end