form Figures
comment Which parameters are to be drawn ?
boolean spectrogram 1
boolean textgrid 0
boolean pitch 0
boolean formants 0
comment Path where the data is
text input_folder
comment Path where the figures will be saved
text output_folder
comment type of file
text regexp *.wav
comment file separator (\ Windows, / MacOS et Unix)
word filesep /
endform 
--------------------------------------------
clearinfo
Erase all
files = Create Strings as file list… filelist 'input_folder$''filesep$''regexp$'
ns = Get number of strings
for i to ns
select 'files'
name$ = Get string… 'i'
sound = Read from file… 'input_folder$''filesep$''name$'
new_sound = Extract one channel: 1
if textgrid = 1
    nametg$ = replace$ (name$, ".wav", ".TextGrid", 0)
    tg = Read from file... 'input_folder$''filesep$''nametg$'
endif

if spectrogram = 1
    select 'new_sound'
    spectro = To Spectrogram: 0.005, 10000, 0.002, 20, "Gaussian"
endif

if formants = 1

    #Female max formant = 5500Hz, Male max Formant = 5000Hz
    beginPause: "Sex of the speaker"
    comment: "Sex of the speaker"
    optionMenu: "sex", 1
        option: "female"
        option: "male"
    endPause: "Continue", 1

    if sex$ = "female"
        select 'new_sound'
        formant = To Formant (burg)... 0.01 5 5500 0.025 50
    elsif sex$ = "male"
        select 'new_sound'
        formant = To Formant (burg)... 0.01 5 5000 0.025 50
    endif
endif

select new_sound
View & Edit

beginPause: "Size of the figure"
    comment: "How big should the figure be ? (large size is advised for sentences)"
    optionMenu: "width", 1
        option: "small"
        option: "large"
endPause: "Continue", 1

if width$ = "small"
    width = 4   
elsif width$ = "large"
    width = 6
endif

time_scale = 100

#View & Edit
beginPause: "TEMPORAL SCALE"
    comment: "I want a temporal tick every XXX miliseconds"
    real: "time_scale", time_scale
endPause: "Continue", 1
time_scale = time_scale/1000

select 'new_sound'
#signal acoustique
Erase all
Viewport: 0, width, 0, 1.5
Green
Solid line
Line width: 1.0

Draw... 0 0 0 0 no Curve
Draw inner box
if textgrid = 1
    Marks bottom every: 1, time_scale, "yes", "yes", "yes"
    Text top: "no", "Time (s)"
elsif textgrid = 0
    Marks bottom every: 1, time_scale, "no", "no", "yes"
endif

if spectrogram = 1

    spectro_scale = 8000
    beginPause: "Spectrogram scale"
        comment: "What is the maximum frequency that the spectrogram should display ?"
        real: "spectro_scale", spectro_scale
    endPause: "Continue", 1

    #Commandes pour dessiner le spectrogramme
    select spectro
    Select outer viewport: 0, width, 1, 3
    Paint: 0, 0, 0, spectro_scale, 100, "yes", 50.0, 6, 0, "no"
    Draw inner box

    Text left: "yes", "Frequency (Hz)"

    Marks left every: 1, 1000, "yes", "yes", "no"
    if textgrid = 0
        Marks bottom every: 1, time_scale, "yes", "yes", "yes"
        Text bottom: "yes", "Time (s)"
    endif
endif

if textgrid = 1

    select 'tg'
    Select outer viewport: 0, width, 1, 4
    Black
    Solid line
    Line width: 1.0
    Draw: 0, 0, "yes", "no", "no"

endif

if pitch = 1

    f0_min = 75
    f0_max = 500
    beginPause: "Pitch scale"
        comment: "Indicate minimal and maximal pitch"
        real: "f0_min", f0_min
        real: "f0_max", f0_max
    endPause: "Continue", 1
    select 'new_sound'
    f0 = To Pitch: 0.0, f0_min, f0_max

    #Commandes pour dessiner la F0
    select f0
    Blue
    Line width: 2
    Select outer viewport: 0, width, 1, 3
    Draw: 0, 0, f0_min, f0_max, "no"
    Text right: "yes", "F0 (Hz)"    
    Line width: 1
    Marks right every: 1, 50, "yes", "yes", "no"

endif

if formants = 1

    select 'formant' 
    Red
    Line width: 1
    Select outer viewport: 0, width, 1, 3
    Speckle: 0, 0, spectro_scale, 30, "no"

endif

#saving figures in png and eps in a new directory
output_directory$ = output_folder$ + filesep$ + "Figures"    
createDirectory: output_directory$

namepng$ = replace$ (name$, ".wav", ".png", 0)
nameeps$ = replace$ (name$, ".wav", ".eps", 0)   

if textgrid = 1
    height = 4  
elsif textgrid = 0
    height = 3
endif

Select outer viewport: 0, width, 0, height
Save as 600-dpi PNG file: output_directory$ + filesep$ + namepng$
Save as EPS file: output_directory$ + filesep$ + nameeps$
select 'new_sound'

#cleaning praat object
select sound 
Remove

select new_sound
Remove

if textgrid = 1
    select 'tg'
    Remove
endif

if spectrogram = 1
    select 'spectro'
    Remove
endif

if pitch = 1
    select 'f0'
    Remove
endif

if formants = 1
    select 'formant'
    Remove
endif

endfor
select 'files'
Remove