0

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.

1 Answer 1

1

The problem is with this line:

Replace interval text... 'tier_silences' 0 0 'labtier_silences2$' 'dur_silence$' Literals

Specifically, the 0 0 bit tells Praat to replace all labels on that tier (or: from interval 0 to interval 0 - this is just how Praat specifies "all labels" in this function call). You want to select, by number, the specific interval whose label you got in the for i to numberofIntervalsSilent for loop.

Since i loops through the interval numbers, you just need to do two things:

  1. Change that line to Replace interval text... 'tier_silences' i i 'labtier_silences2$' 'dur_silence$' Literals
  2. Move that line into the scope of if, so that i is accessible. For example, just below the line where you assign dur_silence$.

I tried this for one file and it works (with the caveat that I also had to switch the order of endfor and endif just above #Replace the labels - I don't know why this isn't buggy for you, but it was for me).

1
  • This worked wonders. Thank you very much for taking the time. I had to change the order of endfor and endif as well.
    – Ro Menz
    Jun 18, 2021 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.