Read and Sort Large Amount of Audio .wav Files

9 views (last 30 days)
Jacks
Jacks on 29 Jan 2024
Commented: Jacks on 29 Jan 2024
Hi there, I am trying to read a large amount of .wav audio files, and I would like to find an easy way to organise them in the raw data array, based upon their file names. I.e. a correct order such as Joe_0_1.wav, Joe_0_2.wav, Joe_1_0.wav, Joe_1_1.wav, Paul_0_1.wav, Paul_1_0.wav, Paul_2_0.wav etc.
Note for example Joe is a folder with subfolder 0,1,2,3 etc, and they each contain multiple ordered .wav files.
I have tried below by using the Audio Tool Box function 'audioDatastore' to get the raw data and seperating the 'Joe' and 'Paul' folders with two while loops. They both read into Y, but after testing, each .wav is not in its desired array position. Thinking for loops might be a better option? But failed at implementing. Any help would be tremendous.
% folder
folder = fullfile('Audio','Joe');
% Audio datastore that points to the specified folder.
ADS = audioDatastore(folder, IncludeSubfolders=true);
% While audio datastore has files to be read still, continue to read
% from the datastore.
m = 1; % initialise count
Y = zeros(48000,21595); % pre allocate raw .wav matrix
normal_length = 48000; % total samples for each .wav file
max = 21595; % total number of .wav files in 'clips' directory
while hasdata(ADS) && m < max
data = read(ADS);
% if not already, make all audio data files 48k in length with zero padding
padding = zeros(normal_length-length(data),1);
data_fix = [data(:);padding(:)];
% append new data file to Y
Y(:,m) = data_fix;
fprintf('Fraction of Joe files read: %.2f\n',progress(ADS))
m = m + 1;
end
% folder
folder = fullfile('Audio','Paul');
% Create an audio datastore that points to the specified folder.
ADS = audioDatastore(folder, IncludeSubfolders=true);
while hasdata(ADS) && m < max
data = read(ADS);
% make all audio data files 48k in size with padding
padding = zeros(normal_length-length(data),1);
data_fix = [data(:);padding(:)];
% append new data file to Y
Y(:,m) = data_fix;
fprintf('Fraction of Paul files read: %.2f\n',progress(ADS))
m = m + 1;
end
  2 Comments
Dyuman Joshi
Dyuman Joshi on 29 Jan 2024
Edited: Dyuman Joshi on 29 Jan 2024
"I would like to find an easy way to organise them based upon their file names. I.e. a correct order such as Joe_0_1.wav, Joe_0_2.wav, Joe_1_0.wav, Joe_1_1.wav, Paul_0_1.wav, Paul_1_0.wav, Paul_2_0.wav etc. "
You can use this FEX submission to get the file names and read them in that particular order - https://in.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort?s_tid=ta_fx_results
Jacks
Jacks on 29 Jan 2024
Thank you for providing the solution! Works great

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 29 Jan 2024
Edited: Stephen23 on 29 Jan 2024
As far as I know, the only way to get get the correct order DATASTORE filenames without affecting other properties/attributes is to provide the sorted filenames explicitly when you create the DATASTORE:
Note that sorting IMAGEDATASTORE names property/attribute once it is created apparently deletes the labels attribute, so possiby affects other types of DATASTORE and possibly other attributes.

Tags

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!