* Play multiple sounds concurrently
@ 2009-12-18 13:44 Markus Luttenberger
2009-12-18 14:02 ` Clemens Ladisch
0 siblings, 1 reply; 2+ messages in thread
From: Markus Luttenberger @ 2009-12-18 13:44 UTC (permalink / raw)
To: alsa-devel
Hey,
I want to play multiple sounds concurrently with ALSA. The audio data is
mono and G.711 a-law encoded. I can successfully play a single source of
audio data but the sound is distorted when I try to play two sources.
Basically, this is what I'm doing:
-----8<-----------------
char buf1[1024]; // filled with audio data from source 1
char buf2[1024]; // filled with audio data from source 2
char wr_buf[1024];
for (int count = 0; count < 1024; count++)
wr_buf[count] = (char) (0.5 * buf1[count]) +
(char) (0.5 * buf2[count]);
snd_pcm_writei(dev_handle, wr_buf, 1024);
-----8<-----------------
I think I have to use a scale value to correct the values or else
clipping occures. However, this is the problem. How do I have to modify
the audio bytes in order to add them together and write them into one
buffer?
greetings..
..Markus
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Play multiple sounds concurrently
2009-12-18 13:44 Play multiple sounds concurrently Markus Luttenberger
@ 2009-12-18 14:02 ` Clemens Ladisch
0 siblings, 0 replies; 2+ messages in thread
From: Clemens Ladisch @ 2009-12-18 14:02 UTC (permalink / raw)
To: Markus Luttenberger; +Cc: alsa-devel
Markus Luttenberger wrote:
> I want to play multiple sounds concurrently with ALSA. The audio data is
> mono and G.711 a-law encoded. I can successfully play a single source of
> audio data but the sound is distorted when I try to play two sources.
>
> wr_buf[count] = (char) (0.5 * buf1[count]) +
> (char) (0.5 * buf2[count]);
A-law is a somewhat logarithmic encoding; simply adding two values with
linear scale factors will not work.
If both your source data and the output device use A-law, you have to
decode the samples to linear PCM, add them, and encode them back. See
<http://git.alsa-project.org/?p=alsa-lib.git;a=blob;f=src/pcm/pcm_alaw.c;hb=HEAD>
for the algorithm that alsa-lib uses; you would use something like
buf[] = s16_to_alaw((alaw_to_s16(buf1[]) + alaw_to_s16(buf2[])) / 2);
If it is possible for your output device to use a linear PCM format,
you might want to use that to avoid the additional encoding step.
HTH
Clemens
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-12-18 14:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-18 13:44 Play multiple sounds concurrently Markus Luttenberger
2009-12-18 14:02 ` Clemens Ladisch
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.