Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.5 kB
2
Indexable
Never
#para cada genero vamos a representar la envolvente la root-mean-square energy, zero-crossing rate,la transformada de fourier, el espectograma, el espectograma de mel y el cromagrama
blues_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/blues/Born To Play Guitar (Official Audio).mp3"
classical_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/classical/Mozart - Lacrimosa.mp3"
country_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/country/Marry Me.mp3"
disco_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/disco/On The Floor.mp3"
hiphop_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/hiphop/Snoop Dogg - Lodi Dodi.mp3"
jazz_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/jazz/Kenny Burrell - Chitlins Con Carne.mp3"
metal_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/metal/Nightmare.mp3"
pop_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/pop/Dance Monkey.mp3"
reggae_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/reggae/Me Gustas Tú.mp3"
rock_song_path="./content/drive/MyDrive/AVD/Trabajos_1_2/Datasets/Music_Genres_Dataset/rock/Seven Nation Army.mp3"
#hacer una lista con todos los paths
paths=[blues_song_path,classical_song_path,country_song_path,disco_song_path,hiphop_song_path,jazz_song_path,metal_song_path,pop_song_path,reggae_song_path,rock_song_path]
#recorrer la lista y hacer un grafico de 3x2  para cada genero con las caracteristicas de cada cancion
for song in paths:
  #hacer print del genero musical
  genero=song.split("/")[-2]
  y, sr = librosa.load(song)
  n_fft=2048 
  frame_length = n_fft 
  hop_length=int(n_fft/4) 
  energy = np.array([sum(abs(y[i:i+frame_length]**2))for i in range(0, len(y), hop_length)])
  energy = energy[np.newaxis, :]
  rmse = librosa.feature.rms(y=y, hop_length=hop_length, center=True)
  zcrs = librosa.feature.zero_crossing_rate(y=y, hop_length=hop_length)
  rms = librosa.feature.rms(y=y)[0]
  D = librosa.stft(y)
  S = np.abs(librosa.stft(y))
  S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000)
  chromagram = librosa.feature.chroma_stft(y=y, sr=sr)
  fig, axs = plt.subplots(2, 3,figsize=(30,10))
  #en el subtitulo poner caracteristicas del genero: y el genero de la cancion que se ha guardado en la variable genero
  fig.suptitle(f'Características de la canción de {genero}')
  axs[0, 0].plot(energy[0])
  axs[0, 0].set_title('Energia')
  axs[0, 1].plot(rmse[0])
  axs[0, 1].set_title('Root-mean-square')
  axs[0, 2].plot(zcrs[0])
  axs[0, 2].set_title('Zero-crossing rate')
  librosa.display.specshow(librosa.amplitude_to_db(np.abs(D), ref=np.max), sr=sr, x_axis='time', y_axis='log',ax=axs[1, 0])
  axs[1, 0].set_title('Transformada de Fourier')
  librosa.display.specshow(librosa.amplitude_to_db(S, ref=np.max), y_axis='mel', sr=sr, fmax=8000, x_axis='time',ax=axs[1, 1])
  axs[1, 1].set_title('Espectograma de mel')
  librosa.display.specshow(chromagram, y_axis='chroma', x_axis='time',ax=axs[1, 2])
  axs[1, 2].set_title('Cromagrama')
  plt.show()
  #imprimir la media de Energía, root-mean-square y zero-crossing rate
  print(f"Media de Energía: {np.mean(energy)}")
  print(f"Media de Root-mean-square: {np.mean(rmse)}")
  print(f"Media de Zero-crossing rate: {np.mean(zcrs)}")
  print("\n")
Leave a Comment