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
- #degree ⇒ Object
-
#initialize(key_signature, spelling) ⇒ ScaleDegree
constructor
A new instance of ScaleDegree.
- #name_for_degree ⇒ Object
- #sign ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(key_signature, spelling) ⇒ ScaleDegree
Returns a new instance of ScaleDegree.
15 16 17 18 |
# File 'lib/head_music/scale_degree.rb', line 15 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.
10 11 12 |
# File 'lib/head_music/scale_degree.rb', line 10 def key_signature @key_signature end |
#spelling ⇒ Object (readonly)
Returns the value of attribute spelling.
10 11 12 |
# File 'lib/head_music/scale_degree.rb', line 10 def spelling @spelling end |
Instance Method Details
#<=>(other) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/head_music/scale_degree.rb', line 35 def <=>(other) if other.is_a?(HeadMusic::ScaleDegree) [degree, sign.semitones] <=> [other.degree, other.sign.semitones] else to_s <=> other.to_s end end |
#degree ⇒ Object
20 21 22 |
# File 'lib/head_music/scale_degree.rb', line 20 def degree scale.letter_name_series_ascending.index(spelling.letter_name.to_s) + 1 end |
#name_for_degree ⇒ Object
43 44 45 46 47 48 |
# File 'lib/head_music/scale_degree.rb', line 43 def name_for_degree return unless scale_type.diatonic? NAME_FOR_DIATONIC_DEGREE[degree] || ((scale_type.intervals.last == 1 || sign == "#") ? "leading tone" : "subtonic") end |
#sign ⇒ Object
24 25 26 27 28 29 |
# File 'lib/head_music/scale_degree.rb', line 24 def sign sign_semitones = spelling.sign&.semitones || 0 usual_sign_semitones = scale_degree_usual_spelling.sign&.semitones || 0 delta = sign_semitones - usual_sign_semitones HeadMusic::Sign.by(:semitones, delta) if delta != 0 end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/head_music/scale_degree.rb', line 31 def to_s "#{sign}#{degree}" end |