snd_pcm_hw_params_set_buffer_size() error: "Invalid argument"
snd_pcm_hw_params_set_buffer_size() error: "Invalid argument"
为什么下面的程序会针对 snd_pcm_hw_params_set_period_size()、snd_pcm_hw_params_set_buffer_size() 和 snd_pcm_hw_params_set_rate() 发出错误?如果我将 SAMPLES 的值提高到 768,它只会向 snd_pcm_hw_params_set_rate() 发出错误。值“512”在这里很重要,因为我正在将程序移植到 ALSA,它使用 512 个样本作为其缓冲区的大小(或 256 帧)。
错误消息只是“无效参数”——我知道,它很晦涩。
#include <stdlib.h>
#include <stdint.h>
#include <alsa/asoundlib.h>
#define STEREO 2
#define SIXTEENBITS 2
#define SAMPLES 512
#define PERIODS 2
#define SAMPLERATE 11025
void snd_error_checker(int error, char *function_name)
{
if (error)
{
printf("Error (%s): %s\n", function_name, snd_strerror(error));
// exit(EXIT_FAILURE);
}
}
int main(void)
{
snd_pcm_t *handle;
uint32_t channels = STEREO;
uint32_t sample_size = SIXTEENBITS;
uint32_t frame_size = sample_size * channels;
snd_pcm_uframes_t frames = SAMPLES / frame_size;
snd_pcm_uframes_t frames_per_period = frames / PERIODS;
int32_t dir = 0; // No idea what this does.
snd_pcm_hw_params_t *params;
int error;
error = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
snd_error_checker(error, "snd_pcm_open()");
snd_pcm_hw_params_malloc(¶ms);
error = snd_pcm_hw_params_any(handle, params);
snd_error_checker(error, "snd_pcm_hw_params_any()");
error = snd_pcm_hw_params_set_period_size(handle, params, frames_per_period, dir);
snd_error_checker(error, "snd_pcm_hw_params_set_period_size()");
error = snd_pcm_hw_params_set_periods(handle, params, PERIODS, dir);
snd_error_checker(error, "snd_pcm_hw_params_set_periods()");
error = snd_pcm_hw_params_set_buffer_size(handle, params, frames);
snd_error_checker(error, "snd_pcm_hw_params_set_buffer_size()");
error = snd_pcm_hw_params_set_rate(handle, params, SAMPLERATE, dir);
snd_error_checker(error, "snd_pcm_hw_params_set_rate()");
error = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_error_checker(error, "snd_pcm_hw_params_set_access()");
error = snd_pcm_hw_params_set_channels(handle, params, STEREO);
snd_error_checker(error, "snd_pcm_hw_params_set_channels()");
error = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
snd_error_checker(error, "snd_pcm_hw_params_set_format()");
error = snd_pcm_hw_params(handle, params);
snd_error_checker(error, "snd_pcm_hw_params()");
snd_pcm_drain(handle);
snd_pcm_close(handle);
exit(EXIT_SUCCESS);
}
硬件参数取决于硬件的能力。您可以使用 get_xxx_min/max 函数来确定硬件支持的内容,或调用 set_xxx_near 来选择最接近的实际支持值。
无论如何,硬件缓冲区大小不需要与应用程序的缓冲区大小相同。
为什么下面的程序会针对 snd_pcm_hw_params_set_period_size()、snd_pcm_hw_params_set_buffer_size() 和 snd_pcm_hw_params_set_rate() 发出错误?如果我将 SAMPLES 的值提高到 768,它只会向 snd_pcm_hw_params_set_rate() 发出错误。值“512”在这里很重要,因为我正在将程序移植到 ALSA,它使用 512 个样本作为其缓冲区的大小(或 256 帧)。
错误消息只是“无效参数”——我知道,它很晦涩。
#include <stdlib.h>
#include <stdint.h>
#include <alsa/asoundlib.h>
#define STEREO 2
#define SIXTEENBITS 2
#define SAMPLES 512
#define PERIODS 2
#define SAMPLERATE 11025
void snd_error_checker(int error, char *function_name)
{
if (error)
{
printf("Error (%s): %s\n", function_name, snd_strerror(error));
// exit(EXIT_FAILURE);
}
}
int main(void)
{
snd_pcm_t *handle;
uint32_t channels = STEREO;
uint32_t sample_size = SIXTEENBITS;
uint32_t frame_size = sample_size * channels;
snd_pcm_uframes_t frames = SAMPLES / frame_size;
snd_pcm_uframes_t frames_per_period = frames / PERIODS;
int32_t dir = 0; // No idea what this does.
snd_pcm_hw_params_t *params;
int error;
error = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
snd_error_checker(error, "snd_pcm_open()");
snd_pcm_hw_params_malloc(¶ms);
error = snd_pcm_hw_params_any(handle, params);
snd_error_checker(error, "snd_pcm_hw_params_any()");
error = snd_pcm_hw_params_set_period_size(handle, params, frames_per_period, dir);
snd_error_checker(error, "snd_pcm_hw_params_set_period_size()");
error = snd_pcm_hw_params_set_periods(handle, params, PERIODS, dir);
snd_error_checker(error, "snd_pcm_hw_params_set_periods()");
error = snd_pcm_hw_params_set_buffer_size(handle, params, frames);
snd_error_checker(error, "snd_pcm_hw_params_set_buffer_size()");
error = snd_pcm_hw_params_set_rate(handle, params, SAMPLERATE, dir);
snd_error_checker(error, "snd_pcm_hw_params_set_rate()");
error = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_error_checker(error, "snd_pcm_hw_params_set_access()");
error = snd_pcm_hw_params_set_channels(handle, params, STEREO);
snd_error_checker(error, "snd_pcm_hw_params_set_channels()");
error = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
snd_error_checker(error, "snd_pcm_hw_params_set_format()");
error = snd_pcm_hw_params(handle, params);
snd_error_checker(error, "snd_pcm_hw_params()");
snd_pcm_drain(handle);
snd_pcm_close(handle);
exit(EXIT_SUCCESS);
}
硬件参数取决于硬件的能力。您可以使用 get_xxx_min/max 函数来确定硬件支持的内容,或调用 set_xxx_near 来选择最接近的实际支持值。
无论如何,硬件缓冲区大小不需要与应用程序的缓冲区大小相同。