Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record audio from AirPod microphone

Tags:

ios

swift

I'm trying to record and speech recognize the audio coming through the microphone of my bluetooth AirPods.

I tried everything I found but with no luck. I'm able to record from the built in microphone but as soon as I set the audio category to bluetooth it crashes.

This is the current version of my code:

askSpeechPermission()

var request = SFSpeechAudioBufferRecognitionRequest()
var listOfInputs = AVAudioSession.sharedInstance().availableInputs

do {
     try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryRecord, mode: AVAudioSessionModeDefault, options: AVAudioSessionCategoryOptions.allowBluetooth)
} catch {

}

let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)

node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
    self.request.append(buffer)
}

And this is the resulting crash error.

*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == hwFormat.sampleRate'

like image 998
Viper8 Avatar asked Nov 07 '22 06:11

Viper8


1 Answers

Have u tried to check your sample rate. To modify your Sample rate you can use

let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: true)

and then you can it to your format

node.installTap(onBus: 0, bufferSize: 1024, format: fmt) { buffer, _ in
    self.request.append(buffer)
}
like image 98
Osman Avatar answered Nov 14 '22 22:11

Osman