Page 1 of 1

[Ruby] Linear Scale Transform (now with Midi Example)

Posted: Thu Feb 28, 2013 8:28 pm
by mikefiction
1st post :)

This Ruby math element transforms a scale linearly from standard 0-1 scale to whatever scale you have use for.

You can hard code the min/max numbers or make them externally selectable as shown in the example.

I don't know how many greens it would take to do this function and I'm not going to try :)

You can of course change the numbers from any "old" scale to any "new" scale - You can scale up, down or sideways, haha - i.e 0-10 to 2-10 etc...

I scaled it back again to show accuracy - turn the knob and you'll see it's not 100% accurate all the time - but we're talking on the micro number level - nothing that's ever affected any application I've used this for.

You can use integer output instead of float if you need whole numbers as shown in the example as well.

To give an idea of more advanced uses - I've used a similar technique with float arrays for sample accurate midi velocity and CC# sequencing in a Ruby arpeggiator, but that is beyond the scope of this simple demonstration.
(See edit)

This could be done in one line if you need a hard-coded linear scale transform.

edit: I've attached a more advanced Midi example using a float array transformed to 1-127 sending velocity data

Here's the math:

Code: Select all

new_value = ( ( old_value - old_min ) / (old_max - old_min) ) * (new_max - new_min) + new_min

Re: [Ruby] Linear Scale Transform (now with Midi Example)

Posted: Fri Mar 01, 2013 1:31 am
by trogluddite
Hi Mike, welcome to the forum.

Great to see such a nicely documented example in a first post - and thanks for the credit on the MIDI readout. :D

The MIDI one could be worked up into quite a nice step sequencer example, I think. Here's a step (sorry, bad pun!) in that direction, that demonstrates how to use a couple of method definitions and event scheduling - to capture only the counter changes, and also to create some note-offs...
Ruby_Linear_Scale_Midi-3.fsm
(21 KiB) Downloaded 1469 times