Class: TEF::Sequencing::Player

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

Overview

TODO:

Add pausing of sequences. Requires the BaseSequence to have a time conversion built-in as well.

Sequene Player class.

This class is meant as a easy means to play BaseSequences. It allows the user to start and stop sequences, play them in parallel, or overwrite them.

Register hooks to be called after an execution step with #after_exec.

Start playing a sequence by using #[]=, then use #delete to stop it prematurely.

Instance Method Summary collapse

Constructor Details

#initializePlayer

Initialize a player instance

It can immediately be used to play BaseSequences or Sheets, though #after_exec callbacks should first be registered.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tef/Sequencing/SequencePlayer.rb', line 28

def initialize()
	@activeSequences = {}
	@sequenceMutex = Mutex.new

	@post_exec_cbs = []

	@collector = EventCollector.new()

	@retryCollecting = false

	@playThread = Thread.new do
		_run_play_thread()
	end

	@playThread.abort_on_exception = true
end

Instance Method Details

#[](key) ⇒ Object



103
104
105
# File 'lib/tef/Sequencing/SequencePlayer.rb', line 103

def [](key)
	@activeSequences[key]
end

#[]=(key, program) ⇒ Object

Insert and start a new program.

This will:

  • Stop and tear the currently playing program under ‘key’ down.

  • Instantiate a SheetSequence IF the program is a Sheet

  • Immediately start playing the new program.

The inserted program is returned. #delete can be used to stop a given program.

Parameters:

  • key

    Arbitrary key to identify the program with.

  • program (BaseSequence, Sheet)

    Program to start playing.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tef/Sequencing/SequencePlayer.rb', line 70

def []=(key, program)
	@sequenceMutex.synchronize do
		if @activeSequences[key]
			@activeSequences[key].teardown
		end

		if program.is_a? Sheet
			program = SheetSequence.new(Time.now(), 1, sheet: program)
		end

		@activeSequences[key] = program
		@retryCollecting = true
	end

	@playThread.run();

	program
end

#after_exec(&block) ⇒ Object

Add a callback to be executed after a animation step.

The block passed to this function will be executed after each EventCollector#execute!, i.e. after every tick of the animation system. This makes it possible to connect it to the Animation system, for example by calling Animation::Handler#update_tick, as well as the parameter stack by calling ParameterStack::Stack#process_changes



54
55
56
# File 'lib/tef/Sequencing/SequencePlayer.rb', line 54

def after_exec(&block)
	@post_exec_cbs << block if block_given?
end

#delete(key) ⇒ Object

Delete a given program by its key. This will stop and tear the specified program down.



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tef/Sequencing/SequencePlayer.rb', line 91

def delete(key)
	@sequenceMutex.synchronize do
		if @activeSequences[key]
			@activeSequences[key].teardown
		end

		@activeSequences.delete key
		@retryCollecting = true
		@playThread.run();
	end
end