Class: HeadMusic::Rudiment::KeySignature
- Inherits:
-
Base
- Object
- Base
- HeadMusic::Rudiment::KeySignature
show all
- Defined in:
- lib/head_music/rudiment/key_signature.rb
Overview
Represents a key signature (traditionally associated with a key)
This class maintains backward compatibility while delegating to Key/Mode internally
Defined Under Namespace
Classes: EnharmonicEquivalence
Constant Summary
collapse
- ORDERED_LETTER_NAMES_OF_SHARPS =
%w[F C G D A E B].freeze
- ORDERED_LETTER_NAMES_OF_FLATS =
ORDERED_LETTER_NAMES_OF_SHARPS.reverse.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(tonic_spelling, scale_type = nil) ⇒ KeySignature
Returns a new instance of KeySignature.
41
42
43
44
45
46
47
|
# File 'lib/head_music/rudiment/key_signature.rb', line 41
def initialize(tonic_spelling, scale_type = nil)
@tonic_spelling = HeadMusic::Rudiment::Spelling.get(tonic_spelling)
@scale_type = HeadMusic::Rudiment::ScaleType.get(scale_type) if scale_type
@scale_type ||= HeadMusic::Rudiment::ScaleType.default
@scale_type = @scale_type.parent || @scale_type
@scale = HeadMusic::Rudiment::Scale.get(@tonic_spelling, @scale_type)
end
|
Instance Attribute Details
#scale ⇒ Object
Returns the value of attribute scale.
35
36
37
|
# File 'lib/head_music/rudiment/key_signature.rb', line 35
def scale
@scale
end
|
#scale_type ⇒ Object
Returns the value of attribute scale_type.
35
36
37
|
# File 'lib/head_music/rudiment/key_signature.rb', line 35
def scale_type
@scale_type
end
|
#tonic_spelling ⇒ Object
Returns the value of attribute tonic_spelling.
35
36
37
|
# File 'lib/head_music/rudiment/key_signature.rb', line 35
def tonic_spelling
@tonic_spelling
end
|
Class Method Details
.default ⇒ Object
10
11
12
|
# File 'lib/head_music/rudiment/key_signature.rb', line 10
def self.default
@default ||= new("C", :major)
end
|
.from_scale(scale) ⇒ Object
28
29
30
31
32
33
|
# File 'lib/head_music/rudiment/key_signature.rb', line 28
def self.from_scale(scale)
tonic = scale.root_pitch.spelling
scale_type = scale.scale_type
new(tonic, scale_type)
end
|
.get(identifier) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/head_music/rudiment/key_signature.rb', line 14
def self.get(identifier)
return identifier if identifier.is_a?(HeadMusic::Rudiment::KeySignature)
@key_signatures ||= {}
if identifier.is_a?(String)
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)
elsif identifier.is_a?(HeadMusic::Rudiment::DiatonicContext)
identifier.key_signature
end
end
|
Instance Method Details
#==(other) ⇒ Object
100
101
102
|
# File 'lib/head_music/rudiment/key_signature.rb', line 100
def ==(other)
alterations == self.class.get(other).alterations
end
|
#alterations ⇒ Object
Also known as:
sharps_and_flats, accidentals
89
90
91
|
# File 'lib/head_music/rudiment/key_signature.rb', line 89
def alterations
flats.any? ? (double_flats + flats) : (double_sharps + sharps)
end
|
#double_flats ⇒ Object
71
72
73
74
75
|
# File 'lib/head_music/rudiment/key_signature.rb', line 71
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 ⇒ Object
59
60
61
62
63
|
# File 'lib/head_music/rudiment/key_signature.rb', line 59
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_equivalence ⇒ Object
120
121
122
|
# File 'lib/head_music/rudiment/key_signature.rb', line 120
def enharmonic_equivalence
@enharmonic_equivalence ||= HeadMusic::Rudiment::KeySignature::EnharmonicEquivalence.get(self)
end
|
#enharmonic_equivalent?(other) ⇒ Boolean
114
115
116
|
# File 'lib/head_music/rudiment/key_signature.rb', line 114
def enharmonic_equivalent?(other)
enharmonic_equivalence.enharmonic_equivalent?(other)
end
|
#flats ⇒ Object
65
66
67
68
69
|
# File 'lib/head_music/rudiment/key_signature.rb', line 65
def flats
spellings.select(&:flat?).sort_by do |spelling|
ORDERED_LETTER_NAMES_OF_FLATS.index(spelling.letter_name.to_s)
end
end
|
#name ⇒ Object
96
97
98
|
# File 'lib/head_music/rudiment/key_signature.rb', line 96
def name
[tonic_spelling, scale_type].join(" ")
end
|
#num_alterations ⇒ Object
85
86
87
|
# File 'lib/head_music/rudiment/key_signature.rb', line 85
def num_alterations
num_sharps + num_flats
end
|
#num_flats ⇒ Object
81
82
83
|
# File 'lib/head_music/rudiment/key_signature.rb', line 81
def num_flats
flats.length + double_flats.length * 2
end
|
#num_sharps ⇒ Object
77
78
79
|
# File 'lib/head_music/rudiment/key_signature.rb', line 77
def num_sharps
sharps.length + double_sharps.length * 2
end
|
#sharps ⇒ Object
53
54
55
56
57
|
# File 'lib/head_music/rudiment/key_signature.rb', line 53
def sharps
spellings.select(&:sharp?).sort_by do |spelling|
ORDERED_LETTER_NAMES_OF_SHARPS.index(spelling.letter_name.to_s)
end
end
|
#spellings ⇒ Object
49
50
51
|
# File 'lib/head_music/rudiment/key_signature.rb', line 49
def spellings
pitches.map(&:spelling).uniq
end
|
#to_s ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'lib/head_music/rudiment/key_signature.rb', line 104
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
|