Using scale exponential base value @classic 0 in Gen~

bradley's icon

I thought this was working but now I'm getting errors. How can I use something like [scale 0. 1. 0. 10. 5.97 @classic 0] in gen~. This has just log jammed me. Patch was almost done and now this.....I appreciate any help.

bradley's icon

Ugh, fixed it defaults to @classic 0 in gen~. duh

Graham Wakefield's icon

Actually there's no @classic at all in gen~. The implementation of [scale] in gen~ is conceptually as follows:

// scale(x, ilo, ihi, olo, ohi, exponent):
x1 = (x - ilo)/(ihi - ilo); // map from input range to 0..1
x2 = pow(x1, exponent); // raise to exponent argument
out = x2 * (ohi-olo) + olo; // re-map to output range

Note that in gen~, care is taken to avoid NaN output by ensuring that division by zero does not occur (e.g. when ilo == ihi), and raising a negative base to a non-integer power does not occur. In either case, the output will be just olo. This is not quite the same as either @classic 0 or @classic 1 in Max, but is closer to @classic 0. One difference: scale in gen~ does not take any special care if the input or output ranges are reversed (i.e. if ilo > ihi or olo > ohi), which is part of what the @classic modes do. In general, gen~ is a lower-level environment, and you may need to insert some of your own bounds-checking etc. if needed. This can be done with a min & max operator before the scale range inputs, for example.

The actual C++ generated is below. safediv will return 0 if the divisor is zero. safepow will return 0 if the base is negative and the exponent is non-integer.

t_sample irange_35 = (in3 - in2);
t_sample orange_36 = (in5 - in4);
t_sample sub_37 = (in1 - in2);
t_sample scale_34 = ((safepow(safediv(sub_37, irange_35), in6) * orange_36) + in4);