All Articles After Effects
Animating with Sound in After Effects, Part 2
Toby Pitman on Sun, July 10th 0 comments
It's a lot of fun using sound to drive animations in After Effects. In Part 2 of this series, Toby Pitman takes you deeper into Arrays, Variables and Controls to create stunning visual effects.

In Part 1 we looked at how we could animate parameters in After Effects by using keyframes that were generated by an audio track using 'Convert Audio To Keyframes'. In those examples we only dealt with parameters that had one value. 

So what happens when we need or want to control parameters that have two or even three values like say Position (X,Y, Z) or Scale (X, Y). Well for this we need to dive into Arrays in our expressions. We'll also look at how we can use Variables to simplify and make our work more readable.

Using these functions combined with expression sliders we can gain more control over our animations.


If you want to access the individual values of a parameter in After Effects with expressions you'll need to separate them out. Here's what I mean. 

You can see the Position values for a Solid in my comp. The first value is X, the second Y.

If I try to pick whip the position of this Solid to the keyframes generated from my audio (see Part 1) two things are going to happen.

My original position values will be overwritten by the Audio Amplitude keyframe values. Both the X and Y positions will be the same. So my solid will only move in a diagonal direction.

So how do I access just the X or Y? Well I'll need to create an Array in my expression. Here's how it works. An Array is a list of values.

The simplest way to explain the syntax for an Array would be - 

[ X , Y ]

It's not that simple to start with as we need to tell After Effects the position of the values in the array. This is done by adding a number in brackets, these numbers always start from 0. So 0=1, 1=2, 2=3 etc...

That would look like this..

[ X[0] , Y[1]]

If I wanted to express the current positional values separately for my solid in an expression I would write this...

[ value[0] , value[1] ]

value being the After Effects term for the, well...the current value! In our case 250, 140.5.

The clever thing about this is I can now add my audio keyframe values to any of the separate position parameters by pick whipping from inside the array. 

The audio keyframes are now only affecting the Y values.

The beauty of going this route is that I still have full control of the overall position using the position values on the layer as the audio keyframes are now just being added to (or offsetting) them.

Here's an example. I'll animate the X position from left to right and the Y from bottom to top and back using plain keyframes and you can see how this is working.

Note: I've added *-1 to the end to invert the values so the solid moves up (not down). 

Here's the result:

You can use this on any parameter that has these array type values. Just to make it more obvious, if my layer was 3D I'd have X, Y and Z. I could then target them like this...

[ value[0] , value[1], value[2] ]


Now if I keep pick whipping into this array it's going to turn into a bit of an unreadable mess which could lead to me making an error so I'm going to clean this up using some variables.

A variable stores a value and that value is referenced by the variable name. Kind of like a shortcut. You store a variable by giving it a name and making it equal to something.

Let's say I write this:

x = value[0]; 

y = value[1];

I could now write my array like so...

[x, y]  this is now the same as  [ value[0] , value[1]]

Now let's say I store my audio keyframe value into another variable called audio.

audio = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider")*-1

My expression could now be written:

x = value[0];

y = value[1];

audio = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider")*-1;

[x, y+audio]

This is much clearer to read and will help later on as we start to add more code. Speaking of which...

Adding Controls

So now I want to add some controls to this so I can choose which direction I want my solid to move in. This is pretty easy to do using some handy Expression Sliders.

With your Solid selected go to Effect > Expression Controls > Slider Control.

Name it (Press Enter) "Y Direction" and Control-Click on the slider to edit the values.

Put in '-1' for the Min value and '1' for the Max value. You can now remove the *-1 from the audio variable as this will be replaced by our Y Direction slider value. At negative values the solid will go up to the music and at positive values the solid will go down. I know this may appear counter-intuitive but it's just the way the canvas works in After Effects.

So now I'll create a variable called "directionY". After an = sign I'll pick whip it to the Y Direction slider:

I now have this...

directionY = effect("Y Direction")("Slider");

In my array setup I can now change it to this...

[x, y+(audio*directionY)] 

I've put the audio*directionY in brackets so it does the multiplying first before it adds it to the value. Here's what we have now.

Now all I have to do is make a slider for the X direction. I just repeat the process that I did for the Y Direction slider. I then create the variable and pick whip it to the X Direction slider...

directionX = effect("X Direction")("Slider");

Then add the sum to the array...

[x+(audio*directionX), y+(audio*directionY)]

All done! Here's the full expression.

I can now fully control the direction of the animated solid using my new sliders. Here's the solid moving up and right.

And down and left...all in time to the beat.

Here's the solid's position being animated with the sliders.

Now this is a pretty simple example but you can apply this sort of stuff to anything you can think of! Here's another quick expression that uses the 'random()' function instead of the sliders.

x = value[0];

y = value[1];

audio = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");

ranX = random(-1,1);

ranY = random(-1,1);

[x+(audio*ranX), y+(audio*ranY)]

This time a similar expression is linked to the emitter of the Particle for quite a nice random effect.

Got that After Effects itch? Don't scratch it, watch these After Effects tutorials instead!

Related Videos
Comments (0)

You must be logged in to comment.

What's New in Logic Pro 10.7
Logic Pro 100
Dream It. Do It.
Do you want to learn What's New in Logic Pro 10.7?
Yes, I want to learn!
No Thanks, I just want to read the article.
Course Advisor
Don't Know Where To Start?
Ask A Course Advisor
Ask Us!
Copy the link below and paste it into an email, forum, or Facebook to share this with your friends.
Make money when you share our links
Become an Ask.Video Affiliate!
The current affiliate rate is: 50%
Classes Start Next Week!
Live 8-week Online Certification Classes for: