使用 SciPy 中的 wavfile.write 在 Python 中编写 wav 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18645544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing wav file in Python with wavfile.write from SciPy
提问by JVE999
I have this code:
我有这个代码:
import numpy as np
import scipy.io.wavfile
import math
rate, data = scipy.io.wavfile.read('xenencounter_23.wav')
data2 = []
for i in range(len(data)):
data2.append([int(round(math.sin(data[i][0])*3000)), int(round(math.sin(data[i][1])*3000))])
data2 = np.asarray(data2)
print data2
scipy.io.wavfile.write('xenencounter_23sin3.wav',rate,data2)
This prints (truncated):
这打印(截断):
[[-2524 2728]
[ -423 -2270]
[ 2270 423]
...,
[-2524 0]
[ 2524 -2728]
[-2270 838]]
The wav file opens and plays in Windows Media Player, so at least its the proper format. However, when opening it with Audacity and looking at the individual samples, they're all 0, and concordantly the file plays no sound at all.
wav 文件在 Windows Media Player 中打开并播放,因此至少它的格式正确。但是,当用 Audacity 打开它并查看单个样本时,它们都是 0,并且相应地文件根本没有播放声音。
What I don't understand is how that numpy array listed above becomes all 0's. It should be below the maximum value for a sample (or above, if it's negative).
我不明白的是上面列出的 numpy 数组是如何变成全 0 的。它应该低于样本的最大值(或高于,如果它是负数)。
采纳答案by JVE999
I found that scipy.io.wavfile.write() writes in 16-bit integer, which explains the larger file sizes when trying to use a 32-bit integer (the default) instead. While I couldn't find a way to change this in wavfile.write, I did find that by changing:
我发现 scipy.io.wavfile.write() 以 16 位整数写入,这解释了在尝试使用 32 位整数(默认值)时更大的文件大小。虽然我在 wavfile.write 中找不到改变它的方法,但我确实通过改变发现了这一点:
data2 = np.asarray(data2)
to
到
data2 = np.asarray(data2, dtype=np.int16)
I could write a working file.
我可以写一个工作文件。
回答by Mike Vella
As you discovered by printing out the output at different points and re-saving what you originally loaded, the line data2.append([int(round(math.sin(data[i][0])*3000)), int(round(math.sin(data[i][1])*3000))])
is the source of the problem.
正如您通过在不同点打印输出并重新保存最初加载的内容所发现的那样,该行data2.append([int(round(math.sin(data[i][0])*3000)), int(round(math.sin(data[i][1])*3000))])
是问题的根源。
I suspect that 3000 is too large of an amplitude. Try 1.
我怀疑 3000 的幅度太大了。尝试 1。
回答by Milothicus
In creating wav files through scipy.io.wavfile.write(), i found that the amplitude is very important. if you create a sine wave with amplitude 150, it sounds like silence when played in VLC. if the amplitude is 100, it sounds like a distorted sine wave, and if you make it 80, it starts to sound like a normal file.
在通过 scipy.io.wavfile.write() 创建 wav 文件时,我发现幅度非常重要。如果您创建振幅为 150 的正弦波,则在 VLC 中播放时听起来像静音。如果振幅为 100,则听起来像失真的正弦波,如果设为 80,则听起来像普通文件。
Definitely have to be careful about the amplitude when creating wave files, but it's not clear to me right now what the maximum level is before it starts clipping or disappearing.
创建波形文件时绝对必须注意幅度,但我现在不清楚它开始削波或消失之前的最大电平是多少。