How to handle audio buffer sizes that are not divisible by 64 when working with libpd?

224 Views Asked by At

libpd's API only allows to process audio in frames of 64 samples at a time. What I've done in the past is set the audio device buffer size to a number that is divisible by 64. No problems there.

Now, I need to make a Windows app that is ASIO compatible. I'm using RtAudio. The issue is that when I initialize my ASIO sound card (Roland FA-66) with the RtAudio API, it ignores the buffer size parameter and chooses one of it's own, which is not divisible by 64.

I thought of a workaround. This would take place in the audio callback function:

  1. Check if buffer size is not divisible by 64. If so:
  2. Tell libpd to process a number of frames larger than the needed size and store that in a temp buffer.
  3. Use memcpy to copy the right amount of frames from the temp buffer to the output buffer.
  4. Use memcpy to store the extra samples in another buffer (called extra) and use them in the following call to the audio callback.

I have not tried this but I think it will work.

However, I'd like to know if there is a "standard" or "well known" procedure for dealing with this problem. Maybe there are warnings or tips that I'm not aware of?

1

There are 1 best solutions below

0
On

Your approach seems valid. The only thing you need to be careful about is the memory alignment.

Here's an excellent (and very short) article about memcpy's pitfalls that may cause you headache: http://www.codepolice.org/c/memcpy.html