Ducking Audio Expressions

Scripting for After Effects

Ducking Audio Expressions

Postby ngambles on 03/7/2009, 2:20 am

I got a PM from petApe after I posted my expression work to Does Sure Target 1.5 work with Soundtrack ? http://www.videocopilot.net/forum/viewtopic.php?f=15&t=10452 asking about fading audio out when a voice track starts talking. Apparently there was a lot of audio and fading it in and out by hand would have been tedious and time consuming.

So he asked if there was a way to do it with expressions and here's what I came up with.

Audio Ducking

Code: Select all
//////////// by Nathan Gambles /////////// March 2009 /////////////////////////


sourceAmp = effect("Audio Amplitude Layer")("Layer").effect("Both Channels")("Slider");     // if you use a plugin like Trapcode SoundKeys like that value here.

holdTime = effect("Padding")("Slider");       //Connecting all the Expression Controls
threshold = effect("Threshold")("Slider");
audPerc = effect("Volume Fade Percent")("Slider");
fadeInTime = effect("Fade In Frames")("Slider");
fadeOutTime = effect("Fade Out Frames")("Slider");

/////////////////////////////////////Fades In the decrease in audio.
function fIn( sAmp, thresh, hT, fiT){             
nextAud = null;      //Initialize a variable to hold the frame number of the next audio level over the threshold
myFrame = timeToFrames( );     //Create a variable that as the current frame value
while( myFrame <= timeToFrames( ) + hT + fiT ){     //This is the loop looking for the next audio level
if ( sAmp.valueAtTime( framesToTime( myFrame )) >= thresh){   
                                  // If one is found its' frame is saved in the variable nextAud
nextAud = myFrame;    //and we break out of the loop
break;
}else{
myFrame += 1;     //if one is not found then we keep looping until we find one or
}}            //reach the max fade in/out time + the padding
if ( nextAud == null ){
ansI = 0;      //If you didn't find one then don't fade at all
}else{      //Otherwise figure out how much to fade
ansI = linear( time, framesToTime( nextAud - hT - fiT), framesToTime(nextAud - hT), 0, 100 )
}
return ansI;     //gives back the answer when we calll the function
}

////////////////////////The Fade Out is the same exact method but looking backward

function fOut( sAmp, thresh, hT, foT ){        
lastAud = null;
myFrame = timeToFrames( );
while( myFrame >= timeToFrames( ) - hT - foT ){
if ( sAmp.valueAtTime( framesToTime( myFrame )) >= thresh){
lastAud = myFrame;
break;
}else{
myFrame -= 1;
}}
if ( lastAud == null ){
ansOut = 0;
}else{
ansOut = linear( time, framesToTime(lastAud + hT), framesToTime( lastAud + hT + foT), 100, 0 )
}
return ansOut;
}

//////////////////////////////////////////////

//we can now use these functions and save the values that
//were returned to varialbes that we can use.

ansO = fOut (sourceAmp, threshold, holdTime, fadeOutTime);
ansI = fIn(sourceAmp, threshold, holdTime, fadeInTime );

percent = Math.max( ansO, ansI ) / 100 ;   
     //Sometimes the values are fading in while the
           //other is fading out and the values cross eachother.
          //In that case I want to keep the higher of the two values
      // or the one that makes the bg audio the quietest. 

           //The rest is just getting all the numbers to play nice
           //together to give a final result

100 - audPerc * percent + value

(Sorry I added code hinting to the code and it makes it look terrible in this window. Copy it over to after effects and it should be much more readable and helpful.)

I used "Convert Audio to Keyframes" for the voice over audio layer I was using. I got something that looked like the image below, minus the colored reference lines I added to help visualize my thought process.
Image

I applied these expression sliders to the audio layer that I was fading in and out.
Image
The last effect is the audio effect -> Stereo Mixer. I applied the above expression to the left channel level then pick-whipped the right level to the left one.

Expressions and Frames
Using expressions is interesting because every calculation starts over at each frame so you can't use them to keep track of what the last value you found was directly but that doesn't stop us from cycling back through past frames or future frames for that matter to get the data we want at that one frame. When we move to the next frame we have to look forward again and backward again and figure out this current value.

