Class: Tonal::Interval

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/tonal/interval.rb

Constant Summary collapse

INTERVAL_OF_EQUIVALENCE =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(upper_ratio, lower_ratio) ⇒ Interval

Returns a new instance of Interval.



11
12
13
14
15
# File 'lib/tonal/interval.rb', line 11

def initialize(upper_ratio, lower_ratio)
  @lower_ratio = lower_ratio.ratio
  @upper_ratio = upper_ratio.ratio
  @interval = @upper_ratio / @lower_ratio
end

Instance Attribute Details

#intervalObject (readonly) Also known as: ratio

Returns the value of attribute interval.



7
8
9
# File 'lib/tonal/interval.rb', line 7

def interval
  @interval
end

#lower_ratioObject (readonly) Also known as: lower

Returns the value of attribute lower_ratio.



7
8
9
# File 'lib/tonal/interval.rb', line 7

def lower_ratio
  @lower_ratio
end

#upper_ratioObject (readonly) Also known as: upper

Returns the value of attribute upper_ratio.



7
8
9
# File 'lib/tonal/interval.rb', line 7

def upper_ratio
  @upper_ratio
end

Instance Method Details

#<=>(rhs) ⇒ Object



36
37
38
# File 'lib/tonal/interval.rb', line 36

def <=>(rhs)
  interval.to_r <=> rhs.interval.to_r
end

#denominizeObject



26
27
28
29
30
# File 'lib/tonal/interval.rb', line 26

def denominize
  ratios = to_a
  lcm = ratios.denominators.lcm
  ratios.map{|r| Tonal::Ratio.new(lcm / r.denominator * r.numerator, lcm)}
end

#inspectObject



32
33
34
# File 'lib/tonal/interval.rb', line 32

def inspect
  "#{self.to_r} (#{upper.to_r} / #{lower.to_r})"
end

#to_aObject



22
23
24
# File 'lib/tonal/interval.rb', line 22

def to_a
  [lower_ratio, upper_ratio]
end