class HeadMusic::LetterName

Music has seven lette names that are used to identify pitches and pitch classes.

Constants

NAMES
NATURAL_PITCH_CLASS_NUMBERS

Attributes

name[R]

Public Class Methods

all() click to toggle source
# File lib/head_music/letter_name.rb, line 17
def self.all
  NAMES.map { |letter_name| get(letter_name) }
end
from_name(name) click to toggle source
# File lib/head_music/letter_name.rb, line 25
def self.from_name(name)
  @letter_names ||= {}
  name = name.to_s.first.upcase
  @letter_names[name] ||= new(name) if NAMES.include?(name)
end
from_pitch_class(pitch_class) click to toggle source
# File lib/head_music/letter_name.rb, line 31
def self.from_pitch_class(pitch_class)
  @letter_names ||= {}
  return nil if pitch_class.to_s == pitch_class

  pitch_class = pitch_class.to_i % 12
  name = NAMES.detect { |candidate| pitch_class == NATURAL_PITCH_CLASS_NUMBERS[candidate] }
  name ||= HeadMusic::PitchClass::SHARP_SPELLINGS[pitch_class].first
  @letter_names[name] ||= new(name) if NAMES.include?(name)
end
get(identifier) click to toggle source
# File lib/head_music/letter_name.rb, line 21
def self.get(identifier)
  from_name(identifier) || from_pitch_class(identifier)
end

Private Class Methods

new(name) click to toggle source
# File lib/head_music/letter_name.rb, line 47
def initialize(name)
  @name = name
end

Public Instance Methods

==(other) click to toggle source
# File lib/head_music/letter_name.rb, line 55
def ==(other)
  to_s == other.to_s
end
pitch_class() click to toggle source
# File lib/head_music/letter_name.rb, line 51
def pitch_class
  HeadMusic::PitchClass.get(NATURAL_PITCH_CLASS_NUMBERS[name])
end
position() click to toggle source
# File lib/head_music/letter_name.rb, line 59
def position
  NAMES.index(to_s) + 1
end
series_ascending() click to toggle source
# File lib/head_music/letter_name.rb, line 83
def series_ascending
  @series_ascending ||= begin
    series = NAMES
    series = series.rotate while series.first != to_s
    series
  end
end
series_descending() click to toggle source
# File lib/head_music/letter_name.rb, line 91
def series_descending
  @series_descending ||= begin
    series = NAMES.reverse
    series = series.rotate while series.first != to_s
    series
  end
end
steps_down(num) click to toggle source
# File lib/head_music/letter_name.rb, line 67
def steps_down(num)
  HeadMusic::LetterName.get(series_descending[num % NAMES.length])
end
steps_to(other, direction = :ascending) click to toggle source
# File lib/head_music/letter_name.rb, line 71
def steps_to(other, direction = :ascending)
  other = HeadMusic::LetterName.get(other)
  other_position = other.position
  if direction == :descending
    other_position -= NAMES.length if other_position > position
    position - other_position
  else
    other_position += NAMES.length if other_position < position
    other_position - position
  end
end
steps_up(num) click to toggle source
# File lib/head_music/letter_name.rb, line 63
def steps_up(num)
  HeadMusic::LetterName.get(series_ascending[num % NAMES.length])
end