Insert to audiobuffer, remove from audiobuffer, etc

Is there a simple way to insert a block of (stereo) audio from one audiobuffer into a designated point of another audiobuffer?
and likewise, to remove a block from a buffer (and subsequently resize it)

(basically, to do what MemoryBlock::insert, and MemoryBlock::removeSection do, but on audiobuffers)

Interesting question. AFAIK there isn’t. You will have to go with AudioBuffer::setSize() and AudioBuffer::copyFrom()

The interesting bit is if your subsequent data is overlapping, so not to overwrite before it is being read.

If you can afford the memory, I would always copy from one buffer to the other, stitching all together (and taking care of crossfades in that moment, using copyFromWithGainRamp and addFromWithGainRamp).

Also it will be more performant in terms of cache instead of working your way backwards…

1 Like

Thanks Daniel, you are always so helpful. I hope the Juce team is appreciating your efforts.

My problem is that i may not be able to afford so much memory. I may be dealing with audio files of an hour or more.

It’s looking like i may have to hack into the audioBuffer class and add a few functions such as these that make use of the low level std library functions like std::memmove.

It’d just be easier if someone already handled it, of course.

Thank you, but no worries, it is my free choice to do so :wink:
I love learning and sharing by reading into problems and stuff…

In this case AudioBuffer is no option at all, since that would mean, the audio needs to be in the memory entirely. In this scenario I would create an AudioFormatReader to read the original audio file and write in a new AudioFormatWriter processing buffer by buffer.
I created for that purpose a class, that handles crossfades in that scenario, holding two AudioFormatReaderSources, reading from one, until the position is reached, where you want to crossfade, create the buffers as combination of copyFromWithGainRamp and addFromWithGainRamp for the in-fading source, and when done just swapping the sources, disposing the spare.
Maybe you can create something similar…

While it might be a nice addition to have the ability to move audio within the block, it wouldn’t solve your problem, for the mentioned reasons…

Good luck!

1 Like

Thanks again Daniel.

Actually, i do want to keep all the audio in memory, even if it is an hour long. But i was hoping not to have to double that memory usage while performing move functions. The documentation i read for std::memmove says:

“Despite being specified “as if” a temporary buffer is used, actual implementations of this function do not incur the overhead of double copying or extra memory. For small count, it may load up and write out registers; for larger blocks, a common approach (glibc and bsd libc) is to copy bytes forwards from the beginning of the buffer if the destination starts before the source, and backwards from the end otherwise, with a fall back to std::memcpy when there is no overlap at all.”

( )

I noticed that Juce’s MemoryBlock class also uses std::memmove for its insert and removeSection methods.

Thanks for the tips on crossfades btw. I was going to look for zero crossings and try to cut/paste at those points, but actually a simple 64 sample crossfade on the start and end points is probably a better way for me to go with this one.