这似乎可以回答我的问题:How to set periods and buffer size in ALSA?,但我有一个不起作用的示例。
frames = 1024;
int dir;
snd_pcm_hw_params_set_period_size_near(pcm_handle, params, &frames, &dir);
snd_pcm_hw_params(pcm_handle, params);
snd_pcm_hw_params_get_period_size(params, &frames, 0);
printf("Frames: %zd\n", frames);
无论帧数是高还是低,当我使用snd_pcm_hw_params_get_period_size()获取其实际设置时,它始终显示512帧。
我知道我要设置的确切数字可能不可用。但是,它不应该将其设置为我所要求的最接近的值吗?
我希望看到它至少在我请求更高的数字时增加,或者在我请求更低的数字时降低,而不是完全相同。
“硬件”设备的hw_params.c输出:
Device: hw (type: HW)
Access types: MMAP_INTERLEAVED RW_INTERLEAVED
Formats: S16_LE S32_LE
Channels: 2 4 6 8
Sample rates: 44100 48000 96000 192000
Interrupt interval: 20-5944309 us
Buffer size: 41-11888617 us
“默认”设备的hw_params.c输出:
Device: default (type: IOPLUG)
Access types: RW_INTERLEAVED
Formats: U8 S16_LE S16_BE S24_LE S24_BE S32_LE S32_BE FLOAT_LE FLOAT_BE MU_LAW A_LAW S24_3LE S24_3BE
Channels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Sample rates: 1-192000
Interrupt interval: 5-4294967295 us
Buffer size: 15-4294967295 us
最小可重现的示例,其中我尝试设置两个缓冲区大小的周期:
/**
* Attempting to set some parameters.
*
* Using the template from: http://equalarea.com/paul/alsa-audio.html
*
**/
#include <alsa/asoundlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#define PCM_DEVICE "default"
int main(int argc, char **argv)
{
unsigned int pcm;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
/* Open the PCM device in playback mode */
if(pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0) < 0) {
printf("ERROR: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE, snd_strerror(pcm));
return 1;
}
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm_handle, params);
/* Set parameters */
if(pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
return 1;
}
if(pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_FLOAT) < 0) {
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
return 1;
}
int channels = 2;
if(pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0) {
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
return 1;
}
int rate = 44100;
if(pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0) {
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
return 1;
}
/* Write parameters */
if(pcm = snd_pcm_hw_params(pcm_handle, params) < 0) {
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
return 1;
}
printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
int ch;
snd_pcm_hw_params_get_channels(params, &ch);
if(ch > 1) {
printf("Channels: %i, stereo\n", ch);
}
else if(ch == 1) {
printf("Channels: %i, mono\n", ch);
}
snd_pcm_hw_params_get_rate(params, &ch, 0);
printf("Rate: %d bps\n", ch);
int dir;
snd_pcm_uframes_t frames = 1024;
printf("Attempting to set frames to %d\n", frames);
snd_pcm_hw_params_set_period_size_near(pcm_handle, params, &frames, &dir);
snd_pcm_hw_params(pcm_handle, params);
snd_pcm_hw_params_get_period_size(params, &frames, 0);
printf("Frames is now: %zd\n", frames);
snd_pcm_hw_params_get_period_time(params, &ch, NULL);
int buffer_size = 52430;
printf("Attempting to set the buffer size to: %d\n", buffer_size);
snd_pcm_hw_params_set_buffer_size(pcm_handle, params, buffer_size);
snd_pcm_hw_params(pcm_handle, params);
snd_pcm_uframes_t temp;
snd_pcm_hw_params_get_buffer_size(params, &temp);
printf("Buffer size is now: %d\n", temp);
return 0;
}
编译为:gcc set_params_fail.c -lasound
输出:
PCM name: 'default'
PCM state: PREPARED
Channels: 2, stereo
Rate: 44100 bps
Attempting to set frames to 1024
Frames is now: 512
Attempting to set the buffer size to: 52430
Buffer size is now: 524288
答案 0 :(得分:1)
调用snd_pcm_hw_params()
后,所有参数都是固定的。您必须在第一次(也是唯一一次)致电snd_pcm_hw_params_set_period_size_near()
之前先致电snd_pcm_hw_params()
。