Ejemplo n.º 1
0
def MIDIPitchByFileName(name, midiPitchMethod, handle, notfound=-1):
	# midiPitchMethod == 'filename' || int/float || f0-seg || centroid-seg
	if midiPitchMethod == 'filename':
		fileHead = os.path.splitext(os.path.split(name)[1])[0]
		test = getMidiPitchFromString(fileHead)
		if test != False: return test
		else: return notfound
	elif midiPitchMethod == 'composite':
		test = getMidiPitchFromString( os.path.splitext(os.path.split(name)[1])[0] )
		if test != False: return test
		else: return MIDIPitchByFileName(name, 'f0-seg', handle)
	elif midiPitchMethod == 'centroid-seg':
		return util.frq2Midi(handle.desc.get('centroid-seg'))
	elif midiPitchMethod == 'f0-seg':
		f0 = handle.desc.get('f0-seg')
		if f0 != 0:
			return util.frq2Midi(f0)
		else:
			return notfound
Ejemplo n.º 2
0
    def analize_spectralPeaks(self,
                              filepath,
                              minamp=0.001,
                              fftsize=2048,
                              hop_length=2048,
                              min_midi=20,
                              max_midi=108):
        import librosa
        import peakutils
        checksum = util.listToCheckSum([
            filepath, fftsize, hop_length, minamp, min_midi, max_midi, 'peaks'
        ])[:12]
        filehead = '%s-%s' % (os.path.splitext(
            os.path.split(filepath)[1])[0], checksum)
        outputpath = os.path.join(self.analdir, '%s-peaks.json' % (filehead))

        if os.path.exists(outputpath):
            fh = open(outputpath, 'r')
            return json.load(fh)
        y, sr = librosa.load(filepath, sr=None, mono=True)
        outputdata = {'sr': sr, 'fftsize': fftsize, 'peaks': []}
        halfwin = int(fftsize / 2)
        binsize = sr / float(fftsize)
        lins = np.linspace(0, halfwin - 1, num=halfwin + 1)

        S = np.abs(librosa.stft(y, n_fft=fftsize, hop_length=hop_length))

        for fidx, frame in enumerate(np.rot90(S)):
            indexes = peakutils.indexes(frame, thres=minamp, min_dist=10)
            # interpolate indexes for better frq accuracy
            interp_indexes = peakutils.interpolate(lins, frame, ind=indexes)
            timesec = fidx * (float(hop_length) / sr)
            framepeaks = []
            for ii in interp_indexes:
                if ii < 0: continue
                midi = util.frq2Midi(ii * binsize)
                if midi < min_midi or midi > max_midi: continue
                iamp = util.ampToDb(
                    np.interp(ii, lins, frame) * (2. / float(fftsize)))
                framepeaks.append([iamp, midi])
            framepeaks.sort(reverse=True)
            outputdata['peaks'].append([timesec, framepeaks])

        fh = open(outputpath, 'w')
        json.dump(outputdata, fh)
        return outputdata