class Coltrane::Theory::Pitch

It describes a pitch, like E4 or Bb5. It's like a note, but it has an octave

Attributes

hash[R]
integer[R]
midi[R]

Public Class Methods

[](*args) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 29
def self.[](*args)
  new *args
end
new(notation_arg = nil, note: nil, octave: nil, notation: nil, frequency: nil) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 10
def initialize(notation_arg = nil,
               note: nil,
               octave: nil,
               notation: nil,
               frequency: nil)

  @integer = begin
    if (n = notation_arg || notation)
      n.is_a?(Integer) ? n : integer_from_notation(n)
    elsif note && octave
      integer_from_note_and_octave(Note[note], octave)
    elsif frequency
      integer_from_frequency(frequency)
    else
      raise(InvalidArgumentsError)
    end
  end
end

Public Instance Methods

+(other) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 67
def +(other)
  case other
  when Integer then Pitch[integer + other]
  end
end
-(other) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 73
def -(other)
  case other
  when Integer then Pitch[integer - other]
  end
end
<=>(other) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 33
def <=>(other)
  integer <=> other.integer
end
==(other) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 56
def ==(other)
  integer == other.integer
end
Also aliased as: eql?, eq
eq(other)
Alias for: ==
eql?(other)
Alias for: ==
frequency() click to toggle source
# File lib/coltrane/theory/pitch.rb, line 60
def frequency
  pitch_class.frequency.octave(octave)
end
name()
Alias for: scientific_notation
notation()
Alias for: scientific_notation
octave() click to toggle source
# File lib/coltrane/theory/pitch.rb, line 45
def octave
  (integer / 12) - 1
end
pitch_class() click to toggle source
# File lib/coltrane/theory/pitch.rb, line 41
def pitch_class
  PitchClass[integer]
end
scientific_notation() click to toggle source
# File lib/coltrane/theory/pitch.rb, line 37
def scientific_notation
  "#{pitch_class}#{octave}"
end
Also aliased as: notation, name, to_s
to_s()
Alias for: scientific_notation

Private Instance Methods

integer_from_frequency(f) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 90
def integer_from_frequency(f)
  octave_from_frequency(f) * 12 + PitchClass.new(frequency: f).integer
end
integer_from_notation(the_notation) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 81
def integer_from_notation(the_notation)
  _, n, o = *the_notation.match(/(\D+)(\d+)/)
  integer_from_note_and_octave(Note[n], o)
end
integer_from_note_and_octave(p, o) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 86
def integer_from_note_and_octave(p, o)
  12 * (o.to_i + 1) + p.integer
end
octave_from_frequency(f) click to toggle source
# File lib/coltrane/theory/pitch.rb, line 94
def octave_from_frequency(f)
  Math.log(f.to_f / PitchClass['C'].frequency.to_f, 2).ceil
end