Class: HeadMusic::Rudiment::KeySignature
- Inherits:
-
Object
- Object
- HeadMusic::Rudiment::KeySignature
show all
- Defined in:
- lib/head_music/rudiment/key_signature.rb
Overview
Represents a key signature.
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.
Instance Attribute Details
#scale ⇒ Object
Returns the value of attribute scale.
6
7
8
|
# File 'lib/head_music/rudiment/key_signature.rb', line 6
def scale
@scale
end
|
#scale_type ⇒ Object
Returns the value of attribute scale_type.
6
7
8
|
# File 'lib/head_music/rudiment/key_signature.rb', line 6
def scale_type
@scale_type
end
|
#tonic_spelling ⇒ Object
Returns the value of attribute tonic_spelling.
6
7
8
|
# File 'lib/head_music/rudiment/key_signature.rb', line 6
def tonic_spelling
@tonic_spelling
end
|
Class Method Details
.default ⇒ Object
11
12
13
|
# File 'lib/head_music/rudiment/key_signature.rb', line 11
def self.default
@default ||= new("C", :major)
end
|
.get(identifier) ⇒ Object
15
16
17
18
19
20
21
22
|
# File 'lib/head_music/rudiment/key_signature.rb', line 15
def self.get(identifier)
return identifier if identifier.is_a?(HeadMusic::Rudiment::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
|
Instance Method Details
#==(other) ⇒ Object
87
88
89
|
# File 'lib/head_music/rudiment/key_signature.rb', line 87
def ==(other)
alterations == self.class.get(other).alterations
end
|
#alterations ⇒ Object
Also known as:
sharps_and_flats, accidentals
76
77
78
|
# File 'lib/head_music/rudiment/key_signature.rb', line 76
def alterations
flats.any? ? (double_flats + flats) : (double_sharps + sharps)
end
|
#double_flats ⇒ Object
58
59
60
61
62
|
# File 'lib/head_music/rudiment/key_signature.rb', line 58
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
46
47
48
49
50
|
# File 'lib/head_music/rudiment/key_signature.rb', line 46
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
#enharmonic_equivalent?(other) ⇒ Boolean
101
102
103
|
# File 'lib/head_music/rudiment/key_signature.rb', line 101
def enharmonic_equivalent?(other)
enharmonic_equivalence.enharmonic_equivalent?(other)
end
|
#flats ⇒ Object
52
53
54
55
56
|
# File 'lib/head_music/rudiment/key_signature.rb', line 52
def flats
spellings.select(&:flat?).sort_by do |spelling|
ORDERED_LETTER_NAMES_OF_FLATS.index(spelling.letter_name.to_s)
end
end
|
#name ⇒ Object
83
84
85
|
# File 'lib/head_music/rudiment/key_signature.rb', line 83
def name
[tonic_spelling, scale_type].join(" ")
end
|
#num_alterations ⇒ Object
72
73
74
|
# File 'lib/head_music/rudiment/key_signature.rb', line 72
def num_alterations
num_sharps + num_flats
end
|
#num_flats ⇒ Object
68
69
70
|
# File 'lib/head_music/rudiment/key_signature.rb', line 68
def num_flats
flats.length + double_flats.length * 2
end
|
#num_sharps ⇒ Object
64
65
66
|
# File 'lib/head_music/rudiment/key_signature.rb', line 64
def num_sharps
sharps.length + double_sharps.length * 2
end
|
#sharps ⇒ Object
40
41
42
43
44
|
# File 'lib/head_music/rudiment/key_signature.rb', line 40
def sharps
spellings.select(&:sharp?).sort_by do |spelling|
ORDERED_LETTER_NAMES_OF_SHARPS.index(spelling.letter_name.to_s)
end
end
|
#spellings ⇒ Object
36
37
38
|
# File 'lib/head_music/rudiment/key_signature.rb', line 36
def spellings
pitches.map(&:spelling).uniq
end
|
#to_s ⇒ Object
91
92
93
94
95
96
97
98
99
|
# File 'lib/head_music/rudiment/key_signature.rb', line 91
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
|