Class: MTK::Sequencers::Sequencer Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/mtk/sequencers/sequencer.rb

Overview

This class is abstract.

Subclass and override #advance to implement a Sequencer.

A Sequencer produces Events::Timelines from a collection of Patterns::Patterns.

Direct Known Subclasses

LegatoSequencer, RhythmicSequencer, StepSequencer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patterns, options = {}) ⇒ Sequencer

Returns a new instance of Sequencer.

Parameters:

  • patterns (Array)

    the list of patterns to be sequenced into a Events::Timeline

  • options (Hash) (defaults to: {})

    the options to create a message with.

Options Hash (options):

  • :max_steps (String)
  • :max_time (String)

    set #max_time

  • :filter (Proc)

    a Proc that will replace the events generated by #next with the results of the Proc

  • :event_builder (Class)

    replace the EventBuilder with a custom Event pattern



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mtk/sequencers/sequencer.rb', line 35

def initialize(patterns, options={})
  @patterns = patterns
  @max_steps = options[:max_steps]
  @max_time = options[:max_time]
  @filter = options[:filter]

  event_builder_class = options.fetch :event_builder, ::MTK::Sequencers::EventBuilder
  @event_builder = event_builder_class.new(patterns, options)

  rewind
end

Instance Attribute Details

#event_builderObject (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_stepsObject

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_timeObject

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

#patternsObject (readonly)

Returns the value of attribute patterns.



27
28
29
# File 'lib/mtk/sequencers/sequencer.rb', line 27

def patterns
  @patterns
end

#stepObject (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

#timeObject (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

#advanceObject (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

#nextObject

Note:

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.

Raises:

  • (StopIteration)


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

#rewindObject

Note:

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_timelineObject

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