Class: HeadMusic::Rudiment::Spelling

Inherits:
Base
  • 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

LetterName =
HeadMusic::Rudiment::LetterName
Alteration =
HeadMusic::Rudiment::Alteration
MATCHER =
/^\s*(#{LetterName::PATTERN})(#{Alteration::PATTERN})?(-?\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.



64
65
66
67
68
69
# File 'lib/head_music/rudiment/spelling.rb', line 64

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.



13
14
15
# File 'lib/head_music/rudiment/spelling.rb', line 13

def alteration
  @alteration
end

#letter_nameObject (readonly)

Returns the value of attribute letter_name.



13
14
15
# File 'lib/head_music/rudiment/spelling.rb', line 13

def letter_name
  @letter_name
end

#pitch_classObject (readonly)

Returns the value of attribute pitch_class.



13
14
15
# File 'lib/head_music/rudiment/spelling.rb', line 13

def pitch_class
  @pitch_class
end

Class Method Details

.fetch_or_create(letter_name, alteration) ⇒ Object



58
59
60
61
62
# File 'lib/head_music/rudiment/spelling.rb', line 58

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/head_music/rudiment/spelling.rb', line 31

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



42
43
44
45
46
47
48
# File 'lib/head_music/rudiment/spelling.rb', line 42

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



50
51
52
53
54
55
56
# File 'lib/head_music/rudiment/spelling.rb', line 50

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



21
22
23
24
25
# File 'lib/head_music/rudiment/spelling.rb', line 21

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

  from_name(identifier) || from_number(identifier)
end

.matching_string(string) ⇒ Object



27
28
29
# File 'lib/head_music/rudiment/spelling.rb', line 27

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

Instance Method Details

#==(other) ⇒ Object



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

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

#enharmonic_equivalenceObject (private)



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

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

#nameObject



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

def name
  [letter_name, alteration].join
end

#natural?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/head_music/rudiment/spelling.rb', line 88

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

#scale(scale_type_name = nil) ⇒ Object



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

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

#to_sObject



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

def to_s
  name
end