├── .gitignore ├── utils1.py ├── fft-features.py ├── LICENSE ├── plot-spectogram.py ├── convert-to-wav.py ├── mfcc-features.py ├── README.md ├── learn.py └── tester.py /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | saved_models/ 3 | *.npy 4 | *.wav 5 | *.pyc 6 | learn2.py 7 | genres/ 8 | graphs/ 9 | fft/ 10 | env/ 11 | bin/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | *.egg-info/ 22 | .installed.cfg 23 | *.egg -------------------------------------------------------------------------------- /utils1.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # Directory where the music dataset is located (GTZAN dataset) 4 | GENRE_DIR = "/home/dhruvesh/Desktop/dsp-final/genres" 5 | 6 | # Directory where the test music is located 7 | TEST_DIR = "/home/dhruvesh/Desktop/dsp-final/genre-test" 8 | 9 | # All the available genres 10 | #GENRE_LIST = [ "blues","classical","country","disco","hiphop","jazz","metal","pop","reggae","rock"] 11 | 12 | GENRE_LIST = [ "blues","classical","country","disco","metal"] -------------------------------------------------------------------------------- /fft-features.py: -------------------------------------------------------------------------------- 1 | import scipy 2 | import scipy.io.wavfile 3 | import os 4 | import sys 5 | import glob 6 | import numpy as np 7 | from utils1 import GENRE_DIR, GENRE_LIST 8 | 9 | # Extracts frequencies from a wavile and stores in a file 10 | def create_fft(wavfile): 11 | sample_rate, song_array = scipy.io.wavfile.read(wavfile) 12 | print(sample_rate) 13 | fft_features = abs(scipy.fft(song_array[:30000])) 14 | print(song_array) 15 | base_fn, ext = os.path.splitext(wavfile) 16 | data_fn = base_fn + ".fft" 17 | np.save(data_fn, fft_features) 18 | 19 | 20 | def main(): 21 | 22 | for label, genre in enumerate(GENRE_LIST): 23 | for fn in glob.glob(os.path.join(GENRE_DIR, genre)): 24 | for wavfile in os.listdir(fn): 25 | if wavfile.endswith("wav"): 26 | create_fft(os.path.join(GENRE_DIR, genre,wavfile)) 27 | 28 | 29 | 30 | 31 | 32 | 33 | if __name__ == "__main__": 34 | main() -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 dhruvesh13 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /plot-spectogram.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import scipy.io.wavfile 4 | import scipy.signal 5 | import matplotlib.pyplot as plt 6 | # from matplotlib.pyplot import specgram 7 | 8 | os.chdir(sys.argv[1]) 9 | # Directory provided as a command line argument will be opened to visualize the files inside 10 | wavfiles = [] 11 | for wavfile in os.listdir(sys.argv[1]): 12 | if wavfile.endswith("wav"): 13 | wavfiles.append(wavfile) 14 | 15 | 16 | 17 | wavfiles.sort() 18 | 19 | # Declare sampling rates and song arrays for each arg 20 | sampling_rates = [] 21 | song_arrays = [] 22 | 23 | # Read wavfiles 24 | for wavfile in wavfiles: 25 | sampling_rate, song_array = scipy.io.wavfile.read(wavfile) 26 | sampling_rates.append(sampling_rate) 27 | song_arrays.append(song_array) 28 | 29 | 30 | i = 1 # plot number 31 | # Plot spectrogram for each wave_file 32 | for song_id, song_array, sampling_rate in zip(wavfiles, song_arrays, sampling_rates): 33 | # Create subplots 34 | plt.subplot(10, 10, i) 35 | i += 1 36 | #plt.title(song_id) 37 | plt.specgram(song_array[:30000], Fs=sampling_rate) 38 | print("Plotting spectrogram of song_id: " + song_id) 39 | 40 | plt.savefig('Spectrogram.png') 41 | plt.show() -------------------------------------------------------------------------------- /convert-to-wav.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import sox 4 | 5 | # Store the directory where all the audio files are saved 6 | genre_dirs = ['/home/dhruvesh/Desktop/dsp-final/genres/blues','/home/dhruvesh/Desktop/dsp-final/genres/classical','/home/dhruvesh/Desktop/dsp-final/genres/country', 7 | '/home/dhruvesh/Desktop/dsp-final/genres/disco','/home/dhruvesh/Desktop/dsp-final/genres/hiphop','/home/dhruvesh/Desktop/dsp-final/genres/jazz','/home/dhruvesh/Desktop/dsp-final/genres/metal', 8 | '/home/dhruvesh/Desktop/dsp-final/genres/pop','/home/dhruvesh/Desktop/dsp-final/genres/reggae','/home/dhruvesh/Desktop/dsp-final/genres/rock' 9 | ] 10 | for genre_dir in genre_dirs: 11 | # change directory to genre_dir 12 | os.chdir(genre_dir) 13 | 14 | # echo contents before altering 15 | print('Contents of ' + genre_dir + ' before conversion: ') 16 | os.system("ls") 17 | 18 | # loop through each file in current dir 19 | for file in os.listdir(genre_dir): 20 | # SOX 21 | os.system("sox " + str(file) + " " + str(file[:-3]) + ".wav") 22 | 23 | # delete .au from current dir 24 | os.system("rm *.au") 25 | # echo contents of current dir 26 | print('After conversion:') 27 | os.system("ls") 28 | print('\n') 29 | 30 | print("Conversion complete. Check respective directories.") -------------------------------------------------------------------------------- /mfcc-features.py: -------------------------------------------------------------------------------- 1 | #from scikits.talkbox.features import mfcc 2 | import scipy.io.wavfile 3 | import numpy as np 4 | import sys 5 | import os 6 | import glob 7 | from utils1 import GENRE_DIR, GENRE_LIST 8 | from python_speech_features import mfcc 9 | #from librosa.feature import mfcc 10 | 11 | # Given a wavfile, computes mfcc and saves mfcc data 12 | def create_ceps(wavfile): 13 | sampling_rate, song_array = scipy.io.wavfile.read(wavfile) 14 | print(sampling_rate) 15 | """Get MFCC 16 | ceps : ndarray of MFCC 17 | mspec : ndarray of log-spectrum in the mel-domain 18 | spec : spectrum magnitude 19 | """ 20 | ceps=mfcc(song_array) 21 | #ceps, mspec, spec= mfcc(song_array) 22 | print(ceps.shape) 23 | #this is done in order to replace NaN and infinite value in array 24 | bad_indices = np.where(np.isnan(ceps)) 25 | b=np.where(np.isinf(ceps)) 26 | ceps[bad_indices]=0 27 | ceps[b]=0 28 | write_ceps(ceps, wavfile) 29 | 30 | # Saves mfcc data 31 | def write_ceps(ceps, wavfile): 32 | base_wav, ext = os.path.splitext(wavfile) 33 | data_wav = base_wav + ".ceps" 34 | np.save(data_wav, ceps) 35 | 36 | 37 | def main(): 38 | 39 | for label, genre in enumerate(GENRE_LIST): 40 | for fn in glob.glob(os.path.join(GENRE_DIR, genre)): 41 | for wavfile in os.listdir(fn): 42 | if wavfile.endswith("wav"): 43 | create_ceps(os.path.join(GENRE_DIR, genre,wavfile)) 44 | 45 | 46 | 47 | if __name__ == "__main__": 48 | main() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Audio-Genre-Classification 2 | Automatic music genre classification using Machine Learning algorithms like- Logistic Regression and K-Nearest Neighbours 3 | 4 | **Language used :** Python 2.7 5 | 6 | This repository consists of development code that classifies music according to the following genres: 7 | 8 | 1. Blues 9 | 10 | 2. Classical (Western) 11 | 12 | 3. Country 13 | 14 | 4. Disco 15 | 16 | 5. Metal 17 | 18 | 6. Pop 19 | 20 | ### The Dataset 21 | 22 | The dataset used for training the model is the GTZAN dataset. A brief of the data set: 23 | 24 | * This dataset was used for the well known paper in genre classification " Musical genre classification of audio signals " by G. Tzanetakis and P. Cook in IEEE Transactions on Audio and Speech Processing 2002. 25 | * The dataset consists of 1000 audio tracks each 30 seconds long. It contains 10 genres, each represented by 100 tracks. The tracks are all 22050Hz Mono 16-bit audio files in .wav format. 26 | * Official web-page: [marsyas.info](http://marsyas.info/download/data_sets) 27 | * Download size: Approximately 1.2GB 28 | * Download link: [Download the GTZAN genre collection](http://opihi.cs.uvic.ca/sound/genres.tar.gz) 29 | 30 | ## Feature of .wav files are generated using: 31 | 1. Fast fourier Transform (FFT) (Classification accuracy- **~70%**) 32 | 2. Mel Frequency Cepstral Coefficients (MFCC) (Classification accuracy- **~75%**) 33 | 34 | ## Algorithms used: 35 | 1. Logistic Regression 36 | 2. K-Nearest Neighbours 37 | 38 | ## How to use project for testing: 39 | 40 | 1. Download dataset from: http://opihi.cs.uvic.ca/sound/genres.tar.gz. 41 | 42 | 2. Extract into suitable directory: BASE_DIR 43 | 44 | 3. Run convert-to-wav.py on each subdir of BASE_DIR. 45 | 46 | 4. Run fft-features.py on each subdir of BASE_DIR. 47 | 48 | 5. Run mfcc-features.py on each subdir of BASE_DIR. 49 | 50 | 6. Run learn.py according to run instruction provided in the code file. 51 | 52 | 7. Run tester.py with an audio file to predict the genre 53 | 54 | **Please note-** I have not provided the audio files used by me in the repo. So please replace the directory address wherever necessary in the code with your own local address. 55 | Also while running the tester.py please make sure that the audio file you use is of .wav format sampled at 22050Hz and mono. 56 | -------------------------------------------------------------------------------- /learn.py: -------------------------------------------------------------------------------- 1 | import sklearn 2 | from sklearn import linear_model 3 | from sklearn.neighbors import KNeighborsClassifier 4 | from sklearn.cross_validation import train_test_split 5 | from sklearn.metrics import confusion_matrix 6 | from sklearn.metrics import accuracy_score 7 | import matplotlib.pyplot as plt 8 | import scipy 9 | import os 10 | import sys 11 | import glob 12 | import numpy as np 13 | from utils1 import GENRE_DIR, GENRE_LIST 14 | from sklearn.externals import joblib 15 | from random import shuffle 16 | 17 | """reads FFT-files and prepares X_train and y_train. 18 | genre_list must consist of names of folders/genres consisting of the required FFT-files 19 | base_dir must contain genre_list of directories 20 | """ 21 | def read_fft(genre_list, base_dir): 22 | X = [] 23 | y = [] 24 | for label, genre in enumerate(genre_list): 25 | # create UNIX pathnames to id FFT-files. 26 | genre_dir = os.path.join(base_dir, genre, "*.fft.npy") 27 | # get path names that math genre-dir 28 | file_list = glob.glob(genre_dir) 29 | for file in file_list: 30 | fft_features = np.load(file) 31 | X.append(fft_features) 32 | y.append(label) 33 | 34 | return np.array(X), np.array(y) 35 | 36 | 37 | """reads MFCC-files and prepares X_train and y_train. 38 | genre_list must consist of names of folders/genres consisting of the required MFCC-files 39 | base_dir must contain genre_list of directories 40 | """ 41 | def read_ceps(genre_list, base_dir): 42 | X= [] 43 | y=[] 44 | for label, genre in enumerate(genre_list): 45 | for fn in glob.glob(os.path.join(base_dir, genre, "*.ceps.npy")): 46 | ceps = np.load(fn) 47 | num_ceps = len(ceps) 48 | X.append(np.mean(ceps[int(num_ceps*1/10):int(num_ceps*9/10)], axis=0)) 49 | #X.append(ceps) 50 | y.append(label) 51 | 52 | print(np.array(X).shape) 53 | print(len(y)) 54 | return np.array(X), np.array(y) 55 | 56 | def learn_and_classify(X_train, y_train, X_test, y_test, genre_list): 57 | 58 | 59 | print(len(X_train)) 60 | print(len(X_train[0])) 61 | 62 | #Logistic Regression classifier 63 | 64 | logistic_classifier = linear_model.logistic.LogisticRegression() 65 | logistic_classifier.fit(X_train, y_train) 66 | logistic_predictions = logistic_classifier.predict(X_test) 67 | logistic_accuracy = accuracy_score(y_test, logistic_predictions) 68 | logistic_cm = confusion_matrix(y_test, logistic_predictions) 69 | print("logistic accuracy = " + str(logistic_accuracy)) 70 | print("logistic_cm:") 71 | print(logistic_cm) 72 | 73 | #change the pickle file when using another classifier eg model_mfcc_fft 74 | 75 | joblib.dump(logistic_classifier, 'saved_models/model_mfcc_log.pkl') 76 | 77 | #K-Nearest neighbour classifier 78 | 79 | knn_classifier = KNeighborsClassifier() 80 | knn_classifier.fit(X_train, y_train) 81 | knn_predictions = knn_classifier.predict(X_test) 82 | knn_accuracy = accuracy_score(y_test, knn_predictions) 83 | knn_cm = confusion_matrix(y_test, knn_predictions) 84 | print("knn accuracy = " + str(knn_accuracy)) 85 | print("knn_cm:") 86 | print(knn_cm) 87 | joblib.dump(knn_classifier, 'saved_models/model_mfcc_knn.pkl') 88 | 89 | plot_confusion_matrix(logistic_cm, "Confusion matrix", genre_list) 90 | plot_confusion_matrix(knn_cm, "Confusion matrix for FFT classification", genre_list) 91 | 92 | 93 | def plot_confusion_matrix(cm, title, genre_list, cmap=plt.cm.Blues): 94 | plt.imshow(cm, interpolation='nearest', cmap=cmap) 95 | plt.title(title) 96 | plt.colorbar() 97 | tick_marks = np.arange(len(genre_list)) 98 | plt.xticks(tick_marks, genre_list, rotation=45) 99 | plt.yticks(tick_marks, genre_list) 100 | plt.tight_layout() 101 | plt.ylabel('True label') 102 | plt.xlabel('Predicted label') 103 | plt.show() 104 | 105 | 106 | def main(): 107 | 108 | base_dir_fft = GENRE_DIR 109 | base_dir_mfcc = GENRE_DIR 110 | 111 | """list of genres (these must be folder names consisting .wav of respective genre in the base_dir) 112 | Change list if needed. 113 | """ 114 | genre_list = [ "blues","classical","country","disco","metal"] 115 | 116 | #genre_list = ["classical", "jazz"] IF YOU WANT TO CLASSIFY ONLY CLASSICAL AND JAZZ 117 | 118 | #use FFT 119 | # X, y = read_fft(genre_list, base_dir_fft) 120 | # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .20) 121 | # print('\n******USING FFT******') 122 | # learn_and_classify(X_train, y_train, X_test, y_test, genre_list) 123 | # print('*********************\n') 124 | 125 | #use MFCC 126 | X,y= read_ceps(genre_list, base_dir_mfcc) 127 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .20) 128 | print("new1",X_train.shape) 129 | print('******USING MFCC******') 130 | learn_and_classify(X_train, y_train, X_test, y_test, genre_list) 131 | print('*********************') 132 | 133 | 134 | if __name__ == "__main__": 135 | main() -------------------------------------------------------------------------------- /tester.py: -------------------------------------------------------------------------------- 1 | import os 2 | import timeit 3 | import numpy as np 4 | from collections import defaultdict 5 | #from scikits.talkbox.features import mfcc 6 | from python_speech_features import mfcc 7 | 8 | from sklearn.metrics import precision_recall_curve, roc_curve 9 | from sklearn.metrics import auc 10 | from sklearn.cross_validation import ShuffleSplit 11 | from sklearn.linear_model.logistic import LogisticRegression 12 | from sklearn.metrics import confusion_matrix 13 | from sklearn.externals import joblib 14 | from utils1 import GENRE_DIR, GENRE_LIST 15 | import scipy 16 | import scipy.io.wavfile 17 | 18 | # from utils import plot_roc, plot_confusion_matrix, GENRE_DIR, GENRE_LIST, TEST_DIR 19 | 20 | # from ceps import read_ceps, create_ceps_test, read_ceps_test 21 | 22 | from pydub import AudioSegment 23 | 24 | genre_list = GENRE_LIST 25 | 26 | clf = None 27 | 28 | #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 29 | # Please run the classifier script first 30 | #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 31 | def create_fft(wavfile): 32 | sample_rate, song_array = scipy.io.wavfile.read(wavfile) 33 | fft_features = abs(scipy.fft(song_array[:30000])) 34 | print(song_array) 35 | base_fn, ext = os.path.splitext(wavfile) 36 | data_fn = base_fn + ".fft" 37 | np.save(data_fn, fft_features) 38 | print data_fn 39 | return data_fn 40 | 41 | 42 | 43 | def create_ceps_test(fn): 44 | """ 45 | Creates the MFCC features from the test files, 46 | saves them to disk, and returns the saved file name. 47 | """ 48 | sample_rate, X = scipy.io.wavfile.read(fn) 49 | # X[X==0]=1 50 | # np.nan_to_num(X) 51 | ceps= mfcc(X) 52 | bad_indices = np.where(np.isnan(ceps)) 53 | b=np.where(np.isinf(ceps)) 54 | ceps[bad_indices]=0 55 | ceps[b]=0 56 | base_fn, ext = os.path.splitext(fn) 57 | data_fn = base_fn + ".ceps" 58 | np.save(data_fn, ceps) 59 | print "Written ", data_fn 60 | return data_fn 61 | 62 | 63 | def read_fft(test_file): 64 | X = [] 65 | y = [] 66 | fft_features = np.load(test_file) 67 | X.append(fft_features) 68 | 69 | for label, genre in enumerate(genre_list): 70 | y.append(label) 71 | # for label, genre in enumerate(genre_list): 72 | # # create UNIX pathnames to id FFT-files. 73 | # genre_dir = os.path.join(base_dir, genre, "*.fft.npy") 74 | # # get path names that math genre-dir 75 | # file_list = glob.glob(genre_dir) 76 | # for file in file_list: 77 | # fft_features = np.load(file) 78 | # X.append(fft_features) 79 | # y.append(label) 80 | 81 | # print(X) 82 | # print(y) 83 | 84 | 85 | return np.array(X), np.array(y) 86 | 87 | 88 | def read_ceps_test(test_file): 89 | """ 90 | Reads the MFCC features from disk and 91 | returns them in a numpy array. 92 | """ 93 | X = [] 94 | y = [] 95 | ceps = np.load(test_file) 96 | num_ceps = len(ceps) 97 | X.append(np.mean(ceps[int(num_ceps / 10):int(num_ceps * 9 / 10)], axis=0)) 98 | for label, genre in enumerate(genre_list): 99 | y.append(label) 100 | return np.array(X), np.array(y) 101 | 102 | 103 | 104 | def test_model_on_single_file(file_path): 105 | clf = joblib.load('saved_models/model_mfcc_knn.pkl') 106 | #clf = joblib.load('saved_models/model_mfcc_knn.pkl') 107 | #clf = joblib.load('saved_models/model_fft_log.pkl') 108 | X, y = read_ceps_test(create_ceps_test(test_file)+".npy") 109 | #X,y=read_fft(create_fft(test_file)+".npy") 110 | #nsamples, nx, ny = X.shape 111 | # X = X.reshape((nsamples,nx*ny)) 112 | # x=X[:30000] 113 | # print(x.shape) 114 | probs = clf.predict_proba(X) 115 | print "\t".join(str(x) for x in genre_list) 116 | print "\t".join(str("%.3f" % x) for x in probs[0]) 117 | probs=probs[0] 118 | max_prob = max(probs) 119 | for i,j in enumerate(probs): 120 | if probs[i] == max_prob: 121 | max_prob_index=i 122 | 123 | print max_prob_index 124 | predicted_genre = genre_list[max_prob_index] 125 | print "\n\npredicted genre = ",predicted_genre 126 | dictionary = dict(zip(probs, genre_list)) 127 | #print dictionary 128 | 129 | for values in sorted(dictionary.iteritems(),reverse=True): 130 | print values 131 | 132 | return predicted_genre 133 | 134 | #probs.sort(reverse=True) 135 | 136 | 137 | if __name__ == "__main__": 138 | 139 | global traverse 140 | for subdir, dirs, files in os.walk(GENRE_DIR): 141 | traverse = list(set(dirs).intersection(set(GENRE_LIST))) 142 | break 143 | 144 | #test_file = "/home/dhruvesh/Desktop/dsp-final/genres/blues/blues.00000.wav" 145 | 146 | test_file = "/home/dhruvesh/Desktop/dsp-final/country.wav" 147 | # nsamples, nx, ny = test_file.shape 148 | # test_file = test_file.reshape((nsamples,nx*ny)) 149 | # should predict genre as "ROCK" 150 | predicted_genre = test_model_on_single_file(test_file) 151 | 152 | --------------------------------------------------------------------------------