Class: Ramekin::NoteAggregator

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

Instance Attribute Summary

Attributes inherited from Processor

#stream

Instance Method Summary collapse

Methods inherited from Processor

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

Constructor Details

#initializeNoteAggregator

Returns a new instance of NoteAggregator.



195
196
197
# File 'lib/ramekin/note_aggregator.rb', line 195

def initialize
  @current_note = nil
end

Instance Method Details

#call(&b) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/ramekin/note_aggregator.rb', line 209

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

  current_l = nil
  current_octave = 4
  @triplets = false

  each do |el|
    next yield el unless Token === el

    case el.type
    when :lbrace
      @triplets = true
      yield el
    when :rbrace
      @triplets = false
      yield el
    when :instrument
      inst = el.meta[:inst]

      current_octave = inst ? inst.octave : 4
      buffer << el
    when :l
      current_l = el
    when :note, :r, :native_tie, :fade
      flush!(&b)
      @current_note = NoteEvent.new(el, current_octave, @triplets)
    when :o
      current_octave = el.value.to_i
    when :octave
      current_octave += (el.value == '<' ? -1 : 1)
    when :tie
      # handle, for example, r^8
      if @current_note.extensions.empty?
        @current_note.extensions << @current_note.default_length
      end

      next
    when :length
      @current_note.extensions << el
    else
      buffer << el
    end
  end

  flush!(&b)
end

#flush!(&b) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'lib/ramekin/note_aggregator.rb', line 199

def flush!(&b)
  if @current_note
    yield @current_note
    @current_note = nil
  end

  buffer.each(&b)
  buffer.clear
end