我在对从麦克风中提取的音频进行下采样时遇到问题。我正在使用 AVAudioEngine 通过以下代码从麦克风中获取样本:
assert(self.engine.inputNode != nil)
let input = self.engine.inputNode!
let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false)
let mixer = AVAudioMixerNode()
engine.attach(mixer)
engine.connect(input, to: mixer, format: input.inputFormat(forBus: 0))
do {
try engine.start()
mixer.installTap(onBus: 0, bufferSize: 1024, format: audioFormat, block: {
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
//some code here
})
} catch let error {
print(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)
此代码在 iPhone 5s 上运行良好,因为麦克风输入为 8000Hz,并且缓冲区填充了来自麦克风的数据。
问题是我希望能够从 iPhone 6s(及更高版本)录制麦克风以 16000Hz 录制的内容。奇怪的是,如果我将 mixernode 与引擎 mainmixernode 连接起来(使用以下代码):
engine.connect(mixer, to: mainMixer, format: …Run Code Online (Sandbox Code Playgroud) 我使用 AVAudioMixerNode 来更改音频格式。这篇文章对我帮助很大。下面的代码给了我我想要的数据。但我在电话扬声器中听到了自己的声音。我该如何预防?
func startAudioEngine()
{
engine = AVAudioEngine()
guard let engine = engine, let input = engine.inputNode else {
// @TODO: error out
return
}
let downMixer = AVAudioMixerNode()
//I think you the engine's I/O nodes are already attached to itself by default, so we attach only the downMixer here:
engine.attach(downMixer)
//You can tap the downMixer to intercept the audio and do something with it:
downMixer.installTap(onBus: 0, bufferSize: 2048, format: downMixer.outputFormat(forBus: 0), block: //originally 1024
{ (buffer: …Run Code Online (Sandbox Code Playgroud)