Class: Xi::Clock

Inherits:
Object show all
Defined in:
lib/xi/clock.rb

Direct Known Subclasses

TidalClock

Constant Summary collapse

DEFAULT_CPS =
1.0
INTERVAL_SEC =
10 / 1000.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cps: DEFAULT_CPS) ⇒ Clock

Returns a new instance of Clock.



13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#init_tsObject (readonly)

Returns the value of attribute init_ts.



11
12
13
# File 'lib/xi/clock.rb', line 11

def init_ts
  @init_ts
end

#latencyObject

Returns the value of attribute latency.



11
12
13
# File 'lib/xi/clock.rb', line 11

def latency
  @latency
end

Instance Method Details

#bpmObject



47
48
49
# File 'lib/xi/clock.rb', line 47

def bpm
  @mutex.synchronize { @cps * 120 }
end

#bpm=(new_bpm) ⇒ Object



51
52
53
# File 'lib/xi/clock.rb', line 51

def bpm=(new_bpm)
  @mutex.synchronize { @cps = new_bpm / 120.0 }
end

#bpsObject



39
40
41
# File 'lib/xi/clock.rb', line 39

def bps
  @mutex.synchronize { @cps * 2 }
end

#bps=(new_bps) ⇒ Object



43
44
45
# File 'lib/xi/clock.rb', line 43

def bps=(new_bps)
  @mutex.synchronize { @cps = new_bps / 2.0 }
end

#cpsObject



31
32
33
# File 'lib/xi/clock.rb', line 31

def cps
  @mutex.synchronize { @cps }
end

#cps=(new_cps) ⇒ Object



35
36
37
# File 'lib/xi/clock.rb', line 35

def cps=(new_cps)
  @mutex.synchronize { @cps = new_cps.to_f }
end

#current_cycleObject



87
88
89
# File 'lib/xi/clock.rb', line 87

def current_cycle
  current_time * cps
end

#current_timeObject



83
84
85
# File 'lib/xi/clock.rb', line 83

def current_time
  Time.now.to_f - @init_ts + @latency
end

#inspectObject



91
92
93
94
# File 'lib/xi/clock.rb', line 91

def inspect
  "#<#{self.class.name}:#{"0x%014x" % object_id} " \
    "cps=#{cps.inspect} #{playing? ? :playing : :stopped}>"
end

#playObject Also known as: start, pause



67
68
69
70
# File 'lib/xi/clock.rb', line 67

def play
  @mutex.synchronize { @playing = true }
  self
end

#playing?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/xi/clock.rb', line 59

def playing?
  @mutex.synchronize { @playing }
end

#seconds_per_cycleObject



79
80
81
# File 'lib/xi/clock.rb', line 79

def seconds_per_cycle
  @mutex.synchronize { 1.0 / @cps }
end

#stopObject



73
74
75
76
# File 'lib/xi/clock.rb', line 73

def stop
  @mutex.synchronize { @playing = false }
  self
end

#stopped?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/xi/clock.rb', line 63

def stopped?
  !playing?
end

#subscribe(stream) ⇒ Object



23
24
25
# File 'lib/xi/clock.rb', line 23

def subscribe(stream)
  @mutex.synchronize { @streams << stream }
end

#unsubscribe(stream) ⇒ Object



27
28
29
# File 'lib/xi/clock.rb', line 27

def unsubscribe(stream)
  @mutex.synchronize { @streams.delete(stream) }
end