I have seen this error pop up around here before but none of the attempts as fixing the issue have been any success for me.
Here's the error in all of it's glory:
Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(hwFormat)'
And my code (as some other issues suggested, I have separated this code from my ViewController class into its own class):
var mic: AKMicrophone!
var tracker: AKFrequencyTracker!
var filter: AKHighPassFilter!
var silence: AKBooster!
init() {
mic = AKMicrophone()
filter = AKHighPassFilter(mic, cutoffFrequency: 200, resonance: 0)
tracker = AKFrequencyTracker(filter)
silence = AKBooster(tracker, gain: 0)
AKSettings.audioInputEnabled = true
AudioKit.output = silence
try! AudioKit.start()
Timer.scheduledTimer(timeInterval: 0.1,
target: self,
selector: #selector(self.informDelegate),
userInfo: nil,
repeats: true)
}
I am using AudioKit 4.7.2, Xcode 10.2.1, and testing on an iPhone 6 Plus (iOS 12.3.2) and an iPhone 7 Plus (iOS 13), both crash on AKMicrophone init.
Any help is greatly appreciated. Thanks in advance.
I've seen other issues say that the following solves the problem:
AKSettings.sampleRate = AudioKit.engine.inputNode.inputFormat(forBus: 0).sampleRate
However, this does not work for me, and AudioKit.engine.inputNode.inputFormat(forBus: 0).sampleRate always evaluated to 0.0.
Few things to try:
AKMicrophone recently was changed to a failable initializer, so try making mic an optional: var mic: AKMicrophone?
I know you are quickly prototyping but do wrap your AudioKit.start() call in a do try:
{
try AudioKit.start()
}
catch
{
AKLog("Error:\(error.localizedDescription)
}
I do not believe AudioKit is updated for iOS 13 so stick with testing on iOS 12 for now
Lastly, As a sanity check I would try initializing the audioKit stuff in viewDidAppear, and of course refer to this article when you are ready to go from prototyping to production as AudioKit should be wrapped in a Singelton pattern: https://stackoverflow.com/questions/44258954/what-is-a-correct-way-to-manage-audiokits-lifecycle
@maksutovic Thanks for the quick reply. I went ahead and made these changes. I put the AudioKit.start() in a do catch block, as well as made AKMicrophone optional. I moved all of the AudioKit related code to the viewDidAppear (it was previously in viewDidLoad).
Even with all of these changes, exactly the same error. However, an even stranger thing, I tried moving all of my AudioKit code, just the core functionality of this one view, to a separate app which had only this one view controller and AudioKit functionality, and it has run perfectly on all of my devices. Maybe this is a problem with how I've set up the rest of the structure of my app? I've ensured the versions, Swift Compiler versions, etc are all the same between these two apps... I can't think of what would be causing the difference.
Thanks
@gm3197 that's very interesting since I and @maksutovic seemed to have the same problem. We couldn't recreate our issue in a separate app, but it would always crash on our app. Strange! I'm thinking we just haven't figured out how to exactly replicate the conditions in our app in the sample test app.
player.stop()
do{
try AudioKit.stop()
}
catch{
print ("")
}
add this line of code before moving to next screen. this works for me..
actually i am calculating the frequency. I was facing the same issue on the next controller.
Same here, here is my attempt to stabilize the issue, gathered several answers into this:
_ = AudioKit.engine.inputNode;
if let audioUnit = AudioKit.engine.inputNode.audioUnit {
AudioOutputUnitStop(audioUnit);
AudioUnitUninitialize(audioUnit);
}
AKSettings.audioInputEnabled = true;
let recordingFormat = AudioKit.engine.inputNode.inputFormat(forBus: 0);
AKSettings.sampleRate = recordingFormat.sampleRate;
microphone = AKMicrophone.init(with: recordingFormat);
tracker = AKFrequencyTracker(microphone);
AudioKit.output = AKBooster(tracker, gain: 0);
try? AudioKit.start();
}
It doesn't fix it but less devices crash, the main problem is iPhone 7.
Having the same issue here on iOS 13 public beta 3. Tried every answer here, to no avail. I also tested with the "Recorder" example, and I also get the same error:
Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == inputHWFormat.sampleRate'
The crash happens on line 116 in the example:
do {
try recorder.record()
} catch { AKLog("Errored recording.") }
The phone I tested with is an iPhone XS (not in silent mode).
EDIT - Here's the initialization part for reference:
let mic = AKMicrophone()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Clean tempFiles !
AKAudioFile.cleanTempDirectory()
// Session settings
AKSettings.bufferLength = .medium
do {
try AKSettings.setSession(category: .playAndRecord, with: .allowBluetoothA2DP)
} catch {
AKLog("Could not set session category.")
}
AKSettings.defaultToSpeaker = true
// Patching
let monoToStereo = AKStereoFieldLimiter(mic, amount: 1)
micMixer = AKMixer(monoToStereo)
micBooster = AKBooster(micMixer)
// Will set the level of microphone monitoring
micBooster.gain = 0
recorder = try? AKNodeRecorder(node: micMixer)
if let file = recorder.audioFile {
player = AKPlayer(audioFile: file)
}
player.isLooping = true
player.completionHandler = playingEnded
moogLadder = AKMoogLadder(player)
mainMixer = AKMixer(moogLadder, micBooster)
AudioKit.output = mainMixer
do {
try AudioKit.start()
} catch {
AKLog("AudioKit did not start!")
}
}
init AKMicrophone as a let in your app锛宯ot in class
I had the same issue but with AudioKit 4.9, it was resolved with no code change.
Hopefully it is fixed in 4.9, reopen if not.
Most helpful comment
Having the same issue here on iOS 13 public beta 3. Tried every answer here, to no avail. I also tested with the "Recorder" example, and I also get the same error:
Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == inputHWFormat.sampleRate'The crash happens on line 116 in the example:
The phone I tested with is an iPhone XS (not in silent mode).
EDIT - Here's the initialization part for reference: