Expression: Audio fadeout start at other layers ending
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.
