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

Instance Method Summary collapse

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

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

#degreeObject



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_degreeObject



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

#signObject



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_sObject



31
32
33
# File 'lib/head_music/scale_degree.rb', line 31

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