简体   繁体   English

将numpy int16音频数组转换为float32

[英]convert numpy int16 audio array to float32

I have raw binary int16 data that I am converting to a numpy array using 我有原始二进制int16数据,正在使用将其转换为numpy数组

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: 当我将数据转换为float32时,音频会失真:

audio = audio.astype(np.float32, order='C')

I'm saving the audio to disk to listen to it using SoundFile: 我将音频保存到磁盘以使用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); 如果我执行astype操作将音频直接写入磁盘,则不会出现失真(即);

# 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: 按照惯例,浮点音频数据被归一化为[-1.0,1.0]的范围,您可以通过缩放来实现:

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. 这可能为您解决了该问题,但是您需要确保soundfile.write写入一个wav标头,该标头指示float32。 It may do that automatically based on the dtype of the array. 它可以根据数组的dtype自动执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM