class Dodecaphony::Pitch

Constants

VALID_PITCHES

Attributes

name[RW]
pitch_number[RW]

Public Class Methods

new(pitch_name) click to toggle source
# File lib/pitch.rb, line 11
def initialize pitch_name
  self.name = pitch_name
  validate_name
  self.pitch_number = adjust_for_accidentals(starting_number)
end

Public Instance Methods

==(other) click to toggle source
# File lib/pitch.rb, line 29
def == other
  numbers = (distance_from(other) == 0)
  names = (starting_letter == other.name.upcase.split(//)[0])

  names && numbers
end
distance_from(second_pitch) click to toggle source
# File lib/pitch.rb, line 17
def distance_from second_pitch
  ensure_number_scale(second_pitch.pitch_number - pitch_number)
end
spell_as_flat() click to toggle source
# File lib/pitch.rb, line 25
def spell_as_flat
  respell :+, "b"
end
spell_as_sharp() click to toggle source
# File lib/pitch.rb, line 21
def spell_as_sharp
  respell :-, "#"
end

Private Instance Methods

accidentals() click to toggle source
# File lib/pitch.rb, line 56
def accidentals
  name.downcase.split(//)[1..-1]
end
adjust_for_accidentals(number) click to toggle source
# File lib/pitch.rb, line 60
def adjust_for_accidentals number
  accidentals.each do |a|
    number += 1 if a == '+' or a == '#'
    number -= 1 if a == '-' or a == 'b' 
  end
  ensure_number_scale number
end
ensure_number_scale(number) click to toggle source
# File lib/pitch.rb, line 68
def ensure_number_scale number
  number % 12
end
respell(method, accidental) click to toggle source
# File lib/pitch.rb, line 50
def respell method, accidental
  num = self.pitch_number
  VALID_PITCHES.key(num) || 
    VALID_PITCHES.key(ensure_number_scale(num.send(method, 1))) + accidental
end
starting_letter() click to toggle source
# File lib/pitch.rb, line 76
def starting_letter
  name.upcase.split(//)[0]
end
starting_number() click to toggle source
# File lib/pitch.rb, line 72
def starting_number
  VALID_PITCHES[starting_letter]
end
validate_name() click to toggle source
# File lib/pitch.rb, line 44
def validate_name
  unless VALID_PITCHES.has_key?(starting_letter)
    raise ArgumentError, 'invalid pitch name'
  end
end