Stable: 2.4.4
Development: 2.5.0-alpha4
Channel number conversion
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
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
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.
Multiply
Like this:
/* Multiply the channels. The more, the better! */ static char *stereo_multiply (const char *stereo, const size_t size, const int to_channels, const long format) { int Bps = sfmt_Bps (format); size_t i; char *output; int multiplier = to_channels / 2; output = (char *)xmalloc (size * multiplier); int iterate = 0; for (i = 0; i < size; i += Bps) { iterate = 0; while (iterate < multiplier) { memcpy (output + (i*multiplier + iterate*Bps), stereo + i, Bps); iterate++; } } return output; }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:
for (i = 0; i < size; i++) { stereo[i*2] = mono[i]; stereo[i*2 + 1] = mono[i]; }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).