class MusicTheory::Note

Attributes

octave[R]
pitch[R]

Public Class Methods

new(pitch=:c, octave=1) click to toggle source
# File lib/lygre/musictheory.rb, line 7
def initialize(pitch=:c, octave=1)
  @pitch = pitch
  @pitch_numeric = PITCHES.index @pitch
  @octave = octave

  if @pitch_numeric.nil?
    raise ArgumentError.new("Invalid pitch #{pitch.inspect}")
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/lygre/musictheory.rb, line 35
def ==(other)
  other.pitch == @pitch &&
    other.octave == @octave
end
diatonic_steps(steps) click to toggle source
# File lib/lygre/musictheory.rb, line 19
def diatonic_steps(steps)
  base_steps = steps % PITCHES.size
  octaves = steps / PITCHES.size
  new_step = @pitch_numeric + base_steps
  if new_step >= PITCHES.size
    new_step -= PITCHES.size
    octaves += 1
  end
  new_pitch = PITCHES[new_step]
  self.class.new(new_pitch, @octave + octaves)
end
hash() click to toggle source
# File lib/lygre/musictheory.rb, line 40
def hash
  [@pitch, @octave].hash
end
value() click to toggle source
# File lib/lygre/musictheory.rb, line 31
def value
  (@octave + 4) * 12 + VALUES[@pitch_numeric]
end