Connecting AVAudioMixerNode to AVAudioEngine

谁都会走 提交于 2019-12-21 20:59:08

问题


I use AVAudioMixerNode to change audio format. this entry helped me a lot. Below code gives me data i want. But i hear my own voice on phone's speaker. How can i prevent it?

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: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in

            //i get audio data here
        }
    )

    //let's get the input audio format right as it is
    let format = input.inputFormat(forBus: 0)
    //I initialize a 16KHz format I need:
    let format16KHzMono = AVAudioFormat.init(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 11025.0, channels: 1, interleaved: true)

    //connect the nodes inside the engine:
    //INPUT NODE --format-> downMixer --16Kformat--> mainMixer
    //as you can see I m downsampling the default 44khz we get in the input to the 16Khz I want
    engine.connect(input, to: downMixer, format: format)//use default input format
    engine.connect(downMixer, to: engine.outputNode, format: format16KHzMono)//use new audio format

    engine.prepare()

    do {
        try engine.start()
    } catch {
        // @TODO: error out
    }
}

回答1:


You can hear your microphone recording through your speakers because your microphone is connected to downMixer, which is connected to engine.outputNode. You could probably just mute the output for the downMixer if you aren't using it with other inputs:

downMixer.outputVolume = 0.0



来源:https://stackoverflow.com/questions/53959379/connecting-avaudiomixernode-to-avaudioengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!