我想将输入节点的采样率从44100更改为8000

我想将输入节点的采样率从44100更改为8000

本文介绍了我想将输入节点的采样率从44100更改为8000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将麦克风中的缓冲区读取到一个数组中,其工作正常为44.1khz,但采样率为8khz会出现错误

I want to read the buffer from my microphone into an array, with 44.1khz its working fine but with the sample rate 8khz it comes to an error

ERROR:    >avae> AVAudioIONodeImpl.mm:884: SetOutputFormat: required condition is false: format.sampleRate == hwFormat.sampleRate
2016-11-26 19:32:40.674 Atem[5800:1168274] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == hwFormat.sampleRate'

使用我的以下代码:

 var engine = AVAudioEngine()
    func setup() {

    print("new")
    let input = engine.inputNode!
    let bus = 0

    let mixer = AVAudioMixerNode()
    engine.attach(mixer)

    engine.connect(input, to: mixer, format: input.outputFormat(forBus: 0))
    //pcmFormatFloat64 -- pcmFormatFloat32
    print(engine.isRunning)
    let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 12000, channels: 1, interleaved: true)

    do {
        try engine.start()


     print(engine.isRunning)

        mixer.installTap(onBus: bus, bufferSize: 1024, format: fmt) { (buffer, time) -> Void in
            // 8kHz buffers!
            print(buffer.format)
        }
    }catch {
        //print("An error occurred \(error)")
        return
    }

}

任何人都可以帮忙吗?

推荐答案

更新后的答案.该答案用于尝试对使用水龙头和混频器的转换进行评分,这是行不通的.

Updated answer this answer used to try to rate convert with taps and mixers, which does not work.

您可以使用AVAudioConverterAVAudioEngine inputNode上进行费率转换:

You can use an AVAudioConverter to do rate conversion on the AVAudioEngine inputNode:

let engine = AVAudioEngine()

func setup() {
    let input = engine.inputNode
    let bus = 0
    let inputFormat = input.outputFormat(forBus: 0)
    let outputFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: true)!

    let converter = AVAudioConverter(from: inputFormat, to: outputFormat)!

    input.installTap(onBus: bus, bufferSize: 512, format: inputFormat) { (buffer, time) -> Void in
        var newBufferAvailable = true

        let inputCallback: AVAudioConverterInputBlock = { inNumPackets, outStatus in
            if newBufferAvailable {
                outStatus.pointee = .haveData
                newBufferAvailable = false
                return buffer
            } else {
                outStatus.pointee = .noDataNow
                return nil
            }
        }

        let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(outputFormat.sampleRate) * buffer.frameLength / AVAudioFrameCount(buffer.format.sampleRate))!

        var error: NSError?
        let status = converter.convert(to: convertedBuffer, error: &error, withInputFrom: inputCallback)
        assert(status != .error)

        // 8kHz buffers!
        print(convertedBuffer.format)
    }

    try! engine.start()
}

这篇关于我想将输入节点的采样率从44100更改为8000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:55