Class: HeadMusic::Spelling
- Inherits:
-
Object
- Object
- HeadMusic::Spelling
show all
- Defined in:
- lib/head_music/spelling.rb
Overview
Represents the spelling of a pitch, such as C# or Db. Composite of a LetterName and an optional Alteration. Does not include the octave. See Pitch for that.
Defined Under Namespace
Classes: EnharmonicEquivalence
Constant Summary
collapse
- MATCHER =
/^\s*([A-G])(#{HeadMusic::Alteration.matcher}?)(-?\d+)?\s*$/i
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(letter_name, alteration = nil) ⇒ Spelling
Returns a new instance of Spelling.
58
59
60
61
62
63
|
# File 'lib/head_music/spelling.rb', line 58
def initialize(letter_name, alteration = nil)
@letter_name = HeadMusic::LetterName.get(letter_name.to_s)
@alteration = HeadMusic::Alteration.get(alteration)
alteration_semitones = @alteration ? @alteration.semitones : 0
@pitch_class = HeadMusic::PitchClass.get(letter_name.pitch_class + alteration_semitones)
end
|
Instance Attribute Details
#alteration ⇒ Object
Returns the value of attribute alteration.
7
8
9
|
# File 'lib/head_music/spelling.rb', line 7
def alteration
@alteration
end
|
#letter_name ⇒ Object
Returns the value of attribute letter_name.
7
8
9
|
# File 'lib/head_music/spelling.rb', line 7
def letter_name
@letter_name
end
|
#pitch_class ⇒ Object
Returns the value of attribute pitch_class.
7
8
9
|
# File 'lib/head_music/spelling.rb', line 7
def pitch_class
@pitch_class
end
|
Class Method Details
.fetch_or_create(letter_name, alteration) ⇒ Object
52
53
54
55
56
|
# File 'lib/head_music/spelling.rb', line 52
def self.fetch_or_create(letter_name, alteration)
@spellings ||= {}
hash_key = [letter_name, alteration].join
@spellings[hash_key] ||= new(letter_name, alteration)
end
|
.from_name(name) ⇒ Object
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/head_music/spelling.rb', line 25
def self.from_name(name)
return nil unless matching_string(name)
letter_name, sign_string, _octave = matching_string(name).captures
letter_name = HeadMusic::LetterName.get(letter_name)
return nil unless letter_name
alteration = HeadMusic::Alteration.get(sign_string)
fetch_or_create(letter_name, alteration)
end
|
.from_number(number) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/head_music/spelling.rb', line 36
def self.from_number(number)
return nil unless number == number.to_i
pitch_class_number = number.to_i % 12
letter_name = HeadMusic::LetterName.from_pitch_class(pitch_class_number)
from_number_and_letter(number, letter_name)
end
|
.from_number_and_letter(number, letter_name) ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/head_music/spelling.rb', line 44
def self.from_number_and_letter(number, letter_name)
letter_name = HeadMusic::LetterName.get(letter_name)
natural_letter_pitch_class = letter_name.pitch_class
alteration_interval = natural_letter_pitch_class.smallest_interval_to(HeadMusic::PitchClass.get(number))
alteration = HeadMusic::Alteration.by(:semitones, alteration_interval) if alteration_interval != 0
fetch_or_create(letter_name, alteration)
end
|
.get(identifier) ⇒ Object
15
16
17
18
19
|
# File 'lib/head_music/spelling.rb', line 15
def self.get(identifier)
return identifier if identifier.is_a?(HeadMusic::Spelling)
from_name(identifier) || from_number(identifier)
end
|
.matching_string(string) ⇒ Object
21
22
23
|
# File 'lib/head_music/spelling.rb', line 21
def self.matching_string(string)
string.to_s.match(MATCHER)
end
|
Instance Method Details
#==(other) ⇒ Object
73
74
75
76
|
# File 'lib/head_music/spelling.rb', line 73
def ==(other)
other = HeadMusic::Spelling.get(other)
to_s == other.to_s
end
|
#name ⇒ Object
65
66
67
|
# File 'lib/head_music/spelling.rb', line 65
def name
[letter_name, alteration].join
end
|
#natural? ⇒ Boolean
82
83
84
|
# File 'lib/head_music/spelling.rb', line 82
def natural?
!alteration || alteration.natural?
end
|
#scale(scale_type_name = nil) ⇒ Object
78
79
80
|
# File 'lib/head_music/spelling.rb', line 78
def scale(scale_type_name = nil)
HeadMusic::Scale.get(self, scale_type_name)
end
|
#to_s ⇒ Object
69
70
71
|
# File 'lib/head_music/spelling.rb', line 69
def to_s
name
end
|