Class: MTK::Sequencers::EventBuilder
- Inherits:
-
Object
- Object
- MTK::Sequencers::EventBuilder
- Defined in:
- lib/mtk/sequencers/event_builder.rb
Overview
A special pattern that takes a list of event properties and/or patterns and emits lists of Events::Events
Constant Summary collapse
- DEFAULT_PITCH =
MTK.Pitch(60)
- DEFAULT_DURATION =
MTK.Duration(1)
- DEFAULT_INTENSITY =
MTK.Intensity(0.75)
Instance Method Summary collapse
-
#initialize(patterns, options = {}) ⇒ EventBuilder
constructor
A new instance of EventBuilder.
-
#next ⇒ Array
Build a list of events from the next element in each Patterns::Pattern.
-
#rewind ⇒ Object
Reset the EventBuilder to its initial state.
Constructor Details
#initialize(patterns, options = {}) ⇒ EventBuilder
Returns a new instance of EventBuilder.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mtk/sequencers/event_builder.rb', line 11 def initialize(patterns, ={}) @patterns = patterns @options = @default_pitch = if .has_key? :default_pitch then MTK.Pitch( [:default_pitch]) else DEFAULT_PITCH end @default_duration = if .has_key? :default_duration then MTK.Duration( [:default_duration]) else DEFAULT_DURATION end @default_intensity = if .has_key? :default_intensity then MTK.Intensity([:default_intensity]) else DEFAULT_INTENSITY end @channel = [:channel] @max_interval = .fetch(:max_interval, 127) rewind end |
Instance Method Details
#next ⇒ Array
Build a list of events from the next element in each Patterns::Pattern
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mtk/sequencers/event_builder.rb', line 24 def next pitches = [] intensities = [] duration = nil @patterns.each do |pattern| pattern_value = pattern.next elements = if pattern_value.is_a? Enumerable and not pattern_value.is_a? MTK::Groups::PitchCollection then pattern_value else [pattern_value] end elements.each do |element| return nil if element.nil? or element == :skip case element when ::MTK::Core::Pitch pitches << element @previous_pitch = element when ::MTK::Core::PitchClass pitches += pitches_for_pitch_classes([element], @previous_pitch) @previous_pitch = pitches.last when ::MTK::Groups::PitchClassSet pitches += pitches_for_pitch_classes(element, @previous_pitch) @previous_pitch = pitches.last when ::MTK::Groups::PitchCollection pitches += element.pitches # this must be after the PitchClassSet case, because that is also a PitchCollection @previous_pitch = pitches.last when ::MTK::Core::Duration duration ||= 0 duration += element when ::MTK::Core::Intensity intensities << element when ::MTK::Core::Interval if @previous_pitches pitches += @previous_pitches.map{|pitch| pitch + element } else pitches << (@previous_pitch + element) end @previous_pitch = pitches.last # TODO? String/Symbols for special behaviors like :skip, or :break (something like StopIteration for the current Pattern?) else STDERR.puts "#{self.class}#next: Unexpected type '#{element.class}'" end end end pitches << @previous_pitch if pitches.empty? duration ||= @previous_duration.abs if intensities.empty? intensity = @previous_intensity else intensity = MTK::Core::Intensity[intensities.map{|i| i.to_f }.inject(:+)/intensities.length] # average the intensities end # Not using this yet, maybe later... # return nil if duration==:skip or intensities.include? :skip or pitches.include? :skip constrain_pitch(pitches) @previous_pitch = pitches.last # Consider doing something different, maybe averaging? @previous_pitches = pitches.length > 1 ? pitches : nil @previous_intensity = intensity @previous_duration = duration if duration.rest? [MTK::Events::Rest.new(duration,@channel)] else pitches.map{|pitch| MTK::Events::Note.new(pitch,duration,intensity,@channel) } end end |
#rewind ⇒ Object
Reset the EventBuilder to its initial state
108 109 110 111 112 113 114 115 116 |
# File 'lib/mtk/sequencers/event_builder.rb', line 108 def rewind @previous_pitch = @default_pitch @previous_pitches = [@default_pitch] @previous_intensity = @default_intensity @previous_duration = @default_duration @max_pitch = nil @min_pitch = nil @patterns.each{|pattern| pattern.rewind if pattern.is_a? MTK::Patterns::Pattern } end |