python-samplerate
python-samplerate copied to clipboard
Int data is not converted to float properly
At this line you appear to be specifying that the dtype be converted to float32 before being sent into the low level library.
The problem is, since this is the audio domain, there is an expectation that float values are in the range -1.0 to 1.0.
Converting the data type from into to float just adds a decimal place, it doesn't do the division by (2^bitWidth) / 2 to properly scale to floating point.
May I suggest converting the data based on the sample bit width in your code?
I think this is a safe assumption, considering libsamplerate is targeted at audio processing.
Something like this:
dtype = samples.dtype
if dtype == np.int16:
samplesFloat = samples.astype(np.float32) / (2**16 / 2)
elif dtype == np.int32:
samplesFloat = samples.astype(np.float32) / (2**32 / 2)
else:
samplesFloat = samples.astype(np.float32)
I think that change absolutely makes sense, but the scaling of integer input should be documented properly. Feel free to prepare a PR.