Class: HeadMusic::ScaleDegree

Inherits:
Object
  • Object
show all
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. 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

Instance Method Summary collapse

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_signatureObject (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

#spellingObject (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



37
38
39
40
41
42
43
44
# File 'lib/head_music/scale_degree.rb', line 37

def <=>(other)
  if other.is_a?(HeadMusic::ScaleDegree)
    [degree, alteration_semitones] <=> [other.degree, other.alteration_semitones]
  else
    # TODO: Improve this
    to_s <=> other.to_s
  end
end

#alterationObject



22
23
24
25
26
27
# File 'lib/head_music/scale_degree.rb', line 22

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::Alteration.by(:semitones, delta) if delta != 0
end

#alteration_semitonesObject



29
30
31
# File 'lib/head_music/scale_degree.rb', line 29

def alteration_semitones
  alteration&.semitones || 0
end

#degreeObject



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_degreeObject



46
47
48
49
50
51
# File 'lib/head_music/scale_degree.rb', line 46

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_sObject



33
34
35
# File 'lib/head_music/scale_degree.rb', line 33

def to_s
  "#{alteration}#{degree}"
end