Divide .wav file into equal segments
26 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2020-5-27
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)
采纳的回答
Walter Roberson
2020-5-28
编辑:Walter Roberson
2020-5-28
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 个评论
Walter Roberson
2021-6-29
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!