create an array of audio files and their length in matlab

To create an array of audio files and their length in MATLAB, you can use the following code:

main.m
% define the path of the directory containing audio files
dir_path = 'path\to\directory';

% load all audio file names in the directory
audio_files = dir(fullfile(dir_path,'*.wav'));

% loop through each audio file and get its length
audio_lengths = zeros(length(audio_files),1);
for i = 1:length(audio_files)
    % read the audio file
    [audio,fs] = audioread(fullfile(dir_path,audio_files(i).name));
    
    % get the length of the audio file
    audio_lengths(i) = length(audio)/fs;
end
493 chars
16 lines

This code will create an array audio_lengths containing the length of each audio file in seconds. Note that the code assumes that the audio files are in .wav format. If your audio files have a different format, you will need to modify the dir function accordingly.

related categories

gistlibby LogSnag