由于wav 文件基本上是原始音频数据,如果没有“原始音频处理”,您将无法更改音高。
这是你可以做的。
您将需要 wave(标准库)和 numpy 模块。
import wave
import numpy as np
打开文件。
wr = wave.open('input.wav', 'r')
# Set the parameters for the output file.
par = list(wr.getparams())
par[3] = 0 # The number of samples will be set by writeframes.
par = tuple(par)
ww = wave.open('pitch1.wav', 'w')
ww.setparams(par)
声音应该在几分之一秒内处理完毕。这减少了混响。尝试将fr 设置为 1;你会听到烦人的回声。
fr = 20
sz = wr.getframerate()//fr # Read and process 1/fr second at a time.
# A larger number for fr means less reverb.
c = int(wr.getnframes()/sz) # count of the whole file
shift = 100//fr # shifting 100 Hz
for num in range(c):
读取数据,将其拆分为左右声道(假设为立体声 WAV 文件)。
da = np.fromstring(wr.readframes(sz), dtype=np.int16)
left, right = da[0::2], da[1::2] # left and right channel
使用 numpy 内置的快速傅里叶变换提取频率。
lf, rf = np.fft.rfft(left), np.fft.rfft(right)
滚动阵列以增加音高。
lf, rf = np.roll(lf, shift), np.roll(rf, shift)
最高频率滚动到最低频率。这不是我们想要的,所以将它们归零。
lf[0:shift], rf[0:shift] = 0, 0
现在使用傅里叶逆变换将信号转换回幅度。
nl, nr = np.fft.irfft(lf), np.fft.irfft(rf)
结合两个渠道。
ns = np.column_stack((nl, nr)).ravel().astype(np.int16)
写入输出数据。
ww.writeframes(ns.tostring())
处理完所有帧后关闭文件。
wr.close()
ww.close()