Class: HeadMusic::ScaleDegree
- Inherits:
-
Object
- Object
- HeadMusic::ScaleDegree
- Includes:
- Comparable
- Defined in:
- lib/head_music/scale_degree.rb
Overview
A scale degree is a number indicating the ordinality of the spelling in the key signature. TODO: Rewrite to accept a tonal_center and a scale type.
Constant Summary collapse
- NAME_FOR_DIATONIC_DEGREE =
[nil, "tonic", "supertonic", "mediant", "subdominant", "dominant", "submediant"].freeze
Instance Attribute Summary collapse
-
#key_signature ⇒ Object
readonly
Returns the value of attribute key_signature.
-
#spelling ⇒ Object
readonly
Returns the value of attribute spelling.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #alteration ⇒ Object
- #degree ⇒ Object
-
#initialize(key_signature, spelling) ⇒ ScaleDegree
constructor
A new instance of ScaleDegree.
- #name_for_degree ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(key_signature, spelling) ⇒ ScaleDegree
Returns a new instance of ScaleDegree.
13 14 15 16 |
# File 'lib/head_music/scale_degree.rb', line 13 def initialize(key_signature, spelling) @key_signature = key_signature @spelling = HeadMusic::Spelling.get(spelling) end |
Instance Attribute Details
#key_signature ⇒ Object (readonly)
Returns the value of attribute key_signature.
8 9 10 |
# File 'lib/head_music/scale_degree.rb', line 8 def key_signature @key_signature end |
#spelling ⇒ Object (readonly)
Returns the value of attribute spelling.
8 9 10 |
# File 'lib/head_music/scale_degree.rb', line 8 def spelling @spelling end |
Instance Method Details
#<=>(other) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/head_music/scale_degree.rb', line 33 def <=>(other) if other.is_a?(HeadMusic::ScaleDegree) [degree, alteration.semitones] <=> [other.degree, other.alteration.semitones] else to_s <=> other.to_s end end |
#alteration ⇒ Object
22 23 24 25 26 27 |
# File 'lib/head_music/scale_degree.rb', line 22 def alteration alteration_semitones = spelling.alteration&.semitones || 0 usual_sign_semitones = scale_degree_usual_spelling.alteration&.semitones || 0 delta = alteration_semitones - usual_sign_semitones HeadMusic::Alteration.by(:semitones, delta) if delta != 0 end |
#degree ⇒ Object
18 19 20 |
# File 'lib/head_music/scale_degree.rb', line 18 def degree scale.letter_name_series_ascending.index(spelling.letter_name.to_s) + 1 end |
#name_for_degree ⇒ Object
41 42 43 44 45 46 |
# File 'lib/head_music/scale_degree.rb', line 41 def name_for_degree return unless scale_type.diatonic? NAME_FOR_DIATONIC_DEGREE[degree] || ((scale_type.intervals.last == 1 || alteration == "#") ? "leading tone" : "subtonic") end |
#to_s ⇒ Object
29 30 31 |
# File 'lib/head_music/scale_degree.rb', line 29 def to_s "#{alteration}#{degree}" end |