Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time between callback calls?

I have a lab project that uses mainly PyAudio and to further understand its way of working I made some measurements, in this case time between callbacks (using callback mode).

I timed it, and got an interesting result

(@256 chunk size, 44.1k fs): 0.0099701;0.0000365;0.0000201;0.0201579

This pattern goes on and on.

Between two longer calls, we have two shorter calls and sometimes the longer call is shorter (mind you I don't do anything else in the program than time the callbacks).

If we average this out we get our desired callback time:

1/44100 * 256 (roughly 5.8ms)

Here is my measurement visualized: enter image description here

So can someone explain what exactly happens here under the hood?

like image 655
function_store Avatar asked Apr 22 '14 08:04

function_store


People also ask

How does a callback function work?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.

What is callback event?

An event callback function is a function in a script that Monitoring Scripting Client calls in response to an event. In your event callback functions, provide code to generate statistics from the data in events.

What are callback functions why do we use them?

A JavaScript callback is a function which is to be executed after another function has finished execution. A more formal definition would be - Any function that is passed as an argument to another function so that it can be executed in that other function is called as a callback function.

Is callback a keyword in JavaScript?

For your top function, callback is the name of the third argument; it expects this to be a function, and it is provided when the method is called. It's not a language keyword - if you did a "find/replace all" of the word "callback" with "batmanvsuperman", it would still work.


1 Answers

What happens under the hood in PortAudio is dependent on a number of factors, including:

  • Which native audio API PortAudio is talking to
  • What buffer size and latency parameters you passed to Pa_OpenStream()
  • The capabilities of the audio hardware and its drivers, including its supported buffer sizes, buffering model and timing characteristics.

Under some circumstances PortAudio will request larger buffers from the native audio API and then invoke the PortAudio user callback multiple times in quick succession. This can happen if you have selected a small callback buffer size and a long latency.

Another scenario is that the native audio API doesn't support the buffer size that you requested for your callback size (framesPerBuffer parameter to Pa_OpenStream()). In this case PortAudio will be forced to use a driver-supported buffer size and then "adapt" between that buffer size and your callback buffer size. This adaption process can cause irregular timing.

Yet another possibility is that the native audio API uses a large ring buffer. Each time PortAudio polls the native host API, it will work to fill the native ring buffer by calling your callback as many times as needed. In this case irregular timing is related to the polling rate.

The above are not the only possibilities.

One likely explanation of what is happening in your case is that PortAudio is calling your callback 3 times in fast succession (a guess would be that the native buffer size is 3x your callback buffer size), for one of the reasons above.

Another possibility is that the native audio subsystem is signalling PortAudio irregularly. This can happen if a system layer below PortAudio is doing similar kinds of buffering to what I described above. I have seen this happen with DirectSound on Windows 7 for example. ASIO4ALL drivers will exhibit +/- 1ms jitter (which is not what you're seeing).

You can try reducing the requested stream latency to 0 and see if that changes the result. This will force double-buffering, which may or may not produce stable output. Another thing to try is to use the paFramesPerBufferUnspecified parameter, which will cause the callback to be called with the native buffer size -- then you can observe whether there is greater periodicity, what that buffer size is, and also whether the buffer size varies from callback to callback.

You didn't say which operating system and host API you're targetting, so it's hard to give more specific details than the above.

The internal buffering models used by the various PortAudio host API backends are described in some detail on the PortAudio wiki.

To answer a related question: why is it like this? Aside from the cases where it is a function of the lower layers of the native audio subsystem, or the buffer adaption process, it is often a result of specifying a large suggested latency to Pa_OpenStream(). Some PortAudio host APIs will relax the buffer periodicity if the specified latency is very high, in order to reduce system load that would be caused by high-frequency timer callbacks.

like image 160
Ross Bencina Avatar answered Dec 20 '22 15:12

Ross Bencina