How do I know how much data I need to feed to the ALSA sound buffer?

56 Views Asked by At

My HW parameters are as follows retrieved from snd_pcm_hw_params_get functions.

  • Access: interleaved
  • Format: 16-bit little endian
  • Channels: 2
  • Periods: 3
  • Period Size: 32000 frames
  • Buffer Time: 2000001 us
  • Buffer Size: 96002 frames
  • Sample Rate: 48001 hz

PCM device is also set to nonblocking mode.

I'm currently figuring out how much to write by using snd_pcm_avail_update() when receiving POLLOUT event from poll(). Based on this answer.

The behavior I'm seeing is the following:

  • Receive first POLLOUT event, get 96002 frames from snd_pcm_avail_update(), write them, hear sound.
  • Receive POLLOUT again, get 32737 frames, write them, no sound.
  • Repeat again with 32191 frames, no sound.
  • Repeat with 32183 frames, have sound again.

No further interruptions in sound after this point.

I find it odd that the first write is the entire size of the buffer and subsequent writes are ~1 period. I'm inclined to believe this is the cause of that pause in audio and I'm just using these functions incorrectly.

It does seem that my writes and the audio I'm hearing go out of sync after that point as the file I'm reading from ends before the sound does.

I've tried many different hw params and even copied ones from popular examples online like linuxjournal and soundprogramming. Up until recently I hadn't been polling the file descriptors and was just using snd_pcm_avail/snd_pcm_avail_update in a loop. Switching to polling definitely helped the problems I was having but I'm still running into this weird pause in audio. Seems almost as if it just stopped reading for a bit and then resumed.

0

There are 0 best solutions below