## # This script opens all sound files in a directory and their associated TextGrids, # computes the mean pitch of all segments that have a name # and writes the results together with the durations of these segment to the # text file "pitch_mean_results.txt" at the same directory of the sound file. # # This script is not very efficient, since it computes the pitch for the whole files and looks only # then for labeled segments - it would be usually much more efficient to compute # the pitch only for these segments. # # Version 1.0, Henning Reetz, 02-jul-2007 # Version 1.1, Henning Reetz, 22-jun-2009; 'tier' added in form ## clearinfo ## 1) Inquire some parameters # ! Note that 'form' may only be used once in a script! form Pitch parameters: comment Leave the directory path empty if you want to use the current directory. word directory comment Which tier should be analyzed? integer tier 1 comment ______________________________________________________________________________________________________________ real step_rate 0.005 real low_F0 50.0 real high_F0 500.0 endform ## 2) delete any pre-existing result file and write a header line result_file$ = directory$+"pitch_mean_results.txt" filedelete 'result_file$' fileappend 'result_file$' File'tab$'Label'tab$'Beginning[s]'tab$'Duration[ms]'tab$'Mean F0 [Hz]'tab$'StDev F0 [Hz]'newline$' # 3) Get file names from a directory # We assume here that it is more likely that a '.TextGrid' file exists if there is a sound file # (whereas sound files often exist without a '.TextGrid' file) Create Strings as file list... file_list 'directory$'*.TextGrid number_of_files = Get number of strings for i_file to number_of_files select Strings file_list grid_file_name$ = Get string... i_file Read from file... 'directory$''grid_file_name$' base_name$ = selected$("TextGrid") print Handling 'base_name$' # try to open .wav files for this TextGrid ext1$ = ".wav" sound_file_name$ = directory$+base_name$+ext1$ if fileReadable (sound_file_name$) # next line not really necessary - just to prevent errors if we cut-n-paste this part into another script where things might be different select TextGrid 'base_name$' # check whether tier 1 is an interval tier. tier_one_is_interval = Is interval tier... tier if tier_one_is_interval = 1 # Compute the pitch of the selected sound. Read from file... 'sound_file_name$' To Pitch... step_rate low_F0 high_F0 # Use the TextGrid to find all labeled segments. select TextGrid 'base_name$' nr_segments = Get number of intervals... tier for i to nr_segments select TextGrid 'base_name$' interval_label$ = Get label of interval... tier i if interval_label$ <> "" # Get the mean pitch value begin_segment = Get starting point... tier i end_segment = Get end point... tier i duration = (end_segment - begin_segment) * 1000 fileappend 'result_file$' 'base_name$''tab$''interval_label$''tab$''begin_segment:4''tab$''duration:3' select Pitch 'base_name$' f0_mean = Get mean... begin_segment end_segment Hertz if f0_mean <> undefined fileappend 'result_file$' 'tab$''f0_mean:1' else fileappend 'result_file$' 'tab$'. endif f0_stdev = Get standard deviation... begin_segment end_segment Hertz if f0_stdev <> undefined fileappend 'result_file$' 'tab$''f0_stdev:1' else fileappend 'result_file$' 'tab$'. endif fileappend 'result_file$' 'newline$' endif # interval has a label endfor # going thru all intervals else # tier 1 is not an interval tier print 'newline$' Tier 1 of 'base_name$'.TextGrid is not an interval tier. File ignored. 'newline$' endif # test whether tier 1 is an interval tier select TextGrid 'base_name$' plus Sound 'base_name$' plus Pitch 'base_name$' Remove printline finished. else # no '.wav' file found printline failed! No sound file 'directory$''base_name$''ext1$' or *'ext2$' found. <*** Remove endif # test for readable sound file endfor # going thru all '.TextGrid' files # clear up select Strings file_list Remove printline Done. printline F0 data written to 'result_file$'.