Continuing with some of the concepts we covered in my recent Expressions Basics Quick Tutorial, in this article we'll be looking at using Expressions as a simple and easily tweakable way to tie the amplitude of an audio layer to another layer. In this case, we'll be using a rhythm track to drive the needle of the same kind of VU meter we used in the Expressions Basic tutorial. This time though, because the actual amplitude of the audio track will be directly driving the motion of the meter, the end result will be a realistic and accurate meter VU meter animation. Let's get started.
We'll start by making a simple meter needle that we can drive with our audio track. We could make something more elaborate, with proper markings and a more realistic needle, or even cut up an actual photo of a meter in Photoshop, but I'll leave those details to you. What we'll show you here is easily translatable to a more realistic image.
To start, drag your audio track onto the Create a New Composition to make a new comp the length of your track. I'm using a short loopable funk bass and drums track:
You can use anything you like, of course.
Next add a new long, narrow Solid to the comp, called Needle. I've made mine 10 pixels wide and 200 pixels high:
Then double-click the solid to open it in its Layer window, grab the Pan Behind (Anchor Point) tool, and shift the anchor point for the Needle down to its bottom edge, so the Needle will rotate around its end instead of its middle.
Next, we need to turn the varying amplitude or level of the audio track into something that we can connect to the rotation of the meter needle. To do this, select the audio track and choose Animation > Keyframe Assistant > Convert Audio To Keyframes. When you do, you'll see a new layer appear in the Timeline window, called Audio Amplitude. Select that and type U (the Über key) to solo the layer's keyframes, and you'll see keyframes for properties called Left Channel, Right Channel, and Both Channels:
What Convert Audio to Keyframes has done is analyze the levels in the audio track's channels and created keyframes based on those level changes. It's also added an Expression Slider, which is just an empty control designed to let you link properties to each other, which is just what we need to do.
At this point we could simply select all the keyframes for Both Channels and paste them into the Rotation property for the Needle, which would give us an animation where the Needle follows the track. But tweaking all those keyframes is really a problem if you need to adjust the motion in the layer later (say if you want to make the needle swings wider). Just pasting the keyframes means that we'd have to adjust each one individually to make a wider swing, test it, adjust again, and so on. And the needle would still be starting at the 12:00 position, because the audio keyframe values don't go below zero. With a real VU meter, the zero amplitude position is more like a 9:00 position or so. However, by linking the Audio Keyframes to the Rotation with an expression, we can easily adjust the way the audio keyframes affect the needle movement, which is the real power of using an expression instead.
So, we first need to create an empty Rotation expression to have something to link to. To do that, type R to solo the Rotation property for the Needle, then option-click the Stopwatch for the Rotation property (option-clicking the stopwatch will add a new expression). When you do, you'll see that the property values for Rotation turn red, meaning they're controlled by an expression, and you'll also see a line of highlighted text in the Timeline saying transform.rotation. You'll also see some additional interface objects appear under the red property values:
To set this empty expression, just click anywhere outside its text box—we won't be typing anything directly into the expression at this point. Instead, we'll use a much simpler method to create the audio-rotation expression.
Take a closer look at those new interface objects created when we made the new expression, you'll see a little swirly icon - this is the Pick Whip. The Pick Whip lets us link expressions to parameters, which is what we'll do next.
Click on the Pick Whip for the Rotation expression, and drag from it up to the Slider control for Both Channels in the Audio Amplitude layer:
(We're using Both Channels because this is essentially a mono track. If you had a lot of stereo separation in your track, you could use the Left and Right Channel slider controls instead to create stereo meters.)
Once you release the Pick Whip, you'll see a complete expression for the needle Rotation property, telling us that the Rotation property is linked to the Audio Amplitude Slider property:
Enable Motion Blur for the Needle layer and for the comp by clicking their respective buttons, do a quick preview, and you'll see something like this:
As you see, we have a meter that responds accurately to the levels in the audio track, all with just a couple of clicks. But there are some adjustments we should make to improve this, which as I mentioned is where the real power of linking the audio with an expression comes in.
First, when the track level is low, the meter should start more in the 9:00 position, like a true VU meter, and it would also be nice if the meter's swing were wider. We can do both of those with a couple of simple additions to the expression.
First, to amplify the range the meter swings, all we need to do is multiply the values coming from the Both Channels Audio Keyframes to give us larger rotation values. To do that, just click on the expression text to enable text entry, and type in "*3" at the end of the Rotation expression, like so (be sure to click outside the text box to set the new text):
This multiplies the values coming from the Audio Keyframes by 3, effectively tripling the range of the meter's swing (you should, of course, experiment with different values here depending on your audio source levels.)
Next, click again on the expression to enable text entry, and type in -60 after the *3. This will subtract 60 from the multiplied keyframe values, which will push the meter over towards the 9:00 position when the audio levels are at zero:
Note: Be sure you enter these values in the order stated—try reversing them to understand why.
Once you have those values in place, another preview should show you something like this:
And there you go: with a simple expression and a little tweaking, you have an animation that would be both tedious and complex to create and adjust with regular keyframing. As always, you should experiment with these concepts on your own to see where they lead you. And above all, have fun!