Class: TEF::Sequencing::BaseSequence

Inherits:
Object
  • Object
show all
Defined in:
lib/tef/Sequencing/BaseSequence.rb

Overview

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.

Direct Known Subclasses

SheetSequence

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offset, slope, **options) ⇒ BaseSequence

Initialize a BaseSequence.

Parameters:

  • offset (Numeric, Time)

    Provide a offset for time-conversion. @see #offset

  • slope (Numeric)

    Provide a slope for time-conversion. @see #slope

  • :start_time (Numeric)

    Local time to begin playing at.

  • :end_time (Numeric)

    Local time to tear down at.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

Instance Attribute Details

#end_timeNumeric? (readonly)

Returns End time of this sequence, in local time. Specifies when to call #teardown. Can be left as nil for no automatic teardown.

Returns:

  • (Numeric, nil)

    End time of this sequence, in local time. Specifies when to call #teardown. Can be left as nil for no automatic teardown.



20
21
22
# File 'lib/tef/Sequencing/BaseSequence.rb', line 20

def end_time
  @end_time
end

#offsetNumeric, Time (readonly)

Returns 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!.

Returns:

  • (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!



27
28
29
# File 'lib/tef/Sequencing/BaseSequence.rb', line 27

def offset
  @offset
end

#slopeNumeric (readonly)

Returns Slope to apply to this sequence.

Returns:

  • (Numeric)

    Slope to apply to this sequence.

See Also:



30
31
32
# File 'lib/tef/Sequencing/BaseSequence.rb', line 30

def slope
  @slope
end

#start_timeNumeric (readonly)

Returns Start time of this sequence, in local time. Specifies when to call #setup.

Returns:

  • (Numeric)

    Start time of this sequence, in local time. Specifies when to call #setup



16
17
18
# File 'lib/tef/Sequencing/BaseSequence.rb', line 16

def start_time
  @start_time
end

#stateSymbol (readonly)

purposes. Can be:

  • :uninitialized (right after construction)

  • :running (after having called setup())

  • :idle (after teardown() was called)

Returns:

  • (Symbol)

    State of this Sequence. Mainly used for internal



37
38
39
# File 'lib/tef/Sequencing/BaseSequence.rb', line 37

def state
  @state
end

Instance Method Details

#append_events(collector) ⇒ Object

Note:

When using BaseSequence as base class, the user shall overload #overload_append_events rather than this function!

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!



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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

#destroy!Object



132
133
134
# File 'lib/tef/Sequencing/BaseSequence.rb', line 132

def destroy!()
	teardown() if @state == :running
end

#overload_append_events(_collector) ⇒ Object



130
# File 'lib/tef/Sequencing/BaseSequence.rb', line 130

def overload_append_events(_collector) end

#parent_end_timeObject



66
67
68
69
70
# File 'lib/tef/Sequencing/BaseSequence.rb', line 66

def parent_end_time
	return nil if @end_time.nil?

	@offset + @end_time / @slope
end

#parent_end_time=(new_time) ⇒ Object



72
73
74
75
76
77
78
79
# 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

#parent_start_timeObject



63
64
65
# File 'lib/tef/Sequencing/BaseSequence.rb', line 63

def parent_start_time
	@offset + @start_time / @slope
end

#setupObject



81
82
83
84
# File 'lib/tef/Sequencing/BaseSequence.rb', line 81

def setup()
	return unless @state == :idle
	@state = :running
end

#teardownObject



86
87
88
89
# File 'lib/tef/Sequencing/BaseSequence.rb', line 86

def teardown()
	return unless @state == :running
	@state = :idle
end