我的应用程序(用 swift 编码)基于音频信号进行实时处理。
我需要获得一个带有来自输入的左右缓冲区的函数(来自 USB 麦克风的 2 个通道)和一个带有输出缓冲区的函数(还有 2 个通道)。
我曾经使用 EZAudio,但我有 2 通道 96K 格式的内存问题。当 EZAudio 停止时,我想换成 Superpowered 或 Audiokit。
我的问题是:我无法在任何这些库中获取带有缓冲区的函数。
Superpowered:我在桥头文件中添加了#import“SuperpoweredIOSAudioIO.h”。
我在 ViewController 中添加了 SuperpoweredIOSAudioIODelegate。这会自动添加中断、权限和 mapchannels 函数,但不会添加 audioProcessingCallback。
我尝试了以下事情:
audio = SuperpoweredIOSAudioIO(delegate: self, preferredBufferSize: 12, preferredMinimumSamplerate: 96000, audioSessionCategory: AVAudioSessionCategoryPlayAndRecord, channels: 2, audioProcessingCallback: audioProcessingCallback, clientdata: UnsafeMutablePointer)
audio.start()
和
func audioProcessingCallback(buffers: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, inputChannels: UInt32, outputChannels: UInt32, numberOfSamples: UInt32, sampleRate: UInt32, hostTime: UInt64) -> Bool {
return true
}
但我得到了错误:
无法将类型“(UnsafeMutablePointer>,UInt32,UInt32,UInt32,UInt32,UInt64)-> Bool”的值转换为预期的参数类型“audioProcessingCallback!” (又名'ImplicitlyUnwrappedOptional<@convention(c)(可选,可选>>>,UInt32,UInt32,UInt32,UInt32,UInt64)-> Bool>')
我用 Swift 找不到这个库的任何例子......
使用 AudioKit,这是我所做的:
let mic = AKMicrophone()
installTap(mic)
AudioKit.output = mic
AudioKit.start()
func installTap(_ input:AKNode) {
input.avAudioNode.installTap(onBus: 0, bufferSize: 1024, format: AudioKit.format) { [weak self] (buffer, time) -> Void in
self?.signalTracker(didReceivedBuffer: buffer, atTime: time)
}
}
func signalTracker(didReceivedBuffer buffer: AVAudioPCMBuffer, atTime time: AVAudioTime){
let samples = UnsafeBufferPointer(start: buffer.floatChannelData?[0], count:1024)
audioProcess.ProcessDataCaptureWithBuffer(samples, numberOfSamples: UInt32(1024))
}
它可以在我的算法中获取即将到来的缓冲区,但它似乎不是“实时”的,我的意思是,非常慢..(对不起,很难解释。)
谢谢!