Class: HeadMusic::Rudiment::Spelling

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/rudiment/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::Rudiment::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.



61
62
63
64
65
66
# File 'lib/head_music/rudiment/spelling.rb', line 61

def initialize(letter_name, alteration = nil)
  @letter_name = HeadMusic::Rudiment::LetterName.get(letter_name.to_s)
  @alteration = HeadMusic::Rudiment::Alteration.get(alteration)
  alteration_semitones = @alteration ? @alteration.semitones : 0
  @pitch_class = HeadMusic::Rudiment::PitchClass.get(letter_name.pitch_class + alteration_semitones)
end

Instance Attribute Details

#alterationObject (readonly)

Returns the value of attribute alteration.



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

def alteration
  @alteration
end

#letter_nameObject (readonly)

Returns the value of attribute letter_name.



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

def letter_name
  @letter_name
end

#pitch_classObject (readonly)

Returns the value of attribute pitch_class.



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

def pitch_class
  @pitch_class
end

Class Method Details

.fetch_or_create(letter_name, alteration) ⇒ Object



55
56
57
58
59
# File 'lib/head_music/rudiment/spelling.rb', line 55

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



28
29
30
31
32
33
34
35
36
37
# File 'lib/head_music/rudiment/spelling.rb', line 28

def self.from_name(name)
  return nil unless matching_string(name)

  letter_name, sign_string, _octave = matching_string(name).captures
  letter_name = HeadMusic::Rudiment::LetterName.get(letter_name)
  return nil unless letter_name

  alteration = HeadMusic::Rudiment::Alteration.get(sign_string)
  fetch_or_create(letter_name, alteration)
end

.from_number(number) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/head_music/rudiment/spelling.rb', line 39

def self.from_number(number)
  return nil unless number == number.to_i

  pitch_class_number = number.to_i % 12
  letter_name = HeadMusic::Rudiment::LetterName.from_pitch_class(pitch_class_number)
  from_number_and_letter(number, letter_name)
end

.from_number_and_letter(number, letter_name) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/head_music/rudiment/spelling.rb', line 47

def self.from_number_and_letter(number, letter_name)
  letter_name = HeadMusic::Rudiment::LetterName.get(letter_name)
  natural_letter_pitch_class = letter_name.pitch_class
  alteration_interval = natural_letter_pitch_class.smallest_interval_to(HeadMusic::Rudiment::PitchClass.get(number))
  alteration = HeadMusic::Rudiment::Alteration.by(:semitones, alteration_interval) if alteration_interval != 0
  fetch_or_create(letter_name, alteration)
end

.get(identifier) ⇒ Object



18
19
20
21
22
# File 'lib/head_music/rudiment/spelling.rb', line 18

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

  from_name(identifier) || from_number(identifier)
end

.matching_string(string) ⇒ Object



24
25
26
# File 'lib/head_music/rudiment/spelling.rb', line 24

def self.matching_string(string)
  string.to_s.match(MATCHER)
end

Instance Method Details

#==(other) ⇒ Object



76
77
78
79
# File 'lib/head_music/rudiment/spelling.rb', line 76

def ==(other)
  other = HeadMusic::Rudiment::Spelling.get(other)
  to_s == other.to_s
end

#enharmonic_equivalenceObject (private)



93
94
95
# File 'lib/head_music/rudiment/spelling.rb', line 93

def enharmonic_equivalence
  @enharmonic_equivalence ||= EnharmonicEquivalence.get(self)
end

#nameObject



68
69
70
# File 'lib/head_music/rudiment/spelling.rb', line 68

def name
  [letter_name, alteration].join
end

#natural?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/head_music/rudiment/spelling.rb', line 85

def natural?
  !alteration || alteration.natural?
end

#scale(scale_type_name = nil) ⇒ Object



81
82
83
# File 'lib/head_music/rudiment/spelling.rb', line 81

def scale(scale_type_name = nil)
  HeadMusic::Rudiment::Scale.get(self, scale_type_name)
end

#to_sObject



72
73
74
# File 'lib/head_music/rudiment/spelling.rb', line 72

def to_s
  name
end