Class: HeadMusic::Rudiment::Pitch::NaturalLetterPitch

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/rudiment/pitch/natural_letter_pitch.rb

Overview

The unaltered pitch of a letter name, in the register that puts it nearest a given midi note number: the pitch a spelling of that number is measured from.

A tritone is the tie-break. Any further and the same letter in the next register is closer, so the alteration measured from here would be the larger of the two ways to spell the note.

Constant Summary collapse

OCTAVE_SEMITONES =
12
TRITONE_SEMITONES =
6

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, letter_name) ⇒ NaturalLetterPitch

Returns a new instance of NaturalLetterPitch.



18
19
20
21
# File 'lib/head_music/rudiment/pitch/natural_letter_pitch.rb', line 18

def initialize(number, letter_name)
  @number = number.to_i
  @letter_name = HeadMusic::Rudiment::LetterName.get(letter_name)
end

Instance Attribute Details

#letter_nameObject (readonly)

Returns the value of attribute letter_name.



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

def letter_name
  @letter_name
end

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

Class Method Details

.get(number, letter_name) ⇒ Object



14
15
16
# File 'lib/head_music/rudiment/pitch/natural_letter_pitch.rb', line 14

def self.get(number, letter_name)
  new(number, letter_name).pitch
end

Instance Method Details

#nearest_register_of(candidate) ⇒ Object (private)



29
30
31
32
33
# File 'lib/head_music/rudiment/pitch/natural_letter_pitch.rb', line 29

def nearest_register_of(candidate)
  candidate += OCTAVE_SEMITONES while (number - candidate.to_i) >= TRITONE_SEMITONES
  candidate -= OCTAVE_SEMITONES while (number - candidate.to_i) <= -TRITONE_SEMITONES
  candidate
end

#pitchObject



23
24
25
# File 'lib/head_music/rudiment/pitch/natural_letter_pitch.rb', line 23

def pitch
  @pitch ||= nearest_register_of(HeadMusic::Rudiment::Pitch.get(letter_name.pitch_class))
end