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

Parameters:

  • args

    one argument representing a ratio, with 1/1r implied, or two arguments representing lower and upper ratios, or four arguments representing two numerator/denominator pairs for the lower and upper ratios

  • 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)
  raise(ArgumentError, "One, two or four arguments required. Either one ratio (the other defaulting to 1/1), two ratios, or two pairs of numerator, denominator", caller[0]) unless [1, 2, 4].include?(args.size)
  args = [args[0],1/1r] if args.length == 1
  klass = reduced ? Tonal::ReducedRatio : Tonal::Ratio
  @lower_ratio, @upper_ratio = case args.size
                               when 2
                                 [klass.new(args[1].antecedent, args[1].consequent), klass.new(args[0].antecedent, args[0].consequent)]
                               when 4
                                 [klass.new(args[2], args[3]), klass.new(args[0], args[1])]
                               end
  @intervalic_ratio = @upper_ratio / @lower_ratio
end

Instance Attribute Details

#intervalic_ratioObject (readonly)

Returns the value of attribute intervalic_ratio.



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

def intervalic_ratio
  @intervalic_ratio
end

#lower_ratioObject (readonly)

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)

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) ⇒ Integer

Returns -1, 0, or 1 depending on whether this interval is less than, equal to, or greater than the other interval.

Examples:

interval1 = Tonal::Interval.new(4,3) => 4/3 (4/3 / 1/1)
interval2 = Tonal::Interval.new(3,2) => 3/2 (3/2 / 1/1)
interval1 <=> interval2 => -1

Returns:

  • (Integer)

    -1, 0, or 1 depending on whether this interval is less than, equal to, or greater than the other interval



99
100
101
# File 'lib/tonal/interval.rb', line 99

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

#approximate(approximant = 0, by_method: :continued_fraction, from: :lower_ratio) ⇒ Tonal::Interval

Returns the interval selected by the approximant key, from a set of approximations generated by the by_method algorithm.

Examples:

interval = Tonal::Interval.new(1.2) => 1.2 (1.2 / 1/1)
interval.approximate => 6/5 (6/5 / 1/1)

Parameters:

  • approximant (Integer) (defaults to: 0)

    the index of the approximant to use

  • by_method (Symbol) (defaults to: :continued_fraction)

    the method to use for approximation (:continued_fraction or :tree_path)

  • from (Symbol) (defaults to: :lower_ratio)

    whether to return the interval calculated from the lower or upper ratio (:lower_ratio or :upper_ratio) of the interval

Returns:

  • (Tonal::Interval)

    the interval selected by the approximant key, from a set of approximations generated by the by_method algorithm



80
81
82
# File 'lib/tonal/interval.rb', line 80

def approximate(approximant=0, by_method: :continued_fraction, from: :lower_ratio)
  _returning_interval(_approximate(intervalic_ratio, by_method, approximant), from:)
end

#denominizeArray<Tonal::Ratio>

Returns the ratios with common denominators.

Examples:

interval = Tonal::Interval.new(3,4) => 4/3 (4/3 / 1/1)
interval.denominize => [3/2, 2/2]

Returns:



50
51
52
53
54
# File 'lib/tonal/interval.rb', line 50

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

#inspectString

Returns a string representation of the interval.

Examples:

interval = Tonal::Interval.new(4,3) => 4/3 (4/3 / 1/1)
interval.inspect => "4/3 (4/3 / 1/1)"

Returns:

  • (String)

    a string representation of the interval



89
90
91
# File 'lib/tonal/interval.rb', line 89

def inspect
  "#{intervalic_ratio.label} (#{upper_ratio.label} / #{lower_ratio.label})"
end

#root_interval(power: 1, root: 2, approximant: nil, by_method: :continued_fraction, from: :lower_ratio) ⇒ Tonal::Interval

Returns the interval representing the root of self raised to a power.

Examples:

interval = Tonal::Interval.new(4/3r) => 4/3 (4/3 / 1/1)
interval.root_interval(power: 1, root: 2) => 1.15 (1.15 / 1/1)

Parameters:

  • power (Integer) (defaults to: 1)

    the power to which the root is raised

  • root (Integer) (defaults to: 2)

    the root to be taken

  • approximant (Integer, nil) (defaults to: nil)

    the index of the approximant to use

  • by_method (Symbol) (defaults to: :continued_fraction)

    the method to use for approximation (:continued_fraction or :tree_path)

  • from (Symbol) (defaults to: :lower_ratio)

    whether to return the interval calculated from the lower or upper ratio (:lower_ratio or :upper_ratio) of the interval

Returns:

  • (Tonal::Interval)

    the interval representing the root of self raised to a power



66
67
68
69
70
# File 'lib/tonal/interval.rb', line 66

def root_interval(power: 1, root: 2, approximant: nil, by_method: :continued_fraction, from: :lower_ratio)
  root_ratio = intervalic_ratio.class.new(intervalic_ratio.to_r.power(power, root))
  resulting_ratio = approximant.nil? ? root_ratio : _approximate(root_ratio, by_method, approximant)
  _returning_interval(resulting_ratio, from:)
end

#to_aArray<Tonal::Ratio>

Returns the upper and lower ratios as an array.

Examples:

interval = Tonal::Interval.new(3,2) => 3/2 (3/2 / 1/1)
interval.to_a => [3/2, 1/1]

Returns:



41
42
43
# File 'lib/tonal/interval.rb', line 41

def to_a
  [upper_ratio, lower_ratio]
end