I am trying to write a script that extract the duration of intervals (in ms) which have a label (in this case "silent") from a single tier. There are several intervals labeled as "silent" on that tier.
I then want the label of the intervals (i.e. label "silent") to be replaced by the duration of the interval that I just extracted. So for instance, first interval labeled as "silent" has a duration of 1.8ms, I want that information printed on that interval so that it now reads "1.8" instead of "silent".
I have written a script that runs with no errors but it prints the duration of the first interval labeled as silent across all other intervals labeled as "silent", instead of printing the duration corresponding to each interval.
This is a loop problem, of course. There's a point where the script needs to say "okay, I already calculated the duration of THIS interval with the label "silent" and I've printed it. Now I'll read, calculate the duration and print it for the next interval with the same label".
Here's my script :
################################################### FORM #########################################################
#Indicate where the files are and fill the labels info
form Choose directory
sentence directory_Textgrid
positive tier_silences 1
sentence directory_Textgrid
endform
################################################### SCRIPT #########################################################
# Main loop - don't forget to close me at the end of the script
# Create the list of objects from the string
Create Strings as file list... list 'directory_Textgrid$'/*.TextGrid
#Loop per file
numberOfFiles = Get number of strings
for ifile to numberOfFiles
select Strings list
file$ = Get string... ifile
base$ = file$ - ".TextGrid"
fil$ = directory_Textgrid$ + file$
#Read TextGrid
Read from file... 'directory_Textgrid$'/'file$'
#select the current TextGrid
select TextGrid 'base$'
#Extract the number of intervals of each tier
numberofIntervalsSilent = Get number of intervals: tier_silences
#Get the label of the intervals
for i to numberofIntervalsSilent
labtier_silences$ = Get label of interval: tier_silences, i
#Only Choose labels "silent"
if labtier_silences$ = "silent"
labtier_silences2$ = labtier_silences$
#Loop per silence in order to get the duration of each silence
onsetSilent = Get starting point: tier_silences, i
offsetSilent = Get end point: tier_silences, i
#Calculate the raw duration of each silence (in ms)
dur_tier_silences = (offsetSilent - onsetSilent) * 1000
dur_silence$ = string$ (dur_tier_silences)
endfor
endif
#Replace the labels
Replace interval text... 'tier_silences' 0 0 'labtier_silences2$' 'dur_silence$' Literals
#Save the TextGrid
Save as text file... 'directory_Textgrid$'/'file$'
endfor
#Clear object list
select all
Remove
I would appreciate if anyone who knows how to write a script on praat could please tell me what I'm doing wrong.