Sha256: 2c429a4d06f524c2c0d1554d629ba51ee6b0e66e4d545ce1e3a8f370d045ed8b

Contents?: true

Size: 1.02 KB

Versions: 14

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Coltrane
  module Theory
    class Frequency
      include Comparable
      attr_reader :frequency

      def initialize(frequency)
        @frequency = frequency.to_f
      end

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

      class << self
        alias [] new
      end

      def to_s
        "#{frequency}hz"
      end

      def to_f
        frequency
      end

      def octave(n)
        Frequency[frequency * 2**n]
      end

      def ==(other)
        frequency == (other.is_a?(Frequency) ? other.frequency : other)
      end

      def octave_up(n = 1)
        octave(n)
      end

      def octave_down(n = 1)
        octave(-n)
      end

      def /(other)
        case other
        when Frequency then FrequencyInterval[1200 * Math.log2(other.frequency / frequency)]
        when Numeric then Frequency[frequency / other]
        end
      end

      def method_missing(method, *args)
        Frequency[frequency.send(method, args[0].to_f)]
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
coltrane-4.1.2 lib/coltrane/theory/frequency.rb
coltrane-4.1.1 lib/coltrane/theory/frequency.rb
coltrane-4.1.0 lib/coltrane/theory/frequency.rb
coltrane-4.0.6 lib/coltrane/theory/frequency.rb
coltrane-4.0.5 lib/coltrane/theory/frequency.rb
coltrane-4.0.4 lib/coltrane/theory/frequency.rb
coltrane-4.0.3 lib/coltrane/theory/frequency.rb
coltrane-3.4.2 lib/coltrane/theory/frequency.rb
coltrane-3.4.1 lib/coltrane/theory/frequency.rb
coltrane-3.4.0 lib/coltrane/theory/frequency.rb
coltrane-3.3.3 lib/coltrane/theory/frequency.rb
coltrane-3.3.2 lib/coltrane/theory/frequency.rb
coltrane-3.3.1 lib/coltrane/theory/frequency.rb
coltrane-3.3.0 lib/coltrane/theory/frequency.rb