def get_onsets(self, _audio=[]): if _audio != []: audio = _audio else: audio = self.audio W = es.Windowing(type=self.winType) c2p = es.CartesianToPolar() fft = es.FFT() onsetDetection = es.OnsetDetection(method=self.onsetMethod, sampleRate=44100) onsets = es.Onsets(alpha=.2) # onsetIndex = [] pool = Pool() for frame in es.FrameGenerator(audio, frameSize=1024, hopSize=512): mag, phase, = c2p(fft(W(frame))) onsetDetection.configure(method=self.onsetMethod) onsetFunction = onsetDetection(mag, phase) pool.add("onsetFunction", onsetFunction) DetectedOnsetsArray = onsets([pool["onsetFunction"]], [1]) return DetectedOnsetsArray
def piano_timing_features(anno_file, audio_file, latency, bpm, max_spectral_centroid=3500, onset_threshold=2, series_delta=0.22, sample_rate=44100): bars, beats, events, chords = symbolic_analysis.rhythm_for_file(anno_file) beats = np.array(beats) events = np.array(events) is_defined = [x[0] != 'N' for x in chords] chords = chords[is_defined] events = events[is_defined] # LOAD AUDIO audio = ess.MonoLoader(filename=audio_file)() duration = float(len(audio)) / sample_rate half_ibi = (beats[1:] - beats[:-1]).mean() / 2 start = max(events[0] - half_ibi, 0) end = min(events[-1] + half_ibi, duration) # LOAD BEATS FROM AUDIO onset_func = ess.OnsetDetectionGlobal()(audio) # CHANGE SILENCE THRESHOLD DEPENDING ON THE BPM silence_th = 0.2 if bpm >= 40 and bpm < 50: silence_th = 0.2 if bpm >= 50 and bpm < 60: silence_th = 0.15 if bpm >= 60 and bpm < 70: silence_th = 0.1 if bpm >= 70 and bpm < 80: silence_th = 0.05 if bpm >= 80: silence_th = 0.02 # COMPUTE ONSETS FROM AUDIO onsets = np.array( list( ess.Onsets(alpha=1, silenceThreshold=silence_th)([onset_func], [1]))) # COMPUTE DEVIATIONS BETWEEN ANNOTATION AND COMPUTED ONSETS devs = feature_extraction.attack_deviations(events, onsets, start, end) f, p, r = onset_measures(events, onsets, f_measure_threshold=0.25) features = { 'onsets': onsets, 'devs': devs, 'f_measure': f, 'precision': p, 'recall': r } return features
def __onset_decider__(self, noise_threshold): e_onsets = e.Onsets(silenceThreshold=noise_threshold, frameRate=44100 / 512.0) onsetdetectsM = [self.onset_candidates] onsetresults = e_onsets(onsetdetectsM, [1]) self.onset_times = onsetresults self.onset_count = len(self.onset_times) self.__get_onset_frames__() self.__get_onset_energies__() self.onset_signal = self.__time2signal__(self.onset_times)
def OnsetsSegmentation(audio, frame_size=1024, frame_hop=512, windowing_type='hann', onsets_method='hfc'): #declaração dos algoritmos que serão usados spec = es_mode.Spectrum() fft = es_mode.FFT() c2p = es_mode.CartesianToPolar() od1 = es_mode.OnsetDetection(method=onsets_method) w = es_mode.Windowing(type=windowing_type) pool = es.Pool() #Função que será executada a cada frame def F(n): spectrum = spec(w(n)) mag, phase, = c2p(fft(w(n))) pool.add('features.spectrum', spectrum) pool.add('features.', phase) pool.add('features.onsetdetection', od1(spectrum, phase)) #define a função contínua de onsets para cada frame qtdFrames = inFrames(audio=audio, algorithm=F, frameSize=frame_size, hopSize=frame_hop) #print("Quantidade de frames: ", qtdFrames) audio_duration = es_mode.Duration()(audio) frame_rate = qtdFrames / audio_duration os = es_mode.Onsets(frameRate=frame_rate) #matriz de algoritmos de detecção de onset executados onset_detection_matrix = es.array([pool['features.onsetdetection']]) #segundo parâmetro é o vetor de pesos para cada detecção de onset onsets = os(onset_detection_matrix, [1]) end_times = es.array(np.append(onsets, audio_duration)) start_times = es.array(np.append([0], onsets)) segments = es_mode.Slicer(endTimes=end_times, startTimes=start_times, timeUnits="seconds")(audio) return segments, onsets
def __detect_onsets(self, file, frame_size, hop_size, windowfnc, normalize) -> None: window = estd.Windowing(size=frame_size, type=windowfnc.value, normalized=normalize) fft = estd.FFT(size=frame_size) pool = es.Pool() pool_add = pool.add cart_to_polar = estd.CartesianToPolar() detect_onset = estd.OnsetDetection(method=self.algo) for frame in estd.FrameGenerator(file.audio, frameSize=frame_size, hopSize=hop_size): mag, phase, = cart_to_polar(fft(window(frame))) pool_add( "features." + self.algo, detect_onset(mag, phase), ) # The onsets algo expects a matrix of features which can be weighted self.onsets = estd.Onsets()(es.array([pool["features." + self.algo]]), [1])
def detect_onset(audio, index): # should be able to fetch the module from cache import essentia.standard as ess_std from essentia import array print("Subprocess {} starts".format(index)) processing_start = time() onset_detector = ess_std.OnsetDetection(method="complex") window = ess_std.Windowing(type="hann") fft = ess_std.FFT() c2p = ess_std.CartesianToPolar() onsets = ess_std.Onsets() frames = [] for frame in ess_std.FrameGenerator(audio, frameSize=1024, hopSize=512): mag, phase = c2p(fft(window(frame))) frames.append(onset_detector(mag, phase)) onsets_array = onsets(array([frames]), [1]) print("Subprocess {} finished. Elapsed time: {:.2}s".format( index, time() - processing_start)) return onsets_array
import numpy as np import pickle import glob import utilFunctions as UF import scipy.spatial.distance as DS import parameters as params import csv rms=ess.RMS() window = ess.Windowing(type = "hamming") spec = ess.Spectrum(size=params.Nfft) zz = np.zeros((params.zeropadLen,), dtype = 'float32') genmfcc = ess.MFCC(highFrequencyBound = 22000.0, inputSize = params.Nfft/2+1, sampleRate = params.Fs) hps = ess.HighPass(cutoffFrequency = 240.0) onsets = ess.Onsets() strokeLabels = ['dha', 'dhen', 'dhi', 'dun', 'ge', 'kat', 'ke', 'na', 'ne', 're', 'tak', 'te', 'tit', 'tun'] taals = {"teen": {"nmatra": 16, "accents": np.array([4, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 3, 1, 1, 1])}, "ek": {"nmatra": 12, "accents": np.array([4, 1, 1, 2, 1, 1, 3, 1, 1, 2, 1, 1])}, "jhap": {"nmatra": 10, "accents": np.array([4, 1, 2, 1, 1, 3, 1, 2, 1, 1])}, "rupak": {"nmatra": 7, "accents": np.array([2, 1, 1, 3, 1, 3, 1])} } rolls = [{"bol": ['dha/dha_02', 'te/te_05', 're/re_04', 'dha/dha_02'], "dur": np.array([1.0, 1.0, 1, 1]), "amp": np.array([1.0, 1.0, 1.0, 1.0])}, {"bol": ['te/te_02', 're/re_05', 'ke/ke_04', 'te/te_02'], "dur": np.array([1.0, 1.0, 1, 1]), "amp": np.array([1.0, 1.0, 1.0, 1.0])}, {"bol": ['ge/ge_02', 'ge/ge_05', 'te/te_04', 'te/te_02'], "dur": np.array([1.0, 1.0, 1, 1]), "amp": np.array([1.0, 1.0, 1.0, 1.0])}, {"bol": ['ge/ge_02', 'ge/ge_05', 'dhi/dhi_04', 'na/na_02'], "dur": np.array([1.0, 1.0, 1, 1]), "amp": np.array([1.0, 1.0, 1.0, 1.0])}, {"bol": ['dha/dha_02', 'dha/dha_02', 'te/te_05', 'te/te_06'], "dur": np.array([1.0, 1.0, 1, 1]), "amp": np.array([1.0, 1.0, 1.0, 1.0])}, {"bol": ['dha/dha_02', 'dha/dha_02', 'dhi/dhi_05', 'na/na_06'], "dur": np.array([1.0, 1.0, 1, 1]), "amp": np.array([1.0, 1.0, 1.0, 1.0])},
def find_peaks(self): getOnsets = es.Onsets() self.onsets = np.array(getOnsets(np.array([self.onsetFunction]), [1])) return self.onsets