Search code examples
pythonaudiofeature-extractiondatadirectory

How can I extract features from audio to dataset?


I have a folder stored on the my desktop, and its contains 187 audio file in WAV format. I want to extract features from audio files and I executed the following code for extract features audio and to save the features in CSV file, but I obtained excel file with no value, its include only the title for each column and the output for len(audio-files) was 0..but its must be 187. How can I solve this problem???

  from glob import glob
    data_dir = './audio featur-extraction\audio-setA/'
    audio_files = glob(data_dir + '*.wav')
    len(audio_files)
        
       
    from librosa import feature
    import numpy as np
    
    fn_list_i = [
        feature.chroma_stft,
        feature.spectral_centroid,
        feature.spectral_bandwidth,
        feature.spectral_rolloff,
      
    ]
      
    fn_list_ii = [
        
      
        
        feature.zero_crossing_rate
            
    ]
      
    
    def get_feature_vector(y,sr):  
      feat_vect_i = [ np.mean(funct(y,sr)) for funct in fn_list_i]
      feat_vect_ii = [ np.mean(funct(y)) for funct in fn_list_ii]
      
      feature_vector =   feat_vect_i + feat_vect_ii  
      return feature_vector
    
    #build the matrix with normal audios featurized
    audios_feat = []
    for file in audio_files:
      '''
      y is the time series array of the audio file, a 1D np.ndarray
      sr is the sampling rate, a number
      '''  
      y,sr = librosa.load(file,sr=None)   
      feature_vector = get_feature_vector(y, sr) 
      audios_feat.append(feature_vector)  
      print('.', end= " ")
      
      print(audios_feat)
      #.........................
    import csv
    
    norm_output = 'normals_00.csv'
    
    
    header =[
        'chroma_stft',
        'spectral_centroid',
        'spectral_bandwidth',
        'spectral_rolloff',
        'zero_crossing_rate',
        
    ]
    
    #WARNING : this overwrites the file each time. Be aware of this because feature extraction step takes time.
    with open(norm_output,'+w') as f:
      csv_writer = csv.writer(f, delimiter = ',')
      csv_writer.writerow(header)
      csv_writer.writerows(audios_feat)

Solution

  •  audio_files = glob(data_dir + '/*.wav')
    

    it solved by add slash before the star symbole