I'm working on a Praat script to:
1- open a wav file
2- split the wav file based on silences
3- concatenate the intervals based on duration, so that the new wav segment files are <= 15 seconds each
4- write new wav segments to separate wav files
I've made some progress on getting this script to work, but I'm having 2 major problems:
1- after i concatenate the segments to create the first 15 sec clip my output stops, therefore I'm missing part of the wav file in the output
2- the clips are being concatenated in reverse order
Here is my script so far. Please help! I'm a novice in scripting in Praat, and I'm totally stumped..
Read from file... Desktop/englishTest.wav
name$ = selected$("Sound", 1)
outputDir$ = "Desktop/praat_output/"
To TextGrid (silences)... 100 0 -25 0.3 0.1 silent sounding
plus Sound 'name$'
Extract intervals where... 1 no "is equal to" sounding
n = numberOfSelected("Sound")
for i to n
soundObject'i'=selected("Sound", i)
endfor
topSound = soundObject1
select topSound
durTop = Get total duration
i = 2
for i to n
select soundObject'i'
dur = Get total duration
if durTop + dur <= 15
select topSound
plus soundObject'i'
topSound = Concatenate
select topSound
durTop = Get total duration
else
select topSound
Save as WAV file... 'outputDir$''name$'_'i'.wav
topSound = soundObject'i'
durTop = dur
endif
endfor
Let's go through your script bit by bit:
Here the first line will have no effect, because the
for
loop initialises its control variable to1
by default. You should write insteadfor i from 2 to n
.This is why your sounds are being concatenated in the wrong order. In Praat,
Concatenate
joins sounds in the order they appear in the Object list. Unfortunately, there are no easy ways to move objects about in the Object list. But you can solve this by copying objects, since newly created objects always appear at the bottom of the list.With these two changes, your script is almost there. The remaining problem is that, because you save your files when they exceed your maximum duration, the last part (which as the remainder, will likely be shorter) never gets saved. You need to remember to save that part separately after the loop ends.
I made some other small changes, like adding a form, changing your variables into more proper arrays and updating the syntax in general (
selectObject
instead ofselect
), but I tried to annotate them when not clear. Putting all of this together, you get something like thisThis could further be improved by, for instance, zero-padding the numbers in your filenames, but that's out of scope. :)
Update: Here's one possibility for improvement, with a slightly different algorithm, and breaking up longer chunks into fragments no larger than your specified maximum.