将numpy int16音频数组转换为float32 [英] convert numpy int16 audio array to float32

查看:868
本文介绍了将numpy int16音频数组转换为float32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有原始的二进制int16数据,正在使用

I have raw binary int16 data that I am converting to a numpy array using

audio = np.fromstring(raw_data, dtype=np.int16)

数据是音频数据.当我将数据转换为float32时,音频会失真:

The data is audio data. When I convert the data to float32, the audio is getting distorted:

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

我将音频保存到磁盘以使用SoundFile收听:

I'm saving the audio to disk to listen to it using SoundFile:

soundfile.write('out.wav', audio, sample_rate)

如果我不执行astype操作而直接将音频直接写入磁盘 ,则不会出现失真(即);

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?

推荐答案

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

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

这可能为您解决了该问题,但是您需要确保soundfile.write编写一个wav标头,该标头指示float32.它可以根据数组的dtype自动执行此操作.

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.

这篇关于将numpy int16音频数组转换为float32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