parte2

mail@pastecode.io avatar
unknown
python
2 months ago
1.5 kB
2
Indexable
Never
csv_directory = './csv2' # new dir
song_data_backup = {
    'Name': [],
    'Genre': [],
    'duration': [],
    'sr': [],
    'n_channels': [],
    'est_bpm': [],
    'avg_amplitude': [],
    # 'fft_transform': [],
}

for csv in os.listdir('./csv'):
    print(f'Reading CSV with name: {csv}')
    data_df = pd.read_csv(f'./csv/{csv}')
    # zero_crossings_array: np.array = np.array([])
    genre = data_df["Genre"][0]

    df_data_template.to_csv(f'{csv_directory}/{genre}_data.csv')

    for song_name in data_df['Name']:
        print(f'Analyzing total zero crossings for song: {song_name}')
        
        row_df = data_df.loc[data_df['Name'] == song_name]
        sig, sr = AudioUtil().open(f'./content/Music_Genres_Dataset/{genre}/{song_name}')
        combined_sig = sig[0] + sig[1] / 2 # combinamos los dos canales de la canción en uno
        zero_rate: int = librosa.feature.zero_crossing_rate(np.array(combined_sig))
        # zero_crossings_array = np.append(zero_crossings_array, total_zeroes)
        row_df['zero_crossing_rate'] = [zero_rate] # probably raises error for being a numpy array
        
        row_df.to_csv(f'{csv_directory}/{genre}_data.csv', mode='a', header=False, index=False)

        del row_df, sig, sr
        gc.collect()
    
    # data_df['total_zero_crossings'] = zero_crossings_array
    # print('SAVING...')
    # data_df.to_csv(f'{csv_directory}/{genre}_data.csv')
    # print('DONE')
    # del total_zeroes, zero_crossings_array, data_df
    gc.collect()    
Leave a Comment