[Portaudio] How to obtain the time point when computer begins to record?

Ross Bencina rossb-lists at audiomulch.com
Tue Mar 22 01:32:11 EDT 2016

Hello Mengqi,

On 22/03/2016 8:33 AM, Mengqi Ren wrote:
> I'm using audio signal to measure the distance between two computers,
> one is transmitter, the other is receiver. Now, I know the timestamp
> when transmitter begins to send audio signal. I also know the
> beginning frame index of its matched segment in the receiver's record.
> So, if I know the timestamp when this record begins, I will know the
> transmitting delay and can calculate the distance between transmitter
> and receiver via transmission time delay of this audio signal. To
> achieve 1 meter estimation accuracy, the required time accuracy is at
> milliseconds.

It sounds as though you also need to synchronise the clocks on the two 

At ~2.9ms / Metre in air, the unknown latency component due to hardware 
buffering and converter latency could be of that order.

I think you will be pushing it to reliably achieve meter accuracy 
portably, with unknown hardware and no calibration.

I'm not sure that I mentioned it earlier, but I don't recommend relying 
on the start time of the stream, you are much better off running the 
stream continuously and computing a time offset between system time and 
the callback buffer timestamps.

> By the way, is there any API that can return the timestamp when the
> transmitter begins to play audio?

Isn't that what you already asked?


> On Mon, Mar 21, 2016 at 4:36 PM, Ross Bencina
> <rossb-lists at audiomulch.com <mailto:rossb-lists at audiomulch.com>> wrote:
>     Hello Mengqi,
>     On 22/03/2016 3:20 AM, Mengqi Ren wrote:
>         In my project, I need to get the computer time when the computer
>         begins
>         to record.
>     In my previous message I asked for your use-case. What I mean is:
>     What do you intend to use the time measurement for? What is your
>     overall goal? I also asked for your requirements, for example, what
>     is your required time accuracy?
>     Please provide this information. It is difficult to provide you with
>     a useful answer without it.
>     There is usually not one "computer time." On most platforms there
>     are multiple time sources and/or multiple time APIs. Do you know the
>     APIs that you intend to use as a time base? Do you need to correlate
>     PortAudio timestamps with timestamps collected from other sources?
>     If it meets your requirements, I recommend using Pa_GetStreamTime().
>     Otherwise on OS X you should look into mach_absolute_time() and on
>     Windows QueryPerformanceCounter() and/or timeGetTime().
>         I'm using both Mac OS X and Windows. Which timestamp will be
>         the highest accurate?What is the difference between Mac OS X and
>         Windows?
>     Each audio API has its own behavior. The exact timing behavior may
>     also depend on your sound card drivers.
>     Speaking broadly, OS X has more consistent timing, because Apple's
>     code controls the scheduling of the CoreAudio callbacks, and
>     timestamps are communicated throughout the API stack. On Windows,
>     with most host APIs, PortAudio will regenerate timestamps when it
>     receives the callback from the host. This can be subject to high
>     jitter, since it is dependent on how the callback is scheduled, but
>     once again, it depends on host API and drivers.
>     On Windows, PortAudio typically takes a time measurement and then
>     subtracts the estimated latency to produce the input ADC time. On
>     OSX, PA performs a similar calculation, but the timestamps are
>     sourced from the driver, which is more accurate. On OS X, latency
>     estimates are based on information from the driver, and are usually
>     more accurate than on Windows, where the latency information is
>     often estimated based on grossly incomplete information. PA never
>     has access to information about all hardware latencies, so there are
>     always limits to the accuracy of latency information.
>     In summary: callback timestamps are subject to jitter (variance
>     between multiple measurements. Typically this is worse on Windows,
>     but not necessarily. And further, they are subject to
>     constant-offset error. The jitter can be reduced by filtering
>     multiple time-stamp measurements. The usual way to deal with offset
>     error is to do a calibrated loopback measurement -- but even this
>     can't compensate for asymmetrical input/output latency.
>     As you can see, it's a big subject. As I said above, without knowing
>     your requirements (and perhaps, how much time you're prepared to
>     invest in getting a good time value) then it's difficult to give a
>     more direct answer.
>     Ross.
>         Thanks,
>         Mengqi
>         On Sun, Mar 20, 2016 at 8:39 PM, Ross Bencina
>         <rossb-lists at audiomulch.com <mailto:rossb-lists at audiomulch.com>
>         <mailto:rossb-lists at audiomulch.com
>         <mailto:rossb-lists at audiomulch.com>>> wrote:
>              Hello Mengqi,
>              On 20/03/2016 7:58 AM, Mengqi Ren wrote:
>                  I'm using PortAudio to record audio signals. In my
>         project, I
>                  need to
>                  know the exact time when computer begins to record.
>              There is no such thing as "exact time". What are your
>         requirements?
>              What is your use-case?
>                  Would you please
>                  tell me when the recordCallback function is involved
>         and when
>                  the first
>                  frame of data enters the input buffer?
>              There are timestamps passed to the callback that you can
>         use. Their
>              accuracy will depend on the platform (you didn't mention your
>              platform). At stream start time they are likely to be at
>         their least
>              accurate. You might need to collect many timestamps and
>         then process
>              them to estimate the start time.
>                  Is there an API that can return
>                  the computer time when microphone begins to record?
>              A combination of the callback timestamps and Pa_GetStreamTime()
>              should get you there.
>              The rest depends on your requirements and the platform(s)
>         that you
>              are targeting.
>              This is old, but it might help:
>         http://www.rossbencina.com/static/writings/portaudio_sync_acmc2003.pdf
>              Ross.
>              _______________________________________________
>              Portaudio mailing list
>         Portaudio at lists.columbia.edu
>         <mailto:Portaudio at lists.columbia.edu>
>         <mailto:Portaudio at lists.columbia.edu
>         <mailto:Portaudio at lists.columbia.edu>>
>         https://lists.columbia.edu/mailman/listinfo/portaudio
>         _______________________________________________
>         Portaudio mailing list
>         Portaudio at lists.columbia.edu <mailto:Portaudio at lists.columbia.edu>
>         https://lists.columbia.edu/mailman/listinfo/portaudio
>     _______________________________________________
>     Portaudio mailing list
>     Portaudio at lists.columbia.edu <mailto:Portaudio at lists.columbia.edu>
>     https://lists.columbia.edu/mailman/listinfo/portaudio
> _______________________________________________
> Portaudio mailing list
> Portaudio at lists.columbia.edu
> https://lists.columbia.edu/mailman/listinfo/portaudio

More information about the Portaudio mailing list