Sha256: 3ceba68df74281997394123c827b335f12abd3e7e7e2405eefb73b60dddbe8ee

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Coltrane
  # Interval describe the logarithmic distance between 2 frequencies.
  # It's measured in cents.
  class FrequencyInterval
    include Comparable

    attr_reader :cents

    class << self
      alias [] new
    end

    def initialize(cents)
      @cents = cents.round
    end

    def semitones
      (cents.to_f / 100).round
    end

    def ascending
      self.class[cents.abs]
    end

    def descending
      self.class[-cents.abs]
    end

    def ascending?
      cents > 0
    end

    def descending?
      cents < 0
    end

    def inversion
      self.class[(-cents.abs % 1200) * (ascending? ? +1 : -1)]
    end

    def opposite
      self.class.new(-cents)
    end

    def interval_class
      IntervalClass.new(semitones)
    end

    def ==(other)
      return false unless other.is_a? FrequencyInterval
      cents == other.cents
    end

    alias eql? ==
    alias hash cents

    def +(other)
      case other
      when Numeric then FrequencyInterval[cents + other]
      when Interval then FrequencyInterval[cents + other.cents]
      end
    end

    def -(other)
      case other
      when Numeric then FrequencyInterval[cents - other]
      when Interval then FrequencyInterval[cents - other.cents]
      end
    end

    def -@
      FrequencyInterval[-cents]
    end

    def <=>(other)
      cents <=> other.cents
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coltrane-2.2.1 lib/coltrane/frequency_interval.rb