Reversing floats and integer values
This will have a simple solution and it may sound stupid to ask after everything else that I've built and found solutions too but I havent been sleeping because of this project properly for months and my pragmatic mind is deteriorating!
Does anyone know the simplest way to reverse the incoming float number range..
For example, I have a mean number coming in from the camera between 0 - 255, but counting down from 255 - 0..I need to reverse this so that I get 0 - 255 instead. How can I do this??
Peas
[!- 255] or [!- 255.] ??
Steve
255.
110.
@Roman ?
@mr_mapes Its a float so its 0. - 255.
Any ideas?
The [scale] object is what you need.
Feed to the incoming data stream into [scale] with the following four arguments - 255. 0. 0. 255.
mr.mapes had it:
[!- 255.]
it's the reverse-subtraction object, note the !. there's one for division too, which gives the reciprocal if the argument is 1:
[!/ 1.]
because addition and multiplication are commutative (order doesn't matter), there are no corresponding [!+] or [!*] objects.
and yes, [scale] will do it too, it's a more general-purpose object for expanding/shrinking/reversing ranges. very handy for things like input sensor data turning into effect sizes, mouse motions to frequencies, etc... you probably don't want 0-127 MIDI CC mapping to 0-20000 Hz, most of the output would sound really high, so you can scale to a more human-tolerable range like 50-2000 or something.
I like using [scale] when I want to change ranges and scales dynamically. Call me old-fashioned but I prefer to y=mx+c with a [* ] and [+ ] as often as possible. Something in my head tells me it's more eco to do it that way...
@seejayjames - why use [scale] to convert MIDI cc to frequency in such a linear fashion when [mtof] does exactly what it says on the box?
I think using [scale] to map to/from frequencies is horrible, that dreaded fifth argument! I'd always recommend sticking with [mtof] or [expr] in these situations.
My old lecturer used to encourage converting one's scale to 0-1, then cube it, then scale and offset the result to get nice log behaviour
@mr_mapes
your alternative approaches to the age-old conundrum of parsing/scaling are insightful and vital, but unfortunately a little vague:
"then scale and offset the result to get nice log behaviour"; the OP and others may not know how to implement y=mx+c for example, particularly as the OP is asking about something as basic as inversion; furthermore, I believe our regular and eminent contributor seejay said:
"you probably don't want 0-127 MIDI CC mapping to 0-20000 Hz...."
thereby suggesting that linear mapping of frequency is not recommended; and Luke underlined this. We often (myself included) forget that every post is a valuable archived resource for those on their way through the edges of the enchanted forest called MaxMSP.
I played an open air festival y'day so forgive the hippy ethos
Brendan
Humble apologies for drifting off topic...
Apologies?? For what? I and other n00bs would merely love to see an implementation of your formulae!
:)
Brendan
So I could use scale to convert the number range im receiving from a camera in a totally different range to control say the volume of a track?
Does anyone know what the maths would be for this using scale?
To convert 255 > 0 to -20 > 0
??
Hi
to invert your input, the first thing is 'remainder subtract', as mr_mapes states: [!- 255] (float or integer);
then [/ 12.5] approx, then [- 20].
Take care with your float or integer preference to ensure it works.
Brendan
I just don't like tones much above 2000 Hz :) and yes, linear doesn't really "sound right", it was just a simple thought to get started. The scale exponent argument takes some experimentation to get used to, or you can just go with [expr], which gives you total control.
you can also use [scale] to simply convert to 0-127 (or maybe 0-158) and then use that to control a gain~, if your intent is to scale audio.
Here are some other options to make curve outputs from linear inputs. You can also just draw in whatever curve you want:
My old lecturer used to encourage converting one's scale to 0-1, then cube it, then scale and offset the result to get nice log behaviour
That's not logarithmic behavior, that's cubic behavior. Depending on your needs, the latter may, for certain values, be close enough to exponential to be a cheap substitute. But it ain't the same thing. Not never nohow.
And it certainly isn't logarithmic.