2

Given an array (of changing length) of frequencies and amplitudes, can I generate a single audio buffer on a sample by sample basis that includes all the tones in the array? If not, what is the best way to generate multiple tones in a single audio unit? Have each note generate it's own buffer then sum those into an output buffer? Wouldn't that be the same thing as doing it all at once?

Working on an iOS app that generates notes from touches, considering using STK but don't want to have to send note off messages, would rather just generate sinusoidal tones for the notes I'm holding in an array. Each note actually needs to produce two sinusoids, with varying frequency and amplitude. One note may be playing the same frequency as a different note so a note off message at that frequency could cause problems. Eventually I want to manage amplitude (adsr) envelopes for each note outside of the audio unit. I also want response time to be as fast as possible so I'm willing to do some extra work/learning to keep the audio stuff as low level as I can.

I've been working with sine wave single tone generator examples. Tried essentially doubling one of these, something like:

Buffer[frame] = (sin(theta1) + sin(theta2))/2

Incrementing theta1/theta2 by frequency1/frequency2 over sample rate, (I realize this is not the most efficient calling sin() ) but get aliasing effects. I've yet to find an example with multiple frequencies or data sources other than reading audio from file.

Any suggestions/examples? I originally had each note generate its own audio unit, but that gave me too much latency from touch to note sounding (and seems inefficient too). I am newer to this level of programming than I am to digital audio in general, so please be gentle if I'm missing something obvious.

4

1 回答 1

2

是的,你当然可以,你可以在你的渲染回调中做任何你喜欢的事情。当你重新设置这个调用时,你可以传入一个指向对象的指针。

该对象可以包含每个音调的开关状态。事实上,该对象可能包含一个负责填充缓冲区的方法。(如果它是一个属性,只需确保对象是非原子的——否则你会因为锁定问题而得到伪像)

你到底想达到什么目的?你真的需要即时生成吗?

如果是这样,您将面临使 remoteIO 音频单元的渲染回调过载的风险,这会给您带来故障和伪影

您可能会在模拟器上侥幸逃脱,然后将其移至设备上,然后神秘地发现它不再工作了,因为您运行的处理器数量减少了 50 倍,并且在下一个回调到达之前无法完成一个回调

话虽如此,你可以逃脱很多

我制作了一个 12 音播放器,可以同时播放任意数量的单个音调

我所做的只是为每个音调设置一个环形缓冲区(我使用的是非常复杂的波形,所以这需要很多时间,实际上我实际上是在第一次运行应用程序并随后从文件中加载它时计算它),并维护每个环都有一个读头和一个启用标志。

然后我在渲染回调中添加所有内容,这在设备上处理得很好,即使所有 12 个都一起播放。我知道文档告诉你不要这样做,它建议只使用这个回调来从另一个缓冲区填充一个缓冲区,但是你可以摆脱很多,它是一个 PITA 编码某种缓冲系统来计算在不同的线程上。

于 2011-07-26T23:47:26.780 回答