Class: MiltonsMachine::Core::MusicMath
- Inherits:
-
Object
- Object
- MiltonsMachine::Core::MusicMath
- 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)
-
+ (Array) compute_deltas(input_set)
Given a set of ordered numbers, compute the differences between each pair.
-
+ (Float) convert_rhythm(rhythm, beat, tempo, unit_of_time = 60.00)
Converts a rhythm unit to it's equivalent of time unit.
-
+ (Float) quantize(input, step = 1.00)
Quantize a given number using the Middle Riser Uniform Quantizer algorithm.
-
+ (Float) random(range)
Return a random value between a range.
-
+ (Float) rescale(value, old_scale, new_scale, base = 1.00)
Converts a scaled values into a new range.
Class Method Details
+ (Array) compute_deltas(input_set)
Given a set of ordered numbers, compute the differences between each pair
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.
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
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
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
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 |