How to implement a consistent reading of all WAV files in the same folder?

It is necessary to conduct a spectral analysis of each WAV file located in the folder. To read the file, use the wav.read function. Further, the data is saved as an numpy array and is further processed.

    2 answers 2

    If I correctly understood the task:

     import os import glob from scipy.io import wavfile as wav my_dir = r'C:\Windows\Media' def process_wavs(files): for f in files: rate, data = wav.read(f) # process data here print('Rate:\t{}\tShape:\t{}\t["{}"]'.format(rate, data.shape, f)) files = glob.glob(os.path.join(my_dir, '*.wav')) process_wavs(files) 

    Conclusion:

     Rate: 44100 Shape: (54080, 2) ["C:\Windows\Media\chimes.wav"] Rate: 44100 Shape: (28480, 2) ["C:\Windows\Media\chord.wav"] Rate: 44100 Shape: (17504, 2) ["C:\Windows\Media\ding.wav"] Rate: 44100 Shape: (29504, 2) ["C:\Windows\Media\ir_begin.wav"] Rate: 44100 Shape: (31552, 2) ["C:\Windows\Media\ir_end.wav"] Rate: 44100 Shape: (44672, 2) ["C:\Windows\Media\ir_inter.wav"] Rate: 22050 Shape: (13591, 2) ["C:\Windows\Media\kaka.Wav"] Rate: 44100 Shape: (57088, 2) ["C:\Windows\Media\notify.wav"] Rate: 44100 Shape: (27936, 2) ["C:\Windows\Media\recycle.wav"] Rate: 44100 Shape: (22048, 2) ["C:\Windows\Media\ringout.wav"] Rate: 44100 Shape: (38400, 2) ["C:\Windows\Media\Speech Disambiguation.wav"] Rate: 44100 Shape: (17792, 2) ["C:\Windows\Media\Speech Misrecognition.wav"] Rate: 44100 Shape: (47104, 2) ["C:\Windows\Media\Speech Off.wav"] Rate: 44100 Shape: (36864, 2) ["C:\Windows\Media\Speech On.wav"] Rate: 44100 Shape: (32192, 2) ["C:\Windows\Media\Speech Sleep.wav"] Rate: 44100 Shape: (71296, 2) ["C:\Windows\Media\tada.wav"] Rate: 44100 Shape: (44544, 2) ["C:\Windows\Media\Windows Balloon.wav"] Rate: 44100 Shape: (53696, 2) ["C:\Windows\Media\Windows Battery Critical.wav"] Rate: 44100 Shape: (44864, 2) ["C:\Windows\Media\Windows Battery Low.wav"] Rate: 44100 Shape: (39808, 2) ["C:\Windows\Media\Windows Critical Stop.wav"] Rate: 44100 Shape: (18144, 2) ["C:\Windows\Media\Windows Default.wav"] Rate: 44100 Shape: (47936, 2) ["C:\Windows\Media\Windows Ding.wav"] Rate: 44100 Shape: (42880, 2) ["C:\Windows\Media\Windows Error.wav"] Rate: 44100 Shape: (60060, 2) ["C:\Windows\Media\Windows Exclamation.wav"] Rate: 44100 Shape: (4960, 2) ["C:\Windows\Media\Windows Feed Discovered.wav"] Rate: 44100 Shape: (22144, 2) ["C:\Windows\Media\Windows Hardware Fail.wav"] Rate: 44100 Shape: (25184, 2) ["C:\Windows\Media\Windows Hardware Insert.wav"] Rate: 44100 Shape: (23552, 2) ["C:\Windows\Media\Windows Hardware Remove.wav"] Rate: 44100 Shape: (5816, 2) ["C:\Windows\Media\Windows Information Bar.wav"] Rate: 44100 Shape: (47586, 2) ["C:\Windows\Media\Windows Logoff Sound.wav"] Rate: 44100 Shape: (39342, 2) ["C:\Windows\Media\Windows Logon Sound.wav"] Rate: 44100 Shape: (6520, 2) ["C:\Windows\Media\Windows Menu Command.wav"] Rate: 44100 Shape: (32384, 2) ["C:\Windows\Media\Windows Minimize.wav"] Rate: 44100 Shape: (2824, 2) ["C:\Windows\Media\Windows Navigation Start.wav"] Rate: 44100 Shape: (56832, 2) ["C:\Windows\Media\Windows Notify.wav"] Rate: 44100 Shape: (21376, 2) ["C:\Windows\Media\Windows Pop-up Blocked.wav"] Rate: 44100 Shape: (88448, 2) ["C:\Windows\Media\Windows Print complete.wav"] Rate: 44100 Shape: (23840, 2) ["C:\Windows\Media\Windows Recycle.wav"] Rate: 44100 Shape: (38720, 2) ["C:\Windows\Media\Windows Restore.wav"] Rate: 44100 Shape: (49728, 2) ["C:\Windows\Media\Windows Ringin.wav"] Rate: 22050 Shape: (11013,) ["C:\Windows\Media\Windows Ringout.wav"] Rate: 44100 Shape: (42350, 2) ["C:\Windows\Media\Windows Shutdown.wav"] Rate: 44100 Shape: (9584, 2) ["C:\Windows\Media\Windows Startup.wav"] Rate: 44100 Shape: (20352, 2) ["C:\Windows\Media\Windows User Account Control.wav"] 

      Getting the list of wav files in yourDIR directory and printing out elementwise:

       import os yourDIR = 'C:/WAVdir/' files = os.listdir(yourDIR) filelist = filter(lambda x: x.endswith('.wav'),files) for i in filelist: print i 

      If necessary, the code can be used with any other filter, instead of the one given .wav .