Class: MTK::Sequencers::Sequencer Abstract
- Inherits:
-
Object
- Object
- MTK::Sequencers::Sequencer
- Defined in:
- lib/mtk/sequencers/sequencer.rb
Overview
Subclass and override #advance to implement a Sequencer.
A Sequencer produces Events::Timelines from a collection of Patterns::Patterns.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#event_builder ⇒ Object
readonly
Used by #to_timeline to builds event lists from the results of Patterns::Pattern#next for the Patterns::Patterns in this Sequencer.
-
#max_steps ⇒ Object
The maximum number of [time,event_list] entries that will be generated for the Events::Timeline.
-
#max_time ⇒ Object
The maximum time (key) that will be generated for the Events::Timeline.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
-
#step ⇒ Object
readonly
The current sequencer step index (the number of times-1 that #next has been called), or -1 if the sequencer has not yet started.
-
#time ⇒ Object
readonly
The current time offset for the sequencer.
Instance Method Summary collapse
-
#advance ⇒ Object
protected
Advance @time to the next time for the Events::Timeline being produced by #to_timeline.
-
#initialize(patterns, options = {}) ⇒ Sequencer
constructor
A new instance of Sequencer.
-
#next ⇒ Object
Advanced the step index and time, and return the next list of events built from the sequencer patterns.
-
#rewind ⇒ Object
Reset the sequencer and all its patterns.
-
#to_timeline ⇒ Object
Produce a Events::Timeline from the Patterns::Patterns in this Sequencer.
Constructor Details
#initialize(patterns, options = {}) ⇒ Sequencer
Returns a new instance of Sequencer.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mtk/sequencers/sequencer.rb', line 35 def initialize(patterns, ={}) @patterns = patterns @max_steps = [:max_steps] @max_time = [:max_time] @filter = [:filter] event_builder_class = .fetch :event_builder, ::MTK::Sequencers::EventBuilder @event_builder = event_builder_class.new(patterns, ) rewind end |
Instance Attribute Details
#event_builder ⇒ Object (readonly)
Used by #to_timeline to builds event lists from the results of Patterns::Pattern#next for the Patterns::Patterns in this Sequencer.
19 20 21 |
# File 'lib/mtk/sequencers/sequencer.rb', line 19 def event_builder @event_builder end |
#max_steps ⇒ Object
The maximum number of [time,event_list] entries that will be generated for the Events::Timeline. nil means no maximum (be careful of infinite loops!)
12 13 14 |
# File 'lib/mtk/sequencers/sequencer.rb', line 12 def max_steps @max_steps end |
#max_time ⇒ Object
The maximum time (key) that will be generated for the Events::Timeline. nil means no maximum (be careful of infinite loops!)
16 17 18 |
# File 'lib/mtk/sequencers/sequencer.rb', line 16 def max_time @max_time end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
27 28 29 |
# File 'lib/mtk/sequencers/sequencer.rb', line 27 def patterns @patterns end |
#step ⇒ Object (readonly)
The current sequencer step index (the number of times-1 that #next has been called), or -1 if the sequencer has not yet started.
25 26 27 |
# File 'lib/mtk/sequencers/sequencer.rb', line 25 def step @step end |
#time ⇒ Object (readonly)
The current time offset for the sequencer. Used for the Events::Timeline times.
22 23 24 |
# File 'lib/mtk/sequencers/sequencer.rb', line 22 def time @time end |
Instance Method Details
#advance ⇒ Object (protected)
Advance @time to the next time for the Events::Timeline being produced by #to_timeline
93 94 95 |
# File 'lib/mtk/sequencers/sequencer.rb', line 93 def advance @time += 1 # default behavior simply advances one beat at a time end |
#next ⇒ Object
this is called automatically by #to_timeline, so you can ignore this method unless you want to hack on sequencers at a lower level.
Advanced the step index and time, and return the next list of events built from the sequencer patterns.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mtk/sequencers/sequencer.rb', line 66 def next if @step >= 0 advance raise StopIteration if @max_time and @time > @max_time end @step += 1 raise StopIteration if @max_steps and @step >= @max_steps events = @event_builder.next events = @filter[events] if @filter events end |
#rewind ⇒ Object
this is called automatically at the beginning of #to_timeline, so you can ignore this method unless you want to hack on sequencers at a lower level.
Reset the sequencer and all its patterns.
82 83 84 85 86 |
# File 'lib/mtk/sequencers/sequencer.rb', line 82 def rewind @time = 0 @step = -1 @event_builder.rewind end |
#to_timeline ⇒ Object
Produce a Events::Timeline from the Patterns::Patterns in this Sequencer.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mtk/sequencers/sequencer.rb', line 49 def to_timeline rewind timeline = MTK::Events::Timeline.new loop do events = self.next if events events = events.reject{|e| e.rest? } timeline[@time] = events unless events.empty? end end timeline end |