rubato icon indicating copy to clipboard operation
rubato copied to clipboard

Question about chunk size

Open obsoleszenz opened this issue 1 year ago • 7 comments

I'm currently trying to make my dj engine (https://codeberg.org/obsoleszenz/librecdj/) sample accurate. Until now I used SincFixedOut and whenever the chunk size from the audio host changed, i offloaded the creation of a new SincFixedOut<T> on a worker thread. Buf to make it sample accurate, i want to partition the buffer into smaller buffers, according to the midi events and their timestamp. The slices would be all of different sizes, meaning i would need multiple Resamplers. As this is a realtime context i can't heap allocate a new Resampler and I wonder if it would be possible to change the chunk size on an existing resampler? Or am I overseeing something?

obsoleszenz avatar May 28 '24 22:05 obsoleszenz

It would be possible to change the chunk size of the SincFixedIn/Out and FastFixedIn/Out. I think the easiest way to do this while avoiding allocations would be to only allow values equal to or smaller than what the resampler was created with. So you create a resampler with the largest chunksize you expect to need, and then reduce it to what is needed at the moment.

There could then be s trait method called set_chunksize or something like that, that changes the input or output chunksize depending on the resampler type (or an error if this isn't supported or if the new value is too large).

HEnquist avatar May 29 '24 20:05 HEnquist

It would be possible to change the chunk size of the SincFixedIn/Out and FastFixedIn/Out. I think the easiest way to do this while avoiding allocations would be to only allow values equal to or smaller than what the resampler was created with. So you create a resampler with the largest chunksize you expect to need, and then reduce it to what is needed at the moment.

There could then be s trait method called set_chunksize or something like that, that changes the input or output chunksize depending on the resampler type (or an error if this isn't supported or if the new value is too large).

Nice! This sounds really good (:

Only allowing chunk sizes <= the initial max_chunk_size would be perfect and all that i need. I actually started playing around with this idea, but i hit an issue in the internals. The needed_input_size became 0 after the first process call. I opened a PR here, but maybe makes sense to start over again: https://github.com/HEnquist/rubato/pull/81

obsoleszenz avatar May 29 '24 21:05 obsoleszenz

That PR has a lot more changes than what is required! See https://github.com/HEnquist/rubato/pull/82

It's a draft and only implemented for SincFixedIn/Out so far but I think it works as it should. Can you try it?

HEnquist avatar May 30 '24 19:05 HEnquist

That PR has a lot more changes than what is required! See #82

It's a draft and only implemented for SincFixedIn/Out so far but I think it works as it should. Can you try it?

Ouh nice! Thank you :) I will give it a spin but might need a couple of days.

obsoleszenz avatar May 31 '24 10:05 obsoleszenz

That PR has a lot more changes than what is required! See #82

It's a draft and only implemented for SincFixedIn/Out so far but I think it works as it should. Can you try it?

Sorry, I didn't find time yet to play around with this further, have to fix my software to play a gig this week and I was a bit overoptimisitic to pull off the major change of a sample accurate audio engine ^^ So I hacked in a intermediate solution and come back to the proper way after the gig :) So sorry for no update...

obsoleszenz avatar Jun 17 '24 01:06 obsoleszenz

@HEnquist I finally found the time and brain to implement sample accurate playback on my side and it works quite good so far :) https://codeberg.org/obsoleszenz/librecdj/pulls/47/files#diff-d075ab60878d56dab23e87a12135b50c80e0fa83

Thanks a lot for implementing this <3

obsoleszenz avatar Aug 21 '24 23:08 obsoleszenz

Great, thanks! Then I'll include this in the next release.

HEnquist avatar Aug 24 '24 20:08 HEnquist

This is included in the v0.16.0 release.

HEnquist avatar Sep 28 '24 09:09 HEnquist