简体   繁体   English

为什么 SDL2 提供的缓冲区,对于音频样本,不够大?

[英]Why the buffer provided by SDL2, for audio samples, is not big enough?

I use SDL2 to create sound with format:我使用 SDL2 创建具有以下格式的声音:

freq = 32 * 1500;
format = AUDIO_F32SYS;
channels = 1;
samples = 1500;

I expect length parameter in my callback to be 6000 ( 1 * sizeof(float) * 1500 ) but it is 3000.我希望回调中的长度参数为 6000 ( 1 * sizeof(float) * 1500 ) 但它是 3000。

I've tried to decrease sample count, use 2 channels, but always provided buffer is as half big as needed.我试图减少样本数量,使用 2 个通道,但始终提供的缓冲区是需要的一半。

This is how I open audio device:这是我打开音频设备的方式:

SDL_AudioSpec want, have;
SDL_zero(want);
want.freq = 32 * 1500;
want.format = AUDIO_F32SYS;
want.channels = 1;
want.samples = 1500;
want.callback = ::PlayDing;
want.userdata = this;
m_Audio = SDL_OpenAudioDevice(nullptr, 0, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
// want == have here

All fields of want and have are equal.所有wanthave领域都是平等的。

This problem started to happen after I switched from Ubuntu to Debian on the same laptop.在我在同一台笔记本电脑上从 Ubuntu 切换到 Debian 后,这个问题开始发生。

I had a wrong assumption that each time the callback gets called it should fill samples amount of audio data.我有一个错误的假设,即每次调用回调时,它都应该填充samples数量的音频数据。

The number of samples that callback needs to provide should be calculated inside callback based on length / sizeof(float) . callback 需要提供的样本数量应该在 callback 内部根据length / sizeof(float)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM