I am trying to use ALSA to input input from a USB audio device and write it to disk as a series of signed short values. What ends up with me are the blocks of what appears to be real data, alternating with large blocks of zeros. I assume that my buffer settings are not configured correctly and I am using memory mapping incorrectly.
What am I trying:
- Sampling frequency: 8K (this is forcibly used by the device)
- buffer size: 2048
- period: 512
- one channel
The device opens correctly and accepts various parameters. After some tweaking, the loop works like:
snd_pcm_avail_update snd_pcm_mmap_begin memcpy data from mmap buffer to array of short snd_pcm_mmap_commit
memcpy is a pointer to an array of short ones and increases by the number of frames returned by each pass.
After that, in a few seconds I will close it and write the subsequent buffer to disk as one short value in each line. What I expect is a second or two PCM data ranging from 1200 to 2300 Hz. What I get is some data with lots of zeros.
I wonder: my values ββfor the buffer and rational period? Has anyone succeeded in using memory mapped output from ALSA?
EDIT: Code
const snd_pcm_channel_area_t *areas; snd_pcm_uframes_t offset, frames, size; short* pCID = (short*)malloc( 50000 * sizeof( short )); short* ppCID = pCID; while( size > 0 ) { frames = size; snd_pcm_mmap_begin (device, &areas, &offset, &frames); short* pd = (short*)areas[0].addr; memcpy( ppCID, (pd + (offset*sizeof(short))), frames * sizeof( short )); ppCID += frames; snd_pcm_mmap_commit(device, offset, frames); size -= frames; }
(validation error removed for clarity)
When everything is said and done, I look through the pCID and write to disk. One value for each row.
ethrbunny
source share