Re: CoreAudio: Calculate total latency between input and output with kAudioUnitSubType_VoiceProcessingIO
Re: CoreAudio: Calculate total latency between input and output with kAudioUnitSubType_VoiceProcessingIO
- Subject: Re: CoreAudio: Calculate total latency between input and output with kAudioUnitSubType_VoiceProcessingIO
- From: Brian Schachter via Coreaudio-api <email@hidden>
- Date: Tue, 21 Jan 2020 16:36:17 -0600
Thank you all for sharing! This is very helpful.
I was working on this problem a few months ago. It was frustrating, and I
wasn't able to find the "correct" calculation myself. I'm still puzzled by
the ioBufferDuration part of the calculation.
I have one question: How do you go about measuring the values? Eric
Herbrandson mentioned "empirically determined values" and Paul Davis said
this:
> The most reliable way to do this is to actually measure it. Not
necessarily the most convenient, however. In Ardour we make this
measurement optional - users will get better capture alignment if they do it
Do you play a metronome out of the speaker (output) while simultaneously
recording it with the mic (input) and afterwards look at the peaks in the
recorded data? If so, is this method of measuring impossible if the user is
wearing headphones? When the output is headphones... the mic won't be able
to pick it up.
Thanks again!
Brian
On Tue, Jan 21, 2020 at 6:06 AM Jonatan Liljedahl via Coreaudio-api <
email@hidden> wrote:
> I'm using PlayAndRecord session mode.
> With internal speaker/mic I get:
>
> 256 frame buffer size
> - buffer duration 5.8 ms
> - input latency 12.15 ms
> - output latency 17.66 ms
>
> And recording a metronome click via speaker -> mic and compensating
> for calculated round-trip latency shows an extra 8 ms delay.
>
> If I turn on MeasurementMode I get:
> - device input latency 0.14 ms
> - output latency 2.7 ms
>
> But still the same extra 8 ms unknown delay.
>
> On Tue, Jan 21, 2020 at 10:13 AM Arshia Cont <email@hidden>
> wrote:
> >
> > Just out of curiosity, what is your AVAudioSession mode? What if you use
> internal speaker/microphone?
> >
> > > On 21 Jan 2020, at 09:36, Jonatan Liljedahl <email@hidden>
> wrote:
> > >
> > > Hi,
> > >
> > > On Mon, Jan 20, 2020 at 9:58 PM Arshia Cont <email@hidden>
> wrote:
> > >>
> > >> Jonathan,
> > >>
> > >> First of all: Paul Davis is right. Both “internalLatency” and
> “outputLatency” values from AVAudioSession are estimations and not exact.
> They are most reliable when used without any external audio. Least reliable
> of course with Bluetooth!
> > >
> > > Of course. In my testing, I get an additional 3ms round-trip latency
> > > that is not part of the calculation.
> > >
> > >> Now:
> > >> I was also surprised to see the ‘4’ factor instead of ‘2’! My only
> raw guess is because my stream is stereo (which doesn’t make sense!).
> > >>
> > >> The formula you sent is more or less correct. It actually depends on
> the values of ioBufferDuration and the other two latencies. Think of it
> this way: If the outputLatency is higher than the ioBuffer it probably
> means that the output buffer is also longer, then in a Pull system you need
> more of the system buffers to fill out that buffer. In most real-time
> scheduling mechanisms we introduce lags to make sure underflow or overflow
> doesn’t occur.
> > >
> > > I just did a quick test here using my app AUM.
> > >
> > > Focusrite Scarlett 6i6 connected via USB and lightning-to-USB adapter,
> > > with loop-back cable from out to in, 44.1kHz, using two different
> > > buffer sizes:
> > >
> > > 64 frame buffer size:
> > > - buffer duration = 1.45 ms
> > > - input latency = 1.02 ms
> > > - output latency = 1.54 ms
> > > - calculated round-trip latency (inputLatency + outputLatency + 2 *
> > > ioBufferDuration) = 5.46 ms
> > >
> > > 256 frame buffer size:
> > > - buffer duration = 5.80 ms
> > > - input latency = 1.02 ms
> > > - output latency = 1.54 ms
> > > - calculated round-trip latency = 14.17 ms
> > >
> > > This is also a stereo stream BTW, which I don't think is relevant. In
> > > both cases I get about 3 ms extra latency, not part of the
> > > calculation. I don't have a setting for buffer sizes smaller than 64
> > > frames, so I didn't test the case where inputLatency >
> > > ioBufferDuration.
> > >
> > > Using `4 * ioBufferDuration` would yield a larger error in the second
> > > case, and it would give a different error in the two cases. I still
> > > believe 2 is the correct factor to use!
> > >
> > >> PS: Nice Apps! ;)
> > >
> > > Thanks! :)
> > >
> > > /Jonatan
> > >
> > >> On 20 Jan 2020, at 19:24, Jonatan Liljedahl <email@hidden>
> wrote:
> > >>
> > >> On Mon, Jan 20, 2020 at 6:36 PM Arshia Cont via Coreaudio-api
> > >> <email@hidden> wrote:
> > >>
> > >> You get the following from AVAudioSession:
> > >> inputLatency
> > >> outputLatency
> > >> ioBufferDuration
> > >>
> > >> Then your throughput latency, assuming a Stereo Stream, would be:
> inputLatency + outputLatency + 4*ioBufferDuration
> > >>
> > >>
> > >> I did the same, but I arrived at inputLatency + outputLatency + 2 *
> > >> ioBufferDuration!
> > >>
> > >> In case inputLatency > ioBufferDuration, you add one more
> ioBufferDuration. Same with outputLatency! This means that when lowering
> your ioBufferDuration, your Session mode becomes important (which directly
> affects input and output latencies). The lowest you can achieve would thus
> be with the Measurement Mode.
> > >>
> > >>
> > >> So you mean the formula would be:
> > >>
> > >> inputLatency + outputLatency + (inputLatency > ioBufferDuration ? 5 :
> > >> 4) * ioBufferDuration
> > >>
> > >> Do you have an explanation why?
> > >>
> > >> --
> > >> /Jonatan
> > >> http://kymatica.com
> > >>
> > >>
> > >
> > >
> > > --
> > > /Jonatan
> > > http://kymatica.com
> >
>
>
> --
> /Jonatan
> http://kymatica.com
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Coreaudio-api mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden