Class: Stretto::MusicElements::Melody
- Inherits:
-
MusicElement
- Object
- MusicElement
- Stretto::MusicElements::Melody
- Defined in:
- lib/stretto/music_elements/melody.rb
Overview
A set of elements that should play sequentially. The elements are separated by underscores, for example C_D_E
A melody alone would be the equivalent to play the elements of it separately, but it is useful to indicate explicitely a melody when used in harmonies (see Harmony). For example, a harmony (C_D+E_Fmaj) will play at the same time C
+ E
for one quarter of a whole note, then D
and Fmaj
will play together.
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
Attributes inherited from MusicElement
Attributes included from Node
Instance Method Summary collapse
-
#<<(element) ⇒ Object
Adds an element to the melody, additionally setting its pattern (see {MusicElement::pattern=).
-
#duration ⇒ Object
Returns the sum of the duration of its elements.
-
#initialize(array_or_hash, pattern = nil) ⇒ Melody
constructor
A new instance of Melody.
Methods inherited from MusicElement
#build_music_string, #end_of_tie?, #start_of_tie?, #substitute_variables!, #to_s
Constructor Details
#initialize(array_or_hash, pattern = nil) ⇒ Melody
Returns a new instance of Melody.
17 18 19 20 21 22 |
# File 'lib/stretto/music_elements/melody.rb', line 17 def initialize(array_or_hash, pattern = nil) = _handle_initial_argument(array_or_hash) _verify_is_pattern(pattern) @elements = [:elements] super([:original_string], pattern) end |
Instance Attribute Details
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
15 16 17 |
# File 'lib/stretto/music_elements/melody.rb', line 15 def elements @elements end |
Instance Method Details
#<<(element) ⇒ Object
Adds an element to the melody, additionally setting its pattern (see {MusicElement::pattern=)
31 32 33 34 |
# File 'lib/stretto/music_elements/melody.rb', line 31 def <<(element) @elements << element element.pattern = @pattern if @pattern end |
#duration ⇒ Object
Returns the sum of the duration of its elements
25 26 27 |
# File 'lib/stretto/music_elements/melody.rb', line 25 def duration @elements.map(&:duration).sum end |