3

I have the code below, which takes an data set(GTZAN) and turns it into an MFCC in dictionary:

DATASET_PATH = '/content/drive/MyDrive/ColabNotebooksNew/PROJECT/ProjectMusic/Data/genres_original'
JSON_PATH = "data_10.json"
SAMPLE_RATE = 22050 #each song is 30s long, with a 22,050 Hz sample rate
TRACK_DURATION = 30 # measured in seconds
SAMPLES_PER_TRACK = SAMPLE_RATE * TRACK_DURATION #=661,500


def save_mfcc(dataset_path, json_path, num_mfcc=13, n_fft=2048, hop_length=512, num_segments=5):

    # dictionary to store mapping, labels, and MFCCs
    data = {
        "mapping": [], #label names. size - (10,)
        "labels": [],  #Stores the 'real' song type(value from 0-9). size - (5992,)
        "mfcc": []     #store the mfccs.size - (5992, 216, 13)
    }

    samples_per_segment = int(SAMPLES_PER_TRACK / num_segments)  #=110250
    num_mfcc_vectors_per_segment = math.ceil(samples_per_segment / hop_length) #=216(math.ceil of 215.332)
    # loop through all genre sub-folder
    for i, (dirpath, dirnames, filenames) in enumerate(os.walk(dataset_path)):

      # ensure we're processing a genre sub-folder level
      if dirpath is not dataset_path:

          # save genre label (i.e., sub-folder name) in the mapping
          semantic_label = dirpath.split("/")[-1]
          data["mapping"].append(semantic_label)
          print("\nProcessing: {}".format(semantic_label))
          # process all audio files in genre sub-dir
          for f in filenames:

            # load audio file

            file_path = os.path.join(dirpath, f)
        
            if file_path != '/content/drive/MyDrive/ColabNotebooksNew/PROJECT/ProjectMusic/Data/genres_original/jazz/jazz.00054.wav': 
              """fileError: Error opening '/content/drive/MyDrive/ColabNotebooksNew/PROJECT/ProjectMusic/Data/genres_original/jazz/jazz.00054.wav': File contains data in an unknown format."""

              signal, sample_rate = librosa.load(file_path, sr=SAMPLE_RATE) #signal= how much samples in the audio file, sample rate = num of sample rate of the audio file, sample=22050

              # process all segments of audio file
              for d in range(num_segments):

                # calculate start and finish sample for current segment
                start = samples_per_segment * d
                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 - time and Coef(13 because num_mfcc=13), 
                mfcc = mfcc.T #[216,13]
                # store only mfcc feature with expected number of vectors
                if len(mfcc) == num_mfcc_vectors_per_segment: #==216
                    data["mfcc"].append(mfcc.tolist())
                    data["labels"].append(i-1)
                    print("{}, segment:{}".format(file_path, d+1))
    # save MFCCs to json file
    with open(json_path, "w") as fp:
        json.dump(data, fp, indent=4) #puts everything in the Json File
# Runs Data Processing     
save_mfcc(DATASET_PATH, JSON_PATH, num_segments=6)

I have been using this code for a long while, it has worked great until today I got an error as below:

TypeError                                 Traceback (most recent call last)
<ipython-input-10-4a9371926618> in <module>
      1 # Runs Data Processing
----> 2 save_mfcc(DATASET_PATH, JSON_PATH, num_segments=6)

<ipython-input-9-8ba1c6e78747> in save_mfcc(dataset_path, json_path, num_mfcc, n_fft, hop_length, num_segments)
     56 
     57                 # extract mfcc
---> 58                 mfcc = librosa.feature.mfcc(signal[start:finish], sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length) #mfcc - time and Coef(13 because num_mfcc=13),
     59                 mfcc = mfcc.T #[216,13]
     60                 # store only mfcc feature with expected number of vectors

TypeError: mfcc() takes 0 positional arguments but 2 positional arguments (and 1 keyword-only argument) were given

About the save_mfcc function:

Extracts MFCCs from music dataset and saves them into a json file along with genre labels.

    :param dataset_path (str): Path to dataset
    :param json_path (str): Path to json file used to save MFCCs
    :param num_mfcc (int): Number of coefficients to extract 
    :param n_fft (int): Interval we consider to apply FFT. Measured in # of samples
    :param hop_length (int): Sliding window for FFT. Measured in # of samples
    :param: num_segments (int): Number of segments we want to divide sample tracks into
    :return:

I don't understand why the problem just appeared today, and how to fix it.

How can I solve the error?

Liav
  • 31
  • 3
  • 1
    Update: The problem was fixed by adding (y=signal[start:finish],sr=sample_rate). As simple as that... – Liav Mar 18 '23 at 14:29

2 Answers2

2

You should be careful when giving the parameters and act like this

mfcc = librosa.feature.mfcc(y=signal[start:finish],sr=sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length) 
0

I also meet this problem during doing my graduation project, you can uninstall librosa0.9.0 version, download the 0.8.0 version and have a try, the Tsinghua mirror source can't be used to install librosa 0.8.0,you'd better to get a vpn to download the version 0.8.0 from the official