Audios doesn't looping right

I'm trying to deleting silence in audios. this code is just work fine when I'm tryng it in one single audio. But, I've 100 audio left. what I'm tryng to do is to get first audio then remove silnce then save audio with new downloaded audiofile from matlab. but what happens in the attached code is shuffling audios and every new file sound downloaded have 2 or more from the original file audios.
clc;
clear all;
C = dir('*.wav');
for i=1:1:length(C)
[data,fs]=audioread(C(i).name);
data = data(1:end,1)/max(data(1:end,1));
% do framing
f_d = 0.25;
f_size = round(f_d * fs);
n = length(data);
n_f = floor(n/f_size); %no. of frames
temp = 0;
for j = 1 : n_f
frames(j,:) = data(temp + 1 : temp + f_size);
temp = temp + f_size;
end
% silence removal based on max amplitude
m_amp = abs(max(frames,[],2)); % find maximum of each frame
id = find(m_amp > 0.03); % finding ID of frames with max amp > 0.03
fr_ws = frames(id,:); % frames without silence
data_r = reshape(fr_ws',1,[]);
plot(data_r); title('speech without silence');
%saving the new audiofile
write_filename = "Silence_" +num2str(i)+".wav";
audiowrite('C:\Users\Mahmoud El-Moghazi\Desktop\Internship\AudioData\'+write_filename,data_r,fs);
end
what i want is looping in all audios ONE BY ONE then remove silence thene download it without inseting any other audio files.

回答(1 个)

hello
I a not sure what you mean by "download " audio files
I interpreted your post as simply looping over a folder and store the new audio files
IMHO, it's better to put them in a separate output folder (so I created the /out for that purpose)to avoid any mix up between original and processed files (that may be reprocessed by inadvertance).
other suggestions :
  • clear frame at the beginning of the loop otherwise the next file operation may be corrupted by what you have done in the previous iteration
  • also use absolute path naming (more robust) , so use fullfile
here the modified code :
clc;
clearvars;
folder= pwd; % put here folder where wav files are
folder_out = [folder '\out']; % output folder
C=dir(fullfile(folder,'*.wav')); %list all .wavs in folder
nFiles=numel(C);
for i= 1:nFiles
% clear some data first
clear frames
% load new data
fname = C(i).name;
[data,fs]=audioread(fullfile(folder,fname));
data = data(:,1)/max(data(:,1));
% do framing
f_d = 0.25;
f_size = round(f_d * fs);
n = length(data);
n_f = floor(n/f_size); %no. of frames
temp = 0;
for j = 1 : n_f
frames(j,:) = data(temp + 1 : temp + f_size);
temp = temp + f_size;
end
% silence removal based on max amplitude
m_amp = abs(max(frames,[],2)); % find maximum of each frame
id = find(m_amp > 0.03); % finding ID of frames with max amp > 0.03
fr_ws = frames(id,:); % frames without silence
data_r = reshape(fr_ws',1,[]);
subplot(211),plot(data);
subplot(212),plot(data_r);
pause(1)
title('speech without silence');
%saving the new audiofile
write_filename = "Silence_" +num2str(i)+".wav";
% audiowrite('C:\Users\Mahmoud El-Moghazi\Desktop\Internship\AudioData\'+write_filename,data_r,fs);
audiowrite(fullfile(folder_out,write_filename),data_r,fs);
end

3 个评论

I just tried it and it is removing the silence and saving it as new file, but the only problem is that there is some shift in the audio files with non repeating parttern, for example 31 >> 36 silenced, and 99 >> 100 silenced
hi
do this happens for all files or only a few ones ?
could you share the audio files that created the issue ?
tx
not all of them but the majority, roughly I can say that only 5 are the same, (5.wav >> silenced5.wav), those 5 correct files excluding the first 1 as it is always right as it is the first itteration.
they are arabic files, I dont think you would understand them additionaly I cant share them as I am not authorized to do so.
Thanks,

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Code Generation and Deployment 的更多信息

产品

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by