Eugene Gordin
Eugene Gordin

Reputation: 4107

How to get hardware sampleRate from iOS device?

In my app I'm using AVAudioEngine for speech recognition, but I'm having hard time understanding how to properly setup inputNode.

I used this tutorial as an example for my code: https://www.raywenderlich.com/155752/speech-recognition-tutorial-ios

This part of the code is giving me headache:

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

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

audioEngine.prepare()
try audioEngine.start()

Basically, my app crashes with this error:

AVAEInternal.h:70:_AVAE_Check: required condition is false: [AVAudioIONodeImpl.mm:897:SetOutputFormat:
(format.sampleRate == hwFormat.sampleRate)]

So my question is, how do I get hardware sampleRate from the device the app is running on, so I can set it to my recording format for the node?

Also, if the running app is being recorded via mac or being screenshared via apple TV I assume I will need to get the hw sampleRate of those device so my app doesn't crash in those cases too?!

Any kind of help is highly appreciated!

Upvotes: 4

Views: 2537

Answers (1)

dave234
dave234

Reputation: 4955

From AVAudioSession for hardware sample rate.

let sampleRate = AVAudioSession.sharedInstance().sampleRate

But this might fit your specific case better.

let sampleRate = node.inputFormat(forBus: 0).sampleRate

Upvotes: 9

Related Questions