Divide .wav file into equal segments
22 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Christine
le 27 Mai 2020
Commenté : Walter Roberson
le 29 Juin 2021
I've been having some trouble dividing my 159567 ms audio file into 3000 ms segments. I've seen on previous threads about using the 'bufer' function, but I'm not quite sure how to implement it here. What ends up happening here is that the correct number of segments are created, but the audio file doesn't get cut into its 3000 ms chunks (instead I have 53 audio files that are exactly the same). Any help would be appreciated.
%epoch into 3 sec nonoverlapping segments
for i = 1
[y, Fs] = audioread('Myfile.wav');
stims{i} = y;
stiml(i) = length(stims{i});
stiml(i) = stiml(i)/44.1; %put lengths into milliseconds in time (not samples). Length: 2 min, 39 s
end
t1 = 1;
t2 = 3000; %ms
k = 1;
while t2 <= stiml %equal to 159567 ms
new_seg = [t1,t2];
File = sprintf('Part%02d.wav', k);
audiowrite(File, y, Fs);
t1=t1+3000;
t2=t2+3000;
k=k+1;
end
2 commentaires
Walter Roberson
le 27 Mai 2020
You are proceeding by 3000 samples, not by 3000 ms. To get 3000 milliseconds in terms of samples you need to multiply Fs by 3000 (milliseconds) / 1000 (milliseconds per second)
Réponse acceptée
Walter Roberson
le 28 Mai 2020
Modifié(e) : Walter Roberson
le 28 Mai 2020
The below will split all .wav in a given directory into 3 second buffers. A directory named after the file will be created inside outdir for each file, and the split files will be named by Part inside the relevant directory.
This code does not assume that the sample frequency is the same for all files.
This code does not assume mono source, and does not assume that the number of channels is the same for each file.
%epoch into 3 sec nonoverlapping segments
projectdir = 'location of files to split';
outdir = 'location of directory to split into'; %can be the same as projectdir
dinfo = dir( fullfile(projectdir, '*.wav') );
filenames = fullfile( projectdir, {dinfo.name} );
nfiles = length(dinfo);
stims = cell(nfiles,1);
stiml = zeros(nfiles,1);
Fss = zeros(nfiles,1);
for K = 1 : nfiles
[y, Fss(K)] = audioread(filenames{K});
stims{K} = y;
stiml(K) = size(y,1) / Fss(K) * 1000;
end
for K = 1 : nfiles
[~, basename, ext] = fileparts( filenames{K} );
thisoutdir = fullfile(outdir, basename);
if ~exist(thisoutdir, 'dir');
try
mkdir(thisoutdir);
catch ME
fprintf('count not make the output directory "%s", skipping file\n', thisoutdir);
continue;
end
end
nchan = size(stims{K},2);
Fs = Fss(K);
buffered_chans = arrayfun(@(Cidx) buffer(stims{K}(:,Cidx), floor(3*Fss{K})), 1:nchan, 'uniform', 0);
nparts = size(buffered_chans{1},2);
for part = 1 : nparts
File = fullfile(thisoutdir, sprintf('Part%02d.wav', part));
this_segment = cell2mat(cellfun(@(C) C(:,part), buffered_chans, 'uniform', 0));
audiowrite(File, this_segment, Fs);
end
end
5 commentaires
awais khan
le 29 Juin 2021
Brace indexing is not supported for variables of this type.
I got this error.
Walter Roberson
le 29 Juin 2021
You are right, it needed a minor fix.
%epoch into 3 sec nonoverlapping segments
projectdir = fullfile(matlabroot, 'toolbox', 'audio', 'samples');
outdir = 'junk_out'; %can be the same as projectdir
dinfo = dir( fullfile(projectdir, '*.wav') );
filenames = fullfile( projectdir, {dinfo.name} );
nfiles = length(dinfo);
stims = cell(nfiles,1);
stiml = zeros(nfiles,1);
Fss = zeros(nfiles,1);
for K = 1 : nfiles
[y, Fss(K)] = audioread(filenames{K});
stims{K} = y;
stiml(K) = size(y,1) / Fss(K) * 1000;
end
for K = 1 : nfiles
[~, basename, ext] = fileparts( filenames{K} );
thisoutdir = fullfile(outdir, basename);
if ~exist(thisoutdir, 'dir');
try
mkdir(thisoutdir);
catch ME
fprintf('count not make the output directory "%s", skipping file\n', thisoutdir);
continue;
end
end
nchan = size(stims{K},2);
Fs = Fss(K);
buffered_chans = arrayfun(@(Cidx) buffer(stims{K}(:,Cidx), floor(3*Fss(K))), 1:nchan, 'uniform', 0);
nparts = size(buffered_chans{1},2);
for part = 1 : nparts
File = fullfile(thisoutdir, sprintf('Part%02d.wav', part));
this_segment = cell2mat(cellfun(@(C) C(:,part), buffered_chans, 'uniform', 0));
audiowrite(File, this_segment, Fs);
fprintf('wrote "%s"\n', File)
end
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Audio I/O and Waveform Generation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!