Python 声音库
我在找一个可以用来处理声音的Python库,想要它能告诉我每一帧的音量是多少。
或者我也想要一个可以做噪音门的软件,最好是命令行工具。
谢谢!!
4 个回答
0
pyo这个库里面有很多很实用的声音处理和合成工具。
from pyo import *
s = Server().boot()
s.start()
sf = SfPlayer('input.aif', speed=[1,.5], loop=True)
gt = Gate(sf, thresh=-24, risetime=0.005, falltime=0.01, lookahead=5, mul=.4).out()
3
Python有一个内置的wave模块。
import wave
import struct
import numpy
# read in the data string
fin = wave.open("input.wav", "rb")
data_string = fin.readframes(fin.getnframes())
wav_params = fin.getparams()
fin.close()
# convert to volume
unpacked = struct.unpack("%dB"%(len(data_string)), data_string)
unpacked = [x**2 for x in unpacked]
# here's the volume
volume = [20 * numpy.log10(numpy.sqrt(i)) for i in unpacked]
noise_level = 40 # 'noise' level
# filter out values below limit
outstring = ""
for i in range(len(data_string)):
if volume[i] > noise_level:
outstring += data_string[i]
else:
outstring += "\0"
# write result to new file
fout = wave.open("output.wav", "wb")
fout.setparams(wav_params)
fout.writeframes(outstring)
fout.close()
这是第一次尝试.. 需要针对较大文件进行优化。特别感谢这篇博客文章。
3
snack这个库可以做到这一点。特别是,这个库支持多种音频文件格式,比如WAV、AU、AIFF、MP3、CSL、SD、SMP和NIST/Sphere文件。它可以播放声音,还可以进行功率谱分析和过滤。