使用音频单元时如何限制内存消耗

发布于 2024-11-28 02:33:33 字数 349 浏览 0 评论 0原文

对于我的应用程序,当用户在其中导航时,我需要在后台播放音乐。 因此,从 MixerHost 开始,我开发了一个能够同时播放 8 首曲目的混音器。然而,它会消耗大量内存,因为 8 个曲目文件完全加载到 8 个缓冲区中。

为了限制内存消耗,我在开始时只加载一小块数据,然后在回调中提供新数据,这样

result = ExtAudioFileRead ( audioFileObject, &numberOfPacketsToRead, bufferList );

效果很好,但有时播放会短暂暂停。我知道问题的根源:在回调中进行 FS 访问。

但是还有其他解决方案来限制内存消耗吗?

For my app, I need to play music on background when user navigate inside it.
So, starting from MixerHost, I developed an audio mixer which is able to play 8 tracks simultaneously. Nevertheless, It consumes to much memory because the 8 tracks files are entirely loaded in 8 buffers.

To limit the memory consumption, I load only a small chunk of data at the beginning, and I feed with new data in the callback like that

result = ExtAudioFileRead ( audioFileObject, &numberOfPacketsToRead, bufferList );

It works quite well, but sometimes the playback is shortly paused. I know the origin of the problem: making FS access in the callback.

But is there another solution to limit memory consumption ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦毁影碎の 2024-12-05 02:33:33

通常处理此问题的方法是使用共享环形缓冲区。环形缓冲区就像实时渲染线程和慢速磁盘访问之间的减震器。创建一个新线程,该线程除了从文件中读取音频并将其存储在环形缓冲区中之外什么也不做。然后,在渲染回调中仅从环形缓冲区中读取。

Apple 提供了适合与音频单元一起使用的环形缓冲区的实现,称为 CARingBuffer。它位于 /Developer/Extras/CoreAudio/PublicUtility/CARingBuffer 中。

The way this is typically handled is with a shared ring buffer. The ring buffer acts like a shock absorber between the real-time render thread and the slow disk accesses. Create a new thread that does nothing but read audio from the file and stores it in the ring buffer. Then, in your render callback just read from the ring buffer.

Apple has provided an implementation of a ring buffer suitable for use with Audio Units, called CARingBuffer. It's available in /Developer/Extras/CoreAudio/PublicUtility/CARingBuffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文