Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There

I want to merge 2 audio streams together in C/C++ and I think I have to user amerge to do so. I have the 2 audio streams decoded in 16 bit PCM format and I want to merge them properly into one audio file. I looked around for amerge filter example but couldnt find any. Does anyone have an example or tutorial or sample code example from ffmpeg that shows how to use this.

Thanks in advance.
Posted

1 solution

The problem seems to be that you don't have a clear idea what it means to "merge" two audio streams. What you probably mean is to mix both audio signals in a way that an audio mixer would do. For that to be possible, the samples of both streams must be taken at the same points in time, i.e. the sampling clocks must have been synchronized. (If that is not the case you first must do a sampling rate conversion of one on the streams, which is another story and not necessarily something for a beginner).

The "mixing" of two synchronized audio signals is actually very simple. If a(n) and b(n) are the n-th samples of the streams a and b, the output stream c is calculated by:
C++
c(n) = f * a(n) + g * b(n);

The two factors f und g are in the range of [0, 1.0] and determine volume with which each of the streams is mixed in. Normally f + g should not exceed 1.0, otherwise you might produce overflows, which sound really nasty in your result.

So the steps you should take could be the following:

(1) Understand the PCM format and how you can access each sample of a stream

(2) Create the output stream and allocate the required buffer space

(3) Program a simple loop over both streams with the above mixing formula

(4) Write the generated stream to a file.

If you have problems in any of those steps, you might want to place a new question and you can now refer more precisely to the step in which you have the difficulty.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900