I am engaged on an iOS recording app utilizing AVAudioEngine and AVAudioSession. The aim is to report in stereo at 48kHz utilizing the built-in iPhone microphone (on fashions that help stereo recording, like iPhone XS and up).
I’ve carried out full management over the audio session and mic configuration, together with choosing information sources and setting enter orientation, polar patterns, and pattern charges.
Right here’s the related code when establishing stereo recording:
if configuration.isStereo {
attempt session.setPreferredSampleRate(16_000) // <- that is the one setting that constantly works
attempt session.setPreferredInputNumberOfChannels(2)
attempt session.setPreferredInputOrientation(.portrait)
// choose the proper information supply and polar sample earlier than this
} else {
attempt session.setPreferredSampleRate(48_000)
attempt session.setPreferredInputNumberOfChannels(1)
}
Downside:
It doesn’t matter what I do, stereo enter all the time finally ends up utilizing 16kHz pattern charge. Even when I explicitly request 48_000 earlier than establishing the audio engine, it silently drops to 16_000.
Mono recordings work high quality at 48kHz.
Stereo recordings are all the time downsampled to 16kHz.
The enter format printed from inputNode.inputFormat(forBus: 0) confirms this.
I’ve verified the chosen enter information supply is “Framsida” (entrance mic), stereo is on the market, and polar sample is stereo. I am utilizing a safeSwitchInputConfiguration() helper that absolutely deactivates the session earlier than reconfiguring to keep away from race situations.
Right here’s a snippet from the logs:
Began recording with 2 ch @ 16000.0 Hz
Enter iPhone Mikrofon data-source: Non-obligatory("Framsida"), stereo-available: true
What I’ve Tried:
setPreferredSampleRate(48_000) earlier than/after activating the session.
Switching between .playAndRecord and .report session classes.
Attempting totally different enter orientations and polar patterns.
Deactivating/activating the session earlier than configuration adjustments.
Verified with a number of gadgets that help stereo enter.
Questions:
Is stereo at 48kHz really supported on iOS (particularly with the built-in mic)?
Has anybody managed to get 2ch @ 48000Hz from AVAudioEngine with AVAudioInputNode?
Might this be a {hardware} limitation or an undocumented OS constraint?
Is CoreAudio or AudioUnit a extra viable route for this?
Bonus Information:
I do see stereo-available: true, and I can change between “Framsida” and “Undertill” as enter information sources, and polar patterns like omnidirectional, subcardioid, and stereo can be found relying on the chosen supply.
Regardless of all that, the system nonetheless provides me 16kHz.
Any insights from anybody who’s cracked this is able to be vastly appreciated.