Class: HeadMusic::Rudiment::Key

Inherits:
DiatonicContext show all
Includes:
Named
Defined in:
lib/head_music/rudiment/key.rb

Overview

Represents a musical key (major or minor)

Constant Summary collapse

QUALITIES =
%i[major minor].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tonic_spelling, quality = :major) ⇒ Key

Returns a new instance of Key.

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/head_music/rudiment/key.rb', line 27

def initialize(tonic_spelling, quality = :major)
  super(tonic_spelling)
  @quality = quality.to_s.downcase.to_sym
  raise ArgumentError, "Quality must be :major or :minor" unless QUALITIES.include?(@quality)
end

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.

#qualityObject (readonly)

Returns the value of attribute quality.



10
11
12
# File 'lib/head_music/rudiment/key.rb', line 10

def quality
  @quality
end

Class Method Details

.get(identifier) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/head_music/rudiment/key.rb', line 12

def self.get(identifier)
  return identifier if identifier.is_a?(HeadMusic::Rudiment::Key)

  @keys ||= {}
  tonic_spelling, quality_name = parse_identifier(identifier)
  hash_key = HeadMusic::Utilities::HashKey.for(identifier)
  @keys[hash_key] ||= new(tonic_spelling, quality_name)
end

.parse_identifier(identifier) ⇒ Object



21
22
23
24
25
# File 'lib/head_music/rudiment/key.rb', line 21

def self.parse_identifier(identifier)
  tonic_spelling, quality_name = identifier.to_s.strip.split(/\s+/)
  quality_name ||= "major"
  [tonic_spelling, quality_name]
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  other = self.class.get(other)
  tonic_spelling == other.tonic_spelling && quality == other.quality
end

#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named

#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named

#localized_name_in_default_localeObject (private) Originally defined in module Named

#localized_name_in_locale_matching_language(locale) ⇒ Object (private) Originally defined in module Named

#localized_name_in_matching_locale(locale) ⇒ Object (private) Originally defined in module Named

#localized_namesObject Originally defined in module Named

Returns an array of LocalizedName instances that are synonymous with the name.

#major?Boolean

Returns:

  • (Boolean)


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

def major?
  quality == :major
end

#minor?Boolean

Returns:

  • (Boolean)


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

def minor?
  quality == :minor
end

#nameObject



65
66
67
# File 'lib/head_music/rudiment/key.rb', line 65

def name
  "#{tonic_spelling} #{quality}"
end

#name=(name) ⇒ Object Originally defined in module Named

#parallelObject



57
58
59
60
61
62
63
# File 'lib/head_music/rudiment/key.rb', line 57

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

#relativeObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/head_music/rudiment/key.rb', line 45

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

#scale_typeObject



33
34
35
# File 'lib/head_music/rudiment/key.rb', line 33

def scale_type
  @scale_type ||= HeadMusic::Rudiment::ScaleType.get(quality)
end

#to_sObject



69
70
71
# File 'lib/head_music/rudiment/key.rb', line 69

def to_s
  name
end