rust-samplerate icon indicating copy to clipboard operation
rust-samplerate copied to clipboard

Add process_into* variants to allow utilizing an existing buffer

Open dsonck92 opened this issue 3 years ago • 1 comments

For some applications, it can be useful to provide an existing buffer as it may already exist (and potentially saves up on allocating memory during a tight loop). The regular process function always creates a Vec which would internally always allocate memory, and usually deallocate shortly after it's used.

  • This PR adds 2 functions:
    • process_into, which does the same as process but writes the result in a user supplied buffer and returns a tuple of the amount of data consumed and produced
    • process_into_last, which does the same for the process_last
  • Adds an example to outline its use
  • Adds a test similar to samplerate_conversion but utilizing the into functions instead

I have to note that I explicitly didn't implement the same thing for convert (as the fork of xhalo32 adds) because convert must not be called in a loop, and thus an in-place allocation of the output buffer is not performance critical (Either the user allocates in one go, potentially messing up the calculation. Or the function allocates in one go).

In fact, I believe allowing such a use might even give the wrong impression.

dsonck92 avatar Oct 10 '21 12:10 dsonck92