Class: HeadMusic::Pitch

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/head_music/pitch.rb

Overview

A pitch is a named frequency represented by a spelling and a register.

Defined Under Namespace

Classes: EnharmonicEquivalence, OctaveEquivalence

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spelling, register) ⇒ Pitch

Returns a new instance of Pitch.



85
86
87
88
# File 'lib/head_music/pitch.rb', line 85

def initialize(spelling, register)
  @spelling = HeadMusic::Spelling.get(spelling.to_s)
  @register = register.to_i
end

Instance Attribute Details

#registerObject (readonly)

Returns the value of attribute register.



5
6
7
# File 'lib/head_music/pitch.rb', line 5

def register
  @register
end

#spellingObject (readonly)

Returns the value of attribute spelling.



5
6
7
# File 'lib/head_music/pitch.rb', line 5

def spelling
  @spelling
end

Class Method Details

.concert_aObject



38
39
40
# File 'lib/head_music/pitch.rb', line 38

def self.concert_a
  get("A4")
end

.fetch_or_create(spelling, register = nil) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/head_music/pitch.rb', line 76

def self.fetch_or_create(spelling, register = nil)
  register ||= HeadMusic::Register::DEFAULT
  return unless spelling && (-1..9).cover?(register)

  @pitches ||= {}
  hash_key = [spelling, register].join
  @pitches[hash_key] ||= new(spelling, register)
end

.from_name(name) ⇒ Object



48
49
50
51
52
# File 'lib/head_music/pitch.rb', line 48

def self.from_name(name)
  return nil unless name == name.to_s

  fetch_or_create(HeadMusic::Spelling.get(name), HeadMusic::Register.get(name).to_i)
end

.from_number(number) ⇒ Object



54
55
56
57
58
# File 'lib/head_music/pitch.rb', line 54

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

  fetch_or_create(HeadMusic::Spelling.from_number(number), (number.to_i / 12) - 1)
end

.from_number_and_letter(number, letter_name) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/head_music/pitch.rb', line 60

def self.from_number_and_letter(number, letter_name)
  letter_name = HeadMusic::LetterName.get(letter_name)
  natural_letter_pitch = natural_letter_pitch(number, letter_name)
  alteration_interval = natural_letter_pitch.smallest_interval_to(HeadMusic::PitchClass.get(number))
  alteration = HeadMusic::Alteration.by(:semitones, alteration_interval) if alteration_interval != 0
  spelling = HeadMusic::Spelling.fetch_or_create(letter_name, alteration)
  fetch_or_create(spelling, natural_letter_pitch.register)
end

.from_pitch_class(pitch_class) ⇒ Object



42
43
44
45
46
# File 'lib/head_music/pitch.rb', line 42

def self.from_pitch_class(pitch_class)
  return nil unless pitch_class.is_a?(HeadMusic::PitchClass)

  fetch_or_create(pitch_class.sharp_spelling)
end

.get(value) ⇒ Object

Fetches a pitch identified by the information passed in.

Accepts:

- a Pitch instance
- a PitchClass instance
- a name string, such as 'Ab4'
- a number corresponding to the midi note number


28
29
30
31
32
# File 'lib/head_music/pitch.rb', line 28

def self.get(value)
  from_pitch_class(value) ||
    from_name(value) ||
    from_number(value)
end

.middle_cObject



34
35
36
# File 'lib/head_music/pitch.rb', line 34

def self.middle_c
  get("C4")
end

.natural_letter_pitch(number, letter_name) ⇒ Object



69
70
71
72
73
74
# File 'lib/head_music/pitch.rb', line 69

def self.natural_letter_pitch(number, letter_name)
  natural_letter_pitch = get(HeadMusic::LetterName.get(letter_name).pitch_class)
  natural_letter_pitch += 12 while (number.to_i - natural_letter_pitch.to_i) >= 6
  natural_letter_pitch -= 12 while (number.to_i - natural_letter_pitch.to_i) <= -6
  get(natural_letter_pitch)
end

Instance Method Details

#+(other) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/head_music/pitch.rb', line 117

def +(other)
  if other.is_a?(HeadMusic::DiatonicInterval)
    # return a pitch
    other.above(self)
  else
    # assume value represents an interval in semitones and return another pitch
    HeadMusic::Pitch.get(to_i + other.to_i)
  end
end

#-(other) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/head_music/pitch.rb', line 127

def -(other)
  case other
  when HeadMusic::DiatonicInterval
    # return a pitch
    other.below(self)
  when HeadMusic::Pitch
    # return an interval
    HeadMusic::ChromaticInterval.get(to_i - other.to_i)
  else
    # assume value represents an interval in semitones and return another pitch
    HeadMusic::Pitch.get(to_i - other.to_i)
  end
end

#<=>(other) ⇒ Object



146
147
148
# File 'lib/head_music/pitch.rb', line 146

def <=>(other)
  midi_note_number <=> other.midi_note_number
end

#==(other) ⇒ Object



141
142
143
144
# File 'lib/head_music/pitch.rb', line 141

def ==(other)
  other = HeadMusic::Pitch.get(other)
  to_s == other.to_s
end

#frequencyObject



158
159
160
# File 'lib/head_music/pitch.rb', line 158

def frequency
  tuning.frequency_for(self)
end

#helmholtz_notationObject



109
110
111
# File 'lib/head_music/pitch.rb', line 109

def helmholtz_notation
  helmholtz_letter_name + helmholtz_marks
end

#midi_note_numberObject Also known as: midi, number



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

def midi_note_number
  (register + 1) * 12 + letter_name.pitch_class.to_i + alteration_semitones.to_i
end

#nameObject



90
91
92
# File 'lib/head_music/pitch.rb', line 90

def name
  [spelling, register].join
end

#naturalObject



113
114
115
# File 'lib/head_music/pitch.rb', line 113

def natural
  HeadMusic::Pitch.get(to_s.gsub(HeadMusic::Alteration.matcher, ""))
end

#natural_steps(num_steps) ⇒ Object



154
155
156
# File 'lib/head_music/pitch.rb', line 154

def natural_steps(num_steps)
  HeadMusic::Pitch.get([target_letter_name(num_steps), register + octaves_delta(num_steps)].join)
end

#scale(scale_type_name = nil) ⇒ Object



150
151
152
# File 'lib/head_music/pitch.rb', line 150

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

#steps_to(other) ⇒ Object



162
163
164
165
# File 'lib/head_music/pitch.rb', line 162

def steps_to(other)
  other = HeadMusic::Pitch.get(other)
  letter_name_steps_to(other) + 7 * octave_changes_to(other)
end

#to_iObject



105
106
107
# File 'lib/head_music/pitch.rb', line 105

def to_i
  midi_note_number
end

#to_sObject



101
102
103
# File 'lib/head_music/pitch.rb', line 101

def to_s
  name
end