BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to mix PCM wave files with Delphi (not play, only mix)

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Multimedia
View previous topic :: View next topic  
Author Message
Tom
Guest





PostPosted: Sun May 14, 2006 10:14 pm    Post subject: How to mix PCM wave files with Delphi (not play, only mix) Reply with quote



Hi.

I am making a program with BDS 2006 which should mix several PCM wave
files together, manage volume of each wavefile, compress them in MP3,
and send them to a broadcast server.

I managed to do the read of the file (using mmio functions), to compress
the wavedata to mp3 (using acm functions of the api), and to send them
to the server (using a socket class i wrote).

The problem is i don't know how to mix the PCM wavedata of the files
together.

In fact, i don't know much about wave principes. I know it's PCM 44100,
stereo, 16 bits, but how must i do to mix all data together : an
average, an addition ? by word ?

I think i managed to adjust volume of the files : i add or remove a
percentage of each word of the data for the whole file. Is it the right
way ?

Best regards,

Tom.
Back to top
Steffen Binas
Guest





PostPosted: Mon May 15, 2006 8:14 am    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote



Tom schrieb:
Quote:
I think i managed to adjust volume of the files : i add or remove a
percentage of each word of the data for the whole file. Is it the right
way ?
It's simply the average by sample per channel.


// Mix one sample of 3 Waves
ResultWave := (Wave1 + Wave2 + Wave3) / 3;

// Mix one sample with adjusting the volume
// Volume is a value (single/double) from 0 to 1
ResultWave := (Wave1 * Volume1 + Wave2 * Volume2 + Wave3 * Volume3) / 3;

It's not very fast to use floating point math, but it's easier to write
the basic idea down.

Regards,
Steffen
Back to top
Tom
Guest





PostPosted: Mon May 15, 2006 3:14 pm    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote



Thanks for your answer !

Steffen Binas a écrit :
Quote:
It's simply the average by sample per channel.

// Mix one sample of 3 Waves
ResultWave := (Wave1 + Wave2 + Wave3) / 3;

I saw on a site someone simply adding the 3 values. What will be the
result of his method, and yours ?

When i work on a 16 bit PCM file (44100 stereo), i suppose i must do the
average word by word, is this right ?

Regards,

Tom
Back to top
Martin James
Guest





PostPosted: Mon May 15, 2006 6:15 pm    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote

Quote:
I saw on a site someone simply adding the 3 values. What will be the
result of his method,

Overflow and either clipping or worse distortion as the word wraps on
coincident peaks.

Rgds,
Martin
Back to top
Tom
Guest





PostPosted: Mon May 15, 2006 10:14 pm    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote

Thank you very much Steffen and Martin, my prog works fine now !

I tried to mix by adding the waves, and by getting the average of the
waves, and in fact both methods are interesting.

The first (adding) mixes the waves without reducing any level (but with
the risk of getting clipping).
The second reduces the level of the sounds, but avoids any clipping.

This seems very simple, but i didn't know how to do, so thanks a lot for
your help.

Regards,

Tom
Back to top
Tsviatko Jongov
Guest





PostPosted: Tue May 16, 2006 7:14 am    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote

Hi,

Quote:
When i work on a 16 bit PCM file (44100 stereo), i suppose i must do the
average word by word, is this right ?

Yes, because you have 16 bit samples, which is equal to one word. If
you have 8 bit PCM you'll have to do the same with each byte.
There is also 24 bit and 32 bit PCM (I'm not sure about 64 bit sound,
bit it's in pro systems).

44100 Hz means that you have 44100 samples per second, but you'll have
to notice that these samples are only for one MONO channel. Since you
have stereo 16 bit PCM, you'll have 44100 samples for the left and
44100 for the right channel. In the stream they are mixed like this -
LEFT sample, RIGHT sample, LEFT sample, RIGHT sample, ....

Example:
44100 Hz, 16 bit, stereo PCM
44100 samples * 2 channels * 16 bit per sample = 176 400 bytes for one
second.

48000 Hz, 16 bit, stereo PCM
48000 samples * 2 channels * 16 bit per sample = 192 000 bytes for one
second.

Hope this will clear up a little the PCM structure.

Regards,
Tsviatko Jongov
johngov at yahoo dot com
Back to top
Lee_Nover
Guest





PostPosted: Tue May 16, 2006 10:14 am    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote

Quote:
Overflow and either clipping or worse distortion as the word wraps on
coincident peaks.

not if you use a larger value to do the sum
the signals are 2bytes, if you add them to a 4 or 8byte value you can avoid clipping with many many channels
also beware of signed and unsigned signals - don't mix them Smile
Back to top
Martin James
Guest





PostPosted: Tue May 16, 2006 12:14 pm    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote

not if you use a larger value to do the sum
the signals are 2bytes, if you add them to a 4 or 8byte value you can avoid
clipping with many many channels
also beware of signed and unsigned signals - don't mix them :)

I assumed that the output channel was also a 16-bit PCM stream. If the
output channel is 48 bits wide, then simple additive mixing would not
introduce any clipping/overflow.

Rgds,
Martin
Back to top
Lee_Nover
Guest





PostPosted: Tue May 16, 2006 1:14 pm    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote

Quote:
I assumed that the output channel was also a 16-bit PCM stream. If the
if it's signed then there's rarely any clipping .. unles all signals have the same phase

but you're right that it can still clip
in cases like these an AGC could be applied (auto gain control)
Back to top
Mark Jacobs
Guest





PostPosted: Wed May 17, 2006 1:14 pm    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote

"Lee_Nover" <Lee_Nover[nospam]@delphi-si.com> wrote in message
news:op.s9m89fttignj8p@stupidirko...
Quote:
I assumed that the output channel was also a 16-bit PCM stream. If the
if it's signed then there's rarely any clipping .. unles all signals have the

same phase
but you're right that it can still clip
in cases like these an AGC could be applied (auto gain control)

Or do what Goldwave does, and scan the entire PCM sample for the highest
voltage (value) in the channels added together (left channel is 16 bits in
each even word, and right in the odd). Then ratio every value in the sample to
scale to a reduced maximum that doesn't clip.

It gives you a sense of power, playing with sound at this level of
engineering! Smile
--

Mark Jacobs
DK Computing
http://www.dkcomputing.co.uk
Back to top
Thaddy
Guest





PostPosted: Fri May 19, 2006 7:14 pm    Post subject: Re: How to mix PCM wave files with Delphi (not play, only mi Reply with quote

Mark Jacobs wrote:

Even in simple signed integer (1+1) div 2 (mixing) I recommend
transferring the whole lot to 32 bit floats first and normalize the lot
afterwards.
No clipping, pristine audio every time. And the cost is minimal.

I have a rather popular and very, very simple freeware BASM library that
can help you with it available here:
http://members.chello.nl/t.koning8/tdkdsplib.zip
I wrote that exactly to do mixing, limiting etc.
It is nothing brilliant but it is concise, small, very fast and will do
the job better than most.

Also note most real good pro software does that. Never use MMX, maybe
SSE family or 3DNow and always process in the float domain (be it
singles or doubles) You CAN here the difference.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Multimedia All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.