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 Sign. Does not include the octave. See Pitch for that.
Defined Under Namespace
Classes: EnharmonicEquivalence
Constant Summary
collapse
- MATCHER =
/^\s*([A-G])(#{HeadMusic::Sign.matcher}?)(-?\d+)?\s*$/i
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(letter_name, sign = nil) ⇒ Spelling
Returns a new instance of Spelling.
60
61
62
63
64
65
|
# File 'lib/head_music/spelling.rb', line 60
def initialize(letter_name, sign = nil)
@letter_name = HeadMusic::LetterName.get(letter_name.to_s)
@sign = HeadMusic::Sign.get(sign)
sign_semitones = @sign ? @sign.semitones : 0
@pitch_class = HeadMusic::PitchClass.get(letter_name.pitch_class + sign_semitones)
end
|
Instance Attribute Details
#letter_name ⇒ Object
Returns the value of attribute letter_name.
9
10
11
|
# File 'lib/head_music/spelling.rb', line 9
def letter_name
@letter_name
end
|
#pitch_class ⇒ Object
Returns the value of attribute pitch_class.
9
10
11
|
# File 'lib/head_music/spelling.rb', line 9
def pitch_class
@pitch_class
end
|
#sign ⇒ Object
Returns the value of attribute sign.
9
10
11
|
# File 'lib/head_music/spelling.rb', line 9
def sign
@sign
end
|
Class Method Details
.fetch_or_create(letter_name, sign) ⇒ Object
54
55
56
57
58
|
# File 'lib/head_music/spelling.rb', line 54
def self.fetch_or_create(letter_name, sign)
@spellings ||= {}
hash_key = [letter_name, sign].join
@spellings[hash_key] ||= new(letter_name, sign)
end
|
.from_name(name) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/head_music/spelling.rb', line 27
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
sign = HeadMusic::Sign.get(sign_string)
fetch_or_create(letter_name, sign)
end
|
.from_number(number) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/head_music/spelling.rb', line 38
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
46
47
48
49
50
51
52
|
# File 'lib/head_music/spelling.rb', line 46
def self.from_number_and_letter(number, letter_name)
letter_name = HeadMusic::LetterName.get(letter_name)
natural_letter_pitch_class = letter_name.pitch_class
sign_interval = natural_letter_pitch_class.smallest_interval_to(HeadMusic::PitchClass.get(number))
sign = HeadMusic::Sign.by(:semitones, sign_interval) if sign_interval != 0
fetch_or_create(letter_name, sign)
end
|
.get(identifier) ⇒ Object
17
18
19
20
21
|
# File 'lib/head_music/spelling.rb', line 17
def self.get(identifier)
return identifier if identifier.is_a?(HeadMusic::Spelling)
from_name(identifier) || from_number(identifier)
end
|
.matching_string(string) ⇒ Object
23
24
25
|
# File 'lib/head_music/spelling.rb', line 23
def self.matching_string(string)
string.to_s.match(MATCHER)
end
|
Instance Method Details
#==(other) ⇒ Object
75
76
77
78
|
# File 'lib/head_music/spelling.rb', line 75
def ==(other)
other = HeadMusic::Spelling.get(other)
to_s == other.to_s
end
|
#name ⇒ Object
67
68
69
|
# File 'lib/head_music/spelling.rb', line 67
def name
[letter_name, sign].join
end
|
#natural? ⇒ Boolean
84
85
86
|
# File 'lib/head_music/spelling.rb', line 84
def natural?
!sign || sign.natural?
end
|
#scale(scale_type_name = nil) ⇒ Object
80
81
82
|
# File 'lib/head_music/spelling.rb', line 80
def scale(scale_type_name = nil)
HeadMusic::Scale.get(self, scale_type_name)
end
|
#to_s ⇒ Object
71
72
73
|
# File 'lib/head_music/spelling.rb', line 71
def to_s
name
end
|