C++ ALSA在中断前需要1024个缓冲区

C++ ALSA在中断前需要1024个缓冲区,c++,buffer,frame,alsa,period,C++,Buffer,Frame,Alsa,Period,我正试图使我的ALSA接口以尽可能低的延迟工作。关于所有帧/周期/缓冲区的ALSA文档非常混乱,所以我在这里询问 下面的代码强制ALSA实际读取1024倍的缓冲区大小,我将缓冲区大小设置为1024字节。writeAudio函数需要1024*1024字节才能减慢速度。我想保留这个相框?尽可能低的计数,这样我就可以在我的应用程序中跟踪播放时间。如果我尝试使用snd_pcm_hw_params_set_periods将周期大小设置为2,我想在将2*1024字节写入缓冲区后,它会减慢读取速度。然而,代码

我正试图使我的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(&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. %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;
}

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

此代码未设置缓冲区大小

snd_pcm_hw_参数设置周期设置周期数。 snd_pcm_hw_参数设置_周期大小将设置周期大小

要使2048帧缓冲区具有两个周期,每个周期1024帧,请将周期数设置为2,周期大小设置为1024

您必须检查所有函数调用是否存在错误,包括snd_pcm_hw_params_set_period_size。

值为2的snd_pcm_hw_params_set_periods设置为periods count似乎失败,错误代码为-22。编辑:当值设置为8时,我可以播放它。因此,总缓冲区不是8*1024字节=8192字节。非常感谢ELI5