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?