Sha256: eda1c7d0e8e95387eb231bdd258eb852ab4fc627ae73870d93b1692ad4b58e17

Contents?: true

Size: 1.14 KB

Versions: 13

Compression:

Stored size: 1.14 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 = nil)
    @tonic_spelling = tonic_spelling
    @scale_type = scale_type || :major
  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

13 entries across 13 versions & 1 rubygems

Version Path
head_music-0.6.4 lib/head_music/key_signature.rb
head_music-0.6.3 lib/head_music/key_signature.rb
head_music-0.6.1 lib/head_music/key_signature.rb
head_music-0.6.0 lib/head_music/key_signature.rb
head_music-0.5.4 lib/head_music/key_signature.rb
head_music-0.5.3 lib/head_music/key_signature.rb
head_music-0.5.2 lib/head_music/key_signature.rb
head_music-0.5.1 lib/head_music/key_signature.rb
head_music-0.5.0 lib/head_music/key_signature.rb
head_music-0.4.0 lib/head_music/key_signature.rb
head_music-0.3.1 lib/head_music/key_signature.rb
head_music-0.3.0 lib/head_music/key_signature.rb
head_music-0.1.5 lib/head_music/key_signature.rb