【问题标题】:How can I get AVAudioEngine to output PCM-16 from the Mic?如何让 AVAudioEngine 从麦克风输出 PCM-16?
【发布时间】:2017-11-05 00:11:56
【问题描述】:

我正在使用AVAudioEngine,我试图让它以 16000Hz 的频率输出.pcmFormatInt16,但我似乎无法让它工作。这就是我正在做的事情:

let audioEngine = AVAudioEngine()
let mixer = AVAudioMixerNode()
let input = self.audioEngine.inputNode!

audioEngine.attach(mixer)
audioEngine.connect(input, to: mixer, format: input.outputFormat(forBus: 0))

let recordingFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16000.0, channels: 1, interleaved: true)

mixer.installTap(onBus: 0, bufferSize: 2048, format: recordingFormat) { [weak self] (buffer, _) in
    // buffer here is all 0's!
}

self.audioEngine.prepare()
try! self.audioEngine.start()

如上所述,当我访问缓冲区时,它总是全为 0,静默。

【问题讨论】:

    标签: ios avaudiosession avaudioengine


    【解决方案1】:

    AVAudioEngine 不支持更改采样率。 你可以使用 AVAudioConverter 来改变采样率

    let inputFormat = input.outputFormat(forBus: 0)
    let recordingFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16000.0, channels: 1, interleaved: true)
    converter = AVAudioConverter(from: inputFormat, to: recordingFormat)
    
    mixer.installTap(onBus: 0, bufferSize: 2048, format: inputFormat) { [weak self] (buffer, _) in
        let convertedBuffer = self?.converter.convertBuffer(additionalBuffer: buffer)
    }
    

    【讨论】:

    • 您可以通过更改连接格式来更改采样率,但并非所有格式都支持这种方式。
    • 如果通过连接格式改变采样率,看起来像改变了采样率,但声音有噪音。例如,在 iPhone6s 中将采样率从 48000 更改为 44100,缓冲声音比记录慢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    相关资源
    最近更新 更多