Divide .wav file into equal segments
26 views (last 30 days)
Show older comments
Christine
on 27 May 2020
Commented: Walter Roberson
on 29 Jun 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 Comments
Walter Roberson
on 27 May 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)
Accepted Answer
Walter Roberson
on 28 May 2020
Edited: Walter Roberson
on 28 May 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 Comments
awais khan
on 29 Jun 2021
Brace indexing is not supported for variables of this type.
I got this error.
Walter Roberson
on 29 Jun 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
More Answers (0)
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!