0

I writing my iOS app to record audio.

I called session.setPreferredIOBufferDuration(0.001) to set the duration, and recorded the intervals between two callbacks.

This is data of my iPhone 8:

max =  198 avg =  2.75314 min =  0 
======================================================
4 2 2 3 5 3 1 3 6 1
2 2 6 0 5 3 1 3 3 5
1 3 3 2 3 5 1 3 3 3
3 4 1 4 3 2 4 2 3 3
3 3 3 3 2 3 3 3 5 1
3 2 3 3 3 3 3 5 1 3
3 3 3 3 3 3 4 1 3 3
3 3 3 2 3 4 2 3 3 3
3 4 5 0 2 3 3 3 3 3
3 3 3 3 2 3 4 2 3 3
3 3 3 3 3 3 4 2 2 4
3 2 4 2 3 2 4 3 4 2
2 3 3 3 3 2 4 3 5 1
2 4 2 3 2 3 4 3 3 2
4 5 1 2 3 4 2 4 1 3
3 3 3 5 2 2 2 4 2 3
3 3 3 6 0 3 3 3 2 3
3 3 4 2 3 3 2 3 3 3
3 3 3 3 5 1 2 3 3 3
3 3 4 4 1 2 3 3 4 2
3 3 3 3 3 2 3 6 0 3
3 3 4 2 3 3 3 2 5 1

And this is data of my iPhone 7:

max =  246 avg =  2.59249 min =  0 
======================================================
3 2 3 2 3 3 5 2 2 3
3 3 3 4 2 2 3 3 3 3
221 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 0 1 7 1
1 3 2 3 3 3 3 2 4 4
1 3 4 2 3 3 5 4 0 2
3 5 2 3 2 3 3 4 2 3
2 6 0 4 2 3 3 3 4 3
1 3 3 3 5 3 1 3 3 2

as you see, the max interval is far away from 1ms I set to AudioSession. and huge numbers form iPhone 7 is appeared much often.

Why iOS don't call my callback smoothly? Can I do any thing, such as set some preference, to get more smooth data?

Thank you.

  • _The minimum I/O buffer duration is at least 0.005 s (256 frames) but might be lower depending on the hardware in use._ https://developer.apple.com/documentation/avfoundation/avaudiosession/1616589-setpreferrediobufferduration?language=objc – fdcpp Apr 25 '20 at 13:20

1 Answers1

1

1) The preferred buffer duration is only a suggestion to the OS. Mostly followed, depending on the device models hardware capabilities. 256 frames is supported on all current iOS devices. Smaller numbers may work on only some newer iOS device models. But iOS devices are free to use different buffer sizes (thus durations) for each audio unit callback. And sometimes does (due to power management, audio route changes, hardware start/stop issues, and etc.)

2) The interval between the starts of Audio Unit buffer callbacks is extremely consistent. Highly variable latency issues are usually the result of the code inside the callbacks taking too long. Did you follow all the audio context real-time rules? (no long operations, no Swift, no Objective C messaging, no locks, no memory management, no file reads or writes, no UI calls, and etc. ?)

hotpaw2
  • 70,107
  • 14
  • 90
  • 153