<jupyter_start><jupyter_text>Music Feature extraction using librosa<jupyter_code>from google.colab import drive
drive.mount('/content/drive', force_remount=True)
import tempfile
from google.colab import files
import os
print( os.getcwd() )
os.chdir("/content/drive/My Drive")
removable_songs = ['000704.mp3', '133297.mp3','098565.npy','098567.npy','098569.npy','000705.mp3','099134.mp3', '108925.mp3','000706.mp3', '000707.mp3', '000708.mp3', '000709.mp3', '003775.mp3', '003776.mp3', '003777.mp3', '003778.mp3', '003779.mp3', '004070.mp3', '004071.mp3', '004072.mp3', '004074.mp3', '004075.mp3', '004076.mp3', '004077.mp3', '004078.mp3', '004079.mp3', '004080.mp3', '011671.mp3', '011672.mp3', '011674.mp3', '011675.mp3', '011677.mp3', '011679.mp3', '011681.mp3', '011682.mp3', '018031.mp3', '018032.mp3', '018034.mp3', '019459.mp3', '024362.mp3', '024364.mp3', '024365.mp3', '024366.mp3', '024367.mp3', '024368.mp3', '024369.mp3', '024370.mp3', '024371.mp3', '116753.mp3']+['004232.mp3', '004234.mp3', '004235.mp3', '004236.mp3', '004239.mp3', '006330.mp3', '006354.mp3', '006367.mp3', '006379.mp3', '006389.mp3', '016744.mp3', '016745.mp3', '016747.mp3', '017631.mp3', '017632.mp3', '017634.mp3', '017635.mp3', '017636.mp3', '017637.mp3', '018037.mp3', '018038.mp3', '018039.mp3', '019674.mp3', '042373.mp3']+['004232.mp3', '004234.mp3', '004235.mp3', '004236.mp3', '004239.mp3', '004846.mp3', '004848.mp3', '004849.mp3', '006342.mp3', '006360.mp3', '006366.mp3', '006372.mp3', '006380.mp3', '006385.mp3', '006459.mp3', '006469.mp3', '022315.mp3', '042373.mp3', '048463.mp3', '048493.mp3', '063803.mp3', '064593.mp3']+['013537.mp3', '013538.mp3', '013539.mp3', '013540.mp3', '014601.mp3', '014602.mp3', '014604.mp3', '135373.mp3']+['015487.mp3', '015488.mp3', '017631.mp3', '017632.mp3', '017634.mp3', '017635.mp3', '017636.mp3', '017637.mp3']+['015487.mp3', '015488.mp3']+['015880.mp3', '016334.mp3', '017605.mp3', '017606.mp3', '017607.mp3', '017608.mp3', '017609.mp3', '017610.mp3', '017611.mp3']
print(len(removable_songs))
import librosa
import librosa.display
import scipy.ndimage
import numpy as np
from pylab import plot, show, figure, imshow
%matplotlib inline
import matplotlib.pyplot as plt
songs = []
for root, dirs, files in os.walk("./Western-Dataset"):
    for file in files:
        if file.endswith(".mp3"):
          if file in removable_songs:
            #print(file)
            pass
          else:
            songs.append(os.path.join(root, file))
print(len(songs)) #7898
songs[7177]
print(songs[10])
print(songs[10].split('/')[-1].replace('.mp3',''))<jupyter_output><empty_output><jupyter_text>To check for a single song<jupyter_code>y,sr = librosa.load(songs[10], duration = 10, sr=16000)
melspectrogram = librosa.feature.melspectrogram(y, sr=16000, n_fft=2048, hop_length=800, n_mels=48)
#use the hop_size of 160 samples, n_mels=128 ( will mean high memory needs for big data), So selecting 48 
#n_fft >= # samples in a single frame, Since windowsize=100 ms = 100*16000/1000 = 1600, n_fft= 2048( nearest power of 2) because FFT algo works faster that way. samples padded with zeros
#hop_length = 50 ms = 16000*50/1000= 800
#total samples for 20 sec duration = 20*16000= 320000
log_melspectrogram = librosa.power_to_db(melspectrogram, ref=np.max)
log_melspectrogram_new = log_melspectrogram[:, :-1].astype('float32')
# need to understand why to do the above above...
print(log_melspectrogram.shape,sr)
print(log_melspectrogram_new.shape)

np.save("./MusicResearchColabNB/Test1.npy", log_melspectrogram_new)

X_logmel_W=[]
l_count=0
# Some corrupt songs need to be removed. so some of this can fail.. For example song at 3960 099134.mp3 ,  , 3960+894=4854 108925.mp3 , 7178etc...
# 4854+2122= 6976
# the base_dir is always commented else it can override the existing files unintentionally.
#base_dir = /content/drive/My Drive//MusicResearchColabNB/MelFeatures

base_dir = "/content/drive/My Drive//MusicResearchColabNB/MelFeatures"

for song in songs[6976:]:
  l_count=l_count+1
  print(l_count, song.split('/')[-1])
  y,sr = librosa.load(song, duration = 20, sr=16000)
  melspectrogram = librosa.feature.melspectrogram(y, sr=16000, n_fft=2048, hop_length=800, n_mels=48)
  log_melspectrogram = librosa.power_to_db(melspectrogram, ref=np.max)
  log_melspectrogram_new = log_melspectrogram[:, :-1].astype('float32')
  log_melspectrogram_arr = np.array(log_melspectrogram_new)
  np_file_nm = song.split('/')[-1].replace('.mp3','.npy')
  X_logmel_W.append(log_melspectrogram_new)
  np.save(base_dir+"/Western_mel_numpy/"+np_file_nm,log_melspectrogram_arr )

#X_logmel_W = np.array(X_logmel_W) 
#print(X_logmel_W[0].shape)
#np.save("./MusicResearchColabNB/Western_dataset.npy", X_logmel_W)
# read a npy and extract its first input...
import numpy as np
y_sample = np.load('./MusicResearchColabNB/MelFeatures/Western_mel_numpy/154414.npy')
y_sample.shape
# To plot a single loaded mp3 file as is
#song =??
#y_sample,sr = librosa.load(song, duration = 20, sr=16000)
#plt.figure(figsize=(15, 5))
#librosa.display.waveplot(y_sample, sr, alpha=0.8)
# This block is when you just want to plot the log_melspectrogram for a single file
#
#single_log_melspectrogram =??
#plt.figure(figsize=(25,10))
#librosa.display.specshow(single_log_melspectrogram
#                         , x_axis ='time'
#                         , y_axis = 'mel'
#                         , sr = sr)
#plt.colorbar(format="%+2.f")
#plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17783882783882785, 'fraction_numerical': 0.19395604395604396, 'mean_word_length': 5.219817767653758, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 125, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '563175', 'license': 'MIT', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2316, 'n_words': 418, 'path': 'MelspectrogramFeatureExtraction.ipynb', 'repo_name': 'singr7/MIRAutoencoder', 'size': '5460'}