1

I try to record audio using AUAudioUnit. I successfully get audio buffers but I also hear recorded sound through the speaker while recording. The question is how to get just buffers not passing sound to the speaker?

func startRecording() {
    setupAudioSessionForRecording()

    do {
        let audioComponentDescription = AudioComponentDescription(
            componentType: kAudioUnitType_Output,
            componentSubType: kAudioUnitSubType_RemoteIO,
            componentManufacturer: kAudioUnitManufacturer_Apple,
            componentFlags: 0,
            componentFlagsMask: 0 )

        try auAudioUnit = AUAudioUnit(componentDescription: audioComponentDescription)
        let audioFormat = AVAudioFormat(commonFormat: .pcmFormatInt16,
                                        sampleRate: sampleRate,
                                        interleaved: true,
                                        channelLayout: AVAudioChannelLayout(layoutTag: kAudioChannelLayoutTag_Mono)!)

        try auAudioUnit.inputBusses[0].setFormat(audioFormat)
        try auAudioUnit.outputBusses[1].setFormat(audioFormat)
    } catch {
        print(error)
    }

    auAudioUnit.isInputEnabled  = true

    auAudioUnit.outputProvider = {(actionFlags, timestamp, frameCount, inputBusNumber, inputData) -> AUAudioUnitStatus in
        let err : OSStatus = self.auAudioUnit.renderBlock(actionFlags,
                                                          timestamp,
                                                          frameCount,
                                                          1,
                                                          inputData,
                                                          .none)
        if err == noErr {
            self.processMicrophoneBuffer(inputDataList:  inputData,
                                         frameCount: UInt32(frameCount) )
        } else {
            print(err)
        }
        return err
    }

    do {
        try auAudioUnit.allocateRenderResources()
        try auAudioUnit.startHardware()
    } catch {
        print(error)
    }
}

SOLUTION:

The solution was found here: https://gist.github.com/leonid-s-usov/dcd674b0a8baf96123cac6c4e08e3e0c

The idea is to call render block inside inputHandler instead of outputProvider

auAudioUnit.inputHandler = { (actionFlags, timestamp, frameCount, inputBusNumber) in
    var bufferList = AudioBufferList(mNumberBuffers: 1,
                                     mBuffers: AudioBuffer(
                                     mNumberChannels: audioFormat!.channelCount,
                                     mDataByteSize: 0,
                                     mData: nil))

    let err: OSStatus = block(actionFlags,
                              timestamp,
                              frameCount,
                              inputBusNumber,
                              &bufferList,
                              .none)
    if err == noErr {
        self.processMicrophoneBuffer(inputDataList: inputData,
                                     frameCount: UInt32(frameCount) )
    } else {
        print(err)
    }
}
Bambuh
  • 21
  • 1
  • 4

1 Answers1

0

One way to silence RemoteIO output is to zero the contents (frameCount samples) of the audio buffers in your recorded input data after you process (copy) each buffer.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • Thank you for your answer and your code sample on github! Found more suitable solution here https://gist.github.com/leonid-s-usov/dcd674b0a8baf96123cac6c4e08e3e0c – Bambuh Apr 10 '20 at 06:46