Class: Xi::Clock
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_CPS =
1.0
- INTERVAL_SEC =
10 / 1000.0
Instance Attribute Summary collapse
-
#init_ts ⇒ Object
readonly
Returns the value of attribute init_ts.
-
#latency ⇒ Object
Returns the value of attribute latency.
Instance Method Summary collapse
- #bpm ⇒ Object
- #bpm=(new_bpm) ⇒ Object
- #bps ⇒ Object
- #bps=(new_bps) ⇒ Object
- #cps ⇒ Object
- #cps=(new_cps) ⇒ Object
- #current_cycle ⇒ Object
- #current_time ⇒ Object
-
#initialize(cps: DEFAULT_CPS) ⇒ Clock
constructor
A new instance of Clock.
- #inspect ⇒ Object
- #play ⇒ Object (also: #start, #pause)
- #playing? ⇒ Boolean
- #seconds_per_cycle ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
- #subscribe(stream) ⇒ Object
- #unsubscribe(stream) ⇒ Object
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_ts ⇒ Object (readonly)
Returns the value of attribute init_ts.
11 12 13 |
# File 'lib/xi/clock.rb', line 11 def init_ts @init_ts end |
#latency ⇒ Object
Returns the value of attribute latency.
11 12 13 |
# File 'lib/xi/clock.rb', line 11 def latency @latency end |
Instance Method Details
#bpm ⇒ Object
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 |
#bps ⇒ Object
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 |
#cps ⇒ Object
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_cycle ⇒ Object
87 88 89 |
# File 'lib/xi/clock.rb', line 87 def current_cycle current_time * cps end |
#current_time ⇒ Object
83 84 85 |
# File 'lib/xi/clock.rb', line 83 def current_time Time.now.to_f - @init_ts + @latency end |
#inspect ⇒ Object
91 92 93 94 |
# File 'lib/xi/clock.rb', line 91 def inspect "#<#{self.class.name}:#{"0x%014x" % object_id} " \ "cps=#{cps.inspect} #{ ? :playing : :stopped}>" end |
#play ⇒ Object 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
59 60 61 |
# File 'lib/xi/clock.rb', line 59 def @mutex.synchronize { @playing } end |
#seconds_per_cycle ⇒ Object
79 80 81 |
# File 'lib/xi/clock.rb', line 79 def seconds_per_cycle @mutex.synchronize { 1.0 / @cps } end |
#stop ⇒ Object
73 74 75 76 |
# File 'lib/xi/clock.rb', line 73 def stop @mutex.synchronize { @playing = false } self end |
#stopped? ⇒ Boolean
63 64 65 |
# File 'lib/xi/clock.rb', line 63 def stopped? ! 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 |