Sha256: 9f52c5a606f326da92aa5b4b253cc7a54fe93c97ea05de0b195c8c8d91e4daf3

Contents?: true

Size: 1.44 KB

Versions: 9

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

# 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.
class HeadMusic::ScaleDegree
  include Comparable

  NAME_FOR_DIATONIC_DEGREE = [nil, "tonic", "supertonic", "mediant", "subdominant", "dominant", "submediant"].freeze

  attr_reader :key_signature, :spelling

  delegate :scale, to: :key_signature
  delegate :scale_type, to: :scale

  def initialize(key_signature, spelling)
    @key_signature = key_signature
    @spelling = HeadMusic::Spelling.get(spelling)
  end

  def degree
    scale.letter_name_series_ascending.index(spelling.letter_name.to_s) + 1
  end

  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

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

  def <=>(other)
    if other.is_a?(HeadMusic::ScaleDegree)
      [degree, sign.semitones] <=> [other.degree, other.sign.semitones]
    else
      to_s <=> other.to_s
    end
  end

  def name_for_degree
    return unless scale_type.diatonic?

    NAME_FOR_DIATONIC_DEGREE[degree] ||
      ((scale_type.intervals.last == 1 || sign == "#") ? "leading tone" : "subtonic")
  end

  private

  def scale_degree_usual_spelling
    HeadMusic::Spelling.get(scale.spellings[degree - 1])
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
head_music-4.0.1 lib/head_music/scale_degree.rb
head_music-4.0.0 lib/head_music/scale_degree.rb
head_music-3.0.1 lib/head_music/scale_degree.rb
head_music-3.0.0 lib/head_music/scale_degree.rb
head_music-2.0.1 lib/head_music/scale_degree.rb
head_music-2.0.0 lib/head_music/scale_degree.rb
head_music-1.0.0 lib/head_music/scale_degree.rb
head_music-0.29.0 lib/head_music/scale_degree.rb
head_music-0.28.0 lib/head_music/scale_degree.rb