def featureExtractionFileWrapper(wav_file, out_file, mt_win, mt_step, st_win, st_step): if not os.path.isfile(wav_file): raise Exception("Input audio file not found!") aF.mtFeatureExtractionToFile(wav_file, mt_win, mt_step, st_win, st_step, out_file, True, True, True)
def feature_extraction(filename): wavFileName = filename.replace('.mp3','.wav') name = filename.replace('.mp3','') command = 'avconv -i ' + filename + ' ' + wavFileName os.system(command) audioFeatureExtraction.mtFeatureExtractionToFile(wavFileName,2.0, 2.0, 0.050, 0.050,name,storeStFeatures=True,storeToCSV=True) return name + '.csv'
def featureExtractionFileWrapper(wavFileName, outFile, mtWin, mtStep, stWin, stStep): if not os.path.isfile(wavFileName): raise Exception("Input audio file not found!") aF.mtFeatureExtractionToFile(wavFileName, mtWin, mtStep, stWin, stStep, outFile, True, True, True)
def extract_mid_features(input_file): class_names = [os.path.basename(input_file)] features = [] fs, x = readAudioFile(input_file) x = stereo2mono(x) mt_size, mt_step, st_win, st_step = 1, 0.4, 0.025, 0.010 [mt_feats, st_feats, _] = mtFeatureExtraction(x, fs, mt_size * fs, mt_step * fs, round(st_win * fs), round(st_step * fs)) mtFeatureExtractionToFile(input_file, mt_size, mt_step, st_win, st_step, input_file, False, True, True) return mt_feats, st_feats, _
def extract_features(filename): ''' Extracts features from a file of audio data Parameters: filename str, the name of the file to extract features from Returns: filename to extracted features ''' # base name of output file output_filename = "computed_features_file" # base list of feature names for labelng with json feature_names = ("Zero_Crosing_Rate", "Energy", "Entropy_of_Energy", "Spectral_Centroid", "Spectral_Spread", "Spectral_Entropy", "Spectral_Flux", "Spectral_Rollof", "MFCC_1", "MFCC_2", "MFCC_3", "MFCC_4", "MFCC_5", "MFCC_6", "MFCC_7", "MFCC_8", "MFCC_9", "MFCC_10", "MFCC_11", "MFCC_12", "MFCC_13", "Chroma_Vector_1", "Chroma_Vector_2", "Chroma_Vector_3", "Chroma_Vector_4", "Chroma_Vector_5", "Chroma_Vector_6", "Chroma_Vector_7", "Chroma_Vector_8", "Chroma_Vector_9", "Chroma_Vector_10", "Chroma_Vector_11", "Chroma_Vector_12", "Chroma_Deviation") # arguments 2 and 3 are not in use # argument 7 and 8 store short term features and enable csv output respectively afe.mtFeatureExtractionToFile(filename, 1, 1, .05, .025, output_filename, True, True, False) # convert to json format csv_features = open(output_filename + '_st.csv', 'r') # add feature names as json keys json_features = [] reader = csv.DictReader(csv_features, feature_names) # add rows to json list for row in reader: json_features.append(row) csv_features.close() # remove unnecessary files os.remove(output_filename + ".csv") os.remove(output_filename + ".npy") os.remove(output_filename + "_st.csv") os.remove(output_filename + "_st.npy") # return the list of formatted features return json_features
def Audio_Feature_Extraction_Extract_Directory(self): pathToSaveFiles = QFileDialog.getExistingDirectory( self.somethingToPass, "Select Directory to Save Files") i = 0 path = self.Audio_Feature_Extraction_DirectoryPath.text() extractionList = [] nameList = [] tempHold = [] for root, directs, files in os.walk(path): for x in files: extractionList.append(root + "/" + x) nameList.append(x) for name in nameList: x = name.split('.') tempHold.append(x[0]) nameList = tempHold if self.Audio_Feature_Extraction_DirectoryWindowTerm.currentText( ) == "ShortTerm": for audio in extractionList: [Fs, x] = audioBasicIO.readAudioFile(audio) print(audio) stFeatures = audioFeatureExtraction.stFeatureExtraction( x, Fs, float(self.Audio_Feature_Extraction_DirectoryWindowSize. text()) * Fs, float( self.Audio_Feature_Extraction_DirectoryStepSize.text()) * Fs) #I think the files are overwriting each other. I am getting #299 files but it should be 5134. I changed the namelist[i] to #just i numpy.savetxt(pathToSaveFiles + "/" + str(i) + ".csv", stFeatures, delimiter=',') i += 1 QMessageBox.about(self.somethingToPass, "Files Created", "Files have been saved as CSV files.") else: for x in extractionList: audioFeatureExtraction.mtFeatureExtractionToFile( x, float(self. Audio_Feature_Extraction_DirectorymidTermWindowSize. text()), float( self. Audio_Feature_Extraction_DirectorymidTermWindowStepSize .text()), float(self.Audio_Feature_Extraction_DirectoryWindowSize. text()), float(self.Audio_Feature_Extraction_DirectoryStepSize.text( )), pathToSaveFiles + "/" + nameList[i], storeStFeatures=True, storeToCSV=True, PLOT=False) i += 1 QMessageBox.about( self.somethingToPass, "Files Created", "Files have been saved as CSV files and .npy files. There ")
def Audio_Feature_Extraction_Extract(self): if self.Audio_Feature_Extraction_SingleFileWindowTerm.currentText( ) == "ShortTerm": [Fs, x] = audioBasicIO.readAudioFile( self.Audio_Feature_Extraction_SingleFilePath.text()) stFeatures = audioFeatureExtraction.stFeatureExtraction( x, Fs, float( self.Audio_Feature_Extraction_SingleFileWindowSize.text()) * Fs, float(self.Audio_Feature_Extraction_SingleFileStepSize.text()) * Fs) options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog fileName, _ = QFileDialog.getSaveFileName(self.somethingToPass, "Where to save?", "", "CSV files (*.csv)", options=options) numpy.savetxt(fileName + ".csv", stFeatures, delimiter=',') QMessageBox.about(self.somethingToPass, "Files Created", "File has been saved as CSV file") ## Let the User specify how many features and what features to see eventually. #============================================================================== # if self.Audio_Feature_Extraction_SingleFileDataVisualisation.isChecked(): # stFeatures = audioFeatureExtraction.stFeatureExtraction(x, Fs, 0.050*Fs, 0.025*Fs) # # labels = ["Zero Crossing Rate", "Energy", "Entropy of Energy", "Spectral Centroid", # "Spectral Spread", "Spectral Entropy", "Spectral Flux", "Spectral Rolloff", # "MFCC 1", "MFCC 2", "MFCC 3", "MFCC 4", # "MFCC 5", "MFCC 6", "MFCC 7", "MFCC 8", # "MFCC 9", "MFCC 10", "MFCC 11", "MFCC 12", "MFCC 13", # "Chroma Vector 1", "Chroma Vector 2", "Chroma Vector 3", "Chroma Vector 4", # "Chroma Vector 5", "Chroma Vector 6", "Chroma Vector 7", "Chroma Vector 8", # "Chroma Vector 9", "Chroma Vector 10", "Chroma Vector 11","Chroma Vector 12", "Chroma Deviation"] # # # for x in range(0, len(labels)-1): # plt.subplot(34,1,x+1); plt.plot(stFeatures[x,:]); plt.xlabel('Frame no'); plt.ylabel(labels[x]) # # plt.show() # #============================================================================== else: options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog fileName, _ = QFileDialog.getSaveFileName(self.somethingToPass, "Where to save?", "", "CSV files (*.csv)", options=options) audioFeatureExtraction.mtFeatureExtractionToFile( self.Audio_Feature_Extraction_SingleFilePath.text(), float( self.Audio_Feature_Extraction_SingleFilemidTermWindowSize. text()), float(self. Audio_Feature_Extraction_SingleFilemidTermWindowStepSize. text()), float( self.Audio_Feature_Extraction_SingleFileWindowSize.text()), float(self.Audio_Feature_Extraction_SingleFileStepSize.text()), fileName, storeStFeatures=True, storeToCSV=True, PLOT=False) QMessageBox.about( self.somethingToPass, "Files Created", "Files have been saved as CSV files and .npy files")