Sha256: 1aeacc8512b7ec892602eab1283b74ad458da97bf98094eb22b0db10b21b68ae

Contents?: true

Size: 1.44 KB

Versions: 10

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

10 entries across 10 versions & 1 rubygems

Version Path
head_music-0.27.0 lib/head_music/scale_degree.rb
head_music-0.26.3 lib/head_music/scale_degree.rb
head_music-0.26.2 lib/head_music/scale_degree.rb
head_music-0.26.1 lib/head_music/scale_degree.rb
head_music-0.26.0 lib/head_music/scale_degree.rb
head_music-0.25.0 lib/head_music/scale_degree.rb
head_music-0.24.5 lib/head_music/scale_degree.rb
head_music-0.24.4 lib/head_music/scale_degree.rb
head_music-0.24.3 lib/head_music/scale_degree.rb
head_music-0.24.2 lib/head_music/scale_degree.rb