Sha256: 3f160f01c917d1f73e1221e9d43077d41ee333a5b6955e50fbf332a4b7e92b60

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

class HeadMusic::ScaleDegree
  include Comparable

  NAME_FOR_DIATONIC_DEGREE = [nil, 'tonic', 'supertonic', 'mediant', 'subdominant', 'dominant', 'submediant']

  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_cycle.index(spelling.letter_name.to_s) + 1
  end

  def sign
    sign_semitones = spelling.sign && spelling.sign.semitones || 0
    usual_sign_semitones = scale_degree_usual_spelling.sign && 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
    if scale_type.diatonic?
      NAME_FOR_DIATONIC_DEGREE[degree] ||
      (scale_type.intervals.last == 1 || sign == '#' ? 'leading tone' : 'subtonic')
    end
  end

  private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
head_music-0.17.0 lib/head_music/scale_degree.rb