Setup some Sliders
So looking at my audio keyframes above and the effect sliders i added I decided I wanted some padding to help smooth out the sharp peaks of the audio.(Those are the green lines in the illustration above) I also wanted to be able to control the speed of the audio fade out and the speed of the fade back in. (The blue lines) There's also a slider to control the percent that the audio decreases and one to set the audio threshold (the yellow line).

One Part at a Time
I thought about it and determined it would be easiest to worry about just the fade out part first.

(The code I wrote was actually for fading in the decrease in audio, which is the same thing but in the code I called it fIn( ) for 'Fade In' because that's how I was thinking about it at the time. Hope that's not too confusing).

At the current frame look forward the number of frames you want to fade in plus your added padding.
Start looking at the current frame and move forward until the first one over the threshold is found then you can stop looking (or break; out of the while loop).

Now you know how many frames you have to go before you reach the first audio that's over the threshold and using the linear( ) function you can determine how much you should be fading out at that frame.

If you don't find any keyframes above the threshold then you won't need any fading out. If you find one on the first frame (the current frame) then the audio is faded all the way out.

Not to abstract I don't think. There is code hinting in the expression to help you through it so I won't repeat it again here.

Do it again backwards
Now to get the fade back up all you need to do is apply the same logic but this time start at the current time and look backwards to see how long it's been since there was audio over the threshold. Wham - bam... linear( ) does the trick again and you know how much you should be fading the audio back up.

Developments
I created the expression for the fade out and the fade in on separate layers at different times.

It's always best to break you work up into the smallest pieces that you can. Honestly when I look at the code above I think it's way to much to look at but that's because I started small, got it working, added the next piece, and the next piece, and finally it all came together. The point is don't get distracted or discouraged by the length of the code.

That's the reason that I broke it up into two functions ( ). I did them at different times and it makes them easier to work with and for other to use if they are so inclined.

Wrap it up
We have the fade in and fade out amounts but sometimes one is fading in while one is fading out like can be seen in my illustration above highlighted by the red circle. This is why we use the Math.max( val1, val2) function.

We will always be fading in or fading out meaning one of the functions will always be working and we only have to pay attention to which one is the "loudest", or in our case the one that keeps it the quietest.

The rest of the code takes 100 % audio and subtracts the calculated percent of the desired audio attenuation and for good measure adds the pre-expression value back in. This still makes it possible to easily control the levels with the Left Level Slider if you need. (Set it to zero initially though so you start working at 100% audio scale.)

There it is. If anyone is still reading I hope it help further the understanding the method behind creating some useful expressions, as well as the code for creating functions in expressions.

You don't need to read or understand what I wrote to get it to work.
Setup Steps Again
Just get your audio layers together.
Convert the voice over to keyframes.
apply the expression controls to the bg audio layer
link them all up like in the picture
adjust the settings to your liking
and you should have some sweet audio ducking happening right inside of After Effects

That was for you petApe. Hope it helps someone.

Cheers
ngambles
 
Posts: 1216
Joined: 09/20/2008, 12:47 pm
Location: Colorado Springs, CO USA

Re: Ducking Audio Expressions

Postby illuminatus23 on 03/7/2009, 5:51 am

Absolutely great man!
Tough project!
illuminatus23
 
Posts: 88
Joined: 02/23/2009, 3:47 pm

Re: Ducking Audio Expressions

Postby tequilaman2006 on 03/14/2009, 2:07 pm

this is really cool-- think it deserves a bump! great job
tequilaman2006
 
Posts: 163
Joined: 08/2/2008, 11:54 pm
Location: the mindless downloads of america

Re: Ducking Audio Expressions

Postby Andrew Kramer on 12/7/2010, 3:45 pm

Pretty cool.
Andrew Kramer
VC Awesome Guy
VC Awesome Guy
 
Posts: 1201
Joined: 01/11/2008, 12:54 pm
Location: Southern California

Re: Ducking Audio Expressions

Postby Michael_Szalapski on 12/7/2010, 4:05 pm

Bumped by The Man Himself, well done.
______________
Michael Szalapski
Fast. Cheap. Good.
Pick two.
Michael_Szalapski
 
