2

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 {...}
MdaG
  • 2,680
  • 2
  • 34
  • 46
  • Have a look at this answer, it has an interesting point about the buffer size in iOS: http://stackoverflow.com/a/35423008/2227743. Not sure if it applies to your specific case, though. – Eric Aya Jun 29 '16 at 16:45

1 Answers1

0

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?

MdaG
  • 2,680
  • 2
  • 34
  • 46