I'm attempting to access real-time microphone data with the following code:
import AVFoundation // for AVAudioEngine
class Mic
{
public let audioEngine = AVAudioEngine()
func startRecording() throws
{
// https://forums.developer.apple.com/thread/44833
//audioEngine.mainMixerNode // causes DIFFERENT crash!
audioEngine.prepare() // CRASHES
let inputNode = audioEngine.inputNode
if inputNode.inputFormat(forBus: 0).sampleRate == 0 {
exit(0);
}
let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
print( "YES! Got some samples!")
}
audioEngine.prepare()
try audioEngine.start()
}
//public
func stopRecording()
{
audioEngine.stop()
}
}
However it crashes on:
audioEngine.prepare() // CRASHES
2019-07-16 17:51:34.448107+0300 realtime_mic[8992:386743] [avae]
AVAEInternal.h:76 required condition is false:
[AVAudioEngineGraph.mm:1318:Initialize: (inputNode != nullptr || outputNode != nullptr)]
realtime_mic[8992:386743] required condition is false: inputNode != nullptr || outputNode != nullptr2019-07-16 17:51:34.449214+0300
As can be seen, I've tried to apply a hack/patch:
// https://forums.developer.apple.com/thread/44833
audioEngine.mainMixerNode
but this causes a different crash:
2019-07-16 17:50:34.315005+0300 realtime_mic[8901:385699] [plugin] AddInstanceForFactory:
No factory registered for id F8BB1C28-BAE8-11D6-9C31-00039315CD46 2019-07-16
17:50:34.349337+0300 realtime_mic[8901:385699]
HALC_ShellDriverPlugIn::Open: Can't get a pointer to the Open routine
2019-07-16 17:50:34.354277+0300 realtime_mic[8901:385699] [ddagg]
AggregateDevice.mm:776 couldn't get default input device, ID = 0, err = 0!
I've sent entitlements just in case: macOS Entitlements audio-input vs. microphone -- but to no avail.
What is the correct way to do this?
Test case at: https://github.com/p-i-/macOS_rt_mic
audioEngine.prepare()
before setting up either an input or an output node. So the error makes perfect sense. – P i