class HeadMusic::PitchClass

A pitch class is a set of pitches separated by octaves.

Constants

FLAT_SPELLINGS
INTEGER_NOTATION
SHARP_SPELLINGS

Attributes

number[R]
spelling[R]

Public Class Methods

[](identifier)
Alias for: get
get(identifier) click to toggle source
# File lib/head_music/pitch_class.rb, line 14
def self.get(identifier)
  @pitch_classes ||= {}
  if HeadMusic::Spelling.matching_string(identifier)
    spelling = HeadMusic::Spelling.get(identifier)
    number = spelling.pitch_class.to_i
  end
  number ||= identifier.to_i % 12
  @pitch_classes[number] ||= new(number)
end
Also aliased as: []

Private Class Methods

new(pitch_class_or_midi_number) click to toggle source
# File lib/head_music/pitch_class.rb, line 28
def initialize(pitch_class_or_midi_number)
  @number = pitch_class_or_midi_number.to_i % 12
end

Public Instance Methods

+(other) click to toggle source

Pass in the number of semitones

# File lib/head_music/pitch_class.rb, line 49
def +(other)
  HeadMusic::PitchClass.get(to_i + other.to_i)
end
-(other) click to toggle source

Pass in the number of semitones

# File lib/head_music/pitch_class.rb, line 54
def -(other)
  HeadMusic::PitchClass.get(to_i - other.to_i)
end
<=>(other) click to toggle source
# File lib/head_music/pitch_class.rb, line 63
def <=>(other)
  to_i <=> other.to_i
end
==(other) click to toggle source
# File lib/head_music/pitch_class.rb, line 58
def ==(other)
  to_i == other.to_i
end
Also aliased as: enharmonic?
black_key?() click to toggle source
# File lib/head_music/pitch_class.rb, line 81
def black_key?
  !white_key?
end
enharmonic?(other)
Alias for: ==
flat_spelling() click to toggle source
# File lib/head_music/pitch_class.rb, line 44
def flat_spelling
  FLAT_SPELLINGS[number]
end
intervals_to(other) click to toggle source
# File lib/head_music/pitch_class.rb, line 67
def intervals_to(other)
  delta = other.to_i - to_i
  inverse = delta.positive? ? delta - 12 : delta + 12
  [delta, inverse].sort_by(&:abs).map { |interval| HeadMusic::ChromaticInterval.get(interval) }
end
sharp_spelling() click to toggle source
# File lib/head_music/pitch_class.rb, line 40
def sharp_spelling
  SHARP_SPELLINGS[number]
end
smallest_interval_to(other) click to toggle source
# File lib/head_music/pitch_class.rb, line 73
def smallest_interval_to(other)
  intervals_to(other).first
end
to_i() click to toggle source
# File lib/head_music/pitch_class.rb, line 32
def to_i
  number
end
to_integer_notation() click to toggle source
# File lib/head_music/pitch_class.rb, line 36
def to_integer_notation
  INTEGER_NOTATION[number]
end
white_key?() click to toggle source
# File lib/head_music/pitch_class.rb, line 77
def white_key?
  [0, 2, 4, 5, 7, 9, 11].include?(number)
end