Search code examples
extendscriptafter-effects

AE Extendscript - exponential autofade expression for audio


I have been using a linear auto-fade expression to auto-fade the end of my background music by just adding the expression below:

fadeTime = 9; 
audio.audioLevelsMin = -50; 
audio.audioLevelsMax = 0; 
layerDuration = outPoint - inPoint; 
singleFrame = thisComp.frameDuration; 
animateOut = linear(time, (outPoint - fadeTime+1), (outPoint-singleFrame), audio.audioLevelsMax, audio.audioLevelsMin); 
[animateOut,animateOut];

However, this is just a linear fade. I'd like to create an exponential fade. Is this even possible in expressions?

Exponential is probably not completely the right word - so for lack of knowing the right word, I'm posting an image below to show what I mean:

enter image description here


Solution

  • The fade you want looks like y=0 - (x^3) between the values of x=-1 and x=1.

    enter image description here So we map the fade to that range by creating a normalised variable t which goes from -1 to 1, and then cube it, and then map that back to the range 1-0

    enter image description here

    fadeTime = 9; 
    levelsMin = -50; 
    levelsMax = 0; 
    
    //not sure why you want to add an extra second to the fade time, but I included the + 1
    fadeStart =  outPoint - fadeTime+1; 
    
    t = linear(time, fadeStart, outPoint, 1, -1); //t goes from 1 to -1 over the fade
    fade = 0.5 + Math.pow(t, 3)/2; // goes from 1 to 0, but with the curve you want
    // map 1 → 0 ⇒ levelsMax → levelsMin
    finalLevel = levelsMin + fade * (levelsMax - audio.audioLevelsMin);
    [finalLevel, finalLevel]