Skip to content

Wrong formula used for BitCrush #396

Open
@ashinkajay

Description

@ashinkajay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions