使用音频单元时如何限制内存消耗
对于我的应用程序,当用户在其中导航时,我需要在后台播放音乐。 因此,从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常处理此问题的方法是使用共享环形缓冲区。环形缓冲区就像实时渲染线程和慢速磁盘访问之间的减震器。创建一个新线程,该线程除了从文件中读取音频并将其存储在环形缓冲区中之外什么也不做。然后,在渲染回调中仅从环形缓冲区中读取。
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.