Class: Coltrane::Theory::Frequency

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/coltrane/theory/frequency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frequency) ⇒ Frequency

Returns a new instance of Frequency.



9
10
11
# File 'lib/coltrane/theory/frequency.rb', line 9

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



52
53
54
# File 'lib/coltrane/theory/frequency.rb', line 52

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

Instance Attribute Details

#frequencyObject (readonly)

Returns the value of attribute frequency.



7
8
9
# File 'lib/coltrane/theory/frequency.rb', line 7

def frequency
  @frequency
end

Instance Method Details

#/(other) ⇒ Object



45
46
47
48
49
50
# File 'lib/coltrane/theory/frequency.rb', line 45

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

#<=>(other) ⇒ Object



13
14
15
# File 'lib/coltrane/theory/frequency.rb', line 13

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

#==(other) ⇒ Object



33
34
35
# File 'lib/coltrane/theory/frequency.rb', line 33

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

#octave(n) ⇒ Object



29
30
31
# File 'lib/coltrane/theory/frequency.rb', line 29

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

#octave_down(n = 1) ⇒ Object



41
42
43
# File 'lib/coltrane/theory/frequency.rb', line 41

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

#octave_up(n = 1) ⇒ Object



37
38
39
# File 'lib/coltrane/theory/frequency.rb', line 37

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

#to_fObject



25
26
27
# File 'lib/coltrane/theory/frequency.rb', line 25

def to_f
  frequency
end

#to_sObject



21
22
23
# File 'lib/coltrane/theory/frequency.rb', line 21

def to_s
  "#{frequency}hz"
end