First time here? Check out the FAQ!
x

Scaling a set of numbers within a min and max, with option to scale linear or exponentially

+1 vote
633 views
Dear all,

Quick question : how can I scale a set of numbers, let's say a ramp between -1 and 1, to be scaled to 0 to 20 instead of -1 to 1? And how can I define whether this scaling should be linear or exponential?

I've been trying out exp, tan, twoLog, etc. but I can't find this simple math thing (I'm terrible at maths).

A few potential uses :

1) scale a linear 0-1 fader from the VCS to have a steeper curve or gentler curver, to be able to have a non-linear result from the fader (for ex. to have higher resolution/slower increase in the first half of the fader)

2) scale incoming random numbers between -1 and 1 (nextRandom) to be instead within, for ex. 0-20000

3) scale 0-127 midi info to floating numbers between 0 and 1

Thanks!

k
asked Oct 16, 2018 in Capytalk & Smalltalk by koenraad (Practitioner) (330 points)

1 Answer

+3 votes
 
Best answer

The general pattern for changing the range and offset of an EventExpression is:

!EventValue01 * !Range + !Offset

In terms of min and max, this would be:

!EventValue01 * (!Max - !Min) + !Min

There are lots of ways to generate nonlinear controls, but one of the most explicit ways is to create a warping function using an into: array. In other words, create a mapping of input-to-output values expressed as points and put them in an Array. Then use your linear function as the input to that mapping. For example:

!EventValue01 into: #({0 @ 0}{0.25 @ 0.0625}{0.5 @ 0.25}{0.75 @ 0.5625} {1 @ 1})

would have an output value of 0.5625 when its input value (the value of your fader or expression) was 0.75. Values in between the points will be linearly interpolated according to how close the input value is to the one of the specified points. In this example, you could get the same effect using:

!EventValue01 squared

and of course you can still do arithmetic on the results to change the min and max, for example

!EventValue01 squared * (!Max - !Min) + !Min

When you are experimenting with different functions and would like to visualize the result, select the expression in the parameter field and use Shift+Ctrl+Y to evaluate that expression and see how it looks on an oscilloscope. You may have better luck visualizing it by using repeatingRamp: rather than a one-shot ramp:.

If you are scaling an EventValue, you can Ctrl+Click on its widget in the VCS to change its min, max, and grid, and you can unlock the VCS to change that widget to use a log, rather than linear, scale.

answered Oct 16, 2018 by ssc (Savant) (127,080 points)
selected Oct 17, 2018 by koenraad
Worth checking out the fader expressions also within the expressions library for some useful capytalk :)
...