def key_essentia_extractor(input_audio_file, output_text_file, **kwargs): """ This function estimates the overall key of an audio track. :type input_audio_file: str :type output_text_file: str """ loader = estd.MonoLoader(filename=input_audio_file, sampleRate=kwargs["SAMPLE_RATE"]) ekey = estd.KeyExtractor(frameSize=kwargs["WINDOW_SIZE"], hopSize=kwargs["HOP_SIZE"], tuningFrequency=kwargs["HPCP_REFERENCE_HZ"]) key, scale, strength = ekey(loader()) result = key + '\t' + scale textfile = open(output_text_file, 'w') textfile.write(result + '\n') textfile.close() return result, strength
def key_extractor(self, frameSize=4096, hpcpSize=12, maxFrequency=3500, minFrequency=25, windowType='hann', profileType='bgate', pcpThreshold=0.2, tuningFrequency=440, weightType='cosine'): """ Wrapper around essentia KeyExtractor algo. This algorithm extracts key/scale for an audio signal. It computes HPCP frames for the input signal and applies key estimation using the Key algorithm. Refer for more details https://essentia.upf.edu/documentation/reference/streaming_KeyExtractor.html Returns: a dictionary with corresponding values for key, scale and strength eg: {'key': 'F', 'scale': 'major', 'strength': 0.7704258561134338} """ audio = array(self.audio_vector) key = estd.KeyExtractor(frameSize=frameSize, hopSize=self.hop_length, tuningFrequency=tuningFrequency) """ TODO: test it with new essentia update key = ess.KeyExtractor(frameSize=frameSize, hopSize=self.hop_length, sampleRate=self.fs, hpcpSize=hpcpSize, maxFrequency=maxFrequency, minFrequency=minFrequency, windowType=windowType, profileType=profileType, pcpThreshold=pcpThreshold, tuningFrequency=tuningFrequency, weightType=weightType) """ key, scale, strength = key.compute(audio) return {'key': key, 'scale': scale, 'strength': strength}
def algorithm_tonal_key_essentia_basic(sound): """ Estimates the tonality of a given audio file. See http://essentia.upf.edu/documentation/reference/std_KeyExtractor.html. :param sound: sound dictionary from dataset :return: dictionary with results per different methods """ results = dict() audio = load_audio_file(file_path=sound[SOUND_FILE_KEY], sample_rate=44100) key_extractor = estd.KeyExtractor() key, scale, strength = key_extractor(audio) results['EssentiaBasic'] = { 'key': '%s %s' % (key, scale), 'strength': strength } return results
def annotate_song(filepath): audio = load_audio(filepath) key, scale, key_strength = es.KeyExtractor(profileType='edma')(audio) key_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] # transforming bemol to sharp if len(key) == 2 and key[1] == 'b': cur_idx = key_list.index(key[0]) new_idx = (cur_idx - 1) % len(key_list) new_key = key_list[new_idx] + '#' key = new_key # for calculating bpm, its proven that RhythmExtractor2013 works best but takes longer rhythm_desc = es.RhythmExtractor2013()(audio) bpm = round(rhythm_desc[0]) del audio return {'bpm': bpm, 'key': key, 'key_scale': scale, 'key_strength': key_strength,}
import os import essentia as e import essentia.standard as estd # CONFIGURATION # ================================================================================ # Default parameters sample_rate = 44100 window_size = 4096 hop_size = 1024 tuning_frequency = 440 # retrieve filenames from folder: soundfiles = os.listdir(audio_folder) if '.DS_Store' in soundfiles: soundfiles.remove('.DS_Store') # ANALYSIS # ================================================================================ print "\nANALYSIS..." for item in soundfiles: loader = estd.MonoLoader(filename=audio_folder + '/' + item, sampleRate=sample_rate) key = estd.KeyExtractor(frameSize=window_size, hopSize=hop_size, tuningFrequency=tuning_frequency) pool = e.Pool() key, scale, strength = key(loader) result = key, scale, strength print item[:20] + '... ', result