반응형

출처 : http://tdistler.com/2010/07/22/audio-resampling-using-ffmpeg-avcodec




resample.tar.bz



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


반응형

'멀티미디어' 카테고리의 다른 글

ffmpeg filter  (0) 2013.10.08
Telechips HW DECODER 동작 순서  (0) 2013.06.05
Ubuntu 에서 Dvico Fusion HDTV7 USB로 TV 보기 HDTV tvheadend  (3) 2013.04.05
Posted by Real_G