Class: Clef::Parser::DSL::VoiceBuilder

Inherits:
Object
  • Object
show all
Includes:
BlockEvaluation
Defined in:
lib/clef/parser/dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(voice) ⇒ VoiceBuilder

Returns a new instance of VoiceBuilder.



212
213
214
215
216
217
218
219
220
# File 'lib/clef/parser/dsl.rb', line 212

def initialize(voice)
  @voice = voice
  @last_duration = Clef::Core::Duration.quarter
  @pending_tie = false
  @pending_articulations = []
  @open_slur = false
  @open_beam = false
  @last_note = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Clef::Parser::DSL::BlockEvaluation

Instance Method Details

#chord(pitch_strs, duration_sym, **opts) ⇒ Object

Parameters:

  • pitch_strs (Array<String>)
  • duration_sym (Symbol)
  • opts (Hash)


249
250
251
252
253
# File 'lib/clef/parser/dsl.rb', line 249

def chord(pitch_strs, duration_sym, **opts)
  duration = Clef::Core::Duration.new(duration_sym, dots: opts.fetch(:dots, 0))
  pitches = pitch_strs.map { |pitch| parse_pitch(pitch) }
  @voice.add(Clef::Core::Chord.new(pitches, duration))
end

#dynamic(type) ⇒ Object

Parameters:

  • type (Symbol)


256
257
258
# File 'lib/clef/parser/dsl.rb', line 256

def dynamic(type)
  @voice.add(Clef::Notation::Dynamic.new(type))
end

#note(pitch_str, duration_sym, **opts) ⇒ Object

Parameters:

  • pitch_str (String)
  • duration_sym (Symbol)
  • opts (Hash)


232
233
234
235
236
237
# File 'lib/clef/parser/dsl.rb', line 232

def note(pitch_str, duration_sym, **opts)
  pitch = parse_pitch(pitch_str)
  duration = Clef::Core::Duration.new(duration_sym, dots: opts.fetch(:dots, 0))
  articulations = Array(opts.fetch(:articulations, []))
  @voice.add(Clef::Core::Note.new(pitch, duration, articulations: articulations, tied: opts[:tied]))
end

#notes(lilypond_string) ⇒ Object

Parameters:

  • lilypond_string (String)


268
269
270
# File 'lib/clef/parser/dsl.rb', line 268

def notes(lilypond_string)
  parse_tokens(lilypond_string).each { |token| add_token(token) }
end

#rest(duration_sym, **opts) ⇒ Object

Parameters:

  • duration_sym (Symbol)
  • opts (Hash)


241
242
243
244
# File 'lib/clef/parser/dsl.rb', line 241

def rest(duration_sym, **opts)
  duration = Clef::Core::Duration.new(duration_sym, dots: opts.fetch(:dots, 0))
  @voice.add(Clef::Core::Rest.new(duration, kind: opts.fetch(:kind, :visible), measures: opts.fetch(:measures, 1)))
end

#tempo(beat_unit:, bpm:) ⇒ Object

Parameters:



262
263
264
265
# File 'lib/clef/parser/dsl.rb', line 262

def tempo(beat_unit:, bpm:)
  duration = beat_unit.is_a?(Clef::Core::Duration) ? beat_unit : Clef::Core::Duration.new(beat_unit)
  @voice.add(Clef::Core::Tempo.new(beat_unit: duration, bpm: bpm))
end

#tuplet(actual, normal, &block) ⇒ Object

Parameters:

  • actual (Integer)
  • normal (Integer)

Raises:

  • (ArgumentError)


274
275
276
277
278
279
280
281
# File 'lib/clef/parser/dsl.rb', line 274

def tuplet(actual, normal, &block)
  raise ArgumentError, "tuplet values must be positive" unless actual.positive? && normal.positive?
  return unless block_given?

  voice = Clef::Core::Voice.new(id: @voice.id)
  evaluate_block(self.class.new(voice), &block)
  @voice.add(Clef::Core::Tuplet.new(actual, normal, voice.elements))
end

#use_voice(voice) ⇒ VoiceBuilder

Parameters:

Returns:



224
225
226
227
# File 'lib/clef/parser/dsl.rb', line 224

def use_voice(voice)
  @voice = voice
  self
end