0

I used AVAudioEngine to gather PCM data from the microphone in iOS and it worked fine, however when I tried moving the project to WatchOS, I get feedback while recording. How would I stop playback from the speakers while recording?

var audioEngine = AVAudioEngine()

try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)

let input = audioEngine.inputNode
let format = input.inputFormat(forBus: 0)

audioEngine.connect(input, to: audioEngine.mainMixerNode, format: format)

try! audioEngine.start()
let mixer = audioEngine.mainMixerNode

let format = mixer.outputFormat(forBus: 0)
let sampleRate = format.sampleRate
let fft_size = 2048

mixer.installTap(onBus: 0, bufferSize: UInt32(fft_size), format: format, 
            block: {(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
    // Processing
}
Audio Guy
  • 66
  • 4

1 Answers1

0

For anyone else that runs into this, I fixed it by removing the connection from the inputNode to the mainMixerNode, and installed the tap straight on the inputNode. The way I was doing it before I guess creates a feedback loop where it's playing back what it's recording. Not sure why this only happens in WatchOS and not on iPhone... perhaps it was playing back from the ear speaker rather than the one next to the mic. Fixed code:

var audioEngine = AVAudioEngine()

try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)

try! audioEngine.start()
let input = audioEngine.inputNode

let format = mixer.outputFormat(forBus: 0)
let sampleRate = format.sampleRate
let fft_size = 2048

input.installTap(onBus: 0, bufferSize: UInt32(fft_size), format: format, 
            block: {(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
    // Processing
}
Audio Guy
  • 66
  • 4