I’m currently working on a project that requires me to do some sampling with Alsa. I’m trying to configure correctly everything but I’m stuck on how to correctly size my reading.
There are two primitives that seem to be interesting for my task:
snd_pcm_hw_params_get_period_time
snd_pcm_hw_params_get_buffer_size
The name of the first one suggests that the output will be the time length of a sampling period, however that’s weird: if I’m setting the sampling rate on f = 44100Hz the sampling period (in nanoseconds) should be T0 = 1e9 / 44100 ~= 22676 ns while the function will answer T1 = 725 us = 725000 ns.
Meanwhile, even if I’ve been asked to use non-locking primitives, I’m trying to profile the time required for locking ‘readi’, and it turns out that the sample requires T2 = 8028603 ns in the best case time and T3 = 12436217 ns in the worst case.
Finally I can’t figure out what’s the meaning of the following two:
snd_pcm_hw_params_get_buffer_time
snd_pcm_hw_params_get_period_size
I don’t get how could I measure the buffer in time and the period in size, However the former returns the same value as get_buffer_size, while the latter returns the same value as get_period_time.
Any hint?
This is my soundcard initialization function.
First of all I set needed parameters
When every parameter has been setted correctly, the parameters are applied to the handle:
After it has been applied the brave programmer can obtain the required data as follows:
get_period_size_min() gives the minimum size in frames of the buffer that will contain the sampling. A buffer having this size is wide enough.
This is pretty counter-intuitive, but the correct sampling period is not given by 1/rate as one may think. One can obtain the sampling period by using the get_period_time() primitive!