Sha256: 4b47cb71c0a0dbea7bb1dc386bb831a1e84dea30d199eba6d2e7dd6c335455c6

Contents?: true

Size: 1.52 KB

Versions: 22

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Coltrane
  module Theory
    # 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
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
coltrane-4.1.2 lib/coltrane/theory/frequency_interval.rb
coltrane-4.1.1 lib/coltrane/theory/frequency_interval.rb
coltrane-4.1.0 lib/coltrane/theory/frequency_interval.rb
coltrane-4.0.6 lib/coltrane/theory/frequency_interval.rb
coltrane-4.0.5 lib/coltrane/theory/frequency_interval.rb
coltrane-4.0.4 lib/coltrane/theory/frequency_interval.rb
coltrane-4.0.3 lib/coltrane/theory/frequency_interval.rb
coltrane-3.4.2 lib/coltrane/theory/frequency_interval.rb
coltrane-3.4.1 lib/coltrane/theory/frequency_interval.rb
coltrane-3.4.0 lib/coltrane/theory/frequency_interval.rb
coltrane-3.3.3 lib/coltrane/theory/frequency_interval.rb
coltrane-3.3.2 lib/coltrane/theory/frequency_interval.rb
coltrane-3.3.1 lib/coltrane/theory/frequency_interval.rb
coltrane-3.3.0 lib/coltrane/theory/frequency_interval.rb
coltrane-3.2.0 lib/coltrane/theory/frequency_interval.rb
coltrane-3.1.3 lib/coltrane/theory/frequency_interval.rb
coltrane-3.1.2 lib/coltrane/theory/frequency_interval.rb
coltrane-3.1.1 lib/coltrane/theory/frequency_interval.rb
coltrane-3.1.0 lib/coltrane/theory/frequency_interval.rb
coltrane-3.0.0 lib/coltrane/theory/frequency_interval.rb