Jus*_*eel 41
所述aubio库已经包裹SWIG并且因此可以被Python使用.在它们的许多特征中包括用于音调检测/估计的几种方法,包括YIN算法和一些谐波梳算法.
但是,如果你想要更简单的东西,我会在一段时间之前编写一些用于音高估计的代码,你可以接受或离开它.它不如在aubio中使用算法那么准确,但它可能足以满足您的需求.我基本上只是将数据的FFT乘以一个窗口(在这种情况下是一个Blackman窗口),平方FFT值,找到具有最高值的bin,并使用最大值的对数在峰值周围使用二次插值和它的两个相邻值来找到基频.我从一些论文中得到的二次插值.
它在测试音调上工作得相当好,但它不像上面提到的其他方法那样强大或准确.通过增加块大小可以提高准确度(或者通过减小块大小来减少).块大小应为2的倍数,以充分利用FFT.另外,我只是确定每个块的基本音高而没有重叠.在写出估计的音高时,我用PyAudio播放声音.
源代码:
# Read in a WAV and find the freq's
import pyaudio
import wave
import numpy as np
chunk = 2048
# open up a wave
wf = wave.open('test-tones/440hz.wav', 'rb')
swidth = wf.getsampwidth()
RATE = wf.getframerate()
# use a Blackman window
window = np.blackman(chunk)
# open stream
p = pyaudio.PyAudio()
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = RATE,
output = True)
# read some data
data = wf.readframes(chunk)
# play stream and find the frequency of each chunk
while len(data) == chunk*swidth:
# write data out to the audio stream
stream.write(data)
# unpack the data and times by the hamming window
indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),\
data))*window
# Take the fft and square each value
fftData=abs(np.fft.rfft(indata))**2
# find the maximum
which = fftData[1:].argmax() + 1
# use quadratic interpolation around the max
if which != len(fftData)-1:
y0,y1,y2 = np.log(fftData[which-1:which+2:])
x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
# find the frequency and output it
thefreq = (which+x1)*RATE/chunk
print "The freq is %f Hz." % (thefreq)
else:
thefreq = which*RATE/chunk
print "The freq is %f Hz." % (thefreq)
# read some more data
data = wf.readframes(chunk)
if data:
stream.write(data)
stream.close()
p.terminate()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48805 次 |
| 最近记录: |