0

如何创建一个“节拍盒”风格的声音引擎,其中一系列声音可以在播放过程中提前排队。但是,这些声音需要在没有任何间隙或打嗝的情况下播放。

我研究了 OpenAL 并尝试使用 alSourceQueueBuffers() 创建具有一系列预缓冲剪辑的源,但没有实现我想要的。

我使用 AudioFileOpenURL 加载我的音频文件,然后使用 AudioFileReadBytes 将其加载到一个字符数组中,使用 alGenBuffers 创建一个缓冲区,然后使用 alBufferData 进行缓冲。

然后我使用 alGenSources 创建一个源,并保留对该源的引用。然后我调用 alSourceQueueBuffers(sourceId, 1, &bufferId) 几次,其中 bufferId 是传递给我的“queueClip”方法的参数,并引用了一些不同的剪辑。

完成此操作并调用 alSourcePlay 后,我听到似乎是我的两个剪辑正在播放,背靠背......但随后什么也没有(我用 3 个音频文件加载它,并使用 alSourceQueueBuffers 随机将它们添加到源几次)。

我还需要知道更新源的最佳方法,向其中添加新声音并从中删除已播放的声音以清理内存等。

4

2 回答 2

3

Are you polling the source to see when it has used up a buffer?

ALuint freeBuffer;
ALint processed;
alGetSourcei (myALSource, AL_BUFFERS_PROCESSED, &processed);
while (processed > 0) {
  // remove spent buffer
  alSourceUnqueueBuffers(myALSource, 1, &freeBuffer);
  // refill buffer with samples, if they're going to be different this time
  // ...
  // re-queue buffer on source
  alSourceQueueBuffers(myALSource, 1, &freeBuffer);
  // check again for more processed buffers
  alGetSourcei (myALSource, AL_BUFFERS_PROCESSED, &processed);
}

You'll need to keep doing this polling to check for spent buffers... I used a simple NSTimer.

于 2009-07-28T17:28:17.783 回答
0

我忽略了一个更简单的选择。在源上设置 AL_LOOPING 属性:

alSourcei (myALSource, AL_LOOPING, AL_TRUE);
于 2009-07-28T17:52:28.950 回答