ios - 带麦克风的 Swift iOS 实时音频处理

标签 ios swift real-time core-audio

我正在 swift iOS 中实现 Goertzel 算法。我找到了一些用于点击麦克风的代码,但我不完全确定它是如何工作的。

对于 Goertzel 函数,我编写了一个函数,其中数据是需要提供的样本

GoertzelAlgo(sampleRate:Float, targetFreq:Float, data:[Double]) -> Double

我想为每个充满实时麦克风数据的样本调用此 GoertzelAlgo 函数,但我不完全确定 installtapOnBus 是如何工作的?我不确定 installTapOnBus 中的代码是否被执行了。

    let audioEngine = AVAudioEngine()

    let inputNode = audioEngine.inputNode
    let sampleCount = 2048
    let bus = 0
    var samplesAsDoubles = Array(count: Int(sampleCount), repeatedValue: CDouble())
    let frameLength = UInt32(sampleCount) // This seems to be ignored when passed into the AudioTap, therefore reassign within block.
    inputNode.installTapOnBus(0, bufferSize:frameLength, format: inputNode.inputFormatForBus(bus), block: { (
        buffer: AVAudioPCMBuffer!,
        audioTime  : AVAudioTime!) in

        // Change incomming buffer size
        buffer.frameLength = UInt32(sampleCount)

        // Populate array with incomming audio samples
        for var i = 0; i < Int(buffer.frameLength); i++
        {
            samplesAsDoubles[i] = Double(buffer.floatChannelData.memory[i])

        }
        //Probably the Groeztel function needs to be called from somewhere here
    })

最佳答案

一种常见的解决方案是使用无锁 FIFO(或循环缓冲区)在 2 或 3 个所需线程(实时输入线程、分析线程和帧率显示线程)之间进行通信。您的 AVAudioEngine 总线分路器仅实现这两个或三个过程中的第一个。

将异步输入 block 中接收到的数据放入FIFO或循环缓冲区(没时间做其他事情),然后在另一个线程中检查是否有数据。

如果不检查来自另一个线程的缓冲区,例如来自在 UI 主循环中运行的计时器回调,您无法判断“installTapOnBus 中的代码甚至正在执行”。

关于ios - 带麦克风的 Swift iOS 实时音频处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292475/

相关文章:

iphone - 如何将设备添加到 iTunesConnect 并发送 IPA 打包的应用程序?

swift - 分段控件返回错误值?

swift - SwiftUI中的图片全屏宽度

user-interface - 图形均衡器

ios - 是否可以在没有 Apple 的 "Wireless Accessory Configuration"功能的情况下配置 WiFi 设备?

ios - 无法安装该应用程序,因为开发人员证书不再有效

hadoop - Spark 作业的输出可以用作另一个 Spark 作业的输入吗?

swift - 如何通过标题快速旋转标记?

ios - CLLocation didupdatetolocation 未调用

swift - 使用 Swift 删除 Realm 模型