Class: HeadMusic::Rudiment::Pitch
- Inherits:
-
Base
- Object
- Base
- HeadMusic::Rudiment::Pitch
show all
- Includes:
- Comparable
- Defined in:
- lib/head_music/rudiment/pitch.rb,
lib/head_music/rudiment/pitch/arithmetic.rb,
lib/head_music/rudiment/pitch/natural_step.rb,
lib/head_music/rudiment/pitch/step_distance.rb,
lib/head_music/rudiment/pitch/helmholtz_notation.rb,
lib/head_music/rudiment/pitch/natural_letter_pitch.rb
Overview
A pitch is a named frequency represented by a spelling and a register.
Defined Under Namespace
Classes: Arithmetic, EnharmonicEquivalence, HelmholtzNotation, NaturalLetterPitch, NaturalStep, OctaveEquivalence, Parser, StepDistance
Constant Summary
collapse
- SEMITONES_PER_OCTAVE =
12
- LOWEST_MIDI_REGISTER =
MIDI note 0 is C-1, so the register numbering starts an octave below zero.
-1
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(spelling, register) ⇒ Pitch
Returns a new instance of Pitch.
92
93
94
95
|
# File 'lib/head_music/rudiment/pitch.rb', line 92
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.
11
12
13
|
# File 'lib/head_music/rudiment/pitch.rb', line 11
def register
@register
end
|
#spelling ⇒ Object
Returns the value of attribute spelling.
11
12
13
|
# File 'lib/head_music/rudiment/pitch.rb', line 11
def spelling
@spelling
end
|
Class Method Details
.concert_a ⇒ Object
44
45
46
|
# File 'lib/head_music/rudiment/pitch.rb', line 44
def self.concert_a
get("A4")
end
|
.fetch_or_create(spelling, register = nil) ⇒ Object
85
86
87
88
89
90
|
# File 'lib/head_music/rudiment/pitch.rb', line 85
def self.fetch_or_create(spelling, register = nil)
register ||= HeadMusic::Rudiment::Register::DEFAULT
return unless spelling && (-1..9).cover?(register)
fetch_or_register([spelling, register].join, spelling, register)
end
|
.from_name(name) ⇒ Object
54
55
56
57
58
|
# File 'lib/head_music/rudiment/pitch.rb', line 54
def self.from_name(name)
return nil unless name == name.to_s
Parser.parse(name)
end
|
.from_number(number) ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/head_music/rudiment/pitch.rb', line 60
def self.from_number(number)
return nil unless number.respond_to?(:to_i)
number_int = number.to_i
return nil unless number == number_int
fetch_or_create(HeadMusic::Rudiment::Spelling.from_number(number), register_of(number_int))
end
|
.from_number_and_letter(number, letter_name) ⇒ Object
The register comes from the natural letter pitch and the alteration from the
spelling, which measures it from the same place.
71
72
73
74
|
# File 'lib/head_music/rudiment/pitch.rb', line 71
def self.from_number_and_letter(number, letter_name)
spelling = HeadMusic::Rudiment::Spelling.from_number_and_letter(number, letter_name)
fetch_or_create(spelling, natural_letter_pitch(number, letter_name).register)
end
|
.from_pitch_class(pitch_class) ⇒ Object
48
49
50
51
52
|
# File 'lib/head_music/rudiment/pitch.rb', line 48
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
32
33
34
35
36
37
38
|
# File 'lib/head_music/rudiment/pitch.rb', line 32
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
40
41
42
|
# File 'lib/head_music/rudiment/pitch.rb', line 40
def self.middle_c
get("C4")
end
|
.natural_letter_pitch(number, letter_name) ⇒ Object
76
77
78
|
# File 'lib/head_music/rudiment/pitch.rb', line 76
def self.natural_letter_pitch(number, letter_name)
NaturalLetterPitch.get(number, letter_name)
end
|
.register_of(midi_note_number) ⇒ Object
The inverse of the register term in #midi_note_number.
Instance Method Details
#+(other) ⇒ Object
128
129
130
|
# File 'lib/head_music/rudiment/pitch.rb', line 128
def +(other)
Arithmetic.new(self, other).sum
end
|
#-(other) ⇒ Object
132
133
134
|
# File 'lib/head_music/rudiment/pitch.rb', line 132
def -(other)
Arithmetic.new(self, other).difference
end
|
#<=>(other) ⇒ Object
141
142
143
|
# File 'lib/head_music/rudiment/pitch.rb', line 141
def <=>(other)
midi_note_number <=> other.midi_note_number
end
|
#==(other) ⇒ Object
136
137
138
139
|
# File 'lib/head_music/rudiment/pitch.rb', line 136
def ==(other)
other = HeadMusic::Rudiment::Pitch.get(other)
to_s == other.to_s
end
|
#enharmonic_equivalence ⇒ Object
#frequency ⇒ Object
153
154
155
|
# File 'lib/head_music/rudiment/pitch.rb', line 153
def frequency
tuning.frequency_for(self)
end
|
#helmholtz_notation ⇒ Object
120
121
122
|
# File 'lib/head_music/rudiment/pitch.rb', line 120
def helmholtz_notation
HelmholtzNotation.new(spelling, register).to_s
end
|
#midi_note_number ⇒ Object
Also known as:
midi, number
#name ⇒ Object
97
98
99
|
# File 'lib/head_music/rudiment/pitch.rb', line 97
def name
[spelling, register].join
end
|
#natural ⇒ Object
124
125
126
|
# File 'lib/head_music/rudiment/pitch.rb', line 124
def natural
HeadMusic::Rudiment::Pitch.get([letter_name, register].join)
end
|
#natural_steps(num_steps) ⇒ Object
149
150
151
|
# File 'lib/head_music/rudiment/pitch.rb', line 149
def natural_steps(num_steps)
NaturalStep.new(letter_name, num_steps).applied_to(self)
end
|
#octave_equivalence ⇒ Object
#pitched? ⇒ Boolean
112
113
114
|
# File 'lib/head_music/rudiment/pitch.rb', line 112
def pitched?
true
end
|
#scale(scale_type_name = nil) ⇒ Object
145
146
147
|
# File 'lib/head_music/rudiment/pitch.rb', line 145
def scale(scale_type_name = nil)
HeadMusic::Rudiment::Scale.get(self, scale_type_name)
end
|
#steps_to(other) ⇒ Object
157
158
159
|
# File 'lib/head_music/rudiment/pitch.rb', line 157
def steps_to(other)
StepDistance.new(self, other).steps
end
|
#to_i ⇒ Object
116
117
118
|
# File 'lib/head_music/rudiment/pitch.rb', line 116
def to_i
midi_note_number
end
|
#to_s ⇒ Object
108
109
110
|
# File 'lib/head_music/rudiment/pitch.rb', line 108
def to_s
name
end
|