使用AudioKit定期播放纯音

bie*_*eda 5 audio ios swift audiokit

我想在给定频率下使用AudioKit振荡器播放非常基本的声音.

例如:以400Hz播放简单的正弦波50毫秒,然后播放100毫秒的静音,然后播放600Hz达50ms ......

我有一个金属视图,我在那里渲染一些视觉刺激.我的目的是使用基本的AKOscillator和CADisplayLink的渲染功能来播放/停止某些帧的声音.

我尝试使用oscillator.play()oscillator.stop()或改变振幅oscillator.amplitude = 0oscillator.amplitude = 1,但结果在这两种情况下是约10毫秒的抖动.

如果我首先创建.wav文件然后播放它们AKPlayer.play()的时机是正确的.

我希望可以随时灵活地使用任何频率.我该如何做与第一种方法类似的事情?将振荡器包装在midi乐器中是可行的方法吗?

hot*_*aw2 3

CADisplayLink 在 UI 线程中运行,因此会产生一些子帧时间抖动,因为 UI 线程的优先级低于音频(可能还包括其他操作系统)线程。

我成功实现可靠的亚毫秒级准确实时合成任意频率音频的唯一方法是将正弦波振荡器(或波形发生器或表查找)放入音频单元回调(iOS 的 RemoteIO)中,并计数样本,然后将所需正弦波(或其他波形)的停止/开始动态插入回调缓冲区,从正确的样本缓冲区偏移索引开始。

At first, I pre-generated sinewave look-up tables; but later did some profiling; and found that just calling the sin() function with a rotating phase, at audio sample rates, took a barely measurable fraction of a percent of CPU time on any contemporary iOS device. If you do use an incrementing phase, make sure to keep the phase in a reasonable numeric range by occasionally adding/subtracting multiples of 2*pi.