def file_extract_essentia(audio_file): """ Essentia feature extraction / tailored code from pyGenreClf Returns: features """ rhythm_feats = [ "rhythm.bpm", "rhythm.bpm_histogram_first_peak_bpm", "rhythm.bpm_histogram_first_peak_weight", "rhythm.bpm_histogram_second_peak_bpm", "rhythm.bpm_histogram_second_peak_spread", "rhythm.bpm_histogram_second_peak_weight", "rhythm.danceability", "rhythm.beats_loudness.mean", "rhythm.beats_loudness.stdev", "rhythm.onset_rate", "rhythm.beats_loudness_band_ratio.mean", "rhythm.beats_loudness_band_ratio.stdev" ] features_total, features_frames = es.MusicExtractor()(audio_file) features = [] for i in range(0, len(rhythm_feats) - 2): features.append(features_total[rhythm_feats[i]]) for i in range(len(rhythm_feats) - 2, len(rhythm_feats)): bands = features_total[rhythm_feats[i]] for j in range(0, len(bands)): features.append(bands[j]) return np.array(features)
def feature_extractor(audio_files): f, features_frames = es.MusicExtractor(lowlevelStats=['mean'], rhythmStats=['mean'], tonalStats=['mean'])(audio_files) print("Filename:", f['metadata.tags.file_name']) #print(sorted(f.descriptorNames())) #loudness = f['lowlevel.average_loudness'] barkbands = f['lowlevel.barkbands.mean'] tuning = f['tonal.tuning_frequency'] flatness = f['lowlevel.barkbands_flatness_db.mean'] #onset = f['rhythm.onset_rate'] rhythm = f['rhythm.bpm_histogram'] #beats_count = f['rhythm.beats_count'] # la escala esta por encima de 1 habŕia que normalizar los valores mel = f['lowlevel.melbands_crest.mean'] mfcc = f['lowlevel.mfcc.mean'] entropy = f['lowlevel.spectral_entropy.mean'] print(mfcc) features = np.hstack([mfcc]) #features = np.hstack([loudness]) n_descriptors = len(features) print('Number of descriptors: ' + str(n_descriptors)) b = np.matrix(features) savetxt(file, b)
def extract_MusicalFeatures(folder, descriptors, filename): file_count = 0 segment_files = get_files_in_dir(folder) print(segment_files) data_file = os.path.join(folder, filename) with open(data_file, 'w') as writer: #adding column names as the first line in csv line2write = ','.join(descriptors + ['instrument']).replace( 'lowlevel.', '') + '\n' writer.write(line2write) for file in segment_files: if '.wav' in file: file_count += 1 if file_count % 20 == 0: #print name of a file every 20 files print(file_count, "files processed, current file: ", file) features, features_frames = ess.MusicExtractor( lowlevelSilentFrames='drop', lowlevelFrameSize=2048, lowlevelHopSize=1024, lowlevelStats=['mean', 'stdev'])(file) selected_features = [ features[descriptor] for descriptor in descriptors ] instrument = file.split('/')[-1].split( '_')[1].lower()[:-4] #class information line2write = str( selected_features)[1:-1] + ',' + instrument + '\n' writer.write(line2write) print("A total of ", file_count, "files processed")
def run(self): # Aux lib to store analyzed tracks analyzed_tracks_database = [] for element in self.file_names: path = self.path / element # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor( lowlevelStats=['mean'], rhythmStats=['mean'], tonalStats=['mean'])(str(path)) analyzed_tracks_database.append( [element, self.type, features['lowlevel.mfcc.mean']]) # Create DataFrame df = pd.DataFrame(analyzed_tracks_database, columns=['element', 'type', 'mfcc']) df.set_index('element', inplace=True) # Check if the database exists or if it is None if not isinstance(self.database, pd.DataFrame): self.database = df else: self.database = self.database.append(df, ignore_index=True)
def callback(ch, method, properties, body): ch.basic_ack(delivery_tag = method.delivery_tag) print(" [x] Received %r" % body)# See all feature names in the pool in a sorted order id = body # Compute all features, aggregate only 'mean' (media) and 'stdev' (desvio padrao) statistics for all low-level, rhythm and tonal frame features - https://essentia.upf.edu/documentation/essentia_python_examples.html features, features_frames = es.MusicExtractor(lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])('/Audios/'+id+'.wav') print("Filename:", features['metadata.tags.file_name']) print(" Pitch Salience:") print(" Mean:", features['lowlevel.pitch_salience.mean']) print(" StDev:", features['lowlevel.pitch_salience.stdev']) print("Loudness:",features['lowlevel.average_loudness']) print("BPM:", features['rhythm.bpm']) print("Beat positions (sec.)", features['rhythm.beats_position']) toSend = [ features['metadata.tags.file_name'], features['lowlevel.pitch_salience.mean'], features['lowlevel.average_loudness'], features['rhythm.bpm'] ] channel.queue_declare(queue='classifyMusic') channel.basic_publish(exchange='', routing_key='classifyMusic', body=json.dumps(toSend)) print(" [x] Sent ",features['metadata.tags.file_name']," to classify!!") print(' [*] Waiting for messages. To exit press CTRL+C')
def predict_instrument(self, folder_path, name): track_path = Path(folder_path) / name if not os.path.isfile(track_path): return 'The file does not exist' # Given track parameters # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor(lowlevelStats=['mean'], rhythmStats=['mean'], tonalStats=['mean'])( str(track_path)) # Creating the row for the dataframe file_values = [{ 'element': name, 'mfcc': features['lowlevel.mfcc.mean'] }] # Creating new DataFrame file_df = pd.DataFrame(file_values) file_df.set_index('element', inplace=True) file_df = file_df.mfcc.apply(pd.Series) # Executing the kNN algorithm return self.knn.predict(file_df)
def extract_features_all(path, track_name): features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev', 'min', 'max'], rhythmStats=['mean', 'stdev', 'min', 'max'], tonalStats=['mean', 'stdev', 'min', 'max'], )(f'{path}{track_name}') save_file = f'../data/essentia/{track_name[:-4]}.json' es.YamlOutput(filename=save_file, format="json")(features)
def detect(file): features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])('1 19.wav') bpm = features['rhythm.bpm'] bpm = round(bpm) return bpm
def get_lowLevelDescriptors(filename): features, features_frames = ess.MusicExtractor( lowlevelSilentFrames='drop', lowlevelFrameSize=2048, lowlevelHopSize=1024, lowlevelStats=['mean', 'stdev'])(filename) descriptors = [ descriptor for descriptor in features.descriptorNames() if 'lowlevel' in descriptor and isinstance(features[descriptor], float) ] return descriptors
def extract_features_intervals(path, track_name): intervals = list(range(1, 31)) for i in intervals: start = i - 1 end = i features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev', 'min', 'max'], rhythmStats=['mean', 'stdev', 'min', 'max'], tonalStats=['mean', 'stdev', 'min', 'max'], startTime=start, endTime=end)(f'{path}{track_name}') save_file = f'../data/essentia_bow/{track_name[:-4]}_{i}.json' es.YamlOutput(filename=save_file, format="json")(features)
def extract_features(file_path): # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features print('extracting features for {}'.format(file_path)) features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])(file_path) feature_dict = {} feature_names = sorted(features.descriptorNames()) for name in feature_names: feature_dict[name] = features[name] json_data = json.dumps(feature_dict, default=default) return json_data
def upload(): file = request.files['song'] file.save('./temp.wav') features, features_frames = es.MusicExtractor(lowlevelStats=['mean'], rhythmStats=['mean'], tonalStats=['mean' ])('./temp.wav') row = [] row.append(features['lowlevel.spectral_complexity.mean']) row.append(features['lowlevel.average_loudness']) row.append(features['lowlevel.dissonance.mean']) row.append(features['lowlevel.pitch_salience.mean']) row.append(features['tonal.tuning_frequency']) row.append(features['tonal.chords_strength.mean']) row.append(features['rhythm.bpm']) row.append(features['rhythm.danceability']) row.append(features['rhythm.beats_count']) row.append(features['metadata.audio_properties.length']) row.extend(map_key(features['tonal.chords_key'])) score = model.predict(row) neighbors = nn_model.kneighbors(1000, row) review = reviewer.generate(neighbors[0]) nearest_neighbors = nn_model.kneighbors(15, row) similar_songs = datasetManager.get_similar_songs(nearest_neighbors[0]) tags = tagger.generate(nearest_neighbors[0]) data = { 'spectral_complexity': features['lowlevel.spectral_complexity.mean'], 'average_loudness': features['lowlevel.average_loudness'], 'dissonance': features['lowlevel.dissonance.mean'], 'pitch_salience': features['lowlevel.pitch_salience.mean'], 'tuning_frequency': features['tonal.tuning_frequency'], 'chords_strength': features['tonal.chords_strength.mean'], 'bpm': features['rhythm.bpm'], 'danceability': features['rhythm.danceability'], 'beats_count': features['rhythm.beats_count'], 'length': features['metadata.audio_properties.length'], 'chords_key': features['tonal.chords_key'], 'score': int(score[0]), 'neighbors': str(neighbors[0]), 'review': review, 'nearest': str(nearest_neighbors[0]), 'similar_songs': similar_songs, 'tags': tags } # data['score'] = int(score[0]) return jsonify(data)
def get_detection(): files_path = os.path.join(app.config['UPLOAD_FOLDER'], '*') files = sorted( glob.iglob(files_path), key=os.path.getctime, reverse=True) _file = os.path.join(app.config['UPLOAD_FOLDER'], files[0]) features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])(_file) print("BPM:", features['rhythm.bpm']) print("Key/scale estimation (using a profile specifically suited for electronic music):", features['tonal.key_edma.key'], features['tonal.key_edma.scale']) return jsonify( bpm = features['rhythm.bpm'], key = features['tonal.key_edma.key'], scale = features['tonal.key_edma.scale'] )
def compute_feature_vector(song_file): # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])('previews/' + song_file) # See all feature names in the pool in a sorted order feature_names = sorted(features.descriptorNames()) selected_features = [] selected_features.append(features['lowlevel.average_loudness']) selected_features.append(features['rhythm.bpm']) selected_features.append(features['lowlevel.dissonance.mean']) selected_features.append(features['lowlevel.zerocrossingrate.mean']) selected_features.append(features['lowlevel.spectral_energy.mean']) fv = np.array(selected_features) print(fv) return fv
def feature_extractor(audio_files): f, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])(audio_files) print("Filename:", f['metadata.tags.file_name']) #print(sorted(f.descriptorNames())) loudness = f['lowlevel.average_loudness'] barkbands = f['lowlevel.barkbands.mean'] tuning = f['tonal.tuning_frequency'] flatness = f['lowlevel.barkbands_flatness_db.mean'] onset = f['rhythm.onset_rate'] rhythm = f['rhythm.bpm_histogram'] #beats_count = f['rhythm.beats_count'] # la escala esta por encima de 1 habŕia que normalizar los valores mel = f['lowlevel.melbands_crest.mean'] mfcc = f['lowlevel.mfcc.mean'] entropy = f['lowlevel.spectral_entropy.mean'] #features = np.hstack([loudness,barkbands,tuning,flatness,onset,mel,mfcc,entropy, rhythm]) features = mfcc n_descriptors = len(features) print('Number of descriptors: ' + str(n_descriptors)) #class_name('audio') i=0 n_files=7 class_name=0 list = [] list = map(str, features) file = open(file_name, 'a') while i < n_files: for c in range(n_files): #class_name('audio'): print ('class_name:', c) file.write(', '.join(list) + ', ' + str(class_name) + '\n') # el cero sería la clase file.close()
def find_features(audio_path, name): features, features_frames = es.MusicExtractor(lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])(audio_path) bpm = features['rhythm.bpm'] helistik = (features['tonal.chords_key'], features['tonal.chords_scale']) energy = features['tonal.tuning_nontempered_energy_ratio'] dance = features['rhythm.danceability'] / 3 #väärtuste vahemik on 0-3 #JSON salvestamine data["nimi"] = name data['bpm'] = round(bpm) helilaad = 'minoor' if(helistik[1] != 'minor'): helilaad = "mažoor" data["helistik"] = {"nimi": helistik[0], "helilaad": helilaad} data["energia"] = round(energy,2) * 100 data["tantsulisus"] = round(dance,2) * 100 #TAGG SÕNAD sr = 16000 audio = es.MonoLoader(filename=audio_path, sampleRate=sr)() musicnn_preds = es.TensorflowPredictMusiCNN(graphFilename='msd-musicnn-1.pb')(audio) classes = musicnn_metadata['classes'] keskmised = np.mean(musicnn_preds.T, axis=1) valitud = [] for i, l in enumerate(keskmised.argsort()[-4:][::-1], 1): valitud.append(classes[l]) data['taggs'] = [valitud[0], valitud[1], valitud[2], valitud[3]]
def main(): try: (opts, args) = getopt.getopt( sys.argv[1:], 'f:h', ['help', 'framesize=', 'hopsize=' ]) # 'f:h' tienen que estar en orden alfabetico except getopt.GetoptError as err: print(err) usage() sys.exit(2) filepath = None frameSize = None hopSize = None if len(opts) != 0: for (o, a) in opts: if o in ('-h', '--help'): usage() sys.exit(2) elif o in ('-f'): filepath = a elif o in ('--framesize'): frameSize = int(a) elif o in ('--hopsize'): hopSize = int(a) else: print("fin de switch") usage() sys.exit(2), else: # no options passed usage() sys.exit(2) if frameSize and hopSize and filepath: import essentia.standard as es # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], mfccStats=['mean'], lowlevelFrameSize=frameSize, lowlevelHopSize=hopSize, tonalStats=['mean', 'stdev'])(filepath) dfs = [] for idx, name in enumerate(features_frames.descriptorNames()): if name.startswith('lowlevel'): feature_name = name.rsplit('.')[1] if isinstance(features_frames[name], np.ndarray): if features_frames[name].ndim == 1: columns = [feature_name] else: columns = [ feature_name + '_' + str(i) for i in range(features_frames[name].shape[1]) ] dfs.append( pd.DataFrame(features_frames[name], columns=columns)) df = pd.concat(dfs, axis=1) file_name = features['metadata.tags.file_name'].rsplit('.')[0] name = file_name.replace(' ', '_') + '_frameSize_' + str( frameSize) + '_hopSize_' + str(hopSize) + '.csv' base_dir = os.getcwd() df.to_csv(os.path.join(base_dir, name)) else: usage() sys.exit(2)
select_features = pickle.load(open('select_features.pkl', 'rb')) # load the model from disk loaded_model = pickle.load(open('model.pkl', 'rb')) passFeatures=['rhythm.bpm_histogram','lowlevel.mfcc.cov','lowlevel.gfcc.cov',\ 'lowlevel.mfcc.icov','lowlevel.gfcc.icov'] list_of_files = {} # open directory of samples to predict for (dirpath, dirnames, filenames) in os.walk(sys.argv[1]): for filename in filenames: if filename.endswith('.wav'): path_file = os.sep.join([dirpath, filename]) row = {'File': filename, 'Features': []} try: features, features_frames = es.MusicExtractor(lowlevelStats=['mean', 'stdev'],\ rhythmStats=[],tonalStats=[])(path_file) except (RuntimeError, TypeError, NameError): pass # log low level features for feature in sorted(features.descriptorNames()): # only log low level features not in the passFeatures list if feature not in passFeatures and not feature.find( 'lowlevel'): # if feature is list then expand if isinstance(features[feature], (list, tuple, np.ndarray)): index = 0 for i in features[feature]: feat = feature + str(index) index = index + 1 if feat in select_features:
def main(): aparser = argparse.ArgumentParser() aparser.add_argument( '-c', action='store', dest='config', help= '-c type of the dataset. For ex: _1s_h100 for 1s with full length hop') aparser.add_argument('-t', action='store', dest='data_type', help='-t type of data original/harmonic/residual') args = aparser.parse_args() if not args.config: aparser.error('Please specify the data config!') conf = args.config if args.data_type == 'original': path_to_dataset = PATH_TO_ORIGINAL_WAV_FILES + conf path_to_features = PATH_TO_ORIGINAL_FEATURES + conf if args.data_type == 'residual': path_to_dataset = PATH_TO_RESIDUAL_WAV_FILES + conf path_to_features = PATH_TO_RESIDUAL_FEATURES + conf if args.data_type == 'harmonic': path_to_dataset = PATH_TO_HARMONIC_WAV_FILES + conf path_to_features = PATH_TO_HARMONIC_FEATURES + conf datasets = sorted(os.listdir(path_to_dataset)) for dataset in datasets: empty_files = 0 print("[Dataset] : " + dataset) folder_path = os.path.join(path_to_dataset, dataset) lrms = sorted(os.listdir(folder_path)) for channel in lrms: channel_path = os.path.join(folder_path, channel) sub_folders = sorted(os.listdir(channel_path)) for sub_folder in sub_folders: sub_folder_path = os.path.join(channel_path, sub_folder) files = sorted(os.listdir(sub_folder_path)) for filename in files: filepath = os.path.join(sub_folder_path, filename) features = essentia.Pool() try: # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor( lowlevelSilentFrames='drop', lowlevelFrameSize=2048, lowlevelHopSize=1024, lowlevelStats=['mean', 'stdev'])(filepath) features_frames = [] except RuntimeError, e: print(filepath + " is almost silent") empty_files += 1 dump_path = os.path.join(path_to_features, dataset, channel, sub_folder) create_folder(dump_path) es.YamlOutput(filename=os.path.join( dump_path, filename.replace('.wav', '.json')), format='json')(features) features = [] filename = [] print("Feature Extraction Completed Successfully for " + dataset) print("Total number of empty file in " + dataset + " is " + str(empty_files))
def essentia_midi(file): pool = essentia.Pool() # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])(file) # You can then access particular values in the pools: print("Filename:", features['metadata.tags.file_name']) print("-" * 80) print("Replay gain:", features['metadata.audio_properties.replay_gain']) print("EBU128 integrated loudness:", features['lowlevel.loudness_ebu128.integrated']) print("EBU128 loudness range:", features['lowlevel.loudness_ebu128.loudness_range']) print("-" * 80) print("MFCC mean:", features['lowlevel.mfcc.mean']) print("-" * 80) print("BPM:", features['rhythm.bpm']) print("Beat positions (sec.)", features['rhythm.beats_position']) print("-" * 80) print( "Key/scale estimation (using a profile specifically suited for electronic music):", features['tonal.key_edma.key'], features['tonal.key_edma.scale']) # BPM Detection # Loading audio file audio = MonoLoader(filename=file)() # # Compute beat positions and BPM rhythm_extractor = RhythmExtractor2013(method="multifeature") bpm, beats, beats_confidence, _, beats_intervals = rhythm_extractor(audio) beat_volume_extractor = BeatsLoudness(beats=beats) beats_loudness, beats_loudness_band_ratio = beat_volume_extractor(audio) # Danceability Detection danceability_extractor = Danceability() danceability, dfa = danceability_extractor(audio) # Melody Detection # Load audio file; it is recommended to apply equal-loudness filter for PredominantPitchMelodia loader = EqloudLoader(filename=file, sampleRate=44100) audio = loader() print("Duration of the audio sample [sec]:") print(len(audio) / 44100.0) pitch_extractor = PredominantPitchMelodia(frameSize=2048, hopSize=1024) pitch_values, pitch_confidence = pitch_extractor(audio) midi_extractor = PitchContourSegmentation(hopSize=1024) onset, duration, midi_pitch = midi_extractor(pitch_values, audio) # Pitch is estimated on frames. Compute frame time positions pitch_times = numpy.linspace(0.0, len(audio) / 44100.0, len(pitch_values)) #Storing in Pool pool.add('MIDIonset', onset) pool.add('MIDIduration', duration) pool.add('MIDIpitch', midi_pitch) pool.add('pitch', pitch_values) pool.add('danceability', danceability) pool.add('beat-loudness', beats_loudness) pool.add('beats', beats) pool.add('bpm', bpm) output = YamlOutput( filename='./analyzer/output.json', format='json', indent=4, writeVersion=False) # use "format = 'json'" for JSON output output(pool)
import confirm_prompt #for confirming user action import json_flattener #for flatenning jsons # [NOTE] Please set the path to dataset here(this is the path to directory where the instrument folders {bas, cel ... vio} will be moved in) path_to_dataset='../../datasets/good-sounds/instruments' instruments = os.listdir(path_to_dataset) instruments.sort() for instrument in instruments: print("[Instrument] : " + instrument) files=sorted(os.listdir(os.path.join(path_to_dataset,instrument))) print("Number of files : "+str(len(files))) for file in files: if(file.endswith('.wav')): filename=os.path.join(path_to_dataset,instrument,file) print("Analysing file : "+filename) # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor(lowlevelSilentFrames='drop', lowlevelFrameSize=2048, lowlevelHopSize=1024, lowlevelStats=['mean', 'stdev'])(filename) features_frames=[] es.YamlOutput(filename = filename.replace('.wav','.json'), format='json')(features) features=[] filename=[] print("Feature Extraction Completed Successfully")
for genre in genreList: songDir = genreDir + genre print(songDir) for subdir, dirs, files in os.walk(songDir): feature_list = [] for file in files: audio = str(songDir + "/" + file) # print(audio) # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])(audio) feature_dict = {} for feature in features.descriptorNames(): if feature.find('lowlevel') != -1 or feature.find( 'rhythm') != -1 or feature.find('tonal') != -1: if type(features[feature]) != str and type( features[feature]) != np.ndarray: if feature.find('mean') != -1 or feature.find( 'stdev') != -1: feature_dict[feature] = features[feature] #preprocessing step.Standerdising the features data_array = []
db_cursor = db_conn.cursor() names = [ 'blues', 'classical', 'country', 'disco', 'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock' ] for name in names: for i in range(10): song_name = name + '.' + '0000' + str(i) + '.wav' print(song_name) # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])("genres_new/" + name + "/" + song_name) song_name = name + '.' + '0000' + str(i) + '.au' hpcp_vector = features["tonal.hpcp.mean"] for i in range(1, 37): sql = "UPDATE songs SET avg_hpcp_" + str( i) + "=? WHERE song_name=?" db_cursor.execute(sql, (float(hpcp_vector[i - 1]), song_name)) db_conn.commit() for i in range(10, 100): song_name = name + '.' + '000' + str(i) + '.wav' print(song_name)
def extract_features(path, sample_rate, frame_size, hop_size): signal, sr = librosa.load(path, sr=sample_rate, duration=5.0) zero_crossing_rate = librosa.feature.zero_crossing_rate( y=signal, frame_length=frame_size, hop_length=hop_size) spectral_centroid = librosa.feature.spectral_centroid(y=signal, sr=sample_rate, n_fft=frame_size, hop_length=hop_size) spectral_contrast = librosa.feature.spectral_contrast(y=signal, sr=sample_rate, n_fft=frame_size, hop_length=hop_size) spectral_bandwidth = librosa.feature.spectral_bandwidth( y=signal, sr=sample_rate, n_fft=frame_size, hop_length=hop_size) spectral_rolloff = librosa.feature.spectral_rolloff(y=signal, sr=sample_rate, n_fft=frame_size, hop_length=hop_size) mfccs = librosa.feature.mfcc(y=signal, sr=sample_rate, n_fft=frame_size, hop_length=hop_size) features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])(path) bpm = features['rhythm.bpm'] key = features['tonal.key_temperley.scale'] keyout = 0 if key == 'minor': keyout = 0 else: keyout = 1 print(path + ' \n') print("BPM:", features['rhythm.bpm']) print("Key: ", key) print(" keyout : ", keyout) return [ keyout, bpm, features['tonal.tuning_diatonic_strength'], features['tonal.chords_changes_rate'], features['tonal.chords_number_rate'], features['rhythm.danceability'], features['rhythm.beats_loudness.mean'], numpy.mean(zero_crossing_rate), numpy.std(zero_crossing_rate), numpy.mean(spectral_centroid), numpy.std(spectral_centroid), numpy.mean(spectral_contrast), numpy.std(spectral_contrast), numpy.mean(spectral_bandwidth), numpy.std(spectral_bandwidth), numpy.mean(spectral_rolloff), numpy.std(spectral_rolloff), numpy.mean(mfccs[1, :]), numpy.std(mfccs[1, :]), numpy.mean(mfccs[2, :]), numpy.std(mfccs[2, :]), numpy.mean(mfccs[3, :]), numpy.std(mfccs[3, :]), numpy.mean(mfccs[4, :]), numpy.std(mfccs[4, :]), numpy.mean(mfccs[5, :]), numpy.std(mfccs[5, :]), numpy.mean(mfccs[6, :]), numpy.std(mfccs[6, :]), numpy.mean(mfccs[7, :]), numpy.std(mfccs[7, :]), numpy.mean(mfccs[8, :]), numpy.std(mfccs[8, :]), numpy.mean(mfccs[9, :]), numpy.std(mfccs[9, :]), numpy.mean(mfccs[10, :]), numpy.std(mfccs[10, :]), numpy.mean(mfccs[11, :]), numpy.std(mfccs[11, :]), numpy.mean(mfccs[12, :]), numpy.std(mfccs[12, :]), numpy.mean(mfccs[13, :]), numpy.std(mfccs[13, :]), features['tonal.hpcp.mean'][0], features['tonal.hpcp.mean'][1], features['tonal.hpcp.mean'][2], features['tonal.hpcp.mean'][3], features['tonal.hpcp.mean'][4], features['tonal.hpcp.mean'][5], features['tonal.hpcp.mean'][6], features['tonal.hpcp.mean'][7], features['tonal.hpcp.mean'][8], features['tonal.hpcp.mean'][9], features['tonal.hpcp.mean'][10], features['tonal.hpcp.mean'][11], features['tonal.hpcp.mean'][12], features['tonal.hpcp.mean'][13], features['tonal.hpcp.mean'][14], features['tonal.hpcp.mean'][15], features['tonal.hpcp.mean'][16], features['tonal.hpcp.mean'][17], features['tonal.hpcp.mean'][18], features['tonal.hpcp.mean'][19], features['tonal.hpcp.mean'][20], features['tonal.hpcp.mean'][21], features['tonal.hpcp.mean'][22], features['tonal.hpcp.mean'][23], features['tonal.hpcp.mean'][24], features['tonal.hpcp.mean'][25], features['tonal.hpcp.mean'][26], features['tonal.hpcp.mean'][27], features['tonal.hpcp.mean'][28], features['tonal.hpcp.mean'][29], features['tonal.hpcp.mean'][30], features['tonal.hpcp.mean'][31], features['tonal.hpcp.mean'][32], features['tonal.hpcp.mean'][33], features['tonal.hpcp.mean'][34], features['tonal.hpcp.mean'][35], features['tonal.hpcp.stdev'][0], features['tonal.hpcp.stdev'][1], features['tonal.hpcp.stdev'][2], features['tonal.hpcp.stdev'][3], features['tonal.hpcp.stdev'][4], features['tonal.hpcp.stdev'][5], features['tonal.hpcp.stdev'][6], features['tonal.hpcp.stdev'][7], features['tonal.hpcp.stdev'][8], features['tonal.hpcp.stdev'][9], features['tonal.hpcp.stdev'][10], features['tonal.hpcp.stdev'][11], features['tonal.hpcp.stdev'][12], features['tonal.hpcp.stdev'][13], features['tonal.hpcp.stdev'][14], features['tonal.hpcp.stdev'][15], features['tonal.hpcp.stdev'][16], features['tonal.hpcp.stdev'][17], features['tonal.hpcp.stdev'][18], features['tonal.hpcp.stdev'][19], features['tonal.hpcp.stdev'][20], features['tonal.hpcp.stdev'][21], features['tonal.hpcp.stdev'][22], features['tonal.hpcp.stdev'][23], features['tonal.hpcp.stdev'][24], features['tonal.hpcp.stdev'][25], features['tonal.hpcp.stdev'][26], features['tonal.hpcp.stdev'][27], features['tonal.hpcp.stdev'][28], features['tonal.hpcp.stdev'][29], features['tonal.hpcp.stdev'][30], features['tonal.hpcp.stdev'][31], features['tonal.hpcp.stdev'][32], features['tonal.hpcp.stdev'][33], features['tonal.hpcp.stdev'][34], features['tonal.hpcp.stdev'][35], ]
import sys import essentia.standard as es import os, os.path import csv folder = "../../MER_audio_taffc_dataset/" total = len(os.listdir(folder + 'Q1/')) + len( os.listdir(folder + 'Q2/')) + len(os.listdir(folder + 'Q3/')) + len( os.listdir(folder + 'Q4/')) counter = 0 with open('./features.csv', 'wb') as csvfile: for i in range(1, 5): for music in os.listdir(folder + 'Q' + str(i) + '/'): features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'])(folder + 'Q' + str(i) + '/' + music) spamwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) spamwriter.writerow([ str(i), str(1) + ':' + str(features['lowlevel.pitch_salience.mean']), str(2) + ':' + str(features['lowlevel.average_loudness']), str(3) + ':' + str(features['rhythm.bpm']) ]) counter = counter + 1 print str(counter) + " em " + str(total)
import numpy as np import csv import sys data = [] # pass these features ot handle nested arrays passFeatures=['rhythm.bpm_histogram','lowlevel.mfcc.cov','lowlevel.gfcc.cov',\ 'lowlevel.mfcc.icov','lowlevel.gfcc.icov'] with open(sys.argv[1]) as csvfile: reader = csv.DictReader(csvfile,delimiter=',') header = reader.fieldnames for row in reader: try: print(sys.argv[2]+row['File']) features, features_frames = es.MusicExtractor(lowlevelStats=['mean', 'stdev'],\ rhythmStats=[],tonalStats=[])(sys.argv[2]+row['File']) except (RuntimeError, TypeError, NameError): pass # log low level features for feature in sorted(features.descriptorNames()): if feature not in passFeatures and !feature.find('lowlevel'): if isinstance(features[feature], (list, tuple, np.ndarray)): index = 0 for i in features[feature]: feat = feature+str(index) row[feat] = i index = index + 1 else: row[feature] = features[feature] data.append(row) # write to csv
import sys sys.path.append('../essentia/src/python') import essentia import essentia.standard as es # Compute all features, aggregate only 'mean' and 'stdev' statistics for all low-level, rhythm and tonal frame features features, features_frames = es.MusicExtractor( lowlevelStats=['mean', 'stdev'], rhythmStats=['mean', 'stdev'], tonalStats=['mean', 'stdev'], analysisSampleRate=4000, lowlevelFrameSize=2048, lowlevelHopSize=512, lowlevelWindowType='blackmanharris62')( '../processed_data/ed/ED003/PS/PS_LLL_1.wav') # See all feature names in the pool in a sorted order print sorted(features.descriptorNames()) print faetures