Class: HeadMusic::Rudiment::ScaleDegree
- Includes:
- Comparable
- Defined in:
- lib/head_music/rudiment/scale_degree.rb
Overview
A scale degree is a number indicating the ordinality of the spelling in the key. 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
- #alteration_semitones ⇒ Object
- #degree ⇒ Object
-
#initialize(key_signature, spelling) ⇒ ScaleDegree
constructor
A new instance of ScaleDegree.
- #name_for_degree ⇒ Object
- #scale_degree_usual_spelling ⇒ Object private
- #to_s ⇒ Object
Constructor Details
#initialize(key_signature, spelling) ⇒ ScaleDegree
Returns a new instance of ScaleDegree.
16 17 18 19 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 16 def initialize(key_signature, spelling) @key_signature = key_signature @spelling = HeadMusic::Rudiment::Spelling.get(spelling) end |
Instance Attribute Details
#key_signature ⇒ Object (readonly)
Returns the value of attribute key_signature.
11 12 13 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 11 def key_signature @key_signature end |
#spelling ⇒ Object (readonly)
Returns the value of attribute spelling.
11 12 13 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 11 def spelling @spelling end |
Instance Method Details
#<=>(other) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 40 def <=>(other) case other when HeadMusic::Rudiment::ScaleDegree [degree, alteration_semitones] <=> [other.degree, other.alteration_semitones] when Numeric degree <=> other when String to_s <=> other.to_s else # If we can't meaningfully compare, return nil (Ruby standard) nil end end |
#alteration ⇒ Object
25 26 27 28 29 30 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 25 def alteration spelling_alteration_semitones = spelling.alteration&.semitones || 0 usual_sign_semitones = scale_degree_usual_spelling.alteration&.semitones || 0 delta = spelling_alteration_semitones - usual_sign_semitones HeadMusic::Rudiment::Alteration.by(:semitones, delta) if delta != 0 end |
#alteration_semitones ⇒ Object
32 33 34 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 32 def alteration_semitones alteration&.semitones || 0 end |
#degree ⇒ Object
21 22 23 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 21 def degree scale.letter_name_series_ascending.index(spelling.letter_name.to_s) + 1 end |
#name_for_degree ⇒ Object
54 55 56 57 58 59 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 54 def name_for_degree return unless scale_type.diatonic? NAME_FOR_DIATONIC_DEGREE[degree] || ((scale_type.intervals.last == 1 || alteration == "#") ? "leading tone" : "subtonic") end |
#scale_degree_usual_spelling ⇒ Object (private)
63 64 65 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 63 def scale_degree_usual_spelling HeadMusic::Rudiment::Spelling.get(scale.spellings[degree - 1]) end |
#to_s ⇒ Object
36 37 38 |
# File 'lib/head_music/rudiment/scale_degree.rb', line 36 def to_s "#{alteration}#{degree}" end |