# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # File name: # zero-x.praat # Title: # zero-x # Author: # Dennis Pasveer, Holger Mitterer # Date: # 2004-09-14, updated 14/08/2023 # Description: # This script automatically adjusts the interval boundaries and text markers (points) # in one or more TextGrid files to the nearest zero crossings in the corresponding # Sound files. It is capable of processing multi-layed TextGrid files. # # Before running the script, select one or more TextGrid objects and their # corresponding Sound objects. # # You will be asked to overwrite existing TextGrid files, or create new TextGrid files. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Ask the user if the original TextGrid file should be overwritten: form: Overwrite original TextGrid files or create new ones boolean: "overwrite", "1" endform if overwrite write_mode =1 else write_mode =2 endif # Determine the number of selected sounds and textgrids: s_count = numberOfSelected ("Sound") tg_count = numberOfSelected ("TextGrid") # Exit if no TextGrid is selected: if s_count = 0 or tg_count = 0 exitScript: "Select at least one Sound and TextGrid." endif # Exit if number of TextGrids and Sounds do not match: if s_count != tg_count exitScript: "The number of Sounds and TextGrids must match." endif # Collect the names and object ids of the selected Sounds and TextGrids: for i from 1 to tg_count snd_name$[i] = selected$("Sound", i) tg_name$[i] = selected$("TextGrid", i) snd_obj[i] = selected("Sound", i) tg_obj[i] = selected("TextGrid", i) endfor writeInfoLine: "Number of TextGrid files to be processed: " + fixed$(tg_count,0) if write_mode = 1 writeInfoLine: "Files will be overwritten." else writeInfoLine: "Files will not be overwritten." endif # Process each Sound and Textgrid: for i from 1 to tg_count # Select the current TextGrid: current_tg = tg_obj[i] current$ = selected$("TextGrid",1) selectObject: current_tg # Determine the number of Tiers in the TextGrid: tier_count = Get number of tiers appendInfoLine: "TextGrid " + current$ + " has "+ fixed$(tier_count,0) + " Tier(s)." # Determine the number of Intervals or Points in each Tier: for j from 1 to tier_count # Keep the Tier type (0 = Points Tier, 1 = Interval Tier): tier_type[j] = Is interval tier: j if tier_type[j] = 1 # The current Tier is an Interval Tier; keep the number of Intervals: current_interval_count = Get number of intervals: j appendInfoLine: "Tier no" + fixed$(j,0)+ " has "+ fixed$(current_interval_count,0) + " Interval(s)." else # The current Tier is a Points Tier; keep the number of Points: current_interval_count = Get number of points: j appendInfoLine: "Tier no" + fixed$(j,0)+ " has "+ fixed$(current_interval_count,0) + " Point(s)." endif time_count[j] = current_interval_count endfor # Add the current Sound to the object selection: plusObject: snd_obj[i] # Enter the editor: Edit editor: current_tg # Run through each Tier: for j from 1 to tier_count boundary_count = time_count[j] # Check if the number of Interval boundaries or Points is at least 1: if boundary_count = 1 if tier_type[j] = 0 # Exceptional case: only one point in tier: appendInfoLine: "In point tier " Move cursor to end of selection Move to nearest zero crossing endif #note: Interval tier with one interval has only start and end boudaries, nothing to move else # Move each Interval boundary or Point to nearest zero crossing: Move cursor to: 0 for boundary_index from 1 to boundary_count - 1 Select next interval Move to nearest zero crossing endfor endif # Move on to the next Tier: Select next tier endfor # Save TextGrid to file: if write_mode = 1 Save whole TextGrid as text file: current$ + ".TextGrid." appendInfoLine: "Saved TextGrid to file " + current$ + ".TextGrid." else Save whole TextGrid as text file: current$ + "_zeroed.TextGrid." appendInfoLine: "Saved TextGrid to file " + current$ + "_zeroed.TextGrid." endif # Close the editor: Close endeditor endfor # Return to the original selection: appendInfoLine: " found " + fixed$(tg_count,0) for i from 1 to tg_count if i = 1 selectObject: snd_obj[i] plusObject: tg_obj[i] else plusObject: snd_obj[i] plusObject: tg_obj[i] endif endfor