Posts: 8287
Joined: 02/2/2010, 1:06 pm
Location: Nashville, TN, USA
Twitter: thegreatszalam

Re: Ducking Audio Expressions

Postby Rob Neal on 12/7/2010, 6:12 pm

Very impressive stuff, but dear God, why?
Wouldn't it just have been easier to use some audio software and used a simple gate?
"Experience is what you have after you've forgotten their name. - Milton Berle"


http://www.robneal.co.uk
Rob Neal
 
Posts: 7164
Joined: 09/30/2008, 4:29 am
Location: London, England

Re: Ducking Audio Expressions

Postby J450NPIE on 12/7/2010, 6:21 pm

Are you going to release this as a preset or plug-in like AK does, so people using it wouldn't have to go through and basically do all that you did. Like entering the expression on every audio clip and creating sliders, and so on and so forth......
"If you want peace-prepare for WAR."
J450NPIE
 
Posts: 1516
Joined: 12/18/2009, 12:33 pm
Location: Metro-Detroit

Re: Ducking Audio Expressions

Postby ngambles on 12/8/2010, 10:03 pm

Wow! We're pulling this one out of the archives.

Rob Neal wrote:Very impressive stuff, but dear God, why?
Wouldn't it just have been easier to use some audio software and used a simple gate?

I suppose it's always easier to have someone else make the tools and then just use them, but that's not as interesting. And now you don't need another program.

J450NPIE wrote:Are you going to release this as a preset or plug-in like AK does, so people using it wouldn't have to go through and basically do all that you did. Like entering the expression on every audio clip and creating sliders, and so on and so forth......

I suppose I could look into that. I'll have to brush up on what I was doing 2 years back. I'll see what I can do.

Cheers
ngambles
 
Posts: 1216
Joined: 09/20/2008, 12:47 pm
Location: Colorado Springs, CO USA

Re: Ducking Audio Expressions

Postby Michael_Szalapski on 12/9/2010, 9:32 am

ngambles wrote:Wow! We're pulling this one out of the archives.

Yeah, blame that silly Kramer kid for that. It was his fault.
______________
Michael Szalapski
Fast. Cheap. Good.
Pick two.
Michael_Szalapski
 
Posts: 8287
Joined: 02/2/2010, 1:06 pm
Location: Nashville, TN, USA
Twitter: thegreatszalam

Re: Ducking Audio Expressions

Postby ontto on 12/27/2010, 4:04 pm

Whoa, great job! I love the feeling when getting expression working after though thinking.
ontto
 
Posts: 12
Joined: 10/7/2010, 11:21 am

Re: Ducking Audio Expressions

Postby ngambles on 12/29/2010, 1:35 am

J450NPIE wrote:Are you going to release this as a preset or plug-in like AK does, so people using it wouldn't have to go through and basically do all that you did. Like entering the expression on every audio clip and creating sliders, and so on and so forth......
Attached, please find an updated custom preset with the Audio Ducking code explained above.

To use:
  • On audio layer, use 'Keyframe Assistant -> Audio to Keyframes' to create layer
  • Apply 'Audio Ducking' preset to your audio layer
  • select the layer created by the keyframe assistant as the 'Audio Keyframe Layer'
  • adjust other 'Audio Ducking' parameters as desired
Screen shot 2010-12-29 at 2.07.18 AM.png


