带有OSX AudioComponent使用者线程和C中回调的音频生产者线程 [英] Audio producer threads with OSX AudioComponent consumer thread and callback in C

查看:44
本文介绍了带有OSX AudioComponent使用者线程和C中回调的音频生产者线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与插件无关,它与独立的应用程序设计有关,并且与我之前问过的几个问题有关.

This question is not about a plugin, it's about a standalone application program design and is connected with few questions I've asked before.

我必须编写一个多线程音频合成函数,其数据处理量远远超过,在 CoreAudio 渲染线程上可以容纳的数量:数千实时独立的幅度和相位内插精确样本正弦波振荡器.借助所有可用的优化功能,这需要任何单个处理器内核都无法承受的CPU能力.

I have to write a multi-threaded audio synthesizing function, whose amount of data crunching by far exceeds what can get accomodated on the CoreAudio render thread: several thousands of independent amplitude and phase interpolating sample-accurate sine-wave oscillators in real time. This requires more CPU power than any single processor core can bear, with all the optimizations available.

我正在竭尽全力地学习它,但这似乎是一堵墙,而不是曲线.使用者线程可以是一个简单的 CA 实时优先级渲染回调,它接受 AudioBufferList iodata 等. …但是生产者线程应该是什么?如果选择另一个AudioComponent,则没有比将其全部放在输出线程上更好的了-它只会变得更加复杂并引入额外的延迟.

I'm doing my best to learn it, but it seems a wall, not a curve. The consumer thread may be a simple CA real-time priority render callback accepting AudioBufferList iodata, etc… …but what should be the producer thread(s)? If choosing another AudioComponent, it does no better than having it all on the output thread - it only gets more complicated and introduces additional latency.

如果将 n 个并行AudioComponents放入一个中,以提供一个环形缓冲区,该环形缓冲区提供给使用者线程,那么如何保证它不会以相同的方式结束线程,保持同步并精确到样本?

If putting n parallel AudioComponents into a graph which feeds a ring buffer, which feeds the consumer thread, how can one guarantee it'll not end up on the same thread, stay in sync and sample-accurate?

如果编写具有连接输出的 n 传统POSIX线程,如何实现CoreAudio拉模型与这样的推模型实时共存?

If writing n traditional POSIX threads with joining outputs, how to achieve the CoreAudio pull-model would coexist with such a push model in real time?

有没有这样的免费示例代码?是否有编写此类代码的参考书,教科书或教程?我尚未找到任何公开可用的信息.这让我有点奇怪,以前没有问过这样的问题吗?

Is there any such freely available example code ? Is there a reference, a textbook or a tutorial in writing such a code? I haven't found any publicly available information. It makes me kind of wonder that such question hadn't been asked before?

提前谢谢!

推荐答案

我的策略是让一个或多个单独的线程实时生成LPCM音频并将其写入适当大小的环形缓冲区.核心音频线程将从此环形缓冲区读取.

My strategy would be to have a separate thread (or threads) generating your LPCM audio in real-time and writing it to a suitably sized ring buffer. The core audio thread would read from this ring buffer.

如果阅读器(核心音频线程)请求的音频多于可用音频,则您会辍学(缓冲区欠载),因为读者无法等待.这表明您需要更大的缓冲区,或者您的音频生成算法不是实时的,需要优化.

If the reader (core audio thread) requests more audio than is available then you get a drop out (buffer underrun), because the reader cannot wait. This indicates that you need a larger buffer, or that your audio generation algorithm is not realtime and needs optimisation.

如果写程序在写满后尝试写入环形缓冲区,则最早的音频将被覆盖,并且您还会遇到另一个故障,这次是由于溢出.在这种情况下,您的音频生成代码运行得太快,需要学习如何等待.在这里,通过条件变量来增加写等待性是很方便的.

If the writer tries to write to the ring buffer when it is full, the oldest audio is overwritten and you have another glitch, this time via overrun. In this case your audio generation code is running too fast and needs to learn how to wait. Here it would be convenient to add write waitability, via a condition variable.

我知道两个很酷的环形缓冲区实现,它们使用虚拟内存技巧来有效地实现模块化内存访问:

I know of two cool ringbuffer implementations that use virtual memory tricks to efficiently implement the modular memory access:

  • Mike Ash's MAMirroredQueue
  • Michael Tyson's TPCircularBuffer

如果您正在寻找更便携的产品,可以使用 CBuffer 用于类Unix系统和 Magic Ring Buffer .

If you're looking for something more portable, there's CBuffer for unix-like systems and Magic Ring Buffer for Windows.

但是!您不必使用棘手的MMU环形缓冲区,可以根据需要使用普通的旧便携式C版本.

However! You don't have to use tricky MMU ring buffers, you can use a plain old portable C version if you like.

这篇关于带有OSX AudioComponent使用者线程和C中回调的音频生产者线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