################################################################ ### ### draw-waveform-pitchtrack-spectrogram-overlaid.praat ### ### Draws waveform, alongside the pitch track overlaid on top of the spectrogram, and optionally a TextGrid of the same name. ### ### The user selects a sound object in the object list, and fills out a form. ### The user may indicate where they want the picture saved to, and in what format (PDF, EPS, or PNG). ### If the user indicates there is a TextGrid with the same name as the sound file in the object list, it will be part of the picture. ### The f0 values are settings for the pitch analysis, and the f0 window is automatically adjusted. ### ### Byron Ahn (byronahn@bu.edu) ### March 12, 2015 ### ### Based heavily on a script by Pauline Welby (welby@ling.ohio-state.edu, welby@icp.inpg.fr) from November 20, 2005 ### ################################################################ form Input Parameter Values comment Enter directory to save the picture (at least 1 box below must be ticked to save files): sentence outputDir ~/Documents/PraatOutput/ boolean save_as_pdf yes boolean save_as_eps no boolean save_as_png no comment If you tick this option, ensure a TextGrid object of the same name exists: boolean TextGrid_object_exists yes comment Settings for the analysis: real startTime 0.0 real endTime 0.0 (= all) positive f0min_(for_analysis) 50 positive f0max_(for_analysis) 500 positive spectrogram_settings_FreqMax 7000 comment Settings for the drawing: integer interval_of_f0_marks_on_the_y-axis 100 boolean mark_f0_intervals_on_the_y-axis yes positive width_of_entire_drawing 8 positive height_of_the_waveform 0.75 positive height_of_the_pitch_track_and_spectrogram 0.75 endform specFMin = 0 # these numbers depend on the margins, which are determined by the font size waveformBot = 'height_of_the_waveform' + 0.92 pitchAndSpecTop = 'waveformBot' - 0.92 pitchAndSpecBot = 'pitchAndSpecTop' + 'height_of_the_pitch_track_and_spectrogram' + 0.92 figBot = 'pitchAndSpecBot' sFullName$ = selected$ () sType$ = extractWord$ (sFullName$, "") sName$ = extractLine$ (sFullName$, " ") # Figure Spectrogram out select Sound 'sName$' sampFreq = Get sampling frequency halfSampFreq = sampFreq / 2 if 'spectrogram_settings_FreqMax' > 'halfSampFreq' spectrogram_settings_FreqMax = round('halfSampFreq'/100)*100 endif # Figure time out if 'endTime' = 0 select Sound 'sName$' durTime = Get total duration startTime = 0 endTime = durTime else durTime = 'endTime' - 'startTime' endif timeStepNum = 'durTime' / 1000 freqStepNum = 'spectrogram_settings_FreqMax' div 250 durTime = round ('durTime' * 1000) / 1000 endTime = round ('endTime' * 1000) / 1000 if 'TextGrid_object_exists' = 1 select TextGrid 'sName$' nTiers = Get number of tiers tierFrac = 'nTiers'/(4+'nTiers') # Calculate the size of the image, based on number of tiers and size of waveform and pitch track if 'nTiers'=2 tgridBot = ('pitchAndSpecBot'-0.43-(('nTiers'-3)*('tierFrac'/('nTiers'+0.8))))/(1-'tierFrac') elsif 'nTiers'=1 tgridBot = ('pitchAndSpecBot'-0.46-(('nTiers'-3)*('tierFrac'/('nTiers'+0.46))))/(1-'tierFrac') else tgridBot = ('pitchAndSpecBot'-0.4-(('nTiers'-3)*('tierFrac'/('nTiers'+2.5))))/(1-'tierFrac') endif ### (I don't know why these fractions work out the way they do ### based on how big TextGrids should be, the equation should be: ### tgridBot = ('pitchAndSpecBot'-0.5)/(1-'tierFrac') ### but this produces too much whitespace.) figBot = 'tgridBot' endif # Make Pitch object select Sound 'sName$' To Pitch (ac)... 0.005 'f0min' 15 no 0.03 0.45 0.01 0.35 0.14 'f0max' ## You can adjust these parameters if you like. In order, they are: ### Time step; pitch floor; candidates; very accurate; ### silence threshold; voicing threshold; octave cost; ### octave-jump cost; voiced/unvoiced cost; pitch ceiling # Make Spectrogram object select Sound 'sName$' To Spectrogram... 0.005 'spectrogram_settings_FreqMax' 'timeStepNum' 'freqStepNum' Gaussian # Specify font type size, color Times Font size... 12 Black # Define size and position of waveform (by specifying grid coordinates) Viewport... 0 'width_of_entire_drawing' 0 'waveformBot' Erase all # Draw waveform select Sound 'sName$' Draw... 'startTime' 'endTime' 0 0 no curve # Define size and position of pitch track / spectrogram Viewport... 0 'width_of_entire_drawing' 'pitchAndSpecTop' 'pitchAndSpecBot' # Draw Spectrogram select Spectrogram 'sName$' Paint... 'startTime' 'endTime' 0 0 100 yes 50 6 0 no # Label y axis Line width... 1 One mark right... 'spectrogram_settings_FreqMax' yes yes yes One mark right... 'specFMin' yes yes no Text right... yes Frequency (Hz) # Draw Pitch select Pitch 'sName$' actf0min = Get minimum... 'startTime' 'endTime' Hertz Parabolic actf0min = 'actf0min' - ('actf0min' mod 'interval_of_f0_marks_on_the_y-axis') actf0max = Get maximum... 'startTime' 'endTime' Hertz Parabolic actf0max = 'actf0max' + ('interval_of_f0_marks_on_the_y-axis' - ('actf0max' mod 'interval_of_f0_marks_on_the_y-axis')) Line width... 10 White Draw... 'startTime' 'endTime' 'actf0min' 'actf0max' no Line width... 3 Blue Draw... 'startTime' 'endTime' 'actf0min' 'actf0max' no # Label y axis Black Line width... 1 if 'mark_f0_intervals_on_the_y-axis' = 1 Marks left every... 1 'interval_of_f0_marks_on_the_y-axis' yes yes no One mark left... 'actf0max' no no yes else One mark left... 'actf0max' yes yes yes One mark left... 'actf0min' yes yes no endif Text left... yes f0 (Hz) if 'TextGrid_object_exists' = 1 # Define size and position of TextGrid Viewport... 0 'width_of_entire_drawing' 0 'tgridBot' # Draw TextGrid select TextGrid 'sName$' Draw... 'startTime' 'endTime' yes no no endif # Define size and position of the entire Picture Viewport... 0 'width_of_entire_drawing' 0 'figBot' # Draw inner box Black Draw inner box # Label x axis Line width... 1 One mark bottom... 'startTime' no yes yes 0 One mark bottom... 'endTime' no yes yes 'durTime' Text bottom... no Time (s) select Spectrogram 'sName$' Remove select Pitch 'sName$' Remove select 'sFullName$' # Write to a file (depending on which boxes were ticked) saveAsName$ = "'sName$'-w-ps" if 'TextGrid_object_exists' = 1 saveAsName$ = "'sName$'-w-ps-t" endif if 'save_as_pdf' = 1 Save as PDF file... 'outputDir$'/'saveAsName$'.pdf endif if 'save_as_eps' = 1 Write to EPS file... 'outputDir$'/'saveAsName$'.eps endif if 'save_as_png' = 1 Save as 300-dpi PNG file... 'outputDir$'/'saveAsName$'.png endif