Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Link frame values to audio amplitude

  • Link frame values to audio amplitude

    Posted by Xavier Bonet on September 25, 2020 at 8:48 pm

    I’m animating a series of lights that must respond to the background audio. So I’ve set up the animated lights in a sub-comp, each frame corresponding to one of 4 light levels. Then in my main comp I’ve converted audio to keyframes. I’ve activated Time Remap on my Lights comp and put this expression:

    levels = Math.round(thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider"));
    if ( levels <= 5 ) { 0 };
    if ( levels > 5 && levels <= 10 ) { 1 };
    if ( levels > 10 && levels <= 15 ) { 2 };
    if ( levels > 15 && levels <= 20 ) { 3 };

    However, when I run my animation, whenever the level is 5 or less frame 0 is shown, as it should, but for everything else frame 3 is shown…

    I’m sure I’m missing out something VERY basic here, but as it’s late at night I’m afraid no matter what I try I can’t fix this that should be rather straightforward.

    <font face=”inherit”>I’ve tried making the Lights comp 20 frames long and distributing the single frames along those 20 frames, then simply linking the Time Remap to the Slider… same issue: once it goes over 5 it gets stuck at 20 until the next time it goes back below 5. </font>

    <font face=”inherit”>I’ve even tried simply copying the Audio Amplitude slider keyframes and </font>pasting them onto the Time Remap with the exact same result.

    <font face=”inherit”>In all cases I’ve tried both </font>rounding<font face=”inherit”> the numbers </font>amplitude<font face=”inherit”> numbers and leaving them as they are.</font>

    <font face=”inherit”>I’m thinking it’s either a bug or the issue here is Time Remap. I know TR doesn’t play well with others, and as I experience every </font>other<font face=”inherit”> time when I want to use the LoopOut expression, or when sub-comps hace different animated layers TR is a real pain to work with, producing somehow overlapping frames, and out-ouf-sync layer animations.</font>

    P.S. As this animation will in the future be used to react to different audios, is there any easier way of doing this that doesn’t involve converting audio to keyframes, which is rather a “manual” thing; I would like to have a template comp where I could swap out the audio every time and the animation updates through the expression, of course.

    Thanks in advance!

    Filip Vandueren replied 5 years, 7 months ago 5 Members · 8 Replies
  • 8 Replies
  • Dan Ebberts

    September 25, 2020 at 9:04 pm

    Time remapping expects time in seconds, not frames. I’d do it like this:

    levels = Math.round(thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider"));

    if (levels <= 5)

    f = 0

    else if (levels <= 10)

    f = 1

    else if (levels <= 15)

    f = 2

    else

    f = 3;

    framesToTime(f);

  • David Henion

    September 25, 2020 at 9:06 pm

    Tried your expression and it worked, but usually I use [] as the return value from an expression.

    Could try to replace each { # ); with [ # ];

    Or try something like this:

    temp=0; // default value if levels is >20
    if ( levels <= 5 ) { temp=0 }; // not really needed
    else if ( levels > 5 && levels <= 10 ) { temp=1 };
    else if ( levels > 10 && levels <= 15 ) { temp=2 };
    else if ( levels > 15 && levels <= 20 ) { temp=3 };
    [temp];

    Alternatively, why not just drive the light’s intensity directly?

  • Xavier Bonet

    September 25, 2020 at 9:13 pm

    Of course! Now that you say it it sound so evident. Thanks for your response. This did the trick.

  • Xavier Bonet

    September 25, 2020 at 9:16 pm

    I’m trying your fix and it still doesn’t work for me. However, just by adding the framesToTime() expression to parse the temp of my levels value, it does the trick.

    Your idea of driving the opacity is interesting… Hadn’t thought of that. But now you said it, it gave me the idea to perhaps add some intermediate frames, where the next light gradually gets brighter, thereby obtaining a more fluid, realistic animation. Cheers for that!

  • David Henion

    September 25, 2020 at 9:37 pm

    Probably not drive the opacity, but intensity of each light – seems way more adaptable later. Driving a time remap can get messy if you later want to add more values.

  • Stephen Dixon

    September 26, 2020 at 4:24 am

    David, the square brackets turn the number into an array. Why do this? You’re converting to an array and then forcing the expressions engine to convert the array into an integer, which could lead to problems.
    The curly brackets form part of the if – else expression:

     if(condition){code block} else if (condition){code block} else {code block}

    You use them when there is more than one line in the block of code that gets executed if the condition is true or false. Older versions of the syntax required them for all if-then expressions, so you can use them for single line blocks without a problem.

    Curly brackets or square brackets have nothing to do with returning a value from an expression, they’re part of JS syntax.

  • David Henion

    September 26, 2020 at 11:50 am

    If I remember right, older versions of AE required all return values to be sent with brackets, even a scalar value like an integer. Maybe not needed but for me it keeps the code more readable. Have never had any problems with it.

    So I wasn’t replacing {} with [] – but was doing two separate things. If the {code block} only has one operation, as in the original post, the {} can be omitted. Secondly, just to try in case it would help, return the value via [].

    Essentially doing something like if (levels<=5) { [0] }; just omitting the {}

    When I tested the OP’s script I just had it drive a scalar value to see the output and didn’t test it driving a time remap – so it was returning the right values – I just forgot time remap uses seconds, not frames.

  • Filip Vandueren

    September 26, 2020 at 3:59 pm

    Hi David,

    always using [#] as a result of your expressions doesn’t make sense from a javascript code perspective.
    It’s actually a quirk in After Effects that that works at all, I don’t remember a time when it was obligatory ?

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy