FFMpeg provides many powerful features for processing audio and video. One cool thing it can do is resample an audio stream. This allows you to convert, say, a 44.1kHz audio stream down to 8kHz, or up to 48kHz. What’s more, FFMpeg can do the conversion to any arbitrary sample rate. This allows you to do cool things like smoothly changing the audio playback speed over time (see sample code below).

There are many pages describing how to resample audio using the ffmpeg command line application, but what about doing resampling in your own program? To do that, you need to use the avcodec library (libavcodec.so on Linux and avcodec.dll on Windows).

  1. Include avcodec.h
  2. Call avcodec_init() to initialize the FFMpeg library.
  3. Create a resampling context using av_resample_init() that describes how you want the resampling done.
  4. Call av_resample() to do the actual resampling on your audio buffer.
  5. When you’re done with the resampling context, delete it with av_resample_close().
  6. Finally, link your application against avcodecavutil, and zlib (it won’t work on Linux without this one).

Here it is in pseudocode:

#include "libavcodec/avcodec.h"

avcodec_init();

struct AVResampleContext* ctx = av_resample_init( ... );

av_resample( ctx, ... );

av_resample_close( ctx );

That’s it… seriously!

Sample Code (Linux):

Here’s a sample program I wrote that takes a raw 44.1kHz/16bit/mono audio file and plays it back using the pulseaudio API. The catch is that it allows you to specify a “skew” parameter which will cause the audio to dynamically speed up and slow down (via resampling). The amount of resampling is controlled by a sine wave, which is what drives the speed changes.

Download: resample.tar.bz

To unpack and build, type:

$ tar -xjvf resample.tar.bz
$ make

First, run the sample with no skew:

$ ./resample audio_16b_44k_mono_pcm_raw 0

Now, try it with a heavy skew:

$ ./resample audio_16b_44k_mono_pcm_raw -10000

Tagged with:  

9 Responses to Audio Resampling Using FFMpeg (avcodec)

  1. Jestan says:

    Hi Tom,
    I am trying to use libavcodec.so through Java Native Access library. It would be helpful if you could provide the link to download the source code. (the link you have provided is broken)

    Thanks.
    -Jestan

    Reply

    Tom reply on July 27th, 2010 12:19 pm:

    Jestin,

    Huh… the link to FFMpeg works for me. Here’s the URL to go directly to the download page: http://www.ffmpeg.org/download.html

    I just pulled the code directly from the svn repo, but they have tar balls also.

    Hope this helps.

    Reply

  2. jeck says:

    Hello Tom
    Could you please fix the link to resample.tar.bz?

    Reply

    Tom reply on August 27th, 2010 6:46 pm:

    jeck

    I just checked the link and I could download the tarball just fine. Right-click, “Save link as…” works. If I just click it, it does open a new tab in my browser… maybe your pop-up blocker is doing it’s job :)

    No matter, I just emailed it to you. Let me know if it doesn’t work.

    Reply

  3. Bukran says:

    Hello Tom.

    When you click on the link to resample.tar.bz it leads you to http://tdistler.com/2010/07/22/media/code/resample.tar.bz, which is not the real ubication of the file. That’s why it seems to be broken.

    I’ve tried typing http://tdistler.com/media/code/resample.tar.bz directly on my web browser and finally succeeded downloading the file. It might be a problem related to the way your posts are stored, I mean with the date that I suppose is automatically added.

    Reply

    Tom reply on September 25th, 2010 11:54 am:

    @Bukran,

    I think you are correct. I’ve fixed the link. Thanks!

    Reply

  4. Hatem says:

    hi,

    thanks for sharing this, but the link is still broken. I’m unable to download the compressed file.

    Could you please email it ?

    Reply

    Tom reply on September 25th, 2010 11:55 am:

    @Hatem,

    Should be fixed now, but I’ll email it.

    Reply

  5. GRR says:

    Hi,

    Thanks for posting the code. I have one question. Looks like av_resample API only excepts input data which is a “short” int. I have a wav file with each value being a double. Can you suggest me what I can do in this case. Can I still use the av_resample API?

    –GRR

    Reply

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>