Class: HeadMusic::Rudiment::ScaleDegree

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

Instance Method Summary collapse

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

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

#alterationObject



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_semitonesObject



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

def alteration_semitones
  alteration&.semitones || 0
end

#degreeObject



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_degreeObject



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_spellingObject (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_sObject



36
37
38
# File 'lib/head_music/rudiment/scale_degree.rb', line 36

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