Class: Musa::Clock::Clock Abstract
Overview
Subclass and implement #run and #terminate
Abstract base class for all clock implementations.
This class defines the interface and callback infrastructure that all
concrete clock implementations must follow. Subclasses must implement
the run and terminate methods.
Callback System
Clocks maintain three callback collections:
- on_start: Called when clock starts running
- on_stop: Called when clock stops
- on_change_position: Called when position changes (seek/jump)
Subclass Responsibilities
Concrete clocks must:
- Implement
run(&block)- Start generating ticks, yield for each tick - Override
stopif additional cleanup is needed (call super) - Override
terminateto callstopthen exit the run loop - Manage @run state properly
- Reset
@stopped = falseat the start ofrun(and instartif applicable)
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize ⇒ Clock
constructor
Initializes the clock with empty callback collections.
-
#on_change_position {|bars, beats, midi_beats| ... } ⇒ void
Registers a callback to be called when playback position changes.
-
#on_start { ... } ⇒ void
Registers a callback to be called when the clock starts.
-
#on_stop { ... } ⇒ void
Registers a callback to be called when the clock stops.
-
#run { ... } ⇒ void
Starts the clock running and generates ticks.
-
#running? ⇒ Boolean
Checks if the clock is currently running.
-
#stop ⇒ void
Stops the clock and fires on_stop callbacks.
-
#terminate ⇒ void
Stops the clock and terminates the run loop.
Constructor Details
#initialize ⇒ Clock
Initializes the clock with empty callback collections.
74 75 76 77 78 79 80 |
# File 'lib/musa-dsl/transport/clock.rb', line 74 def initialize @run = nil @stopped = false @on_start = [] @on_stop = [] @on_change_position = [] end |
Instance Method Details
#on_change_position {|bars, beats, midi_beats| ... } ⇒ void
This method returns an undefined value.
Registers a callback to be called when playback position changes.
This is typically used for handling seek/jump operations where the transport position changes non-linearly.
144 145 146 |
# File 'lib/musa-dsl/transport/clock.rb', line 144 def on_change_position(&block) @on_change_position << block end |
#on_start { ... } ⇒ void
This method returns an undefined value.
Registers a callback to be called when the clock starts.
Multiple callbacks can be registered and will be called in order.
112 113 114 |
# File 'lib/musa-dsl/transport/clock.rb', line 112 def on_start(&block) @on_start << block end |
#on_stop { ... } ⇒ void
This method returns an undefined value.
Registers a callback to be called when the clock stops.
Multiple callbacks can be registered and will be called in order.
125 126 127 |
# File 'lib/musa-dsl/transport/clock.rb', line 125 def on_stop(&block) @on_stop << block end |
#run { ... } ⇒ void
This method typically runs in a loop until #terminate is called.
Subclasses should call @on_start callbacks when starting.
Subclasses should call #stop (not @on_stop directly) when stopping.
This method returns an undefined value.
Starts the clock running and generates ticks.
This method should block and yield once per tick. Subclasses must implement this method.
Subclasses should reset @stopped = false at the start of run
to allow stop/start cycles.
164 165 166 |
# File 'lib/musa-dsl/transport/clock.rb', line 164 def run raise NotImplementedError end |
#running? ⇒ Boolean
Checks if the clock is currently running.
85 86 87 |
# File 'lib/musa-dsl/transport/clock.rb', line 85 def running? @run end |
#stop ⇒ void
This method returns an undefined value.
Stops the clock and fires on_stop callbacks.
Idempotent: calling stop multiple times only fires callbacks once. Subclasses that need additional stop logic (e.g., pausing a timer) should override and call super.
96 97 98 99 100 101 |
# File 'lib/musa-dsl/transport/clock.rb', line 96 def stop return if @stopped @stopped = true @on_stop.each(&:call) end |
#terminate ⇒ void
179 180 181 |
# File 'lib/musa-dsl/transport/clock.rb', line 179 def terminate raise NotImplementedError end |