Skip to contentSkip to conversation

playing samples in parallel

DISCUSSION Torsten Mohr 2 participants 4 messages Feb 24, 2002 – Feb 26, 2002

lore
Share & tools
lore
#0 lore raw

Hi everybody,

i'm new to this list, so i hope this question is
not off-topic.

I'm trying to play several samples in parallel or at certain undetermined points, e.g. when an event happens.

To test this i load two different WAV files, that i can play independently, start the first one, do a "sleep(1)" and then start the second one.

The samples play only for a very short time, this seems to be related to the "buffer_time".

What value should i use for buffer_time? I've got the length of the sample, its rate, its number of channels and its bits per samplevalue.

I tried length*1000000/rate/channels/bits_per_sample.

This really seems to be the time in micro seconds, but one is played far too short.

In detail, how i try to play a sample:
I open one handle for each time i want to play a sample using "plughw:0,0" in NON_BLOCK mode, set format, channel, rate.
Then i set the buffer_time according to the formula above, i also tried a "500000". I chose the period_time to be 100000.
Then i get buffer_size and period_size.
After that i set start_threshold to buffer_size and avail_min to period_size. Could this be the problem?

xfer_align is set to 1 and then i start playing it by using "writei".

This way i start to play one sample, do a "sleep(1)", start to play another sample, do a "sleep(1)", then i play the first sample again, using another handle.

Has anybody got any hints for me?

Has anybody got a working example where two sounds are played in parallel? This should be used in games somewhere.

Do i need to use a handle for each sample i try to play?

Do i need to chose another device name instead of "plughw:0,0" for each sample?

I'd also like to use an async_handler to inform my program when a started sample is played completely. How can i do this? It seems that the handler is called (buffer_time/period_time) times during each sample, is that correct?

Best regards,
Torsten.

http://www.s.netic.de/tmohr/linux.html

_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

#2 lore raw
Quoted 1 line

i don't know, is my question off-topic?

its more of a general audio programming question than something ALSA specific.

Quoted 5 lines

Where can i get some help?
Would it help to mail the sources?
Is it an obvious mistake i make?
I really tried to describe the problem in detail, are there any important points missing?

i couldn't even really get my head around your problem description, because it takes such a fundamentally different approach to even describing what you're trying to do.

if all you want to do is to play two samples at the same time, then

   system ("aplay -D some_share_PCM_device some_sample_file &");

should be enough. presumably you have more exacting needs than this, and/or have discovered that the share device does not work.

typically programs that have multiple independent audio streams that need to be heard simultaneously mix them internally before delivering them to the audio API. in your case, you'd probably have a buffer that was zero-filled most of the time, and when you wanted to play samples, you'd additively store them in that buffer. then you just repeatedly deliver the buffer to ALSA over and over and over. when there is data in it, you'll hear it, when there isn't, you'll have silence.

with some audio hardware, you can open the device multiple times with no effort. however, this is not a portable approach since most hardware does not allow this.

--p

_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel