Skip to main content
Inspiring
April 11, 2023
Answered

Expression: Audio fadeout start at other layers ending

  • April 11, 2023
  • 2 replies
  • 451 views

Hey there!

I have an expression that's not really working properly (sometimes it does, sometimes it doesn't). Hope to find some suggestions to improve it here...

The expression should be able to do the following things:

  • fade out over 1s (needs to be adjustable).
  • fade out position is the end of another layer within the same composition. This layer is targeted by a ^ in the audio layer name. Ex: «audio ^6» The fade out of this audio starts where the comp on layer 6 ends.
  • fade out position needs to be shiftable by some seconds forwards or backwards.

 

Example: I have various layers in a timeline. One of them is a composition that ends at second 15, one is an audio layer with at least 60s of length. The 15s composition is on layer 6, the audio file is on layer 10. The expression on the Audio Levels property checks its own name that's called «audio ^6». It sees the «^» and targets the layer after that - in that case layer 6, the 15s composition. It checks where that composition ends and starts its fade out at that point.

My code right now looks like this:

var sourceLayer = thisComp.layer(parseInt(thisLayer.name.split("^")[1])); // searching for ^and targeting the layer number.
var fadeDuration = 2; // fad-out length.
var offset = 0; // offset of the fade-out.
var fadeOutStart = sourceLayer.outPoint - offset - (fadeDuration / thisComp.frameDuration);
if (time >= sourceLayer.outPoint + offset) {
value = linear(time, sourceLayer.outPoint + offset, sourceLayer.outPoint + offset + fadeDuration, [0,0], [-100,-100]);
} else {
value = [0,0];
}

 

In some compositions the expression works like a charm, Sometimes it gets the fade out wrong by a few seconds and sometimes a fade out isn't even on the timeline anymore. I can't tell, where the error is.

Does anyone have an idea? Or a better workaround? Maybe I'm just stuck within my existing expression...

I'm working on After Effects 2023. The expression should be working back to After Effects 2021.

This topic has been closed for replies.
Correct answer Dan Ebberts

You're right about the offset. This should work better:

var sourceLayer = thisComp.layer(parseInt(thisLayer.name.split("^")[1])); // searching for ^and targeting the layer number.
var fadeDuration = 2; // fad-out length.
var offset = 1; // offset of the fade-out.
linear(time, sourceLayer.outPoint + offset, sourceLayer.outPoint + offset + fadeDuration, value, [-192,-192]);

2 replies

Dan Ebberts
Community Expert
Community Expert
April 11, 2023

I think this does what you want:

var sourceLayer = thisComp.layer(parseInt(thisLayer.name.split("^")[1])); // searching for ^and targeting the layer number.
var fadeDuration = 2; // fad-out length.
var offset = 0; // offset of the fade-out.
linear(time, sourceLayer.outPoint, sourceLayer.outPoint + offset + fadeDuration, value, [-192,-192]);
Inspiring
April 11, 2023

Hi Dan!
Thanks for your approach. That's correct - it really works better than my initial expression.

I also tried another workaround and got this solution that worked in my sample project so far:

fadeDuration = 2; // duration of fade (in seconds)
startLevel = value; // starting audio level
layerName = thisLayer.name; // get name of current layer
targetIndex = parseInt(layerName.split('^')[1]); // get index number from layer name
delayLayer = thisComp.layer(targetIndex); // layer to delay the fade out until
delayTime = delayLayer.outPoint; // time at which the delayLayer ends
delayTime += 0; // offset the delay time by -2 seconds

if (time < delayTime){
    startLevel;
} else {
    linear(time, delayTime, delayTime + fadeDuration, [startLevel[0], startLevel[1]], [-75, -75]);
}

 

It's quite a monster but right now, it does what it should.
In your code the offset didn't work for me.


Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
April 11, 2023

You're right about the offset. This should work better:

var sourceLayer = thisComp.layer(parseInt(thisLayer.name.split("^")[1])); // searching for ^and targeting the layer number.
var fadeDuration = 2; // fad-out length.
var offset = 1; // offset of the fade-out.
linear(time, sourceLayer.outPoint + offset, sourceLayer.outPoint + offset + fadeDuration, value, [-192,-192]);
Mylenium
Legend
April 11, 2023

Your string parsing code doesn't make much sense. You have nothing in there that actually looks for the "audio" prefix or whatever else you were to use and just look for a number. Inevitably, this may then match to multiple layers that have e.g. a 6 somewhere in their name. You need to harden your code with proper string processing/ a regular expression and include the prefix somewhere.

 

Mylenium

Inspiring
April 11, 2023

Hi Mylenium!
Thanks for your answer!

My goal is that the expression does not search the name of a layer, but the layer number (#). This way it wouldn't matter what the name of a layer is and the expression has a clear target.