ALSA在中断之前服用1024个缓冲区
ALSA takes 1024 buffers before interrupt
我正在尝试使我的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(¶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. %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()
。
相关文章:
- 找到一种有效的方法,在 2 个巨大的缓冲区上执行 MAX,每字节字节
- 是否可以在 OpenGL 中的同一调用中呈现两个具有不同索引起点的不同缓冲区?
- 绘制一个对象,比较模具缓冲区的两个不同值
- 只有第一个缓冲区可以在C++中实现的Double缓冲区中工作
- 如何通过1024字节缓冲区和强力冲洗在插座连接上发送6个字符的C弦
- memcmp - 是否有更快的方法来按位比较两个缓冲区
- ALSA在中断之前服用1024个缓冲区
- 在使用OpenGl中使用转换反馈的GL_SEPARATE_ATTRIB时,如何读取多个缓冲区
- 在Win32管道上将两个缓冲区合并为一个写操作
- OpenGL中的缓冲区究竟是什么?如何使用多个缓冲区来发挥优势
- 在OpenGL中为顶点、uvs和法线使用一个缓冲区是否比使用三个缓冲区性能更好
- 在析构函数c++中释放多个缓冲区
- 关于处理超过 1024 个套接字描述符
- 如何在一个屏幕中使用两个缓冲区?可能吗?(快板)
- 连接多个缓冲区的有效方法
- 处理超过1024个套接字
- std::带有多个缓冲区的fstream
- 如何通过 bash shell 输入 c++ 字符串 >= 1024 个字符?
- WSARecv()和多个缓冲区
- 来自UDP连接的第一个缓冲区包含垃圾-随后的连接是好的