Equivalent to Max/MSP [scale]

Looking for a way of scaling my data and I really like how Max does it.
aka [scale] (input-low input-high output-low output-high)

In SCAMP I have been doing:

1 fund = data['f0'] # this is reading from a JSON file
2 xmin, xmax = min(fund), max(fund) # this gets the min/max of input
3 for i, val in enumerate(fund): # normalize 
4     fund[i] = (val-xmin) / (xmax-xmin)
5 midiFund = [round(element * 30) + 50 for element in fund] 
# the first int above is the effectively the range 
# the second int is where to place that range

Anyway, it seems like this is beating around the bush quite a bit. Is there a simpler way to get desired results?

You could borrow some code from someone else :slight_smile:

E.g. if you put this file in your project and import it,

Then you can write e.g.

Mapping.linlin(min_in, max_in, min_out, max_out)

for linear mapping. There are also linexp, explin, and expexp for exponential or logarithmic warping during mapping.

1 Like