I need to record an audio signal from a microfone sampled with 16,000Hz. I am using AVAudioEngine and Swift:
let input = audioEngine.inputNode
let mixer = AVAudioMixerNode()
audioEngine.attach(mixer)
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 16000.0, channels: 1, interleaved: false)
audioEngine.connect(input, to: mixer, format: input.inputFormat(forBus: 0))
mixer.installTap(onBus: 0, bufferSize: 2048, format: format) {
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in ... }
Unfortunately the app crashes with this error:
central] 54: ERROR: [0x3b281e40] >avae> AVAudioNode.mm:751: AUSetFormat: error -10868 *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'error -10868'
What am I doing wrong?
My idea was to use a mixer node to downsample from 44100 Hz to 16000Hz.
Is there a better solution than using a mixer?
EDIT:
Using AVAudioCommonFormat.pcmFormatFloat32 instead of AVAudioCommonFormat.pcmFormatInt16 works better. It does not lead to error messages or app crashes and on my iPhone 5 it even receives the correct sound signal. However, in the Xcode simulator (iPhone 7 / iPhone X) I only receive 0 as samples. I tested Siri in the simulator with the microphone and it works, so the microphone is not broken. What could be the problem?