The strategy discussed above is coded as follows (the code file is available as Genre_classification.ipynb in GitHub):
- Download the dataset and import the relevant packages:
import sys, re, numpy as np, pandas as pd, music21, IPython, pickle, librosa, librosa.dsiplay, osfrom glob import globfrom tqdm import tqdmfrom keras.utils import np_utils
- Loop through the audio files to extract the mel spectrogram input features of the input audio, and store the output genre for the audio input:
song_specs=[]genres = []for genre in os.listdir('...'): # Path to genres folder song_folder = '...' # Path to songs folder for song in os.listdir(song_folder): if song.endswith('.au'): signal, sr = librosa.load(os.path.join(song_folder, song), ...