简体   繁体   English

将 WAV 格式从 32 位浮点数更改为 16 位 PCM

[英]Changing WAV format from 32-bit floating point to 16 bit PCM

I'm trying to convert my 32-bit floating point sampled WAVE file to 16-bit integer WAVE file.我正在尝试将我的 32 位浮点采样 WAVE 文件转换为 16 位整数 WAVE 文件。

I want to generate 16-bit integer WAV file from ultrasonic sine wave which range from -1.414 to 1.414.我想从范围从 -1.414 到 1.414 的超声波正弦波生成 16 位整数 WAV 文件。 In SciPy.io.wavfile , the output format is fixed according to type array which I generated.SciPy.io.wavfile 中,输出格式根据我生成的类型数组固定。 But I changed my 32-bit float format array to 16-bit by multiplying '32767' with casting to get integer format value, but the result frequency is not similar at all.但是我通过将 '32767' 与强制转换相乘以获取整数格式值,将我的 32 位浮点格式数组更改为 16 位,但结果频率根本不相似。

Is there any way to change my from 32-bit floating point to 16-bit PCM using SciPy library?有什么办法可以使用 SciPy 库将我的 32 位浮点数更改为 16 位 PCM?

Not sure about using SciPy for this but you could easily get your data into a 16bit integer and then use what ever audio package you like to write it to a wav file.不确定是否为此使用SciPy ,但您可以轻松地将数据转换为 16 位整数,然后使用您喜欢的任何音频包将其写入wav文件。

Divide your data by 1.414 so that it is in the range [-1, 1], then multiply it by 32767 so that it is within the range of an int16 variable.将您的数据除以 1.414,使其在 [-1, 1] 范围内,然后将其乘以 32767,使其在 int16 变量的范围内。 Then cast it using numpy.astype() .然后使用numpy.astype()投射它。 The solution would look something like this:解决方案如下所示:

import numpy as np

float32_data /=1.414
float32_data *= 32767
int16_data = float32_data.astype(np.int16)

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

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