Search code examples
objectpraat

Praat script to remove silence, cannot select and remove objects


I am new to praat, so maybe this is an easy question. I try to create a script in praat that will go through some objects in my object list with a for loop and will remove silent intervals. I have created this script that I will put in a for loop.

selectObject: i
inten=To Intensity... 100 0 "no"
txt=To TextGrid (silences)... -35 0.1 0.05 'silent' 'sounding'
selectObject: i
plusObject: txt
Extract intervals where: 1, "no", "contains", "sounding"
myobj=Concatenate
Copy: string$(i)
selectObject: inten
plusObject: txt
plusObject: myobj
Remove

Although I get what I want my problem is in the line

Extract intervals where: 1, "no", "contains", "sounding"

In this line the parts that contain sound are extracted and selected. Then I use concatenate immediately to create the desired file and select it. However because this line produces more that one variable I cannot save it. Therefore later on I do not have a reference to remove it from my object directory. Although I could still run it like that my directory would be flooded and I do not think this is elegant. Any suggestions?


Solution

  • This might sound obvious, but you can save the ID numbers of the multiple extracted intervals by saving each of them into an indexed variable, and then re-selecting them when you need to remove them.

    Below you'll find an edited and commented version of your script. I changed some variable names to make it easier to follow.

    sound = selected()
    intensity = To Intensity: 100, 0, "no"
    textgrid = To TextGrid (silences): -35, 0.1, 0.05, silent, sounding
    
    # Select multiple objects at once
    selectObject: sound, textgrid
    
    Extract intervals where: 1, "no", "contains", "sounding"
    
    # Save the ID numbers of the currently selected intervals
    total_parts = numberOfSelected()
    for i to total_parts
      part[i] = selected(i)
    endfor
    
    # No need to make a copy just to rename
    sounding = Concatenate
    Rename: string$(sound)
    
    # Select all the objects you want to remove
    selectObject: intensity, textgrid
    # Including the extracted intervals
    for i to total_parts
      plusObject: part[i]
    endfor
    # And remove them
    Remove
    
    # If you knew beforehand what objects to select,
    # you could also use removeObject()
    
    # Select your concatenated sound
    selectObject: sounding
    

    As an aside, the selection plugin available through CPrAN was written to make it easier to manage Praat object selections for cases such as this. The two loops inserted above could have been replaced with calls to @saveSelection() and @restoreSelection() (full disclosure: I wrote that plugin).