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(*args, reduced: true) ⇒ Tonal::Interval

Returns the interval of the given ratios.

Examples:

Tonal::Interval.new(2,3) => (3/2) ((3/2) / (1/1))

Parameters:

  • args

    two arguments representing ratios or four arguments representing two pairs of numerator/denominator

  • reduced (defaults to: true)

    boolean determining whether to use Tonal::ReducedRatio or Tonal::Ratio

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tonal/interval.rb', line 17

def initialize(*args, reduced: true)
  klass = reduced ? Tonal::ReducedRatio : Tonal::Ratio
  raise(ArgumentError, "Two or four arguments required. Either two ratios, or two pairs of numerator, denominator", caller[0]) unless [2, 4].include?(args.size)
  @lower_ratio, @upper_ratio = case args.size
                               when 2
                                 [klass.new(args[0].antecedent, args[0].consequent), klass.new(args[1].antecedent, args[1].consequent)]
                               when 4
                                 [klass.new(args[0],args[1]), klass.new(args[2], args[3])]
                               end
  @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



48
49
50
# File 'lib/tonal/interval.rb', line 48

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

#denominizeObject



38
39
40
41
42
# File 'lib/tonal/interval.rb', line 38

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

#inspectObject



44
45
46
# File 'lib/tonal/interval.rb', line 44

def inspect
  "#{interval} (#{upper} / #{lower})"
end

#to_aObject



34
35
36
# File 'lib/tonal/interval.rb', line 34

def to_a
  [lower_ratio, upper_ratio]
end