Class: HeadMusic::Rudiment::Pitch

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

#registerObject (readonly)

Returns the value of attribute register.



11
12
13
# File 'lib/head_music/rudiment/pitch.rb', line 11

def register
  @register
end

#spellingObject (readonly)

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_aObject



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_cObject



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.



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

def self.register_of(midi_note_number)
  midi_note_number / SEMITONES_PER_OCTAVE + LOWEST_MIDI_REGISTER
end

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_equivalenceObject (private)



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

def enharmonic_equivalence
  @enharmonic_equivalence ||= HeadMusic::Rudiment::Pitch::EnharmonicEquivalence.get(self)
end

#frequencyObject



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

def frequency
  tuning.frequency_for(self)
end

#helmholtz_notationObject



120
121
122
# File 'lib/head_music/rudiment/pitch.rb', line 120

def helmholtz_notation
  HelmholtzNotation.new(spelling, register).to_s
end

#midi_note_numberObject Also known as: midi, number



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

def midi_note_number
  (register - LOWEST_MIDI_REGISTER) * SEMITONES_PER_OCTAVE + spelling.semitones_above_c
end

#nameObject



97
98
99
# File 'lib/head_music/rudiment/pitch.rb', line 97

def name
  [spelling, register].join
end

#naturalObject



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_equivalenceObject (private)



167
168
169
# File 'lib/head_music/rudiment/pitch.rb', line 167

def octave_equivalence
  @octave_equivalence ||= HeadMusic::Rudiment::Pitch::OctaveEquivalence.get(self)
end

#pitched?Boolean

Returns:

  • (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_iObject



116
117
118
# File 'lib/head_music/rudiment/pitch.rb', line 116

def to_i
  midi_note_number
end

#to_sObject



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

def to_s
  name
end

#tuningObject (private)



171
172
173
# File 'lib/head_music/rudiment/pitch.rb', line 171

def tuning
  @tuning ||= HeadMusic::Rudiment::Tuning.new
end