Merna
Merna

Reputation: 13

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)

Upvotes: 1

Views: 1448

Answers (2)

Lukasz Tracewski
Lukasz Tracewski

Reputation: 11407

The error is in this line:

data_dir = './audio featur-extraction\audio-setA/'

It's the extra slash. Replace it with backslash (as in rest of the path) and you will be good to go. In future: debug your code. Step line by line through the code and find the error. If length of the array with paths to process is zero, nothing will be computed.

It should be

data_dir = r'audio featur-extraction\audio-setA\' # for Windows 

or

data_dir = 'audio featur-extraction/audio-setA/' # for Mac / Linux. 

More generally, use os.path.joinin your code

Upvotes: 2

Merna
Merna

Reputation: 13

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

it solved by add slash before the star symbole

Upvotes: 0

Related Questions