Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity › Forums › Adobe After Effects Expressions › How to generate a static waveform presentation of an audio file.

  • How to generate a static waveform presentation of an audio file.

    Posted by Mac Smiechowski on June 17, 2014 at 3:02 pm

    Hello ladies and gents.

    First of all, I’m an absolute newbie on CreativeCow, so if I’m doing something wrong (like for ex. used wrong title etc.) please do tell me. I’m really happy that I have a chance to be a part of CreativeCow Community 🙂

    So I’ve started my journey with After Effects just recently and I’m trying to figure out how to generate a static waveform, based on an audio file in the project.

    How I’d like it to work? Well basically, I’d like to drag and drop an audio file into the project (might convert it to keyframes) and than auto generate a static representation of the waveform using expressions.

    As an example:
    If you visit soundcloud.com and play any song, you can see a waveform that is being “filled up” as the song goes (the filling up part is not important for now, just how they present the waveform).

    That’s the effect I want to achieve (no animations). And I’d love to do it using expressions, so that I don’t have to start a new project for every new audio file.

    I hope I explained the problem well enough.
    Guidelines, explenations and pieces of expressions are welcome!

    Thank you in advance!

    Aivaras Sutkus replied 10 years ago 3 Members · 5 Replies
  • 5 Replies
  • Kevin Camp

    June 17, 2014 at 5:42 pm

    i think this will get you what you want.

    first you’ll want to create a new comp, add a file that has audio and then convert the audio to key frames.

    then add a new solid with the width that you want each line to be and the height that you want the maximum height to be (ex: 4 x 100), and drag it bellow the audio amplitude layer (that was created by convert audio to keyframes).

    on that solid you’ll want to add these expressions.

    for position:
    target = thisComp.layer(“Audio Amplitude”);
    offset = 1;
    x = (index – target.index) * (width + offset);
    value + [x,0]

    and for scale:
    lines = 100;
    maxAmp = 18;
    target = thisComp.layer(“Audio Amplitude”);
    amp = target.effect(“Both Channels”)(“Slider”);
    smpl = amp.numKeys / lines;
    smplT = framesToTime((index – target.index) * smpl – (smplT / 2));
    avg = amp.smooth(framesToTime(smpl)/2, smpl, smplT)
    s = linear(avg,0,maxAmp,0,100);
    [100,s]

    the position expression will offset the solid (and copies of the solid) horizontally by the width of the solid + the number of pixels set by the offset value (default 1 px).

    the scale expression then divides up the audio amplitude ‘both channels’ value into the number of ‘lines’ that you set (default 100). you’ll need to determine the ‘maxAmp’ value by looking at the ‘both channels’ keyframe values.

    so once you have those values set, just duplicate that solid the number of times based on your ‘lines’ value and it should build your audio wave form.

    to update the wave form, add another file with audio and convert the audio to keframes. by default the new audio amplitude layer will be at the top of the comp and the expression will look at the first ‘audio amplitude’ layer that it finds, so the wave form should change immediately. but since both expressions assume that the lines (the solids with the expressions) will be immediately bellow the audio amplitude layer, you’ll want to delete the old amplitude layer and probably the actual audio layer, or at least move it to the bottom of the comp.

    different audio layers may have different max audio levels, so you may need to adjust that value in the expression. make that adjsutment on one layer, then select that scale property and choose edit>copy expression only. then choose all the other ‘line’ layers and paste.

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Kevin Camp

    June 17, 2014 at 6:11 pm

    to clamp the audio amplitude scale expression to a max of 100%, you could change the s line to this:

    s = Math.min(100,linear(avg,0,maxAmp,0,100));

    that will make it better resemble the example you posted, and it may make it so you don’t need to adjust the maxAmp value as often.

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Mac Smiechowski

    June 18, 2014 at 4:50 am

    Hello Kevin. Thank you for such a quick and indepth reply! This is amazing.

    There is but one problem with the code below:
    lines = 100;
    maxAmp = 18;
    target = thisComp.layer("Audio Amplitude");
    amp = target.effect("Both Channels")("Slider");
    smpl = amp.numKeys / lines;
    smplT = framesToTime((index - target.index) * smpl - (smplT / 2));
    avg = amp.smooth(framesToTime(smpl)/2, smpl, smplT)
    s = Math.min(100,linear(avg,0,maxAmp,0,100));
    [100,s]

    When I use it in that form, it drops an error on me:

    After Effects warning: Class ‘global’ has no property or method named ‘smplT’
    Epression disabled.

    Error occured at line 6.
    Comp: ‘Comp 1’
    Layer: 2 (‘Deep red solid 1’)
    Property: ‘Scale’

    I’m no programmer, but is that due to the smplT variable being used at the end of line 6 (smplT / 2) before it has any value? I mean in line 6 we set a variable smplT, and part of it is being controlled by smplT itself (that has no value yet). After changing the variable name to ‘smpl’ at the end of line 6 it seems to work alright (atleast looks alright). But I’m too much of a beginner to assess if it’s ‘doing it right’ 🙂

    I will test it out when I’m at work and fiddle with the expressions a bit.

    Thank you so much for your help Kevin, I’m in your debt!
    So glad I stumbled upon CreativeCow 🙂

    I will keep you posted with the updates.

    —–
    Mac Smiechowski

  • Kevin Camp

    June 18, 2014 at 5:02 pm

    yep.. that line had a typo…

    smplT = framesToTime((index – target.index) * smpl – (smpl / 2));

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Aivaras Sutkus

    September 5, 2016 at 3:35 pm

    Hello guys,
    really good post, that’s exactly what I was looking for. But I faced one problem as I tried to generate waveform in AE using this method. When I played the song I noticed that waveform, which I generated from that song, doesn’t really match in some parts. It seemed that peaks and low parts just moved a bit from their original position. To better understand what I’m trying to say I made this picture:

    https://i.imgur.com/X5mndN3.jpg
    The black waveform is the one I generated with AE and the red one I made using online waveform generator and it also shows how waveform of this song really needs to be. I marked worst parts with the arrows.

    I tried to generate this waveform many times with different options, but every time I get similar waveform. Any ideas what could be wrong and how to solve this ?

    View post on imgur.com

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy