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))
Tonal::Interval.new(2,3,3,4) => (9/8) ((3/2) / (4/3))
Tonal::Interval.new(3) => (3/1) ((3/1) / (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)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tonal/interval.rb', line 21

def initialize(*args, reduced: true)
  args = [1/1r, args[0]] if args.length == 1
  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



53
54
55
# File 'lib/tonal/interval.rb', line 53

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

#denominizeObject



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

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

#inspectObject



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

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

#to_aObject



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

def to_a
  [lower_ratio, upper_ratio]
end