fix(dither): correct HighPass channel state and bit depth calculation #807
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR corrects the way
HighPass
dithering works with stereo or multi-channel audio, that I should have caught in the first place. Throwing a couple of other things in while at it.Problem
The
HighPass
dither usesBlue
noise, which is a high-pass filtered noise (white - prev_white
). The problem was that in any audio beyond mono, we only had a single Blue noise generator. This meant theprev_white
state was leaking across channel boundaries. This broke the high-pass filter characteristic and created correlation between channels.Solution
Now
HighPass
dither creates per-channel generators with independent RNG seeds, so each channel maintains its ownprev_white
state. The other algorithms don't need this since they're stateless.The implementation also handles dynamic channel count and sample rate changes at span boundaries.
Other improvements
Changed LSB amplitude calculation to use
f64
intermediate precision to avoid overflow and precision loss. This makes the code more practical and correct for even the most theoretical of audio bit depths.Added
size_hint()
andExactSizeIterator
implementation forDither
.