From b9b69e655a86e70d4ee585cb78344c173b9ef1e3 Mon Sep 17 00:00:00 2001 From: Anmol Mishra Date: Sun, 22 Dec 2024 09:56:25 +0100 Subject: [PATCH] [fix] Fix errors in lecture 12 code Path for GTZAN dataset added in comments. Following fixes were done - - Add error handling during audio load to handle erroneous `mp3` files - Update positional arguments in `librosa.feature.mfcc` to keyword arguments Signed-off-by: Anmol Mishra --- .gitignore | 3 +++ .../code/extract_data.py | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index b6e4761..79c88a5 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# json files +*.json \ No newline at end of file diff --git a/12- Music genre classification: Preparing the dataset/code/extract_data.py b/12- Music genre classification: Preparing the dataset/code/extract_data.py index ef0f851..d41dad6 100644 --- a/12- Music genre classification: Preparing the dataset/code/extract_data.py +++ b/12- Music genre classification: Preparing the dataset/code/extract_data.py @@ -3,7 +3,7 @@ import math import librosa -DATASET_PATH = "path/to/marsyas/dataset" +DATASET_PATH = "path/to/gtzan/dataset" # can be downloaded from 'https://huggingface.co/datasets/marsyas/gtzan/resolve/main/data/genres.tar.gz' JSON_PATH = "data_10.json" SAMPLE_RATE = 22050 TRACK_DURATION = 30 # measured in seconds @@ -48,7 +48,10 @@ def save_mfcc(dataset_path, json_path, num_mfcc=13, n_fft=2048, hop_length=512, # load audio file file_path = os.path.join(dirpath, f) - signal, sample_rate = librosa.load(file_path, sr=SAMPLE_RATE) + try: + signal, sample_rate = librosa.load(file_path, sr=SAMPLE_RATE) + except Exception: + continue # process all segments of audio file for d in range(num_segments): @@ -58,7 +61,7 @@ def save_mfcc(dataset_path, json_path, num_mfcc=13, n_fft=2048, hop_length=512, finish = start + samples_per_segment # extract mfcc - mfcc = librosa.feature.mfcc(signal[start:finish], sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length) + mfcc = librosa.feature.mfcc(y=signal[start:finish], sr=sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length) mfcc = mfcc.T # store only mfcc feature with expected number of vectors