Skip to content
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

Wrong formula used for BitCrush #396

Open
ashinkajay opened this issue Jan 28, 2025 · 0 comments · May be fixed by #397
Open

Wrong formula used for BitCrush #396

ashinkajay opened this issue Jan 28, 2025 · 0 comments · May be fixed by #397

Comments

@ashinkajay
Copy link

ashinkajay commented Jan 28, 2025

In the cpp file Bitcrush.h, the scaleFactor used to multiply the the array to create the representation using new bit depth is 2**bit_depth.
But this will result in a representation with more than the expected bits.

The expected formula is: scaleFactor = (2**new_bit_depth)/2+1
New formula reference

Here is a python code showing the number unique values in the actual and expected representations with a new bit depth

import numpy as np

def generate_audio(bit_depth, verbose=False):
    """
    generate a normalized audio with 2**bit_depth unique values
    """
    power = 2**bit_depth/2
    array = np.arange(-1*power, power+1)
    audio = (array/max(array))[1::]
    if verbose:
        print(f"Input bit_depth: {bit_depth}")
        print(f"Possible unique_values: {2**bit_depth}")
        print(f"Actual unique_values: {len(np.unique(audio))}")
    return audio

audio = generate_audio(bit_depth=16, verbose=True)
new_bit_depth = 4

new_formula_scale = (2**new_bit_depth)/2+1
pedalboard_scale = 2**new_bit_depth
aud_new_formula = np.round(audio*new_formula_scale)/new_formula_scale
aud_pedalboard = np.round(audio*pedalboard_scale)/pedalboard_scale

print(f"Possible unique values with new bitdepth of {new_bit_depth}: {2**new_bit_depth}")
print("Expected unique values with new formula:", len(np.unique(aud_new_formula)))
print("Pedalboard implementation unique values:", len(np.unique(aud_pedalboard)))

Output of the above code

Input bit_depth: 16
Possible unique_values: 65536
Actual unique_values: 65536
Possible unique values with new bitdepth of 4: 16
Expected unique values with new formula: 19
Pedalboard implementation unique values: 33

The error in pedalboard's implementation is huge compared to the new formula.
For a given audio with bit-depth of 16,
Example 1) when new bith depth is 4, pedalboard implementation gives a error of additional 17 unique values
Example 2) when new bith depth is 8, pedalboard implementation gives a error of additional 257 unique values

while the new formula always gives an error of additional 3 unique values

@ashinkajay ashinkajay linked a pull request Jan 28, 2025 that will close this issue
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 a pull request may close this issue.

1 participant