AVAudioEngine下采样问题 [英] AVAudioEngine downsample issue

查看:62
本文介绍了AVAudioEngine下采样问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对从麦克风获取的音频进行下采样时遇到问题.我正在使用AVAudioEngine通过以下代码从麦克风中采样:

I'm having an issue with downsampling audio taken from the microphone. I'm using AVAudioEngine to take samples from the microphone with the following code:

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

此代码在iPhone 5s上非常有效,因为麦克风输入为8000Hz,并且缓冲区中充满了来自麦克风的数据.

This code works great on the iPhone 5s since the microphone input is 8000Hz, and the buffer gets filled with data from the microphone.

问题是我希望能够从iPhone 6s(及更高版本)记录哪些麦克风以16000Hz记录.奇怪的是,如果我将mixernode与引擎mainmixernode连接在一起(使用以下代码):

The problem is that I want to be able to record from iPhone 6s (and upwards) which microphone records with 16000Hz. And whats weird is that if I connect the mixernode with the engines mainmixernode (with the following code):

engine.connect(mixer, to: mainMixer, format: audioFormat)

这实际上是有效的,并且我获得的缓冲区具有8000Hz的格式,并且声音可以完美地降采样,唯一的问题是声音也来自我不想要的扬声器(如果我不想要的话)将其连接,缓冲区为空).

this actually works, and the buffer I get has the format of 8000Hz and the sound comes out perfectly downsampled, only problem is that the sound also comes out from the speaker which I don't want (and if I don't connect it the buffer is empty).

有人知道如何解决此问题吗?

Does anyone know how to resolve this issue?

非常感谢您的帮助,意见或想法.

Any help, input or thought is very much appreciated.

推荐答案

我通过简单地将混音器的音量更改为0来解决了这个问题.

I solved this issue by simply changing my mixers volume to 0.

mixer.volume = 0

这使我能够利用引擎主混音器的强大功能将任何采样率重新采样到所需的采样率,而不会听到直接从扬声器传出的麦克风反馈环路.如果有人需要任何澄清,请告诉我.

This makes me able to take advantage of the engines main mixer awesome ability to resample any samplerate to my desired samplerate, and not hearing the microphone feedback loop coming directly out from the speakers. If anyone needs any clarifying about this please let me know.

这是我的代码:

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))
mixer.volume = 0
engine.connect(mixer, to: mainMixer, format: audioFormat)

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

这篇关于AVAudioEngine下采样问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