Class: MiltonsMachine::Core::MusicMath

Inherits:
Object
  • Object
show all
Defined in:
lib/miltons_machine/core/music_math.rb

Overview

Class: MusicMath

This class provides additional additional methods and services not found in the standard library

Class Method Summary (collapse)

Class Method Details

+ (Array) compute_deltas(input_set)

Given a set of ordered numbers, compute the differences between each pair

Parameters:

  • input_set (Array)

    the set of numbers we want deltas

Returns:

  • (Array)

    an array of deltas or differences



18
19
20
21
22
23
24
25
# File 'lib/miltons_machine/core/music_math.rb', line 18

def self.compute_deltas( input_set )
  result = []
  input_set.each_with_index do |input, index|
    next if index == 0
    result << (input_set[index - 1] - input)
  end
  result
end

+ (Float) convert_rhythm(rhythm, beat, tempo, unit_of_time = 60.00)

Converts a rhythm unit to it's equivalent of time unit.

Parameters:

  • rhythm (Numeric)

    a fraction of a whole note

  • beat (Numeric)

    the unit for a given measure - for example 1/4 = beat in 4/4 time

  • tempo (Numeric)

    how many beats per unit of time ex: a 58 Metronome Mark would equal 58

  • unit_of_time (Numeric) (defaults to: 60.00)

    the time in seconds ex: 58 Metronome Mark would be 60 seconds (e.g 1 minute)

Returns:

  • (Float)

    the equivalent time value



48
49
50
# File 'lib/miltons_machine/core/music_math.rb', line 48

def self.convert_rhythm( rhythm, beat, tempo, unit_of_time = 60.00 )
  ( rhythm / beat) * ( unit_of_time / tempo )
end

+ (Float) quantize(input, step = 1.00)

Quantize a given number using the Middle Riser Uniform Quantizer algorithm

Parameters:

  • input (Numeric)

    the value we wish to quantize

  • step (Numeric) (defaults to: 1.00)

    the quantization step size

Returns:

  • (Float)

    the quantized value

See Also:



35
36
37
# File 'lib/miltons_machine/core/music_math.rb', line 35

def self.quantize( input, step = 1.00 )
  (step * ( (input / step ) + 0.5 )).floor
end

+ (Float) random(range)

Return a random value between a range

Parameters:

  • range (Range)

    the range to find a random number value between

Returns:

  • (Float)

    a new random value



80
81
82
83
# File 'lib/miltons_machine/core/music_math.rb', line 80

def self.random(range)
  return range.first if range.first == range.last
  range.first + (Random.new.rand * (range.last - range.first).abs)
end

+ (Float) rescale(value, old_scale, new_scale, base = 1.00)

Converts a scaled values into a new range

Parameters:

  • value (Numeric)

    the value tha we wish to rescale

  • old_scale (Range)

    the scale or range that the value originated in

  • new_scale (Range)

    the new scale that we wish to stretch or shrink the value into

  • base (Numeric) (defaults to: 1.00)

    the optional exponent of operations

Returns:

  • (Float)

    the new value as a member of the new scale



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/miltons_machine/core/music_math.rb', line 61

def self.rescale( value, old_scale, new_scale, base = 1.00)
  return new_scale.last if value >= old_scale.last
  return new_scale.first  if value <= old_scale.first
  if base == 1.00
     return ( (( new_scale.last - new_scale.first) / (old_scale.last.to_f - old_scale.first)) *
              (value - old_scale.first) ) + new_scale.first
  end

  # This looks more complicated than it is super-folded up
  ( ((new_scale.last - new_scale.first) / (base.to_f - 1.00)) *
      ((base.to_f ** ((value - old_scale.first) / (old_scale.last.to_f - old_scale.first)) ) - 1.0) )  +
      new_scale.first
end