Class: Musa::Clock::ExternalTickClock
- Defined in:
- lib/musa-dsl/transport/external-tick-clock.rb
Overview
Clock driven by external tick() calls for integration and testing.
ExternalTickClock doesn't generate its own ticks. Instead, ticks are triggered manually by calling the #tick method.
Activation Model
IMPORTANT: ExternalTickClock requires manual tick generation. After calling
transport.start (which returns immediately, doesn't block), you must call
clock.tick() repeatedly from your external system to generate ticks.
This activation model is appropriate for:
- Testing: Precise control over timing for step-by-step debugging
- Game engine integration: Game loop controls tick timing
- Frame-based systems: One tick per frame or custom logic
- Offline rendering: Generate ticks as fast as needed
Operation
- Call #run to initialize (doesn't block, returns immediately)
- Call #tick repeatedly to generate ticks manually
- Call #terminate when done
Differences from Other Clocks
Unlike TimerClock and InputMidiClock, ExternalTickClock's run method
does not block - it returns immediately. This allows your external
system to control the timing loop.
Instance Method Summary collapse
-
#initialize(do_log: nil) ⇒ ExternalTickClock
constructor
Creates a new externally-controlled clock.
-
#run { ... } ⇒ void
Initializes the clock (non-blocking).
-
#stop ⇒ void
Stops the clock and fires on_stop callbacks.
-
#terminate ⇒ void
Terminates the clock.
-
#tick ⇒ void
Generates one tick manually.
Constructor Details
#initialize(do_log: nil) ⇒ ExternalTickClock
Creates a new externally-controlled clock.
77 78 79 80 81 82 83 |
# File 'lib/musa-dsl/transport/external-tick-clock.rb', line 77 def initialize(do_log: nil) do_log ||= false super() @do_log = do_log end |
Instance Method Details
#run { ... } ⇒ void
This method does NOT block
This method returns an undefined value.
Initializes the clock (non-blocking).
Unlike other clocks, this method doesn't block. It stores the block and calls on_start callbacks, then returns immediately. Ticks are generated by calling #tick.
95 96 97 98 99 100 |
# File 'lib/musa-dsl/transport/external-tick-clock.rb', line 95 def run(&block) @stopped = false @on_start.each(&:call) @run = true @block = block end |
#stop ⇒ void
This method returns an undefined value.
Stops the clock and fires on_stop callbacks.
120 121 122 123 |
# File 'lib/musa-dsl/transport/external-tick-clock.rb', line 120 def stop @run = false super end |
#terminate ⇒ void
This method returns an undefined value.
Terminates the clock.
Delegates to #stop which fires on_stop callbacks and sets @run to false.
130 131 132 |
# File 'lib/musa-dsl/transport/external-tick-clock.rb', line 130 def terminate stop end |
#tick ⇒ void
Only works if #run has been called
Thread-safe for integration with external event loops
This method returns an undefined value.
Generates one tick manually.
If the clock is running, calls the registered block (typically sequencer.tick). Has no effect if clock is not running.
111 112 113 114 115 |
# File 'lib/musa-dsl/transport/external-tick-clock.rb', line 111 def tick if @run @block.call if @block end end |