Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/tonal/extensions.rb

Instance Method Summary collapse

Instance Method Details

#best_fitting_edo(min_edo: 5, max_edo: 72) ⇒ Array

Returns with the EDO and its error best fitting the given ratios contained in self.

Examples:

[3/2r].best_fitting_edo) => [53, 0.07]
[7/4r, 3/2r].best_fitting_edo => [41, 3.46]

Parameters:

  • min_edo (Integer) (defaults to: 5)

    the mininum edo to search

  • max_edo (Integer) (defaults to: 72)

    the maximum edo to search

Returns:

  • (Array)

    with the EDO and its error best fitting the given ratios contained in self



407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/tonal/extensions.rb', line 407

def best_fitting_edo(min_edo: 5, max_edo: 72)
  (min_edo..max_edo).map do |edo|
    step_size = 1200.0 / edo

    total_error_for_edo = to_cents.map do |r_cents|
      quantized = (r_cents / step_size).round * step_size
      (r_cents - quantized).abs
    end.sum

    [edo, total_error_for_edo.round(2)]
  end.min_by{|_, error| error}
end

#denominatorsArray Also known as: consequents

Returns of denominators of elements of self.

Examples:

[Tonal::Ratio.new(3,2), Tonal::Ratio.new(5,4)].denominators => [2, 4]

Returns:

  • (Array)

    of denominators of elements of self



368
# File 'lib/tonal/extensions.rb', line 368

def denominators = self.map(&:denominator)

#denominizeArray

Returns an array of ratios with equalized denominators.

Examples:

[4/3r, 3/2r].denominize => [(8/6), (9/6)]

Returns:

  • (Array)

    an array of ratios with equalized denominators



375
376
377
378
# File 'lib/tonal/extensions.rb', line 375

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

#lcmInteger

Returns least common multiple of integer elements of self.

Examples:

[3, 2, 7].lcm => 42

Returns:

  • (Integer)

    least common multiple of integer elements of self



355
# File 'lib/tonal/extensions.rb', line 355

def lcm = self.reduce(1, :lcm)

#meanFloat

Returns the mean of the elements of self.

Examples:

[1, 2].mean => 1.5

Returns:

  • (Float)

    the mean of the elements of self



391
# File 'lib/tonal/extensions.rb', line 391

def mean = self.sum / self.count.to_f

#modulo_translate(lower = 0, upper) ⇒ Array

Returns translated modularly.

Examples:

[-6.617469071022061, 4.755369851099594, 7.588140911919945, -6.49706614430203].modulo_translate(-3, 5)
=> [1.382530928977939, 4.755369851099594,-0.411859088080055,  1.50293385569797]

Parameters:

  • lower (defaults to: 0)

    the lower bound of the modulo range

  • upper

    the upper bound of the modulo range

Returns:

  • (Array)

    translated modularly



451
452
453
454
455
456
# File 'lib/tonal/extensions.rb', line 451

def modulo_translate(lower=0, upper)
  range = (upper - lower) == 0 ? 1 : upper - lower
  map do |value|
    (value - lower) % range + lower
  end
end

#numeratorsArray Also known as: antecedents

Returns of numerators of elements of self.

Examples:

[3/2r, 5/4r].numerators => [3, 5]

Returns:

  • (Array)

    of numerators of elements of self



361
# File 'lib/tonal/extensions.rb', line 361

def numerators = self.map(&:numerator)

#ratio_from_prime_divisions(reduced: false) ⇒ Tonal::Ratio

Returns ratio reconstructed from the result of a prime factor decomposition.

Examples:

[[[3, 1]], [[2, 1]]].ratio_from_prime_divisions => (3/2)

Parameters:

  • reduced (Boolean) (defaults to: false)

    if a reduced or unreduced ratio is returned

Returns:

  • (Tonal::Ratio)

    ratio reconstructed from the result of a prime factor decomposition



398
# File 'lib/tonal/extensions.rb', line 398

def ratio_from_prime_divisions(reduced: false) = reduced ? Tonal::ReducedRatio.new(Prime.int_from_prime_division(self.first), Prime.int_from_prime_division(self.last)) : Tonal::Ratio.new(Prime.int_from_prime_division(self.first), Prime.int_from_prime_division(self.last))

#rescale(new_min = 0, new_max) ⇒ Array

Returns rescaled by new minimum and new maximum.

Examples:

[0.47943068514319154, 0.7161083818132802, 0.19855867360591783].rescale(0,3)
=> [1.6280871600341376, 3.0, 0.0]

Parameters:

  • new_min (defaults to: 0)
  • new_max

Returns:

  • (Array)

    rescaled by new minimum and new maximum



435
436
437
438
439
440
441
442
# File 'lib/tonal/extensions.rb', line 435

def rescale(new_min=0, new_max)
  old_min = min
  old_max = max

  self.map do |x|
    new_min + ((x - old_min) * (new_max - new_min)) / (old_max - old_min)
  end
end

#rpad(min_size, value = nil) ⇒ Array

Returns padded to the right up to n, with value. value default is nil.

Examples:

[3,2].rpad(3, 12) => [3, 2, 12]

Parameters:

  • min_size
  • value (defaults to: nil)

Returns:

  • (Array)

    padded to the right up to n, with value. value default is nil



342
# File 'lib/tonal/extensions.rb', line 342

def rpad(min_size, value = nil) = self.dup.rpad!(min_size, value)

#rpad!(min_size, value = nil) ⇒ Array

Returns self replaced by array padded to the right up to n, with value. value default is nil.

Examples:

[3,2].rpad!(3, 12) => [3, 2, 12]

Parameters:

  • min_size
  • value (defaults to: nil)

Returns:

  • (Array)

    self replaced by array padded to the right up to n, with value. value default is nil



331
332
333
334
# File 'lib/tonal/extensions.rb', line 331

def rpad!(min_size, value = nil)
  self.length > min_size ? self : (min_size - self.length).times { self << value }
  self
end

#to_centsArray Also known as: cents

Returns of cent values for ratio or rational elements of self.

Examples:

[3/2r, 4/3r].to_cents => [701.96, 498.04]

Returns:

  • (Array)

    of cent values for ratio or rational elements of self



384
# File 'lib/tonal/extensions.rb', line 384

def to_cents = self.map{|r| r.to_cents}

#to_rRational

Returns from first and last element of array. Ideally to be used with tuples.

Examples:

[4,3].to_r => (4/3)

Returns:

  • (Rational)

    from first and last element of array. Ideally to be used with tuples.



462
# File 'lib/tonal/extensions.rb', line 462

def to_r = Rational(numerator, denominator)

#to_vectorVector Also known as: vector

Returns self converted to a vector.

Examples:

[3,2].to_vector => Vector[3, 2]

Returns:

  • (Vector)

    self converted to a vector



348
# File 'lib/tonal/extensions.rb', line 348

def to_vector = Vector[*self]

#translate(value) ⇒ Array

Returns translated by value.

Examples:

[0.24184760813024642, 0.49344034900361244, 0.07231824070126536].translate(-0.07231824070126536) = [0.16952936742898106, 0.4211221083023471, 0.0]

Parameters:

  • value (Numeric)

    the value that is translating self

Returns:

  • (Array)

    translated by value



425
# File 'lib/tonal/extensions.rb', line 425

def translate(value) = self.map{|e| e + value}