Sha256: 3f993e3106cab8b5d0bec771c7cb710fd49cba75ce8a17023165eee0b266a181

Contents?: true

Size: 767 Bytes

Versions: 14

Compression:

Stored size: 767 Bytes

Contents

# frozen_string_literal: true

module Coltrane
  # It describes a pitch, like E4 or Bb5. It's like a note, but it has an octave
  class Pitch
    attr_reader :number

    def initialize(pitch)
      case pitch
      when String  then @number = number_from_name(pitch)
      when Numeric then @number = pitch
      when Pitch   then @number = pitch.number
      end
    end

    def number_from_name(pitch_string)
      _, note, octaves = pitch_string.match(/(.*)(\d)/).to_a
      Note[note].number + 12 * octaves.to_i
    end

    def name
      "#{note.name}#{octave}"
    end

    def octave
      number / 12
    end

    def note
      Note[number]
    end

    def +(other)
      Pitch.new(number + (other.is_a?(Pitch) ? other.number : other))
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
coltrane-1.2.4 lib/coltrane/pitch.rb
coltrane-1.2.3 lib/coltrane/pitch.rb
coltrane-1.2.2 lib/coltrane/pitch.rb
coltrane-1.2.1 lib/coltrane/pitch.rb
coltrane-1.2.0 lib/coltrane/pitch.rb
coltrane-1.1.2 lib/coltrane/pitch.rb
coltrane-1.1.1 lib/coltrane/pitch.rb
coltrane-1.1.0 lib/coltrane/pitch.rb
coltrane-1.0.26 lib/coltrane/pitch.rb
coltrane-1.0.24 lib/coltrane/pitch.rb
coltrane-1.0.22 lib/coltrane/pitch.rb
coltrane-1.0.21 lib/coltrane/pitch.rb
coltrane-1.0.20 lib/coltrane/pitch.rb
coltrane-1.0.2 lib/coltrane/pitch.rb