Class: Coltrane::Theory::FrequencyInterval

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

Overview

Interval describe the logarithmic distance between 2 frequencies. It’s measured in cents.

Direct Known Subclasses

IntervalClass

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cents) ⇒ FrequencyInterval

Returns a new instance of FrequencyInterval.



16
17
18
# File 'lib/coltrane/theory/frequency_interval.rb', line 16

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

Instance Attribute Details

#centsObject (readonly) Also known as: hash

Returns the value of attribute cents.



10
11
12
# File 'lib/coltrane/theory/frequency_interval.rb', line 10

def cents
  @cents
end

Instance Method Details

#+(other) ⇒ Object



60
61
62
63
64
65
# File 'lib/coltrane/theory/frequency_interval.rb', line 60

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

#-(other) ⇒ Object



67
68
69
70
71
72
# File 'lib/coltrane/theory/frequency_interval.rb', line 67

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

#-@Object



74
75
76
# File 'lib/coltrane/theory/frequency_interval.rb', line 74

def -@
  FrequencyInterval[-cents]
end

#<=>(other) ⇒ Object



78
79
80
# File 'lib/coltrane/theory/frequency_interval.rb', line 78

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

#==(other) ⇒ Object Also known as: eql?



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

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

#ascendingObject



24
25
26
# File 'lib/coltrane/theory/frequency_interval.rb', line 24

def ascending
  self.class[cents.abs]
end

#ascending?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/coltrane/theory/frequency_interval.rb', line 32

def ascending?
  cents > 0
end

#descendingObject



28
29
30
# File 'lib/coltrane/theory/frequency_interval.rb', line 28

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

#descending?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/coltrane/theory/frequency_interval.rb', line 36

def descending?
  cents < 0
end

#interval_classObject



48
49
50
# File 'lib/coltrane/theory/frequency_interval.rb', line 48

def interval_class
  IntervalClass.new(semitones)
end

#inversionObject



40
41
42
# File 'lib/coltrane/theory/frequency_interval.rb', line 40

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

#oppositeObject



44
45
46
# File 'lib/coltrane/theory/frequency_interval.rb', line 44

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

#semitonesObject



20
21
22
# File 'lib/coltrane/theory/frequency_interval.rb', line 20

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