Question

Suppose I want to write a minimal capture playback application. I initialized the device with following parameters. Sample Rate: 8000 Sample Format: SND_PCM_FORMAT_S16_LE Channels:1 for both capture and playback and then i started reading writing simultaneously from the device in chunks of 128 frames. How much i have to read and write at a time?. Is there any problems regarding that?

Was it helpful?

Solution

First of all you may check asoundrc file. It contains information on buffer size. So it is better not overcome it or you may get overrun (too much data written, old data is dropped and some part of sound was missed). But size of the buffer can be increased in asoundrc.
Also writing with a too small portions must be avoided. Otherwise you may get underruns (not enough data to play, buffer becomes empty). For instance, writing with a portions of one sample may give a lot of underruns.
Thus the best approach is to choose write size depending on system performance, load, buffer size and with a reference to sound source. In your case: 8k, 16 bit sample -->> 8k*2B/sec --> 16kB/sec. So if you write with a portions of 16k then your application needs to wake up and put new data to alsa every second. It must be ok for most of the cases.
In contrast, writing of each sample (16b) separately gives the following result: application needs to write to alsa every 125 us (1/8k sec). For instance, HZ in linux (how often it can switch between threads) if often 100-1000. Thus, it is not possible to write each sample separately with no harm to the sound.

OTHER TIPS

Each PCM device has a ring buffer. Its size is configured together with the other hardware parameters.

For playback devices, the applications writes data into the buffer, and the device reads out of it. If the applications wants to write but the buffer is full, it waits. If the device wants to read but the buffer is empty, you get an underrun.

For capture devices, the device writes data into the buffer, and the application reads out of it. If the application wants to read but the buffer is empty, it waits. If the device wants to write but the buffer is full, you get an overrun.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top