Class: HeadMusic::Rudiment::Pitch
- Inherits:
-
Base
- Object
- Base
- HeadMusic::Rudiment::Pitch
show all
- Includes:
- Comparable
- Defined in:
- lib/head_music/rudiment/pitch.rb
Overview
A pitch is a named frequency represented by a spelling and a register.
Defined Under Namespace
Classes: EnharmonicEquivalence, OctaveEquivalence, Parser
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(spelling, register) ⇒ Pitch
Returns a new instance of Pitch.
91
92
93
94
|
# File 'lib/head_music/rudiment/pitch.rb', line 91
def initialize(spelling, register)
@spelling = HeadMusic::Rudiment::Spelling.get(spelling.to_s)
@register = register.to_i
end
|
Instance Attribute Details
#register ⇒ Object
Returns the value of attribute register.
7
8
9
|
# File 'lib/head_music/rudiment/pitch.rb', line 7
def register
@register
end
|
#spelling ⇒ Object
Returns the value of attribute spelling.
7
8
9
|
# File 'lib/head_music/rudiment/pitch.rb', line 7
def spelling
@spelling
end
|
Class Method Details
.concert_a ⇒ Object
42
43
44
|
# File 'lib/head_music/rudiment/pitch.rb', line 42
def self.concert_a
get("A4")
end
|
.fetch_or_create(spelling, register = nil) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/head_music/rudiment/pitch.rb', line 82
def self.fetch_or_create(spelling, register = nil)
register ||= HeadMusic::Rudiment::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
52
53
54
55
56
|
# File 'lib/head_music/rudiment/pitch.rb', line 52
def self.from_name(name)
return nil unless name == name.to_s
Parser.parse(name)
end
|
.from_number(number) ⇒ Object
58
59
60
61
62
63
|
# File 'lib/head_music/rudiment/pitch.rb', line 58
def self.from_number(number)
number_int = number.to_i
return nil unless number == number_int
fetch_or_create(HeadMusic::Rudiment::Spelling.from_number(number), (number_int / 12) - 1)
end
|
.from_number_and_letter(number, letter_name) ⇒ Object
.from_pitch_class(pitch_class) ⇒ Object
46
47
48
49
50
|
# File 'lib/head_music/rudiment/pitch.rb', line 46
def self.from_pitch_class(pitch_class)
return nil unless pitch_class.is_a?(HeadMusic::Rudiment::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
30
31
32
33
34
35
36
|
# File 'lib/head_music/rudiment/pitch.rb', line 30
def self.get(value)
return value if value.is_a?(HeadMusic::Rudiment::Pitch)
from_pitch_class(value) ||
from_name(value) ||
from_number(value)
end
|
.middle_c ⇒ Object
38
39
40
|
# File 'lib/head_music/rudiment/pitch.rb', line 38
def self.middle_c
get("C4")
end
|
.natural_letter_pitch(number, letter_name) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/head_music/rudiment/pitch.rb', line 74
def self.natural_letter_pitch(number, letter_name)
number_int = number.to_i
natural_letter_pitch = get(HeadMusic::Rudiment::LetterName.get(letter_name).pitch_class)
natural_letter_pitch += 12 while (number_int - natural_letter_pitch.to_i) >= 6
natural_letter_pitch -= 12 while (number_int - natural_letter_pitch.to_i) <= -6
get(natural_letter_pitch)
end
|
Instance Method Details
#<=>(other) ⇒ Object
152
153
154
|
# File 'lib/head_music/rudiment/pitch.rb', line 152
def <=>(other)
midi_note_number <=> other.midi_note_number
end
|
#==(other) ⇒ Object
147
148
149
150
|
# File 'lib/head_music/rudiment/pitch.rb', line 147
def ==(other)
other = HeadMusic::Rudiment::Pitch.get(other)
to_s == other.to_s
end
|
#enharmonic_equivalence ⇒ Object
#frequency ⇒ Object
164
165
166
|
# File 'lib/head_music/rudiment/pitch.rb', line 164
def frequency
tuning.frequency_for(self)
end
|
#helmholtz_letter_name ⇒ Object
222
223
224
225
226
227
|
# File 'lib/head_music/rudiment/pitch.rb', line 222
def helmholtz_letter_name
spelling_str = spelling.to_s
return spelling_str.downcase if HeadMusic::Rudiment::Register.get(register).helmholtz_case == :lower
spelling_str
end
|
#helmholtz_marks ⇒ Object
229
230
231
|
# File 'lib/head_music/rudiment/pitch.rb', line 229
def helmholtz_marks
HeadMusic::Rudiment::Register.get(register).helmholtz_marks
end
|
#helmholtz_notation ⇒ Object
115
116
117
|
# File 'lib/head_music/rudiment/pitch.rb', line 115
def helmholtz_notation
helmholtz_letter_name + helmholtz_marks
end
|
#midi_note_number ⇒ Object
Also known as:
midi, number
100
101
102
|
# File 'lib/head_music/rudiment/pitch.rb', line 100
def midi_note_number
(register + 1) * 12 + letter_name.pitch_class.to_i + alteration_semitones.to_i
end
|
#name ⇒ Object
96
97
98
|
# File 'lib/head_music/rudiment/pitch.rb', line 96
def name
[spelling, register].join
end
|
#natural_steps(num_steps) ⇒ Object
160
161
162
|
# File 'lib/head_music/rudiment/pitch.rb', line 160
def natural_steps(num_steps)
HeadMusic::Rudiment::Pitch.get([target_letter_name(num_steps), register + octaves_delta(num_steps)].join)
end
|
#octave_adjustment_to(other) ⇒ Object
179
180
181
|
# File 'lib/head_music/rudiment/pitch.rb', line 179
def octave_adjustment_to(other)
(pitch_class_above?(other) ? 1 : 0)
end
|
#octave_changes_to(other) ⇒ Object
175
176
177
|
# File 'lib/head_music/rudiment/pitch.rb', line 175
def octave_changes_to(other)
other.register - register - octave_adjustment_to(other)
end
|
#octave_equivalence ⇒ Object
#octaves_delta(num_steps) ⇒ Object
199
200
201
202
203
204
205
206
207
|
# File 'lib/head_music/rudiment/pitch.rb', line 199
def octaves_delta(num_steps)
octaves_delta = (num_steps.abs / 7) * ((num_steps >= 0) ? 1 : -1)
if wrapped_down?(num_steps)
octaves_delta -= 1
elsif wrapped_up?(num_steps)
octaves_delta += 1
end
octaves_delta
end
|
#pitch_class_above?(other) ⇒ Boolean
183
184
185
|
# File 'lib/head_music/rudiment/pitch.rb', line 183
def pitch_class_above?(other)
natural_pitch_class_number > other.natural_pitch_class_number
end
|
#scale(scale_type_name = nil) ⇒ Object
156
157
158
|
# File 'lib/head_music/rudiment/pitch.rb', line 156
def scale(scale_type_name = nil)
HeadMusic::Rudiment::Scale.get(self, scale_type_name)
end
|
#steps_to(other) ⇒ Object
168
169
170
171
|
# File 'lib/head_music/rudiment/pitch.rb', line 168
def steps_to(other)
other = HeadMusic::Rudiment::Pitch.get(other)
letter_name_steps_to(other) + 7 * octave_changes_to(other)
end
|
#target_letter_name(num_steps) ⇒ Object
217
218
219
220
|
# File 'lib/head_music/rudiment/pitch.rb', line 217
def target_letter_name(num_steps)
@target_letter_name ||= {}
@target_letter_name[num_steps] ||= letter_name.steps_up(num_steps)
end
|
#to_i ⇒ Object
111
112
113
|
# File 'lib/head_music/rudiment/pitch.rb', line 111
def to_i
midi_note_number
end
|
#to_s ⇒ Object
107
108
109
|
# File 'lib/head_music/rudiment/pitch.rb', line 107
def to_s
name
end
|
#wrapped_down?(num_steps) ⇒ Boolean
209
210
211
|
# File 'lib/head_music/rudiment/pitch.rb', line 209
def wrapped_down?(num_steps)
num_steps.negative? && target_letter_name(num_steps).position > letter_name.position
end
|
#wrapped_up?(num_steps) ⇒ Boolean
213
214
215
|
# File 'lib/head_music/rudiment/pitch.rb', line 213
def wrapped_up?(num_steps)
num_steps.positive? && target_letter_name(num_steps).position < letter_name.position
end
|