Sha256: fea5f32eee798baa098af8177944b1c7aedd0ea3613519afad6cbd67a198bc43

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

class HeadMusic::KeySignature
  attr_reader :tonic_spelling
  attr_reader :scale_type

  SHARPS = %w{F# C# G# D# A# E# B#}
  FLATS = %w{Bb Eb Ab Db Gb Cb Fb}

  delegate :pitch_class, to: :tonic_spelling, prefix: :tonic

  def initialize(tonic_spelling, scale_type = :major)
    @tonic_spelling = tonic_spelling
    @scale_type = scale_type
  end

  def sharps
    SHARPS.first(num_sharps)
  end

  def flats
    FLATS.first(num_flats)
  end

  def num_sharps
    (HeadMusic::Circle.of_fifths.index(tonic_pitch_class) - scale_type_adjustment) % 12
  end

  def num_flats
    (HeadMusic::Circle.of_fourths.index(tonic_pitch_class) + scale_type_adjustment) % 12
  end

  def sharps_or_flats
    return sharps if @tonic_spelling.to_s =~ /#/
    return flats if @tonic_spelling.to_s =~ /b/
    num_sharps <= num_flats ? sharps : flats
  end

  private

  def scale_type_adjustment
    scale_type == :minor ? 3 : 0
  end

  def major?
    @scale_type.to_sym == :major
  end

  def minor?
    @scale_type.to_sym == :minor
  end

  def relative_major_pitch_class
    return tonic_pitch_class if major?
    return (tonic_pitch_class.to_i + 3) % 12 if minor?
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
head_music-0.1.1 lib/head_music/key_signature.rb