split wav file based on txt file (two vectors - start time and duration)
2 views (last 30 days)
Show older comments
Hello,
I have a wav file (dialogue between mother and her son) , and i have already split the data for each speaker , meaning i have a txt file witch containing data (in seconds) about the start time and duration of speaking for each speaker ;
######
for example ,
mother.txt :
start time duration
1.011 4.457
5.974 1.166
8.26 8.616
17.493 5.009
24.688 7.303
35.011 2.388
38.71 6.061
45.993 3.898
51.73 12.096
#######
and i want to cut each part of the txt file from the wav file (creating wav file for each part) and merge them into one wav file( witch have all the words/sentences that the mother said in the dialogue )
which functions should i use ?
thanks a lot!
0 Comments
Accepted Answer
Mathieu NOE
on 4 Jan 2021
hello Fatima
happy new year !
this code will do the trick :
[song,FS]=audioread('demo.wav');
channel = 1;
song = song(:,channel);
% create list of start / stop time indexes
% mother.txt :
% start time
start_time = [1.011,
5.974,
8.26 ,
17.493,
24.688,
35.011,
38.71,
45.993,
51.73];
% duration
duration = [4.457,
1.166,
8.616,
5.009,
7.303,
2.388,
6.061,
3.898,
12.096];
stop_time = start_time+duration;
% main loop %
song_extract_complete = [];
for ci = 1:length(start_time)
% then determine start and stop indexes , knowing the start and stop times
song_start_ind = round(start_time(ci)*FS); % must be an integer value
song_stop_ind = round(stop_time(ci)*FS); % must be an integer value
% then extract
song_extract = song(song_start_ind:song_stop_ind);
song_extract_complete = [song_extract_complete; song_extract];
end
% export in wav format
audiowrite('demo_out.wav',song_extract_complete,FS);
2 Comments
More Answers (1)
dpb
on 4 Jan 2021
That only would take basic math operations to turn the start times and durations into indices into the the .wav file based on the sample rate of the file.
The index would simply be
iStart=tStart/Fs;
and the number of samples per duration
nSamples=tDuaration/Fs;
so each end time would be
iEnd=iStart+nSamples;
You may want to adjust the start time by +1 for time being zero-based while the MATLAB array indices begin at one, but one presumes the sampling frequency is something like 44 kHz so that adjustment is probably immaterial in practice.
2 Comments
dpb
on 4 Jan 2021
Just iterate over the computed indexing arrays and write those subsections of the input array to a new file; one for each subject.
Of course, having the indices means you could also just process the original file directly without the need to actually create a separate file, although there might be other reasons to do so.
Give it a try and see how far get...it's more educational to learn by doing than just have somebody hand over a solution that don't see how was done.
See Also
Categories
Find more on Audio and Video Data 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!