Below is the PresetEffects.xml code used for the custom effect. This code can be added to the PresetEffects.xml file to prevent the 'Missing' error from popping up on the custom effect, but it is not necessary for proper functionality. Just thought I'd include it in case someone is interested.
Code: Select all
<Effect matchname="AudioDuck" name="AudioDuck">
     <Layer name="$$$/AE/Preset/Layer=Audio Keyframe Layer" default_self="true"/>
   <Group name="$$$/AE/Preset/Channels=Target Channel(s)">
      <Checkbox name="$$$/AE/Preset/bothC=Both Channels" default="true"/>
      <Checkbox name="$$$/AE/Preset/leftChannel=Left Channel" default="false"/>
      <Checkbox name="$$$/AE/Preset/rightChannel=Right Channel" default="false"/>
   </Group>
    <Slider name="$$$/AE/Preset/Threshold=Threshold" default="20.0" valid_min="0.0" valid_max="10000.0" slider_min="0.0" slider_max="100.0"/>
   <Slider name="$$$/AE/Preset/FadeIn=Fade Frames In" default="10.0" valid_min="0.0" valid_max="10000.0" slider_min="0.0" slider_max="100.00"/>
   <Slider name="$$$/AE/Preset/Padding=Hold Frames" default="2.0" valid_min="0.00" valid_max="10000" slider_min="0.00" slider_max="100.00"/>
   <Slider name="$$$/AE/Preset/FadeOut=Fade Frames Out" default="5" valid_min="0.00" valid_max="10000" slider_min="0.00" slider_max="100.00"/>
   <Slider name="$$$/AE/Preset/VolumePercent=Volume Fade Percentage" default="40.0" valid_min="0.00" valid_max="100" slider_min="0.00" slider_max="100.00"/>
</Effect>


Cheers
Attachments
AudioDuck_v2.ffx.zip
Audio Ducking custom effects preset.
(2.98 KiB) Downloaded 1405 times
ngambles
 
Posts: 1216
Joined: 09/20/2008, 12:47 pm
Location: Colorado Springs, CO USA

Re: Ducking Audio Expressions

Postby vercinger on 07/15/2012, 9:29 pm

I've been trying to get some sort of audio ducking to work with Premiere Pro CS5 for +10 hours, but I was told that AE had an easy way to do this, but apparently it doens't work for me either and I've been stuck in AE for +5 hours now with this Animation Preset .. :(

I've tried to apply this SO many times but nothing happens..

Using AE CS5 this is how I try and do this:

1. Take my narration track and "keyframe assistant > convert audio to keyframe"
2. Then I select the clips that I want the audio to be ducked on
3. Animation > Apply Animation Preset > AudioDuckv2
4. Select Audio KeyFrame Layer to the created keyframe layer

Then I test it out, but alas, no audio ducking has been performed..

Has it something to do that the audio I'm trying to duck is on .avi and not .wav, or can anyone else tell me what I'm doing wrong? I am desperaaateeee :(
vercinger
 
Posts: 2
Joined: 07/15/2012, 9:19 pm

Re: Ducking Audio Expressions

Postby ngambles on 07/16/2012, 7:30 pm

Most likely what is happening is that all your audio is above or below the "threshold" default limit in the preset. You'll need to look at your keyframe values on the "audio amplitude" layer and determine what value to set the threshold to. Different audio will have different values.

Are your "Left Level" and "Right Level" always 100% or 60%, etc...
Screen shot 2012-07-16 at 8.32.17 PM.png
If so then your threshold is not appropriately set.

For example:
Screen shot 2012-07-16 at 8.25.43 PM.png
For audio like this I'd use a threshold of approx. 60 or 65 to get just the loudest parts, which is significantly different than the default of 20. If you leave the default threshold for this audio the expressions will always bring the audio down because it is all over 20.

Other possible issues:
  • You're not using an English Version of After Effects and the expressions don't work because of this.
  • You don't have the "Stereo Mixer" audio plugin installed.

**Note**
The example above uses audio from a video clip, so the issue is not that you are getting the audio from an avi file.

Cheers
ngambles
 
Posts: 1216
Joined: 09/20/2008, 12:47 pm
Location: Colorado Springs, CO USA

Re: Ducking Audio Expressions

Postby vercinger on 07/20/2012, 11:45 am

I have 0 plug-ins installed for After Effects, so I guess that might be it. So far I've been using Audacity (audio editing tool) to duck audio :)

Thanks for the reply, I'll try it out next time I have a project that needs audio ducking :)
vercinger
 
Posts: 2
Joined: 07/15/2012, 9:19 pm

Re: Ducking Audio Expressions

Postby KenzoNL on 03/14/2013, 2:25 pm

Ngambles, thanks a lot for the preset. It's working smoothly.
No more Audition for audioducking & voice-overs!
KenzoNL
 
Posts: 1
Joined: 03/14/2013, 2:21 pm


Return to After Effects Expressions