Page 1 of 1

Min/Max value

Posted: March 19th, 2006, 8:43 am
by hollow man
Hi all,

I've the scale of a layer animated with an audio expression.

I want to specify a minimum and maximum value.

How can I do that with this expression:

Code: Select all

temp = thisComp.layer("Amplitude audio").effect("Les deux couches")("Curseur");
[temp, temp]
Thanks :D

Posted: March 19th, 2006, 12:51 pm
by vidpat
To make sure a value is within a certain range, you could use a conditional statement or a combination of the min() and max() functions. Any of the following approaches are equivalent.

Code: Select all

var temp = thisComp.layer("Amplitude audio").effect("Les deux couches")("Curseur");

var min = 0.2;
var max = 0.8;

// conditional statements
if (temp < min) {
    temp = min;
}
else if (temp > max) {
    temp = max;
}

//     or
// ternary operators
temp = ((temp < min) ? min : ((temp > max) ? max : temp));

//     or
// Math functions
temp = Math.min(Math.max(temp, min), max);

[temp, temp];


Posted: March 19th, 2006, 1:54 pm
by hollow man
Thanks a lot vidpat!

It works perfectly! :D

Posted: March 19th, 2006, 3:14 pm
by Paul Tuersley
A couple of other things you might find useful. To clip the values to a certain range you can also use:

Code: Select all

var temp = thisComp.layer("Amplitude audio").effect("Les deux couches")("Curseur");
temp = clamp(temp, 0.2, 0.8);
[temp,temp];
But I usually use this linear expression alongside 'Convert Audio to Keyframes'. It provides a simple way of converting one range of values to another:

Code: Select all

var temp = thisComp.layer("Amplitude audio").effect("Les deux couches")("Curseur");
temp = linear(temp, 0, 10, 50, 100);
[temp,temp];
So that example would convert the audio values between 0 and 10 into scale values of 50 to 100.

Posted: March 20th, 2006, 10:11 am
by Mylenium
Paul Tuersley wrote:A couple of other things you might find useful. To clip the values to a certain range you can also use:

Code: Select all

var temp = thisComp.layer("Amplitude audio").effect("Les deux couches")("Curseur");
temp = clamp(temp, 0.2, 0.8);
[temp,temp];
But I usually use this linear expression alongside 'Convert Audio to Keyframes'. It provides a simple way of converting one range of values to another:

Code: Select all

var temp = thisComp.layer("Amplitude audio").effect("Les deux couches")("Curseur");
temp = linear(temp, 0, 10, 50, 100);
[temp,temp];
So that example would convert the audio values between 0 and 10 into scale values of 50 to 100.
Yepp, I'm definitely with you here. Conditional statemants are so *argh* for this type of stuff.

Mylenium

Posted: April 5th, 2007, 5:55 pm
by byronnash
Is there a way to scale the values instead of clipping them? Basically the equivalent of linear() but without any manual interaction.

Posted: April 6th, 2007, 3:46 am
by Mylenium
byronnash wrote:Is there a way to scale the values instead of clipping them? Basically the equivalent of linear() but without any manual interaction.
Mmh, not that I know of. But you could cycle thru all keys (while (time >= thisComp.duration){})and find the min/max values and then use them in your linear().

Mylenium