class TEF::Sequencing::BaseSequence
Base sequence class.
It implements the minium necessary tools to make different Sequences work together nicely. It's main function is to provide {#append_events}, which is used by a {Player} to fetch the next queued event for execution.
Attributes
@return [Numeric, nil] End time of this sequence, in local time.
Specifies when to call {#teardown}. Can be left as nil for no automatic teardown.
@return [Numeric, Time] The offset to apply to this Sequence,
used when converting between local-time and parent-time. This MAY be modified during runtime, though this may cause some events to be skipped!
@return [Numeric] Slope to apply to this sequence. @see offset
@return [Numeric] Start time of this sequence, in local time.
Specifies when to call {#setup}
@return [Symbol] State of this Sequence. Mainly used for internal purposes. Can be:
-
:uninitialized (right after construction)
-
:running (after having called setup())
-
:idle (after teardown() was called)
Public Class Methods
Initialize a BaseSequence
.
@param [Numeric, Time] offset Provide a offset for time-conversion.
@see #offset
@param [Numeric] slope Provide a slope for time-conversion.
@see #slope
@param [Numeric] :start_time Local time to begin playing at. @param [Numeric] :end_time Local time to tear down at.
# File lib/tef/Sequencing/BaseSequence.rb, line 48 def initialize(offset, slope, **options) @start_time ||= options[:start_time] || 0; @end_time ||= options[:end_time]; @offset = offset; @slope = slope.round(6); # Explanation: For some bizarre reason, rounding is necessary # to avoid a glitch in the timing math. 6 digits of precision # should be precise enough anyways. @state = :uninitialized @opts_hash = options; end
Public Instance Methods
Look for the next possible event that this sequence wants to execute. Will ensure that this sequence's {#setup} and {#teardown} blocks are called at the appropriate time.
Should only be called by a {Player} or another sequence!
@note When using BaseSequence
as base class, the user
shall overload {#overload_append_events} rather than this function!
# File lib/tef/Sequencing/BaseSequence.rb, line 100 def append_events(collector) return if @state == :uninitialized local_collector = collector.offset_collector(@offset, @slope); # Return if the collector has events before our start time return if local_collector.has_events? && local_collector.event_time < @start_time if !@end_time.nil? if @state == :running local_collector.add_event({ time: [@end_time, local_collector.start_time + 0.01].max, code: proc { self.teardown() } }) end return if local_collector.start_time >= @end_time end if @state == :idle local_collector.add_event({ time: [@start_time, local_collector.start_time + 0.01].max, code: proc { self.setup() } }); end overload_append_events(local_collector) end
# File lib/tef/Sequencing/BaseSequence.rb, line 132 def destroy!() teardown() if @state == :running end
# File lib/tef/Sequencing/BaseSequence.rb, line 130 def overload_append_events(_collector) end
# File lib/tef/Sequencing/BaseSequence.rb, line 66 def parent_end_time return nil if @end_time.nil? @offset + @end_time / @slope end
# File lib/tef/Sequencing/BaseSequence.rb, line 72 def parent_end_time=(new_time) if(new_time.nil?) @end_time = nil; return; end @end_time = (new_time - @offset) * @slope; end
# File lib/tef/Sequencing/BaseSequence.rb, line 63 def parent_start_time @offset + @start_time / @slope end
# File lib/tef/Sequencing/BaseSequence.rb, line 81 def setup() return unless @state == :idle @state = :running end
# File lib/tef/Sequencing/BaseSequence.rb, line 86 def teardown() return unless @state == :running @state = :idle end