Class: Clef::Core::KeySignature

Inherits:
Object
  • Object
show all
Defined in:
lib/clef/core/key_signature.rb

Constant Summary collapse

MAJOR_ACCIDENTALS =
{
  c: {count: 0, type: :natural},
  g: {count: 1, type: :sharp},
  d: {count: 2, type: :sharp},
  a: {count: 3, type: :sharp},
  e: {count: 4, type: :sharp},
  b: {count: 5, type: :sharp},
  fis: {count: 6, type: :sharp},
  cis: {count: 7, type: :sharp},
  f: {count: 1, type: :flat},
  bes: {count: 2, type: :flat},
  ees: {count: 3, type: :flat},
  aes: {count: 4, type: :flat},
  des: {count: 5, type: :flat},
  ges: {count: 6, type: :flat},
  ces: {count: 7, type: :flat}
}.freeze
MINOR_ACCIDENTALS =
{
  a: {count: 0, type: :natural},
  e: {count: 1, type: :sharp},
  b: {count: 2, type: :sharp},
  fis: {count: 3, type: :sharp},
  cis: {count: 4, type: :sharp},
  gis: {count: 5, type: :sharp},
  dis: {count: 6, type: :sharp},
  ais: {count: 7, type: :sharp},
  d: {count: 1, type: :flat},
  g: {count: 2, type: :flat},
  c: {count: 3, type: :flat},
  f: {count: 4, type: :flat},
  bes: {count: 5, type: :flat},
  ees: {count: 6, type: :flat},
  aes: {count: 7, type: :flat}
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tonic, mode = :major) ⇒ KeySignature

Returns a new instance of KeySignature.

Parameters:

  • tonic (Pitch, Symbol, String)
  • mode (Symbol) (defaults to: :major)


45
46
47
48
49
# File 'lib/clef/core/key_signature.rb', line 45

def initialize(tonic, mode = :major)
  @tonic = normalize_tonic(tonic)
  validate_mode!(mode)
  @mode = mode
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



41
42
43
# File 'lib/clef/core/key_signature.rb', line 41

def mode
  @mode
end

#tonicObject (readonly)

Returns the value of attribute tonic.



41
42
43
# File 'lib/clef/core/key_signature.rb', line 41

def tonic
  @tonic
end

Instance Method Details

#accidentalsHash

Returns:

  • (Hash)


52
53
54
55
56
57
# File 'lib/clef/core/key_signature.rb', line 52

def accidentals
  table = (mode == :major) ? MAJOR_ACCIDENTALS : MINOR_ACCIDENTALS
  table.fetch(tonic_key)
rescue KeyError
  raise ArgumentError, "unsupported #{mode} key tonic: #{tonic.to_lilypond}"
end

#preferred_transpose_spellingSymbol

Returns:

  • (Symbol)


60
61
62
# File 'lib/clef/core/key_signature.rb', line 60

def preferred_transpose_spelling
  (accidentals[:type] == :flat) ? :flat : :sharp
end