Sha256: aaf5523e25330d32a56d1678d407a4411d46cfb7618438a87c36a854f61629d4

Contents?: true

Size: 957 Bytes

Versions: 8

Compression:

Stored size: 957 Bytes

Contents

# frozen_string_literal: true

module Coltrane
  module Theory
    class Frequency
      attr_reader :frequency

      def initialize(frequency)
        @frequency = frequency.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

8 entries across 8 versions & 1 rubygems

Version Path
coltrane-3.2.0 lib/coltrane/theory/frequency.rb
coltrane-3.1.3 lib/coltrane/theory/frequency.rb
coltrane-3.1.2 lib/coltrane/theory/frequency.rb
coltrane-3.1.1 lib/coltrane/theory/frequency.rb
coltrane-3.1.0 lib/coltrane/theory/frequency.rb
coltrane-3.0.0 lib/coltrane/theory/frequency.rb
coltrane-3.0.0.rc1 lib/coltrane/theory/frequency.rb
coltrane-3.0.0.pre lib/coltrane/theory/frequency.rb