Class: Clef::Core::Voice

Inherits:
Object
  • Object
show all
Defined in:
lib/clef/core/voice.rb

Constant Summary collapse

ELEMENT_TYPES =
[Note, Rest, Chord, Tuplet, Tempo].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: :default) ⇒ Voice

Returns a new instance of Voice.

Parameters:

  • id (Symbol) (defaults to: :default)


11
12
13
14
# File 'lib/clef/core/voice.rb', line 11

def initialize(id: :default)
  @id = id
  @elements = []
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/clef/core/voice.rb', line 8

def id
  @id
end

Instance Method Details

#add(element) ⇒ Voice

Parameters:

Returns:

Raises:

  • (ArgumentError)


18
19
20
21
22
23
# File 'lib/clef/core/voice.rb', line 18

def add(element)
  raise ArgumentError, "element must be a musical element" unless musical_element?(element)

  @elements << element
  self
end

#elementsArray<Note, Rest, Chord, Tuplet, Tempo>

Returns:



26
27
28
# File 'lib/clef/core/voice.rb', line 26

def elements
  @elements.dup.freeze
end

#total_lengthRational

Returns:

  • (Rational)


31
32
33
# File 'lib/clef/core/voice.rb', line 31

def total_length
  @elements.reduce(Rational(0, 1)) { |memo, element| memo + element.length }
end