gpt4 book ai didi

ios - 如何让 AVAudioEngine 从麦克风输出 PCM-16?

转载 作者:行者123 更新时间:2023-11-28 21:03:20 25 4
gpt4 key购买 nike

我正在使用 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,无声。

最佳答案

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)
}

关于ios - 如何让 AVAudioEngine 从麦克风输出 PCM-16?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47116933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com