What is the max buffer size in a portaudio callback?

49 Views Asked by At

I’m using PortAudioSharp to get input audio for processing. The data is delivered to my code in adaptive-length chunks (FramesPerBufferUnspecified) of unmanaged system memory via a callback. Code in a callback must be quick and must avoid duration-risky behaviors, such as allocating memory from the heap.

I need to be able to do nontrivial processing in F#/C#.

The callback doesn’t release its chunk explicitly. Clearly each chunk is guaranteed valid for the duration of a quick callback, but how long exactly is it guaranteed valid?

To be as safe as possible given the constraints and given the lack of a chunk-lifetime guarantee, I have to hand off the chunk to my own memory as quickly as possible.

One idea is for the callback simply to send each chunk to an via a ConcurrentQueue. There is no guarantee how long a chunk can wait in the queue before becoming invalid, so the queue-consuming code immediately copies the chunk to managed memory and passes it to an async function for processing. There is a catch here: the chunk could become invalid before the agent gets around to making a copy of it.

A safer plan is for the callback to copy each chunk into an existing buffer from a pool. All buffers are preallocated to the same size, large enough for the largest possible chunk.

The question is, how big does the buffer size have to be to cover the maximum possible variable chunk size? I can’t find any discussion of this in the docs.

Relevant docs:

0

There are 0 best solutions below

Related Questions in F#