Class: Ramekin::Legato

Inherits:
Processor show all
Defined in:
lib/ramekin/legato.rb

Constant Summary collapse

TIEABLE_EVENTS =
%i(
  y
  rely
  v
  relv
  o
  p
  hex
  octave
  transpose
  adsr
  bpm
  native_tie
)

Instance Attribute Summary

Attributes inherited from Processor

#stream

Instance Method Summary collapse

Methods inherited from Processor

#buffer, call, compose, #each, #flush!, #next!, #peek

Constructor Details

#initializeLegato



69
70
71
72
# File 'lib/ramekin/legato.rb', line 69

def initialize
  @in_legato = false
  @seen_legato = false
end

Instance Method Details

#buffer!(note, &b) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/ramekin/legato.rb', line 164

def buffer!(note, &b)
  if note.rest?
    yield note
  else
    buffer << note
  end
end

#call(&b) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ramekin/legato.rb', line 116

def call(&b)
  return enum_for(:process) unless block_given?

  each do |el|
    if @in_legato
      if tieable?(el)
        buffer << el
      elsif NoteEvent === el
        if @seen_legato
          @seen_legato = false
          buffer << el
        else
          @in_legato = false
          @seen_legato = false
          flush_legato!(&b)
          buffer!(el, &b)
        end
      elsif Token === el && el.type == :legato_tie
        @seen_legato = true
      else
        @in_legato = false
        @seen_legato = false
        flush_legato!(&b)
        yield el
      end
    else
      if tieable?(el)
        buffer << el
      elsif NoteEvent === el
        flush!(&b)
        buffer!(el, &b)
      elsif Token === el && el.type == :legato_tie
        @seen_legato = true
        @in_legato = true
      else
        flush!(&b)
        yield el
      end
    end
  end

  if @in_legato
    flush_legato!(&b)
  else
    flush!(&b)
  end
end

#flush_legato!(&b) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ramekin/legato.rb', line 74

def flush_legato!(&b)
  non_notes = []
  non_notes << buffer.pop while buffer.any? && !buffer.last.is_a?(NoteEvent)

  return if buffer.empty?
  return flush!(&b) if buffer.size == 1

  yield LegatoStart.new
  last = buffer.pop
  error! "legato marks must follow a note", el: last unless NoteEvent === last
  buffer.each(&b)

  # ending legato on a bend is complicated - let Bend handle it
  case last
  when Bend then last.end_legato!; yield last
  else yield LegatoLastNote.new(last)
  end
  buffer.clear
ensure
  non_notes.reverse_each(&b)
end

#tieable?(t) ⇒ Boolean



96
97
98
99
100
# File 'lib/ramekin/legato.rb', line 96

def tieable?(t)
  return true if NoteEvent === t && t.tie?
  return true if Token === t && TIEABLE_EVENTS.include?(t.type)
  false
end