Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/tonal/extensions.rb
Instance Method Summary collapse
-
#best_fitting_edo(min_edo: 5, max_edo: 72) ⇒ Array
With the EDO and its error best fitting the given ratios contained in self.
-
#denominators ⇒ Array
(also: #consequents)
Of denominators of elements of self.
-
#denominize ⇒ Array
An array of ratios with equalized denominators.
-
#lcm ⇒ Integer
Least common multiple of integer elements of self.
-
#mean ⇒ Float
The mean of the elements of self.
-
#modulo_translate(lower = 0, upper) ⇒ Array
Translated modularly.
-
#numerators ⇒ Array
(also: #antecedents)
Of numerators of elements of self.
-
#ratio_from_prime_divisions(reduced: false) ⇒ Tonal::Ratio
Ratio reconstructed from the result of a prime factor decomposition.
-
#rescale(new_min = 0, new_max) ⇒ Array
Rescaled by new minimum and new maximum.
-
#rpad(min_size, value = nil) ⇒ Array
Padded to the right up to n, with value.
-
#rpad!(min_size, value = nil) ⇒ Array
Self replaced by array padded to the right up to n, with value.
-
#to_cents ⇒ Array
(also: #cents)
Of cent values for ratio or rational elements of self.
- #to_efr(as: :ratios) ⇒ Tonal::ExtendedRatio
- #to_interval ⇒ Tonal::Interval
-
#to_r ⇒ Rational
From first and last element of array.
- #to_sefr(as: :ratios) ⇒ Tonal::SubharmonicExtendedRatio
-
#to_vector ⇒ Vector
(also: #vector)
Self converted to a vector.
-
#translate(value) ⇒ Array
Translated by value.
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.
437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/tonal/extensions.rb', line 437 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 |
#denominators ⇒ Array Also known as: consequents
Returns of denominators of elements of self.
388 |
# File 'lib/tonal/extensions.rb', line 388 def denominators = self.map(&:denominator) |
#denominize ⇒ Array
Returns an array of ratios with equalized denominators.
395 396 397 398 |
# File 'lib/tonal/extensions.rb', line 395 def denominize l = denominators.lcm map{|r| Tonal::Ratio.new(l / r.denominator * r.numerator, l)} end |
#lcm ⇒ Integer
Returns least common multiple of integer elements of self.
375 |
# File 'lib/tonal/extensions.rb', line 375 def lcm = self.reduce(1, :lcm) |
#mean ⇒ Float
Returns the mean of the elements of self.
421 |
# File 'lib/tonal/extensions.rb', line 421 def mean = self.sum / self.count.to_f |
#modulo_translate(lower = 0, upper) ⇒ Array
Returns translated modularly.
481 482 483 484 485 486 |
# File 'lib/tonal/extensions.rb', line 481 def modulo_translate(lower=0, upper) range = (upper - lower) == 0 ? 1 : upper - lower map do |value| (value - lower) % range + lower end end |
#numerators ⇒ Array Also known as: antecedents
Returns of numerators of elements of self.
381 |
# File 'lib/tonal/extensions.rb', line 381 def numerators = self.map(&:numerator) |
#ratio_from_prime_divisions(reduced: false) ⇒ Tonal::Ratio
Returns ratio reconstructed from the result of a prime factor decomposition.
428 |
# File 'lib/tonal/extensions.rb', line 428 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.
465 466 467 468 469 470 471 472 |
# File 'lib/tonal/extensions.rb', line 465 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.
362 |
# File 'lib/tonal/extensions.rb', line 362 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.
351 352 353 354 |
# File 'lib/tonal/extensions.rb', line 351 def rpad!(min_size, value = nil) self.length > min_size ? self : (min_size - self.length).times { self << value } self end |
#to_cents ⇒ Array Also known as: cents
Returns of cent values for ratio or rational elements of self.
404 |
# File 'lib/tonal/extensions.rb', line 404 def to_cents = self.map{|r| r.to_cents} |
#to_efr(as: :ratios) ⇒ Tonal::ExtendedRatio
499 |
# File 'lib/tonal/extensions.rb', line 499 def to_efr(as: :ratios) = as == :ratios ? Tonal::ExtendedRatio.new(ratios: self) : Tonal::ExtendedRatio.new(partials: self) |
#to_interval ⇒ Tonal::Interval
415 |
# File 'lib/tonal/extensions.rb', line 415 def to_interval = Tonal::Interval.new(*self) |
#to_r ⇒ Rational
Returns from first and last element of array. Ideally to be used with tuples.
492 |
# File 'lib/tonal/extensions.rb', line 492 def to_r = Rational(numerator, denominator) |
#to_sefr(as: :ratios) ⇒ Tonal::SubharmonicExtendedRatio
506 |
# File 'lib/tonal/extensions.rb', line 506 def to_sefr(as: :ratios) = as == :ratios ? Tonal::SubharmonicExtendedRatio.new(ratios: self) : Tonal::SubharmonicExtendedRatio.new(partials: self) |
#to_vector ⇒ Vector Also known as: vector
Returns self converted to a vector.
368 |
# File 'lib/tonal/extensions.rb', line 368 def to_vector = Vector[*self] |
#translate(value) ⇒ Array
Returns translated by value.
455 |
# File 'lib/tonal/extensions.rb', line 455 def translate(value) = self.map{|e| e + value} |