I have two G.711 files/streams and I want to merge them. They represent each side of a telephone conversation. I know I can create a 'stereo' .wav file but I would rather create a single channel (mono) file. Can I just add together each contemporaneous data point? If not, how should I do this. Many thanks.

# Merging G.711 files

Started by ●December 11, 2006

Reply by ●December 11, 20062006-12-11

"Andrew Chalk" <achalk@magnacartasoftware.com> writes:> I have two G.711 files/streams and I want to merge them. They represent each > side of a telephone conversation. I know I can create a 'stereo' .wav file > but I would rather create a single channel (mono) file. Can I just add > together each contemporaneous data point? If not, how should I do this.Are you asking if you can just add the two samples _after they're decoded_? If so, then you could do this, but depending on your data path width, you run the risk of overflow. For example, if the decoder provides a 16-bit output and you form the 16-bit sum of the two 16-bit outputs, you can overflow your 16-bit integer. A simple way to avoide this is to do the add (assuming you're in C) like this: int16_t x1, x2, y; y = (int16_t)(((int32_t)x1 + x2) >> 1); This performs the sum using 32 bits so you don't overflow, then shifts the sum right one bit (the maximum growth factor is one bit) before storing to the output y. -- % Randy Yates % "And all that I can do %% Fuquay-Varina, NC % is say I'm sorry, %%% 919-577-9882 % that's the way it goes..." %%%% <yates@ieee.org> % Getting To The Point', *Balance of Power*, ELO http://home.earthlink.net/~yatescr

Reply by ●December 11, 20062006-12-11

Thanks. These are 8-bit samples but I assume the logic of your suggestion still holds. Regards, Andrew "Randy Yates" <yates@ieee.org> wrote in message news:m38xhdc31i.fsf@ieee.org...> "Andrew Chalk" <achalk@magnacartasoftware.com> writes: > >> I have two G.711 files/streams and I want to merge them. They represent >> each >> side of a telephone conversation. I know I can create a 'stereo' .wav >> file >> but I would rather create a single channel (mono) file. Can I just add >> together each contemporaneous data point? If not, how should I do this. > > Are you asking if you can just add the two samples _after they're > decoded_? > > If so, then you could do this, but depending on your data path width, > you run the risk of overflow. For example, if the decoder provides a > 16-bit output and you form the 16-bit sum of the two 16-bit outputs, > you can overflow your 16-bit integer. > > A simple way to avoide this is to do the add (assuming you're in C) > like this: > > int16_t x1, x2, y; > y = (int16_t)(((int32_t)x1 + x2) >> 1); > > This performs the sum using 32 bits so you don't overflow, then > shifts the sum right one bit (the maximum growth factor is one > bit) before storing to the output y. > -- > % Randy Yates % "And all that I can do > %% Fuquay-Varina, NC % is say I'm sorry, > %%% 919-577-9882 % that's the way it goes..." > %%%% <yates@ieee.org> % Getting To The Point', *Balance of > Power*, ELO > http://home.earthlink.net/~yatescr

Reply by ●December 12, 20062006-12-12

Andrew Chalk wrote:> Thanks. These are 8-bit samples but I assume the logic of your suggestion > still holds.Randy's suggestion will only work if you first decode the G.711 samples into "16-bit" linear PCM samples. mu/A-law compression used by G.711 is not a linear operation and peforming arithmetic directly on the 8-bit values will not give you the desired results. The only improvement I can make on Randy's solution is to do away with typecasting to 32 bits since G.711 uses 13 or 14 bits of dynamic range and a sum of 2 samples will never overflow in a 16-bit variable. You still need to right-shift by 1 to bring the sum into the acceptable range. C

Reply by ●December 12, 20062006-12-12

