Class: Stretto::MusicElements::HarmonicChord

Inherits:
Chord show all
Defined in:
lib/stretto/music_elements/harmonic_chord.rb

Overview

Represents a non-regular chord (see Chord)

A harmonic chord can be specied by joining notes, or even chords, together with the + symbol. For example, the chord C+E+G is the same as the chord Cmaj, and the chord C+D+E is an irregular chord (not included in the standard named chords) which will have the notes C, D and E. Note that as the notes are specified by their note notation, the default octave is 5, instead of 3 as the normal chords.

Constant Summary

Constants inherited from Chord

Chord::CHORD_INTERVALS, Chord::DEFAULT_OCTAVE

Constants included from AttackDecay

AttackDecay::DEFAULT_ATTACK, AttackDecay::DEFAULT_DECAY

Constants included from Duration

Duration::DEFAULT_DURATION, Duration::DEFAULT_TUPLET_DENOMINATOR, Duration::DEFAULT_TUPLET_NUMERATOR, Duration::DURATIONS

Instance Attribute Summary

Attributes inherited from Chord

#instrument, #key_signature, #named_chord, #original_named_chord

Attributes included from AttackDecay

#original_attack, #original_decay

Attributes included from Duration

#end_tie, #original_duration, #start_tie

Attributes inherited from MusicElement

#original_string, #pattern

Attributes included from Node

#next, #prev

Instance Method Summary collapse

Methods inherited from Chord

#==, #base_note, #inversions, #notes, #pivot_note

Methods included from AttackDecay

#attack, #attack=, #build_attack_and_decay, #decay, #decay=

Methods included from Duration

#build_duration_from_token, #end_of_tie?, parse_duration, #start_of_tie?, #tied_duration, #tied_elements

Methods inherited from MusicElement

#build_music_string, #end_of_tie?, #start_of_tie?, #to_s

Constructor Details

#initialize(string_or_options, pattern = nil) ⇒ HarmonicChord

Returns a new instance of HarmonicChord.



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

def initialize(string_or_options, pattern = nil)
  token = case string_or_options
    when String then Stretto::Parser.parse_harmonic_chord!(string_or_options)
    else string_or_options
  end
  super(token, pattern)
  @notes = normalize_notes(@notes)
end

Instance Method Details

#durationObject



32
33
34
# File 'lib/stretto/music_elements/harmonic_chord.rb', line 32

def duration
  @notes.map(&:duration).max
end

#elementsObject



27
28
29
# File 'lib/stretto/music_elements/harmonic_chord.rb', line 27

def elements
  notes
end

#normalize_notes(base_notes) ⇒ Object

Builds the @notes instance variable, flattening the notes and the notes in a chord into a single array of elements



44
45
46
47
48
49
50
51
52
# File 'lib/stretto/music_elements/harmonic_chord.rb', line 44

def normalize_notes(base_notes)
  base_notes.inject([]) do |notes, element|
    element.pattern = @pattern
    case element
      when Note   then notes << element
      when Chord  then notes + element.notes
    end
  end.uniq
end

#substitute_variables!Object



37
38
39
40
# File 'lib/stretto/music_elements/harmonic_chord.rb', line 37

def substitute_variables!
  @duration        = @notes.map(&:duration).max
  @notes.each{ |note| note.pattern = @pattern }
end