问题描述
我有一个2秒16bit的单信道8kHz的wav文件,我需要改变它的体积。
I have a 2 seconds 16bit single channel 8khz wav file and I need to change its volume.
它应该是相当简单的,因为改变量是相同的改变的信号的幅度,以及我只需要衰减它,即乘以对于0和1之间的数字但它不工作:在新的声音较低,但非常充分的噪音。我在做什么错了?
It should be quite straightforward, because changing the volume is the same as changing the amplitude of the signal, and I just need to attenuate it, that is to multiply it for a number between 0 and 1. But it doesn't work: the new sound is lower but VERY full of noise. What am I doing wrong?
下面是我的code:
import wave, numpy, struct
# Open
w = wave.open("input.wav","rb")
p = w.getparams()
f = p[3] # number of frames
s = w.readframes(f)
w.close()
# Edit
s = numpy.fromstring(s, numpy.int16) * 5 / 10 # half amplitude
s = struct.pack('h'*len(s), *s)
# Save
w = wave.open("output.wav","wb")
w.setparams(p)
w.writeframes(s)
w.close()
感谢你们!
推荐答案
正如你在问题的意见看,有几种解决方案,一些更有效率。
As you can see in the comments of the question, there are several solutions, some more efficient.
立即由Jan德沃夏克(以下简称* 5部分裁剪和溢出),并发现问题的简单的解决办法是:
The problem was immediately detected by Jan Dvorak ("the * 5 part is clipping and overflowing") and the straightforward solution was:
s = numpy.fromstring(s, numpy.int16) / 10 * 5
在这种情况下,这种解决方案很适合我,只是不够好。
In this case, this solution was perfect for me, just good enough.
感谢各位乡亲!
这篇关于更改蟒蛇WAV文件的音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!