Sync animation to music using After Effects expressions

During the making of the music video Let It Carry You by José González, I needed a way to animate a lot of objects and sync them to music. I would later handle of the file to another person that wasn’t familiar with After Effect’s expressions, so I needed a simple UI to control parameters. Here’s how I did it.

Convert audio to keyframes

For the best result, you need to have your music or sounds in separate files, like guitar in one track, bass drum in another track and so on. If you want you can download the sample files in this tutorial here.

1. First you need to import all you sound/music assets and put them in seperate compositions.
1. First you need to import all you sound/music assets and put them in separate compositions.

 

02-convert-audio-to-keyframes
2. Open up each sound comp, right click on the audio layer’s source name and choose Keyframe Assistant > Convert Audio to Keyframes

 

03-audio-keyframes
3. This will create a null object with an effect on it. This effect contains the sliders – Left Channel, Right Channel and Both Channels (if the sound is in stereo). Your could now connect a lot of properties to those sliders to control scaling, position, opacity, rotation or any other numerical values. Next we will connect it to scale our example graphics.

Connect to different values

Start with creating the object you want to trigger. We could place this object in another comp, but to keep it simple we’ll putt it in the same comp as the sound. Make sure the graphics anchor point is in center (or the point where you want the graphic to be scaled from). Press Y and drag the circle that looks like a aim to move the anchor point.

1. Open up scale on the graphic by pressing S, and ALT-click on the stopwatch to enter the world of expressions.

 

2. Take the pick wip in scale and drag it Slider in Left Chanel. This will create an expression that connects the values from the keyframe slider to the scale. (If you don’t see the pick whip, make sure the Layer Switches pan-button is selected in the bottom left). If the source sound is different in the left and right channel, we could even connect them to both, affecting only the scale X with the left channel and scale Y with the right. Like this:
x = thisComp.layer("Audio Amplitude").effect("Left Channel")("Slider"); //Get the left channel
y = thisComp.layer("Audio Amplitude").effect("Right Channel")("Slider"); //Get the right channel
[x, y] //Apply them to the X and Y scale axis.

 

This is what it looks like with a kick drum connected to the triangle’s scale property. As you see, the triangle disappear completely when there is silence in the sound, maybe not exactly as we wanted. And if we would have scaled the layer before, it would now be reset.


Adjust the output from the audio sliders

If we instead want to start the animation from the original value, we’ll have to add some extra expressions.

Open up the expression field for scale in the triangle-comp, and copy this:

x = thisComp.layer("Audio Amplitude").effect("Left Channel")("Slider"); //Get the left channel
y = thisComp.layer("Audio Amplitude").effect("Right Channel")("Slider"); //Get the right channel
[value[0]+x, value[1]+y] //Apply them to the X and Y scale axis.

What we added here is the

[value[0]+x, value[1]+y]

The value is a predefined array that contains the original values of the scale properties. Eg if we had set our scale values to scale X: 10 and scale Y: 7, then value would be value[10, 7]. The first value ( value[0] ) is the x-axis and value[1] is the y-axis. So when we write value[0]+x, we say that –Take this layer’s scale-x value and add the value of the variable x to it. 

 

We could also controll how much of the audio-sliders values we would like to add to the layer by multiply the variables x and y. To do this we could just do

x = x * 0.5

This will cut the x value in half.

The complete code:

x = thisComp.layer("Audio Amplitude").effect("Left Channel")("Slider"); //Get the left channel
y = thisComp.layer("Audio Amplitude").effect("Right Channel")("Slider"); //Get the right channel
x = x * 0.5; //Half of the original value
y= y * 2; //Dubble of the original value
[value[0]+x, value[1]+y] //Apply them to the X and Y scale axis.

Adjust the output from the audio sliders with slider controlls

A more user friendly way of controlling the amount of effect from the audio sliders is to add a slider controll, instead of multiplying the values.

1. Drag a slider controller onto the Triangle layer from the Effect & Presets panel and rename it to Bass drum
Connect value to slider controll
2. Place the caret in the code where you want the value from the slider to be, and drag the pick whip to the slider control.
x = thisComp.layer("Audio Amplitude").effect("Left Channel")("Slider"); //Get the left channel
y = thisComp.layer("Audio Amplitude").effect("Right Channel")("Slider"); //Get the right channel
x = x * effect("Bass drum")("Slider"); //Multiply the value with a slider control
y= y * effect("Bass drum")("Slider"); //Multiply the value with a slider control
[value[0]+x, value[1]+y] //Apply them to the X and Y scale axis.

Now you can adjust the value of the effect with the slider control, and even animate the amount:

 

And that’s it! Something you could do to make it easier to work with, is to move the slider controls to the main comp so you can control a lot of different objects in the same place. Another thing you may want to do, is to smooth out the keyframes generated in the audio with the smoother effect. But that will be another tutorial.

I hope you learned something from this tutorial, and please don’t hesitate to write a comment if you wonder something, or send me a tweet @jonassandstedt.

 


Posted

in

,

by

Tags:

Comments

3 responses to “Sync animation to music using After Effects expressions”

  1. Jonas Sandstedt Avatar

    If you don’t wanna write any code, I can recommend bying Trapcode Sound Keys instead 🙂 https://www.redgiant.com/user-guide/trapcode-sound-keys/

  2. Ghost Avatar

    Wonderful! Thank you! Still works for CS6 the UI just looks a little different and the expression panel is smaller by default. But, I was able to get it to work. Thanks a bunch.

    1. Jonas Sandstedt Avatar

      Happy to hear it still works!

Leave a Reply

Your email address will not be published. Required fields are marked *