我正在努力让我的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. %s\n", PCM_DEVICE, snd_strerror(pcm));
}
snd_pcm_hw_params_alloca(¶ms);
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. %s\n", 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. %s\n", snd_strerror(pcm));
}
if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, m_channels)) < 0)
{
printf("ERROR: Can't set channels number. %s\n", 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. %s\n", 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. %s\n", 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;
}
答案 0 :(得分:1)
(一帧不一定是一个字节;请不要混淆它们。)
此代码未设置缓冲区大小。
snd_pcm_hw_params_set_periods()
设置句点数
snd_pcm_hw_params_set_period_size()
会设置期间大小。
要有一个2048帧缓冲区,其中包含两个周期,每帧1024帧,请将周期数设置为2,将周期大小设置为1024.
您必须检查所有函数调用是否有错误,包括snd_pcm_hw_params_set_period_size()
。