read multiple audio files as an index, and get length of each file in matlab

You can use audioread function to read the audio files and dir function to get the files in a directory. To get the length of each file, you can use the length function on the audio data.

Here's an example code:

main.m
% specify path to the directory containing audio files
path = './audio_files/';

% get list of audio files in the directory
dir_info = dir([path '*.wav']);
% alternatively, you can use different extension such as .mp3, .flac, etc.

% loop through each file and read it
for i = 1:numel(dir_info)
    % read audio file
    [audio_data, sample_rate] = audioread([path dir_info(i).name]);
    
    % get length of audio file in seconds
    audio_length_seconds = length(audio_data) / sample_rate;
    
    disp(sprintf('%s length: %.2f seconds', dir_info(i).name, audio_length_seconds));
end
588 chars
18 lines

This code reads all the audio files in the audio_files directory with .wav extension, and then prints out the length (in seconds) of each file.

related categories

gistlibby LogSnag