Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert numpy int16 audio array to float32

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?

like image 434
deef Avatar asked Mar 02 '17 00:03

deef


1 Answers

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.

like image 99
jaket Avatar answered Nov 11 '22 07:11

jaket