【问题标题】:Record callback function in Swift with Superpowered or AudioKit audio libraries使用 Superpowered 或 AudioKit 音频库在 Swift 中录制回调函数
【发布时间】:2017-11-29 11:02:36
【问题描述】:

我的应用(用 swift 编码)根据音频信号进行实时处理。

我需要获得一个带有来自输入的左右缓冲区的函数(来自 USB 麦克风的 2 个通道)和一个带有输出缓冲区的函数(还有 2 个通道)。

我曾经使用 EZAudio,但我有 2 通道 96K 格式的内存问题。随着 EZAudio 停止,我想换成 Superpowered 或 Audiokit。

我的问题是:我无法在任何这些库中获取带有缓冲区的函数。

超能力: 我在桥头文件中添加了#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>>, 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))
    }

它可以在我的算法中获取即将到来的缓冲区,但它似乎不是“实时”的,我的意思是,非常慢..(抱歉,很难解释。)

谢谢!

【问题讨论】:

    标签: ios swift audio audiokit superpowered


    【解决方案1】:

    如果您需要进行实时处理,则不应使用 Swift(或 ObjC)。目前在 AudioKit 中执行此操作的方法是创建一个 AUAudioUnit 子类并在其中进行处理。但是,如果您只需要更快的音频抽头,那么 AKLazyTap 是一个不错的解决方案。它与普通的水龙头不同之处在于您必须轮询它以获取数据,但此方法允许缓冲区重用,因此您可以尽可能快地调用它。

    以下是使用 AKLazyTap 获取峰值的示例:

    import UIKit
    import AudioKit
    
    class ViewController: UIViewController {
    
        let microphone = AKMicrophone()
        var tap: AKLazyTap?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            AudioKit.output = microphone
            AKSettings.ioBufferDuration = 0.002 // This is to decrease latency for faster callbacks.
    
            tap = AKLazyTap(node: microphone.avAudioNode)
    
            guard tap != nil,
                let buffer = AVAudioPCMBuffer(pcmFormat: microphone.avAudioNode.outputFormat(forBus: 0), frameCapacity: 44100) else {
                fatalError()
            }
    
            // Your timer should fire equal to or faster than your buffer duration
            Timer.scheduledTimer(withTimeInterval: AKSettings.ioBufferDuration / 2, repeats: true) { _ in
    
                var timeStamp = AudioTimeStamp()
                self.tap?.fillNextBuffer(buffer, timeStamp: &timeStamp)
    
                if buffer.frameLength == 0 { return } // This is important, since we're polling for samples, sometimes it's empty, and sometimes it will be double what it was the last call.
    
                let leftMono = UnsafeBufferPointer(start: buffer.floatChannelData?[0], count:Int(buffer.frameLength))
                var peak = Float(0)
                for sample in leftMono {
                    peak = max(peak, fabsf(sample))
                }
                print("number of samples \(buffer.frameLength) peak \(peak)")
    
            }
    
            AudioKit.start()
        }
    }
    

    【讨论】:

    • 从下面的答案(Gabor Szanto)来看,水龙头确实有点慢。由于我需要一个完美的时间戳,我更喜欢切换到 Superpowered。谢谢!
    • 我编辑了答案以包含准确的时间戳。
    • 这个水龙头的设置方式,一点也不慢。在此示例中,它与底层 C 回调一样快,但数据已从音频线程中移出,以便可以使用 Swift/ObjC。这使得它非常适合快速分析基于音频流触发 UI 之类的事情。检查速度的最好方法是测量。试试我的例子中的 tap,然后将 buffer.frameLength 与 AVAudioNode tap 中的 buffer.frameLength 进行比较,你会发现 AKLazyTap 快了大约 20 倍。
    • 感谢您的信息。我将尝试实施它。你知道为什么我能得到的最大峰值是 0.707 而不是 1.0 吗?某处是否存在限制或增益低于 1?
    • .707 听起来像是获得 RMS 的结果。尝试使用上面显示的方法获取峰值。
    【解决方案2】:

    Superpowered 是一个 C++ API,因为不建议将 Swift 用于实时处理。用 C++ 编写您的音频处理代码。 “点击”很慢,使用 SuperpoweredIOSAudioIO 实时获取音频。

    【讨论】:

    • 谢谢!实际上,我的算法并没有那么大,并且可以在 Swift 和 Objective-C 中使用。但是,我需要一个完美的时间戳。我可以在不同的课程中完全用 C++ 进行返工(以防万一!)。但是,我仍然想从我的 VC(在 Swift 中)调用算法的主要函数,但我没有设法实现 audioProcessingCallback 函数。能否请你帮忙?谢谢!
    • 使用Objective-C++(即Objective-C但可以使用C++,只需将.m文件重命名为.mm)。然后将其连接到 Swift。
    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多