MdaG MdaG - 1 year ago 134
iOS Question

How do I change the tap frequency on the input bus?

I have this code where I want input from the iPhone's microphone. I want the input ten times a second given the sampling rate of 44100 Hz. The tap has to occur "every 4410 samples". But no matter how I do it, the tapping occurs every ~400 ms giving me 16384 samples each time.

What is the correct way to control the tapping frequency?

self.audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setPreferredSampleRate(44100.0)
try audioSession.setPreferredIOBufferDuration(4410.0 / 44100.0)
try audioSession.setCategory(AVAudioSessionCategoryRecord)
try audioSession.setActive(true)
audioSession.requestRecordPermission() {
[unowned self] (allowed: Bool) -> Void in
if allowed {
self.audioEngine = AVAudioEngine()
self.audioInputNode = self.audioEngine.inputNode!
let format: AVAudioFormat = self.audioInputNode.outputFormatForBus(0)
self.audioInputNode.installTapOnBus(0, bufferSize: UInt32(4410), format: format, block: {
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) in
// buffer length is 16384
do {
try self.audioEngine.start()
} catch {}
} else {...}
} catch {...}

Answer Source

I took a break from this problem and returned to it today with XCode 8.1 The above code just works now and I have no idea why. Bug in earlier version of AVAudioSession?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download