Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Expression to change color of a text layer based on amplitude of audio layer

  • Expression to change color of a text layer based on amplitude of audio layer

    Posted by Alfred Hammer on August 6, 2017 at 5:48 pm

    Hello,

    I’m trying to create a text layer that changes color with every snare drum hit using a while loop that increments a flag variable when the amplitude crosses a certain threshold and then use an if else if ladder to assign different rgb values for each of the flag values. It doesnt work at all as intended, it just switches between the colors. I’m a beginner with expressions and cant for the life of me figure out what is wrong as the logic seems to be okay. I would be extremely grateful for a correction or an alternate approach.

    Thank you

    //x<10000 to prevent ae from thinking this is an infinite loop and getting a time out
    //c to account for the fact that a drum hit lasts for more than 1 frame


    op=thisComp.layer("Amp").effect("Sound Keys")("Output 1");
    flag=0
    x=0
    c=0

    R=1
    G=1
    B=1
    A=1

    while(x<10000)
    {

    if((op>60)&&(c==0))
    {
    flag++;
    c=1;}
    else if((op<60)&&(c==1))
    {
    c=0;
    }

    if(flag==0)
    {
    R=0
    G=0.8
    B=linear(op,0,80,.4,.8);
    A=1
    }
    else if(flag==1)
    {
    R=1
    G=0
    B=0
    A=1
    }
    else
    {
    R=0
    G=0
    B=0
    A=0
    }
    x++
    [R,G,B,A]
    }

    Alfred Hammer replied 8 years, 8 months ago 2 Members · 4 Replies
  • 4 Replies
  • Dan Ebberts

    August 6, 2017 at 6:31 pm

    I can’t tell exactly how you want this to work, but it appears (forgive me if I’m wrong) that you may not be aware that expressions have no memory. That is, nothing persists from frame to frame. At any given frame, you can look at past (or future) pre-expression values of the property using valueAtTime(), but you can’t save any calculations for later use.

    Dan

  • Alfred Hammer

    August 6, 2017 at 6:50 pm

    Yipes, no memory perfectly explains the issue I’ve been having. If you had to assign a unique defined fill color to a text layer whenever a particular condition is met(in this case amplitude of audio layer increasing 60), how would you do this? Is it even possible without memory?

    Thank you so much for your time

  • Dan Ebberts

    August 6, 2017 at 7:04 pm

    The trick is, at each frame, you have to figure out if an event you’re looking for has happened in the past, and if so, how long ago did it happen? The only tool you have to do this is valueAtTime(). You can set up a loop that starts at the current time and goes back in time, frame by frame, looking for the start of the event. It’s essentially what I do here:

    https://motionscript.com/design-guide/audio-trigger.html

    Your application may be less complicated, depending on what you need it to do.

    Dan

  • Alfred Hammer

    August 11, 2017 at 11:25 am

    Managed to get it to work after going through your tutorial. Thanks you Dan

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