ALSA在中断之前服用1024个缓冲区

ALSA takes 1024 buffers before interrupt

本文关键字:1024个 缓冲区 中断 ALSA      更新时间:2023-10-16

我正在尝试使我的ALSA接口与尽可能低的延迟工作。有关所有帧/期间/缓冲区的ALSA文档都非常令人困惑,所以我在这里问。

以下代码迫使ALSA实际读取缓冲区大小的1024倍(我将缓冲区大小设置为1024字节)。writeAudio函数在放慢速度之前需要1024 * 1024字节。我想保留此帧吗?计数尽可能低,因此我可以在应用程序本身中跟踪游戏时间。如果我尝试使用snd_pcm_hw_params_set_periods将周期大小设置为2(我想它将在将2 * 1024字节写入缓冲区后放慢读数。)但是,代码中的此更改不会改变行为;播放器仍然缓冲1024 * 1024字节,然后缓冲缩短到扬声器播放音频的速度。

tldr;1024 * 1024字节缓冲大小对我来说太多了,如何降低它?以下是我的代码。是的,我正在用单声道输出播放未签名的8位。

int32_t ALSAPlayer::initPlayer(ALSAConfig cfg)
{
std::cout << "=== INITIALIZING ALSA ===" << std::endl;
if(!cfg.channels || !cfg.rate)
{
    std::cout << "ERROR: player config was bad" << std::endl;
    return -1;
}
m_channels = cfg.channels;
m_rate = cfg.rate;
m_frames = 1024;
uint32_t tmp;
uint32_t buff_size;
int dir = 0;
/* 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. %sn", PCM_DEVICE, snd_strerror(pcm));
}
snd_pcm_hw_params_alloca(&params);
snd_pcm_hw_params_any(pcm_handle, params);
if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
    printf("ERROR: Can't set interleaved mode. %sn", snd_strerror(pcm));
}
if ((pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S8)) < 0)
{
    printf("ERROR: Can't set format. %sn", snd_strerror(pcm));
}
if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, m_channels)) < 0)
{
    printf("ERROR: Can't set channels number. %sn", snd_strerror(pcm));
}
if ((pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &m_rate, &dir)) < 0)
{
    printf("ERROR: Can't set rate. %sn", snd_strerror(pcm));
}
// force the ALSA interface to use exactly *m_frames* number of frames
snd_pcm_hw_params_set_period_size(pcm_handle, params, m_frames, dir);
/* Write parameters */
if ((pcm = snd_pcm_hw_params(pcm_handle, params)) < 0)
{
    printf("ERROR: Can't set harware parameters. %sn", snd_strerror(pcm));
}
std::cout << "ALSA output device name:        " << snd_pcm_name(pcm_handle) << std::endl;
std::cout << "ALSA output device state:       " << snd_pcm_state_name(snd_pcm_state(pcm_handle)) << std::endl;
snd_pcm_hw_params_get_channels(params, &tmp);
std::cout << "ALSA output device channels:    " << tmp << std::endl;
snd_pcm_hw_params_get_rate(params, &tmp, 0);
std::cout << "ALSA output device rate:        " << tmp << std::endl;
snd_pcm_hw_params_get_period_size(params, &m_frames, &dir);
buff_size = m_frames * m_channels;
std::cout << "ALSA output device frames size: " << m_frames << std::endl;
std::cout << "ALSA output device buffer size: " << buff_size << "(should be 1024)" << std::endl;
return 0;
}
int ALSAPlayer::writeAudio(byte* buffer, uint32_t buffSize)
{
int pcmRetVal;
if(buffSize == 0)
{
    snd_pcm_drain(pcm_handle);
    snd_pcm_close(pcm_handle);
    return -1;
}
if((pcmRetVal = snd_pcm_writei(pcm_handle, buffer, m_frames)) == -EPIPE)
{
    snd_pcm_prepare(pcm_handle);
}
else if(pcm < 0)
{
    std::cout << "ERROR: could not write to audio interface" << std::endl;
}
return 0;
}

(一个帧不一定是一个字节;请不要混淆它们。)

此代码未设置缓冲区大小。

snd_pcm_hw_params_set_periods()设置周期数。
snd_pcm_hw_params_set_period_size()将设置周期大小。

拥有一个2048-FRAME缓冲液,每个带有两个周期,每个框架为1024帧,将周期数设置为2,而周期大小为1024。

您必须检查所有函数呼叫是否有错误,包括snd_pcm_hw_params_set_period_size()