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
awais khan
2021년 6월 29일
Brace indexing is not supported for variables of this type.
I got this error.
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!