read all audio files in one directory

Hi, I want to remove a frequency from all my mp3 songs, my idea is to use the audioread and audioinfo functions to copy a song in my workspace, see if i can use the filter I created, with a sample rate of 44.1kHz and cut out the frequency (1kHz +/- 300Hz). With audiowrite I can create a .wav file without that frequency. But its a lot of work to do that for every song, is it possible to read the first song in one directory, filter it and generate a wav file in another directory and continue with the second song in the directory and so on? audioread expects the whole filename, I have no idea how to find out how many and which songs are in the directory, can anyone help me?

 Accepted Answer

folder='C:\Users\malek\Documents\MATLAB'
audio_files=dir(fullfile(folder,'*.m'))
for k=1:numel(audio_files)
filename=audio_files(k).name
%Do what you want with filename
% create a new file name 'new_file' for example
folder_destination='C:\Users\malek\Documents' % for example
file_dest=fullfile(folder_destination,'new_file'
% .....
end

12 Comments

Your own audio file names probably do not end in .m
Note that the original question was more about handling the files than it was about the audio processing.
And your folder is different too. Use the code in the FAQ (the link I gave in my answer below) and replace the obvious things like file and folder names and extensions, and it should run. If it doesn't, check that the files are actually there and post your non-working code, along with any error messages (ALL the red text, not just part of it).
I happen to need to use the audioread() function, in order to later do a fft().
The code is able to retrieve the first audio, but then it stops.
This is the code and the error.
folder = 'C:\Users\bla bla bla'
audio_files = dir(fullfile(folder,'*.wav'))
for k=1:numel(audio_files)
filename = audio_files(k).name
[y(k),Fs(k)] = audioread('filename');
end
%The Error
Error using audioread (line 90)
The filename specified was not found in the MATLAB path.
Error in Situacion_problema (line 7)
[y(k),Fs(k)] = audioread('filename');
D = 'C:\Users\bla bla bla';
S = dir(fullfile(D,'*.wav'));
N = numel(S);
y = cell(1,N);
Fs = cell(1,N);
for k = 1:N
F = fullfile(D,S(k).name);
[y{k},Fs{k}] = audioread(F);
end
Javier
Javier on 17 Nov 2020
Edited: Javier on 17 Nov 2020
Thank you so much sir! It worked perfectly.
Cesar Lobo
Cesar Lobo on 26 Nov 2020
Edited: Cesar Lobo on 26 Nov 2020
javier would you be so kind to show me the work you did for the fft?
I need to write a code for fft of a song as well and i cant do it.
thank you in advance
Stephen can you also help me writting a code to plot the fft of a song
Did you try fft()? If so, start your own, new question in a new thread. Post your code and data there.
my data is a random song. the code is supposed to import a song from a folder and performe the fft. i want to see the graph
OK, so did you call audioread() followed by fft()? If not, why not?
see the first example in the fft documentation for information on plotting

Sign in to comment.

More Answers (1)

2 Comments

Hola image Analyst
I have attempted to use your code in the link on MatlabFandom - I am struggling to actually save the .wav files I need.
The files are hundreds of audiofiles relating to speach recognition, they all sit in different subfolders, each with its own date (there are 2 files per day). There are hundreds of subfolders with .wav files in them. I would like the .wav files to be read and saved in an array called 'AudioArray'. I run your code below, I receive an output listing all the file names. But there is no raw data available for me to analyse. I also want to do some processing once the file has been read - in this case I have just put max(audioArray) to show this. Gracias for all help!
% Specify the folder where the files live.
myFolder = 'C:\Users\myname\Documents\voicedata\Audio';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '**/*.wav'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
AudioArray = audioread(fullFileName);
maxaudio=max(AudioArray); %calculates the max value of each audiostored in audioArray
drawnow; % Force display to update immediately.
end
AudioArray{k} = audioread(fullFileName);
maxaudio(k) = max(AudioArray{k});

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!