ALSA 的snd_pcm_hw_params_set_access函数在使用SND_PCM_ACCESS_RW_NONINTERLEAVED 访问类型调用时失败,报告存在无效参数。相同的代码适用于SND_PCM_ACCESS_RW_INTERLEAVED 访问。
我试图更改对 snd_pcm_hw_params_* 函数的调用顺序,但没有任何效果。
接下来,我认为我的硬件可能不支持非交错播放,但根据这篇文章,如果ALSA 子系统本身不支持非交错播放,那么它会在将非交错数据发送到硬件之前对其进行交错。因此,非交错访问应该始终可用。
那么为什么非交错访问似乎不受支持?
以下是适用于交错播放的代码,但对于非交错播放会产生此问题:
int err;
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf(stderr, "ALSA: allocate hw_params error: %s\n", snd_strerror(err));
throw 5;
}
if ((err = snd_pcm_hw_params_any(pb_dev, hw_params)) < 0) {
fprintf(stderr, "ALSA: hw_params_any error: %s\n", snd_strerror(err));
throw 5;
}
if ((err = snd_pcm_hw_params_set_access(pb_dev, hw_params, (pAudioCtx->sample_fmt < AV_SAMPLE_FMT_U8P) ? SND_PCM_ACCESS_RW_INTERLEAVED : SND_PCM_ACCESS_RW_NONINTERLEAVED)) < 0) {
fprintf(stderr, "ALSA: set access type error: %s\n", snd_strerror(err));
throw 5;
}
if ((err = snd_pcm_hw_params_set_channels(pb_dev, hw_params, pAudioCtx->channels)) < 0) {
fprintf(stderr, "ALSA: set channel count error: %s\n", snd_strerror(err));
throw 5;
}
snd_pcm_format_t sample_format;
switch (pAudioCtx->sample_fmt) {
case AV_SAMPLE_FMT_U8: case AV_SAMPLE_FMT_U8P: sample_format = SND_PCM_FORMAT_U8; break;
case AV_SAMPLE_FMT_S16: case AV_SAMPLE_FMT_S16P: sample_format = SND_PCM_FORMAT_S16; break;
case AV_SAMPLE_FMT_S32: case AV_SAMPLE_FMT_S32P: sample_format = SND_PCM_FORMAT_S32; break;
case AV_SAMPLE_FMT_FLT: case AV_SAMPLE_FMT_FLTP: sample_format = SND_PCM_FORMAT_FLOAT; break;
case AV_SAMPLE_FMT_DBL: case AV_SAMPLE_FMT_DBLP: sample_format = SND_PCM_FORMAT_FLOAT64; break;
default: fprintf(stderr, "sampleformat %d is not supported\n", pAudioCtx->sample_fmt);
throw 5;
}
if ((err = snd_pcm_hw_params_set_format(pb_dev, hw_params, sample_format)) < 0) {
fprintf(stderr, "ALSA: set sample format error: %s\n", snd_strerror(err));
throw 5;
}
if ((err = snd_pcm_hw_params_set_rate_near(pb_dev, hw_params, (unsigned int*)&pAudioCtx->sample_rate, 0)) < 0) {
fprintf(stderr, "ALSA: set sample rate error: %s\n", snd_strerror(err));
throw 5;
}
if ((err = snd_pcm_hw_params(pb_dev, hw_params)) < 0) {
fprintf(stderr, "ALSA: set parameters error: %s\n", snd_strerror(err));
throw 5;
}
执行此操作会产生以下输出:
ALSA: set access type error: Invalid argument