【发布时间】:2016-04-22 09:23:42
【问题描述】:
我正在尝试对声音文件进行简单的音量调整。我正在使用 python 2.7 和以下
图书馆:
import numpy as np
import scipy.io.wavfile as wv
import matplotlib.pyplot as plt
import pyaudio
import wave
我尝试了 2 种方法,我试图将声音放大 2 倍,即。 n=2。第一个是从这里改变的动态范围限制器方法 (http://bastibe.de/2012-11-02-real-time-signal-processing-in-python.html):
def limiter(self, n):
#best version so far
signal=self.snd_array
attack_coeff = 0.01
framemax=2**15-1
threshold=framemax
for i in np.arange(len(signal)):
#if amplitude value * amplitude gain factor is > threshold set an interval to decrease the amplitude
if signal[i]*n > threshold:
gain=1
jmin=0
jmax=0
if i-100>0:
jmin=i-100
else:
jmin=0
if i+100<len(signal):
jmax=i+100
else:
jmax=len(signal)
for j in range(jmin,jmax):
#target gain is amplitude factor times exponential to smoothly decrease the amp factor (n)
target_gain = n*np.exp(-10*(j-jmin))
gain = (gain*attack_coeff + target_gain*(1-attack_coeff))
signal[j]=signal[j]*gain
else:
signal[i] = signal[i]*n
print max(signal),min(signal)
plt.figure(3)
plt.plot(signal)
return signal
第二种方法是我进行硬膝压缩以将声音值的幅度降低到阈值以上,然后通过幅度增益因子放大整个信号。
def compress(self,n):
print 'start compress'
threshold=2**15/n+1000
#compress all values above the threshold, therefore limiting the audio amplitude range
for i in np.arange(len(self.snd_array)):
if abs(self.snd_array[i])>threshold:
factor=1+(threshold-abs(self.snd_array[i]))/threshold
else:
factor=1.0
#apply compression factor and amp gain factor (n)
self.snd_array[i] = self.snd_array[i]*factor*n
print np.min(self.snd_array),np.max(self.snd_array)
plt.figure(2)
plt.plot(self.snd_array,'k')
return self.snd_array
在这两种方法中,文件听起来都失真了。在幅度接近阈值的点处,音乐听起来削波和噼啪作响。我认为这是因为它在阈值附近“变平”。我尝试在限制器函数中应用指数,但即使我让它非常迅速地减小,它也不能完全消除噼啪声。如果我改变 n=1.5 声音不会失真。如果有人能给我任何关于如何消除噼啪声失真或链接到其他音量调制代码的指示,将不胜感激。
【问题讨论】: