This may be easy but I'm no expert in MatLab. I have a bacth of wav files and I need to extract a 30s sample from each. Example:
1.wav - need 1:34-2:04
2.wav - need 5:15 - 5:45
Ideally I'd like to run a code which will take each wav file and extract the correct time period according to a pre-generated table and save each snippet as a new wav file (eg, 1_snip.wav would be the 30secs I need to analyze). Any points in the right direction would be great. Thanks!
First you extract raw audio from the recording: [signal,fs] = audioread('file.wav');
The sampling rate is stored inside fs, representing your samples per second. So, 5 seconds would be 5*fs. It is thus possible to extract chunks with specific time stamps: sig_chunk = signal(start_in_s*fs+1: end_in_s*fs);
It's also possible to take timestamp data from a table and use the for loop to cut audio.
The safest option would be to load them individually and use a mask for each then save. For example:
[x1,fs] = audioread('fileName1.wav');
tinit = 1*60 + 34; % In seconds
tend = 2*60 + 4;
ll = floor(tinit*fs) : floor(tend*fs);
x1 = x1(ll); % apply the mask to the segment of audio you want
audiowrite('fileName1edit.wav',x1,fs,'BitsPerSample',24)
However, if you have a big number of files to deal with, a less reliable but more comfortable solution could be to dump all the wav files in a structure
Files = dir('*.wav');
and load them calling
[x,fs] = audioread(Files(idx).name);
within a for loop of length(Files)
inside which you could prompt a box dialog asking for the minute and second to begin and minute and second to end. For example:
for idx = 1 : length(Files)
[x,fs] = audioread(Files(idx).name);
prompt = {'Min start:','Second start:','Min end:','Second end:'};
T = inputdlg(prompt,'Enter the times',[1,20]);
Ninit = round(fs*(str2num(T{1})*60 + str2num(T{2})));
Nend = round(fs*(str2num(T{3})*60 + str2num(T{4})));
ll = Ninit:Nend;
x = x(ll); % or x = x(Ninit:Nend);
audiowrite(Files(idx).name,...);
end
See the documentation for inputdlg()
for further examples. If you are not editing the string for the output audio file in audiowrite()
with an _edit.mat
or similar, make a backup of your files in a folder, for safety.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.