I have raw binary int16 data that I am converting to a numpy array using
audio = np.fromstring(raw_data, dtype=np.int16)
The data is audio data. When I convert the data to float32, the audio is getting distorted:
audio = audio.astype(np.float32, order='C')
I'm saving the audio to disk to listen to it using SoundFile:
soundfile.write('out.wav', audio, sample_rate)
If I write the audio directly to disk without doing the astype
operation, there is no distortion (ie);
# no distortion
audio = np.fromstring(raw_data, dtype=np.int16)
soundfile.write('out.wav', audio, sample_rate)
# distortion
audio = np.fromstring(raw_data, dtype=np.int16)
audio = audio.astype(np.float32, order='C')
soundfile.write('out.wav', audio, sample_rate)
What is the proper way to convert the data type here?
By convention, floating point audio data is normalized to the range of [-1.0,1.0] which you can do by scaling:
audio = audio.astype(np.float32, order='C') / 32768.0
This may fix the problem for you but you need to make sure that soundfile.write
writes a wav header that indicates float32. It may do that automatically based on the dtype of the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With