Class: HeadMusic::Rudiment::Key

Inherits:
QualifiedDiatonicContext show all
Defined in:
lib/head_music/rudiment/key.rb

Overview

Represents a musical key (major or minor)

Constant Summary collapse

QUALITIES =
%i[major minor].freeze
MAJOR_KEY_NAMES_BY_FIFTHS =

The conventional reading of a signature that carries no interpretation of its own: the major key with that many sharps or flats.

A signature underdetermines its key -- two sharps is D major, B minor, E dorian, or A mixolydian -- so this is a fallback for the consumers that cannot proceed without a tonic (LilyPond's \key, and the Diatonic guideline), not a claim about the music.

Signatures themselves are unbounded: a theoretical key such as G♯ major counts each double accidental twice and reaches eight. The table stops at seven, because past that there is no conventional major key to name.

{
  -7 => "C♭", -6 => "G♭", -5 => "D♭", -4 => "A♭", -3 => "E♭", -2 => "B♭", -1 => "F",
  0 => "C",
  1 => "G", 2 => "D", 3 => "A", 4 => "E", 5 => "B", 6 => "F♯", 7 => "C♯"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from HeadMusic::Rudiment::QualifiedDiatonicContext

Instance Attribute Details

#alias_name_keysObject (readonly) Originally defined in module Named

Returns the value of attribute alias_name_keys.

#name_keyObject (readonly) Originally defined in module Named

Returns the value of attribute name_key.

Class Method Details

.default_qualifierObject



36
37
38
# File 'lib/head_music/rudiment/key.rb', line 36

def self.default_qualifier
  :major
end

.for_fifths(fifths) ⇒ HeadMusic::Rudiment::Key

Returns the major key with that signature.

Parameters:

  • fifths (Integer)

    sharps as positive, flats as negative

Returns:

Raises:

  • (ArgumentError)

    for a signature outside -7..+7, which names no conventional major key



29
30
31
32
33
34
# File 'lib/head_music/rudiment/key.rb', line 29

def self.for_fifths(fifths)
  tonic = MAJOR_KEY_NAMES_BY_FIFTHS[fifths]
  raise ArgumentError, "no conventional key for a signature of #{fifths} fifths" if tonic.nil?

  get("#{tonic} major")
end

.invalid_qualifier_messageObject



44
45
46
# File 'lib/head_music/rudiment/key.rb', line 44

def self.invalid_qualifier_message
  "Quality must be :major or :minor"
end

.valid_qualifiersObject



40
41
42
# File 'lib/head_music/rudiment/key.rb', line 40

def self.valid_qualifiers
  QUALITIES
end

Instance Method Details

#major?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/head_music/rudiment/key.rb', line 50

def major?
  quality == :major
end

#minor?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/head_music/rudiment/key.rb', line 54

def minor?
  quality == :minor
end

#parallelObject



70
71
72
73
74
75
76
# File 'lib/head_music/rudiment/key.rb', line 70

def parallel
  if major?
    self.class.get("#{tonic_spelling} minor")
  else
    self.class.get("#{tonic_spelling} major")
  end
end

#relativeObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/head_music/rudiment/key.rb', line 58

def relative
  if major?
    # Major to relative minor: down a minor third (3 semitones)
    relative_pitch = tonic_pitch + -3
    self.class.get("#{relative_pitch.spelling} minor")
  else
    # Minor to relative major: up a minor third (3 semitones)
    relative_pitch = tonic_pitch + 3
    self.class.get("#{relative_pitch.spelling} major")
  end
end