## # # This script opens one TextGrid with segments in one Tier, computes the durations of every segment # that has a label and writes the result in milliseconds to a file whose name starts with the filename and # ends with 'durations.txt'. This file is deleted without warning prior to writing anything to it. # # Version 1.0, Henning Reetz, 25-jun-2007 # Version 1.1, Henning Reetz, 13-jun-2009; inserted 'tier' as a variable # Version 2.0, Henning Reetz, 09-dec-2014; new Praat script syntax; correct interval counter # Version 2.1, Henning Reetz, 16-dec.-2014 added 'removeObject:' # # Tested with Praat 5.4.0 # ## # clear information window clearinfo # set one file name base_name$ = "g071a000" # set the tier to be analysed (very helpful to declare it here once; 'tier' is used at many places in the script) tier = 1 # construct a name for the result file by adding '_duration.txt' to the base name result_file$ = base_name$ + "_durations.txt" # Read and select the .TextGrid file: (note that we do not need the sound file itself!) Read from file: "'base_name$'.TextGrid" # selecting is not really necessary here; just to be sure selectObject: "TextGrid 'base_name$'" # check whether tier 'tier' is an interval tier. Get this information first tier_one_is_interval = Is interval tier: 'tier' # check now whether tier 'tier' is an interval tier. if tier_one_is_interval <> 1 # in case it is not an interval tier, inform the user and stop program printline 'newline$' Tier 'tier' of 'file_name$'.TextGrid is not an interval tier. 'newline$' Job aborted. exit endif # otherwise (i.e. tier 'tier' is an interval tier) assume that there are some labeled intervals in this tier and start working # get the number of intervals of Tier 'tier' number_of_intervals = Get number of intervals: 'tier' # Delete any pre-existing variant of the output file and write a header to it # The header line is actually first stored in a variable 'header_row$' which is then written to the file filedelete result_file$ header_row$ = "File" + tab$ + "label" + tab$ + "Duration (ms.)" + newline$ header_row$ > 'result_file$' # Preset an counter for labelled intervals to inform user at the end of script nr_labelled_intervals = 0 # go thru all intervals for i to number_of_intervals # get the label of the 'i'th interval interval_label$ = Get label of interval: 'tier', 'i' # check whether label is not empty, i.e. there is a name given if interval_label$ <> "" # this segment has a label; so get time of beginning of segment begin_interval = Get starting point: 'tier', 'i' # get end of segment end_interval = Get end point: 'tier', 'i' # duration in ms is the distance between beginning and end times 1000 duration = (end_interval - begin_interval) * 1000 # now write the data, separated by tabulators to the out_file. # Note that there is a space after "out_file$" to separate the channel, where the data is written to # from the actual stuff which follows, which is actually the stuff that is send to this channel fileappend 'result_file$' 'base_name$''tab$''interval_label$''tab$''duration:2''newline$' # increase labelled intervals counter nr_labelled_intervals += 1 # in case there was not a labeled segment, we join here again endif # we increase the variable 'i' by one endfor # we come here when the variable 'i' is larger than 'number_of_intervals; # tell the user that we're done printline 'nr_labelled_intervals' durations of labels written to 'result_file$'. # cleanup - remove TextGrid object removeObject: "TextGrid 'base_name$'"