def computeNoveltyCurve(self, filename, frameSize=1024, hopSize=512, windowType='hann', weightCurveType='inverse_quadratic', sampleRate=44100.0, startTime=0, endTime=2000): loader = EasyLoader(filename=filename, startTime=startTime, endTime=endTime, sampleRate=sampleRate, downmix='left') fc = FrameCutter(frameSize=frameSize, hopSize=hopSize, silentFrames="keep", startFromZero=False, lastFrameToEndOfFile=True) window = Windowing(type=windowType, zeroPhase=True, zeroPadding=1024 - frameSize) freqBands = FrequencyBands( sampleRate=sampleRate) # using barkbands by default pool = Pool() spec = Spectrum() loader.audio >> fc.signal fc.frame >> window.frame >> spec.frame spec.spectrum >> freqBands.spectrum freqBands.bands >> (pool, 'frequency_bands') essentia.run(loader) noveltyCurve = std.NoveltyCurve(frameRate=sampleRate / float(hopSize), weightCurveType=weightCurveType)( pool['frequency_bands']) return noveltyCurve
frameSize = 2048 hopSize = 128 weight = 'hybrid' print("Frame size: %d" % frameSize) print("Hop size: %d" % hopSize) print("weight: %s" % weight) audio = es.MonoLoader(filename=input_file)() w = es.Windowing(type='hann') s = es.Spectrum() freq_bands = es.FrequencyBands() bands_energies = [] for frame in es.FrameGenerator(audio, frameSize=frameSize, hopSize=hopSize): bands_energies.append(freq_bands(s(w(frame)))) novelty = es.NoveltyCurve(frameRate=44100. / hopSize, weightCurveType=weight)(numpy.array(bands_energies)) bpm, candidates, magnitudes, tempogram, _, ticks, ticks_strength, sinusoid = es.BpmHistogram( frameRate=44100. / hopSize)(novelty) print("BPM = %0.1f" % bpm) #pylab.plot(novelty) #pylab.show() pylab.matshow(tempogram.transpose(), origin='lower', aspect='auto') pylab.show()
def computeNoveltyCurve(filename, pool): loader = EasyLoader(filename=filename, sampleRate=pool['samplerate'], startTime=STARTTIME, endTime=ENDTIME, downmix=pool['downmix']) fc = FrameCutter(frameSize=int(pool['framesize']), silentFrames='noise', hopSize=int(pool['hopsize']), startFromZero=False) window = Windowing(type=pool['window'], zeroPhase=False) #freqBands = FrequencyBands(frequencyBands=EqBands, sampleRate=pool['samplerate']) freqBands = FrequencyBands(sampleRate=pool['samplerate']) spec = Spectrum() hfc = HFC() loader.audio >> fc.signal fc.frame >> window.frame >> spec.frame spec.spectrum >> freqBands.spectrum spec.spectrum >> hfc.spectrum freqBands.bands >> (pool, 'frequency_bands') hfc.hfc >> (pool, 'hfc') essentia.run(loader) pool.set('size', loader.audio.totalProduced()) pool.set('length', pool['size'] / pool['samplerate']) # compute a weighting curve that is according to frequency bands: frequencyBands = pool['frequency_bands'] nFrames = len(frequencyBands) weightCurve = np.sum(frequencyBands, axis=0) weightCurve = [val / float(nFrames) for val in weightCurve] weightCurve = essentia.normalize(weightCurve) #pyplot.plot(weightCurve) #pyplot.show() noveltyCurve = std.NoveltyCurve(frameRate=pool['framerate'], weightCurveType=pool['weight'], weightCurve=weightCurve)(frequencyBands) #for x in noveltyCurve: pool.add('novelty_curve', x) #return # derivative of hfc seems to help in finding more precise beats... hfc = essentia.normalize(pool['hfc']) dhfc = essentia.derivative(hfc) for i, val in enumerate(dhfc): if val < 0: continue noveltyCurve[i] += val # low pass filter novelty curve: env = std.Envelope(attackTime=2. / pool['framerate'], releaseTime=2. / pool['framerate'])(noveltyCurve) # apply median filter: windowSize = 8 #samples size = len(env) filtered = zeros(size) for i in range(size): start = i - windowSize if start < 0: start = 0 end = start + windowSize if end > size: end = size start = size - windowSize filtered[i] = env[i] - np.median(env[start:end]) if filtered[i] < 0: filtered[i] = 0 #pyplot.subplot(311) #pyplot.plot(noveltyCurve) #pyplot.subplot(312) #pyplot.plot(env, 'r') #pyplot.subplot(313) #pyplot.plot(filtered, 'g') #pyplot.show() #for x in noveltyCurve: pool.add('novelty_curve', x) for x in filtered: pool.add('novelty_curve', x)