Stable: 2.5.2
Development: 2.6-alpha3
Hi Daper,
I would like to ask, what does it mean this code convering mono->stereo.
/* Double the channels from */
static char *mono_to_stereo (const char *mono, const size_t size,
const long format)
{
int Bps = sfmt_Bps (format);
size_t i;
char *stereo;
stereo = (char *)xmalloc (size * 2);
for (i = 0; i < size; i += Bps) {
memcpy (stereo + (i * 2), mono + i, Bps);
memcpy (stereo + (i * 2 + Bps), mono + i, Bps);
}
return stereo;
}
I'm playing with pulseaudio and made this function duplicating stereo to 4 channel sound. But it seems very silly to me. Why is Bps added to second channel in mono_to_stereo? How would you construct general stereo to n-channels function? Do I have to split the stereo into separate channels and duplicate them?
Like stereo-> ch1,ch2
Quad-> f_left ch1, r_left ch1
Quad-> f_right ch2, r_right ch2
/* Quad the channels from */
static char *stereo_to_quad (const char *stereo, const size_t size,
const long format)
{
int Bps = sfmt_Bps (format);
size_t i;
char *quad;
quad = (char *)xmalloc (size * 2);
for (i = 0; i < size; i += Bps) {
memcpy (quad + (i * 2), stereo + i, Bps);
memcpy (quad + (i * 2 + Bps), stereo + i, Bps);
}
return quad;
}
Thanks
daper
Tue, 2008-07-22 09:24
Permalink
Bps means bytes per sample -
Bps means bytes per sample - the size of a sound sample. MOC (like most players) uses interleaved PCM format. It means that one sample stream (buffer) contains samples for all channels. For stereo they are in form: LRLRLRLT... where L is a sample for left speaker and R is a sample for right speaker. if you have mono sound in a buffer and want to convert to interleaved PCM buffer you must allocate twice as much space and double the sample for each channel. Thats what the memcpy does: it copies the sample (mono sample) to both channels. If all samples were 32 bit integer numbers the function would look like:
You must do similar job for 4-channel, but I'm not sure what is the order of channels in this format (I'm not even exactly sure for stereo if the first sample is left).
nemozny
Wed, 2008-07-23 09:19
Permalink
Multiplication succesful
Thanx Daper,
I rewrote the function to n-channel support. Even (%2 == 0) number only. It's needless, but is there a possibility to get odd number?
(sorry I forgot the source at home)
Have a nice day
Nemozny
nemozny
Thu, 2008-07-24 09:29
Permalink
Multiply
Like this:
daper
Tue, 2008-07-29 07:19
Permalink
If you want stereo to
If you want stereo to 3-channel conversion you should probably mix the two channels to create the center channel, but I'm only guessing. Mixing is also simple: it's the average value of allsource samples.