I am not sure that adding the values will correctly merge the two sides of the conversation. This data is mu-law, not linear. Isn't the merging process more complicated than simple addition? Regards, Andrew "Randy Yates" <yates@ieee.org> wrote in message news:m38xhdc31i.fsf@ieee.org...> "Andrew Chalk" <achalk@magnacartasoftware.com> writes: > >> I have two G.711 files/streams and I want to merge them. They represent >> each >> side of a telephone conversation. I know I can create a 'stereo' .wav >> file >> but I would rather create a single channel (mono) file. Can I just add >> together each contemporaneous data point? If not, how should I do this. > > Are you asking if you can just add the two samples _after they're > decoded_? > > If so, then you could do this, but depending on your data path width, > you run the risk of overflow. For example, if the decoder provides a > 16-bit output and you form the 16-bit sum of the two 16-bit outputs, > you can overflow your 16-bit integer. > > A simple way to avoide this is to do the add (assuming you're in C) > like this: > > int16_t x1, x2, y; > y = (int16_t)(((int32_t)x1 + x2) >> 1); > > This performs the sum using 32 bits so you don't overflow, then > shifts the sum right one bit (the maximum growth factor is one > bit) before storing to the output y. > -- > % Randy Yates % "And all that I can do > %% Fuquay-Varina, NC % is say I'm sorry, > %%% 919-577-9882 % that's the way it goes..." > %%%% <yates@ieee.org> % Getting To The Point', *Balance of > Power*, ELO > http://home.earthlink.net/~yatescr

Reply by ●December 12, 20062006-12-12

Thanks. That's what I thought. Do you have a URL on how to do the decoding? Thanks, - Andrew "Chetan Vinchhi" <vinchhi@gmail.com> wrote in message news:1165897945.679904.88730@j44g2000cwa.googlegroups.com...> > Andrew Chalk wrote: >> Thanks. These are 8-bit samples but I assume the logic of your suggestion >> still holds. > > Randy's suggestion will only work if you first decode the G.711 samples > into "16-bit" linear PCM samples. mu/A-law compression used by G.711 is > not a linear operation and peforming arithmetic directly on the 8-bit > values will not give you the desired results. The only improvement I > can make on Randy's solution is to do away with typecasting to 32 bits > since G.711 uses 13 or 14 bits of dynamic range and a sum of 2 samples > will never overflow in a 16-bit variable. You still need to right-shift > by 1 to bring the sum into the acceptable range. > > C >

Reply by ●December 12, 20062006-12-12

Andrew Chalk wrote:> I have two G.711 files/streams and I want to merge them.What are you going to do with them after you merge them?> They represent each > side of a telephone conversation. I know I can create a 'stereo' .wav file > but I would rather create a single channel (mono) file. Can I just add > together each contemporaneous data point?No, because G.711 is a logarithmic value, similar to an 8-bit float value. If you add them it will be like you are multiplying them! So, as has already been said, the proper way to add the streams would be to decode them to 16 bit PCM, add them, and reconvert them to G.711. Now, if you are looking for a short-cut, and the stream is just going to be sent to a speaker, then you can simply take to 'odd' samples from one stream and interleave them with the 'even' samples of the other stream. I have seen (heard) this technique used with 3 or 4 streams with good results. -- Phil Frisbie, Jr. Hawk Software http://www.hawksoft.com

Reply by ●December 12, 20062006-12-12

On Dec 12, 11:10 am, "Phil Frisbie, Jr." <p...@hawksoft.com> wrote:> > Now, if you are looking for a short-cut, and the stream is just going to > be sent to a speaker, then you can simply take to 'odd' samples from one > stream and interleave them with the 'even' samples of the other stream. > I have seen (heard) this technique used with 3 or 4 streams with good > results.For 2 streams, this sounds like a rough equivalent of downsampling by a factor of 2 and adding. Wouldn't it give rise to strange artifacts if there is significant enery in high frequencies (e.g. for some female voices)? C

Reply by ●December 12, 20062006-12-12

On Dec 12, 11:04 am, "Andrew Chalk" <ach...@magnacartasoftware.com> wrote:> Thanks. That's what I thought. Do you have a URL on how to do the decoding?G.711 is an ITU-T standard. You should be able to download it from the ITU website (you get up to 3 free downloads per year). Decoding is most simply done by a 8-bit table look-up. For encoding, you have to implement the formula in the standards. What is your target platform? You can probably google for implementations. C

Reply by ●December 12, 20062006-12-12

"Andrew Chalk" <achalk@magnacartasoftware.com> writes:> Thanks. That's what I thought. Do you have a URL on how to do the decoding?Andrew - I apologize for the confusion. I thought you had decoded the file to linear PCM already. -- % Randy Yates % "Though you ride on the wheels of tomorrow, %% Fuquay-Varina, NC % you still wander the fields of your %%% 919-577-9882 % sorrow." %%%% <yates@ieee.org> % '21st Century Man', *Time*, ELO http://home.earthlink.net/~yatescr