Skip to content

Fix audio resampling functionality #7858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 84 commits into
base: master
Choose a base branch
from

Conversation

sakertooth
Copy link
Contributor

@sakertooth sakertooth commented Apr 22, 2025

This PR improves the usage of libsamplerate when resampling.

Changes:

  • Redirect all libsamplerate usage to AudioResampler. This ensures we are always using libsamplerate with the same resampling logic, preventing bugs.

  • Call src_process as many times as necessary to resample all of the source audio we need to fill up the destination buffer. Before, LMMS was only calling src_process once, and assumed that libsamplerate always read all of the input data it gave it, which isn't necessarily true and can cause input frames to be dropped. Any input frames not read in the current iteration are stored within a small array in AudioResampler and will be used on the next call to src_process.

  • Remove the use of buffer margins. This was needed to accommodate for libsamplerate's transport delay, but this involved expensive copies and allocations, and the margin added may not be adequate depending on how long the transport delay needs to be, which wasn't accounted for. To fix this, we allow the transport delay to occur on the onset of when AudioResampler is first used, which then the delay will be removed on subsequent resampling.

TODO:

  • Fix GigPlayer (needs to use the write callback method instead of trying to write the data in blocks and "guess" how many input frames will be used)
  • Fix LADSPA (has the same problem GigPlayer had)

@sakertooth sakertooth marked this pull request as draft April 22, 2025 17:31
@sakertooth sakertooth marked this pull request as ready for review April 23, 2025 00:24
@sakertooth sakertooth changed the title Improve correctness of AudioResampler Fix audio resampling logic Apr 23, 2025
@sakertooth sakertooth marked this pull request as draft April 24, 2025 13:39
@sakertooth sakertooth marked this pull request as ready for review April 27, 2025 10:54
@sakertooth
Copy link
Contributor Author

sakertooth commented May 18, 2025

The recent changes did not fix 8000, 48000, 96000 sample rates sounding glitchy for me.

Please don't test right now, I think I broke some stuff and I am making some changes to the API. Maybe I should just stash my changes instead of pushing immediately if I want to back out on some of them, since other developers wont know.

Edit: This is why I drafted the PR. I am not done with it, but I am a bit in the wrong here because I kept backing out on this PR, if it wasn't ready then I should've waited until I got it in the state I wanted.

@sakertooth
Copy link
Contributor Author

I actually reverted back to the working version right after you sent this.

@sakertooth
Copy link
Contributor Author

sakertooth commented May 18, 2025

I am facing a dilemma where I am trying to make the resampler API as hard to use wrong as I can, because the problem with how libsamplerate was being used before was that callers would generate input frames and not check if all of what they generated was consumed, so those input frames will be dropped or "leak" in the next callback. I am trying to write an API that is hard to use wrong but also clean and simple, but maybe I cant have both honestly.

@bratpeki
Copy link
Member

Oh, yikes. Let me know if I can help any way. If this needs to expand into a set of PRs, that's fine, too.

@sakertooth
Copy link
Contributor Author

Oh, yikes. Let me know if I can help any way. If this needs to expand into a set of PRs, that's fine, too.

It's fine, its mostly me being a perfectionist. I have reverted the PR back to where it was before, if I have something that I know is what I want and is working, only then will I push.

@bratpeki
Copy link
Member

Okay, please let me know when testing can continue, either on Discord or by requesting a review.

@sakertooth
Copy link
Contributor Author

Okay, I think this is actually ready for review now (and testing assuming CI builds fine). I stopped overthinking and came up with a decent design. It should work well.

@sakertooth sakertooth marked this pull request as ready for review May 31, 2025 12:27
@sakertooth sakertooth requested review from szeli1 and bratpeki May 31, 2025 12:29
@sakertooth
Copy link
Contributor Author

One thing I want to do in the future after this is merged is making the interpolation feature in export actually work. This PR should make that process a lot simpler. I didn't want to do it here because it might be a bit disorienting since the changes will be all over the place.

The reason why -Wunused-variable being
treated as an error isn't a great idea.

1. Using [[nodiscard]]
2. Return value is checked only within an assert

But one could argue filling the remaining
partially filled buffer with silence is more
correct.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants