如何让 AVAudioEngine 从麦克风输出 PCM-16?
How can I get AVAudioEngine to output PCM-16 from the Mic?
我正在使用 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)
}
我正在使用 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)
}