Class: Stretto::MusicElements::Melody

Inherits:
MusicElement show all
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)

Attributes inherited from MusicElement

#original_string, #pattern

Attributes included from Node

#next, #prev

Instance Method Summary (collapse)

Methods inherited from MusicElement

#build_music_string, #end_of_tie?, #start_of_tie?, #substitute_variables!, #to_s

Constructor Details

- (Melody) initialize(array_or_hash, pattern = nil)



17
18
19
20
21
22
# File 'lib/stretto/music_elements/melody.rb', line 17

def initialize(array_or_hash, pattern = nil)
  options = _handle_initial_argument(array_or_hash)
  _verify_is_pattern(pattern)
  @elements = options[:elements]
  super(options[:original_string], pattern)
end

Instance Attribute Details

- (Object) elements (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

- (Object) <<(element)

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

- (Object) duration

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