Reputation: 51
I'm currently using snd_pcm_writei to play a sound file that was previously loaded into an array of short (16 bits PCM format). To play this sound I create a buffer (short*), that contains a period (or fragment). Then, I use a while loop to call snd_pcm_writei which gives me that line:
int err = snd_pcm_writei(handle, buffer, frames);
It is pretty simple to understand how it works, and everything works fine, I can hear the sound. However, I'd like to try to use mmap instead of writei, but I don't really get it. I'm facing the lack of documentation and clear examples. Can anyone explain how mmap works with alsa, and how to convert my code to something that uses mmap? Basically, I'd still like to play what's in my array, using the buffer (so an array of short that has a size of one period). Thanks.
Upvotes: 3
Views: 4611
Reputation: 180260
First you need to set the access type one of the MMAP types (typically, SND_PCM_ACCESS_MMAP_INTERLEAVED
instead of SND_PCM_ACCESS_RW_INTERLEAVED
).
When you want to write to the buffer, call snd_pcm_mmap_begin()
with the number of frames you want to write. If this function succeeds, it returns a pointer to the buffer (areas[0].addr
, or multiple pointers for non-interleaved or complex access types), an offset into the buffer (offset
), and how many frames you actually can write.
After writing the samples, call snd_pcm_mmap_commit()
with the actual number of frames you have written.
Please note that using mmap does not make sense when you are copying the sample from your own buffer into the device's buffer (this is exactly the same what snd_pcm_writei()
already does).
You can reduce latency only if you are generating the samples on the fly and can write them directly to the device's buffer.
Upvotes: 1