ALSA库中从pcm句柄或名称获取物理播放设备文件名的最简单方法是什么?
例如,对于pcm设备hw:0,0
,我想获得/dev/snd/pcmC0D0p
。这很简单(只是转移索引),但对于"默认"或任何其他别名,这不是那么明显。
答案 0 :(得分:1)
回答自己。通过从snd_pcm_info获取卡和设备编号,可以间接地执行此操作。
int err;
const char *device_name = "default";
snd_pcm_t *pcm;
err = snd_pcm_open(&pcm, device_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
if (err < 0) {
fprintf(stderr, "Failed to open audio device '%s': %s\n", device_name, snd_strerror(err));
return false;
}
snd_pcm_info_t *info;
err = snd_pcm_info_malloc(&info);
if (err < 0) {
fprintf(stderr, "Failed to allocate PCM info: %s\n", snd_strerror(err));
goto fail;
}
err = snd_pcm_info(pcm, info);
if (err < 0) {
fprintf(stderr, "Failed to get PCM device info: %s\n", snd_strerror(err));
snd_pcm_info_free(info);
goto fail;
}
int card_no = snd_pcm_info_get_card(info);
if (card_no < 0) {
fprintf(stderr, "Failed to get PCM card number: %s\n", snd_strerror(card_no));
snd_pcm_info_free(info);
goto fail;
}
unsigned dev_no = snd_pcm_info_get_device(info);
printf("The ALSA path is: /dev/snd/pcmC%dD%up\n", card_no, dev_no);
snd_pcm_info_free(info);
fail:
snd_pcm_close(pcm);