class HeadMusic::KeySignature

Represents a key signature.

Constants

ORDERED_LETTER_NAMES_OF_FLATS
ORDERED_LETTER_NAMES_OF_SHARPS

Attributes

scale[R]
scale_type[R]
tonic_spelling[R]

Public Class Methods

default() click to toggle source
# File lib/head_music/key_signature.rb, line 12
def self.default
  @default ||= new('C', :major)
end
get(identifier) click to toggle source
# File lib/head_music/key_signature.rb, line 16
def self.get(identifier)
  return identifier if identifier.is_a?(HeadMusic::KeySignature)

  @key_signatures ||= {}
  tonic_spelling, scale_type_name = identifier.strip.split(/\s/)
  hash_key = HeadMusic::Utilities::HashKey.for(identifier.gsub(/#|♯/, ' sharp').gsub(/(\w)[b♭]/, '\\1 flat'))
  @key_signatures[hash_key] ||= new(tonic_spelling, scale_type_name)
end
new(tonic_spelling, scale_type = nil) click to toggle source
# File lib/head_music/key_signature.rb, line 29
def initialize(tonic_spelling, scale_type = nil)
  @tonic_spelling = HeadMusic::Spelling.get(tonic_spelling)
  @scale_type = HeadMusic::ScaleType.get(scale_type) if scale_type
  @scale_type ||= HeadMusic::ScaleType.default
  @scale_type = @scale_type.parent || @scale_type
  @scale = HeadMusic::Scale.get(@tonic_spelling, @scale_type)
end

Public Instance Methods

==(other) click to toggle source
# File lib/head_music/key_signature.rb, line 84
def ==(other)
  signs == self.class.get(other).signs
end
accidentals()
Alias for: signs
double_flats() click to toggle source
# File lib/head_music/key_signature.rb, line 59
def double_flats
  spellings.select(&:double_flat?).sort_by do |spelling|
    ORDERED_LETTER_NAMES_OF_FLATS.index(spelling.letter_name.to_s)
  end
end
double_sharps() click to toggle source
# File lib/head_music/key_signature.rb, line 47
def double_sharps
  spellings.select(&:double_sharp?).sort_by do |spelling|
    ORDERED_LETTER_NAMES_OF_SHARPS.index(spelling.letter_name.to_s)
  end
end
enharmonic_equivalent?(other) click to toggle source
# File lib/head_music/key_signature.rb, line 98
def enharmonic_equivalent?(other)
  other = KeySignature.get(other)
  enharmonic_equivalence.equivalent?(other)
end
flats() click to toggle source
# File lib/head_music/key_signature.rb, line 53
def flats
  spellings.select(&:flat?).sort_by do |spelling|
    ORDERED_LETTER_NAMES_OF_FLATS.index(spelling.letter_name.to_s)
  end
end
name() click to toggle source
# File lib/head_music/key_signature.rb, line 80
def name
  [tonic_spelling, scale_type].join(' ')
end
num_flats() click to toggle source
# File lib/head_music/key_signature.rb, line 69
def num_flats
  flats.length + double_flats.length * 2
end
num_sharps() click to toggle source
# File lib/head_music/key_signature.rb, line 65
def num_sharps
  sharps.length + double_sharps.length * 2
end
sharps() click to toggle source
# File lib/head_music/key_signature.rb, line 41
def sharps
  spellings.select(&:sharp?).sort_by do |spelling|
    ORDERED_LETTER_NAMES_OF_SHARPS.index(spelling.letter_name.to_s)
  end
end
sharps_and_flats()
Alias for: signs
signs() click to toggle source
# File lib/head_music/key_signature.rb, line 73
def signs
  flats.any? ? flats : sharps
end
Also aliased as: sharps_and_flats, accidentals
spellings() click to toggle source
# File lib/head_music/key_signature.rb, line 37
def spellings
  pitches.map(&:spelling).uniq
end
to_s() click to toggle source
# File lib/head_music/key_signature.rb, line 88
def to_s
  if sharps.any?
    sharps.length == 1 ? '1 sharp' : "#{sharps.length} sharps"
  elsif flats.any?
    flats.length == 1 ? '1 flat' : "#{flats.length} flats"
  else
    'no sharps or flats'
  end
end

Private Instance Methods

enharmonic_equivalence() click to toggle source
# File lib/head_music/key_signature.rb, line 105
def enharmonic_equivalence
  @enharmonic_equivalence ||= EnharmonicEquivalence.get(self)
end