Adapted SaveAudio
[sgc2.git] / InitialiseSGC2.praat
blob0f7b82b496a7020d2815e92e49fcaf5ebce95d5d
2 # SpeakGoodChinese 2.0
3
4 # Intializing Praat script
6 #     SpeakGoodChinese: InitializeSGC2.praat defines and sets the global variables
7 #     and loads general SGC2 code 
8 #     
9 #     Copyright (C) 2007-2010  R.J.J.H. van Son and 2010 the Netherlands Cancer Institute
10 #     The SpeakGoodChinese team are:
11 #     Guangqin Chen, Zhonyan Chen, Stefan de Koning, Eveline van Hagen, 
12 #     Rob van Son, Dennis Vierkant, David Weenink
13
14 #     This program is free software; you can redistribute it and/or modify
15 #     it under the terms of the GNU General Public License as published by
16 #     the Free Software Foundation; either version 2 of the License, or
17 #     (at your option) any later version.
18
19 #     This program is distributed in the hope that it will be useful,
20 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
21 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 #     GNU General Public License for more details.
23
24 #     You should have received a copy of the GNU General Public License
25 #     along with this program; if not, write to the Free Software
26 #     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
27
29 # Information for logging
30 currentDate$ = date$()
31 dateYear$ = right$(currentDate$, length(currentDate$) - rindex(currentDate$, " "))
32 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
33 currentLogDirectory$ = ""
34 feedbackTablePrefix$ = "Feedback"
35 feedbackTableName$ = ""
37 # Define canvas
38 viewportMargin = 5
39 yWordlist = 11
40 yFeedback = yWordlist + 5
41 yPinyin = yFeedback + 10
42 yContour = yPinyin + 15
43 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
44 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
45 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
46 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
48 # Pop-Up window colors
49 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
50 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
52 # Initialize parameters
53 alertText$ = ""
54 te.currentWord = 1
55 te.numberOfWords = 0
56 te.recordedSound = 0
57 te.saveAudio = 0
58 pinyin$ = ""
59 character$ = ""
60 buttons$ = ""
61 config$ = ""
62 wordlistNum = 1
63 wordlistName$ = ""
64 wordlist$ = ""
65 mainPage.play = -1
66 config.deleteWordlist = -1
67 config.displayNumbers = 1
68 config.displayPinyin = 1
69 config.displayChar = 0
70 config.displayTrans = 1
71 config.useSoundExample = 1
72 config.synthesis$ = "_DISABLED_"
73 config.input$ = "Microphone"
74 config.showBackground = 1
75 sgc2.noiseThresshold = -30
76 config.strict = 0
77 config.savePerf$ = ""
78 config.saveAudio$ = ""
80 sgc_ToneProt.minimumPitch = 40
81 sgc_ToneProt.maximumPitch = 600
83 # Platform dependent settings
84 if macintosh or windows
85         config.displayChar = 1
86         config.displayNumbers = 0
87 endif
88 if windows and endsWith(build_SHA$, " XP")
89         config.displayChar = 0
90         config.displayNumbers = 0
91 endif
92 if unix
93         config.displayChar = 0
94         config.displayNumbers = 1
95 endif
97 if unix
98         samplingFrequency = 44100
99 elsif macintosh
100         samplingFrequency = 44100
101 elsif windows
102         samplingFrequency = 44100
103 endif
104 recordingTime = 2
106 logging = 0
107 config.language$ = "EN"
108 config.register = 249
109 config.logPerformance = 0
110 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
111 preferencesLogDir$ = "'preferencesAppDir$'/log"
112 preferencesTableDir$ = "'preferencesAppDir$'/Data"
113 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
114 localWordlistDir$ = sgc2wordlists$
116 # Global word lists must be installed BY THE ADMINISTRATOR
117 # This means, create the directory 'globalwordlists$' and
118 # fill it with wordlist directories containing wordlist.Table
119 # and audio files. They can be copied from a local directory.
120 globalwordlists$ = ""
121 globaltablelists$ = ""
122 globalTTSdir$ = ""
123 if unix
124         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
125         globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"     
126         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
127 elsif macintosh
128         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
129         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
130         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
131 elsif windows
132         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
133         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
134         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
135 endif
136 sgc2.synthesizer = -1
137 # Define a Praat TTS command that will set sgc2.synthesizer
138 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
139 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
140 sgc2.alignedTextGrid = -1
142 ##############################################################
144 config.shuffleLists = 1
145 defaultFontSize = 12
146 defaultFont$ = "Helvetica"
147 defaultLineWidth = 1
149 # Set up directories if they do not exist already
150 call set_up_directories
152 # Get saved preferences
153 call read_preferences ""
155 # Set up TTS system
156 speakCommandDir$ = "'preferencesAppDir$'/TTS"
157 speakCommandFile$ = ""
158 call set_up_TTS
160 # Set inital language
161 call set_language 'config.language$'
163 # Get the word-list
164 call load_word_list "'localWordlistDir$'" 0
166 # Set up evaluation table
167 initialiseSGC2.toneevaluation_table$ = ""
168 call initialize_toneevaluation_tables
170 # Draw inital window
171 call init_window
173 ###############################################################
175 # Obligatory procedures
177 ###############################################################
178 # Initialize Demo Window
179 procedure init_window
180     demo Erase all
181         demo Line width... 'defaultLineWidth'
182         demo 'defaultFont$'
183         call set_font_size 'defaultFontSize'
184         demo Black
185         
186         # If there is no recorded sound, try to read stored recording
187         if recordedSound$ = "" and config.saveAudio$ <> ""
188                 .pinyin$ = ""
189                 select Table 'wordlist$'
190                 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
191                         .pinyin$ = Get value... 'te.currentWord' Pinyin
192                         .outputName$ = "'config.saveAudio$'/'.pinyin$'.wav"
193                         if fileReadable(.outputName$)
194                                 te.recordedSound = Read from file: .outputName$
195                                 recordedSound$ = selected$("Sound")
196                                 call recognizeTone
197                         endif
198                 endif
199         endif
200         
201         # Update screen
202         call reset_viewport
203         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
204         call set_window_title 'buttons$' '.windowTitle$'
205         # Display the word-list
206         call write_word_list
207     # Wipe screen and draw background
208     call wipeArea 'wipeContourArea$'
209         if config.showBackground
210                 call draw_background Background
211         endif
212     # Define buttons in a table
213     call init_buttons
214     # Set Play button
215         call Set_Play_Button
216     
217         # Draw the contour
218         call draw_tone_contour
219         # Logging light
220         call paint_saveAudio_light
221         # Set play button
222         
223 endproc
225 # Make sure all Preferences directories are available
226 procedure set_up_directories
227         .dirPath$ = "'preferencesAppDir$'"
228         createDirectory(.dirPath$)
229         .dirPath$ > '.dirPath$'/directory.txt
230         createDirectory("'.dirPath$'/wordlists")
231         .dirPath$ > '.dirPath$'/wordlists/directory.txt
232         createDirectory("'.dirPath$'/pitchmodels")
233         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
234         createDirectory("'.dirPath$'/log")
235         .dirPath$ > '.dirPath$'/log/directory.txt
236         createDirectory("'.dirPath$'/TTS")
237         .dirPath$ > '.dirPath$'/TTS/directory.txt
238 endproc
241 # Retrieve and store setting between sessions
243 procedure read_preferences .preferencesFile$
244         if not fileReadable(.preferencesFile$)
245                 .preferencesFile$ = preferencesAppFile$
246         endif
247         if fileReadable(.preferencesFile$)
248                 Read from file... 'preferencesAppFile$'
249                 .preferenceTable$ = selected$("Table")
250                 .numPrefKeys = Get number of rows
251                 for .row to .numPrefKeys
252                         .variableName$ = Get value... '.row' Key
253                         if variableExists(.variableName$)
254                                 .variableValue = Get value... '.row' Value
255                                 if .variableValue <> undefined
256                                         '.variableName$' = '.variableValue'
257                                 endif
258                         elsif variableExists(.variableName$+"$")
259                                 .variableValue$ = Get value... '.row' Value
260                                 .variableName$ = .variableName$+"$"
261                                 '.variableName$' = "'.variableValue$'"
262                         endif
263                 endfor
264                 
265                 select Table '.preferenceTable$'
266                 Remove
267         endif
268 endproc
270 procedure write_preferences .preferencesFile$
271         Create Table with column names... Preferences 0 Key Value
272         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
273                 .preferencesFile$ = preferencesAppFile$
274         endif
275         .row = 0
276         
277         for .tableNum from 1 to 2
278                 if .tableNum = 1
279                         .table$ = "Config"
280                 elsif .tableNum = 2
281                         .table$ = "MainPage"
282                 endif
283                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
285                 select Table '.table$'
286                 .numTableRows = Get number of rows
287                 for .tablerow to .numTableRows
288                         select Table '.table$'
289                         .label$ = Get value... '.tablerow' Label
290                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
291                         .keyName$ = .variableName$
292                         .value$ = ""
293                         if variableExists(.variableName$)
294                                 .value = '.variableName$'
295                                 .value$ = "'.value'"
296                         elsif variableExists(.variableName$ + "$")
297                                 .variableName$ = .variableName$ + "$"
298                                 .value$ = '.variableName$'
299                         elsif index(.variableName$, "_") > 0
300                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
301                                 .keyName$ = .variableName$
302                                 select Table Preferences
303                                 .prefRow = Search column... Key '.keyName$'
304                                 if .prefRow <= 0
305                                         if variableExists(.variableName$)
306                                            .value = '.variableName$'
307                                            .value$ = "'.value'"
308                                         elsif variableExists(.variableName$ + "$")
309                                            .variableName$ = .variableName$ + "$"
310                                            .value$ = '.variableName$'
311                                         endif
312                                 endif
313                         endif
315                         if .value$ <> ""
316                                 select Table Preferences
317                                 Append row
318                                 .row += 1
319                                 Set string value... '.row' Key '.keyName$'
320                                 Set string value... '.row' Value '.value$'
321                         endif
322                 endfor
323         endfor
324         
325         # Some extra settings
326         select Table Preferences
327         Append row
328         .row += 1
329         Set string value... '.row' Key wordlistDir
330         Set string value... '.row' Value 'localWordlistDir$'
332         select Table Preferences
333         Append row
334         .row += 1
335         Set string value... '.row' Key wordlistName
336         Set string value... '.row' Value 'wordlistName$'
338         select Table Preferences
339         Write to table file... '.preferencesFile$'
340         Remove
342 endproc
344 ###############################################################
346 # TTS (speech synthesis). Place commands in user info
348 # Prerequisites
349 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
350 # speakCommandFile$ = ""
352 # Make sure sgc2.synthesizer is set!
353 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
354         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
355         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
356 endproc
358 procedure set_up_TTS
359         if sgc2.synthesizer > 0
360                 select sgc2.synthesizer
361                 Remove
362                 sgc2.synthesizer = -1
363         endif
364         # Note, the TTScommand must set sgc2.synthesizer!
365         if config.strict
366                 'sgc2.advancedTTScommand$'
367         else
368                 'sgc2.normalTTScommand$'
369         endif
371         if macintosh
372                 .ttscommand$ = "speak"
373                 .osName$ = "OSX"
374                 .quote$ = ""
375         elsif unix
376                 .ttscommand$ = "espeak"
377                 .osName$ = "UNIX"
378                 .quote$ = ""
379         elsif windows
380                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
381                 .osName$ = "WIN"
382                 .quote$ = """"
383         endif
384         # Global TTS command has precedence
385         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
386                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
387                 if config.synthesis$ = "_DISABLED_"
388                         # Default is ON
389                         config.synthesis$ = "eSpeak"
390                 endif
391         endif
393         # Local TTS command, if there is no global one
394         # Command does not exist yet, create it if eSpeak is installed
395         # Make sure to quote the path!
396         if speakCommandFile$ = ""
397                 # Autodetect synthesizer command
398 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
399 #                       .command_path$ = ""
400 #                       if macintosh
401 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
402 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
403 #                       elsif unix
404 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
405 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
406 #                       elsif windows
407 #                               if fileReadable(.ttscommand$)
408 #                                       .command_path$ = .ttscommand$
409 #                               endif
410 #                       endif
411 #                       # Remove any newlines
412 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
414 #                       # Command path found
415 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
416 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
417 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
418 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
419 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
420 #                       endif
421 #               endif
422 #               if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
423                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
424                 endif
425                 deleteFile("'speakCommandDir$'/command_path.txt")
426         endif
427         
428         # Close off (other TTS maybe a later option)
429         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
430                 config.synthesis$ = "eSpeak"
431         endif
432 endproc
434 ###############################################################
436 # Evaluation tables
438 # Prerequisites
439 # initialiseSGC2.toneevaluation_table$ = ""
440 procedure initialize_toneevaluation_tables
441         # Remove old table
442         if initialiseSGC2.toneevaluation_table$ != ""
443                 select Table 'initialiseSGC2.toneevaluation_table$'
444                 Remove
445         endif
446         initialiseSGC2.toneevaluation_table$ = ""
447         
448         if config.savePerf$ <> "" and fileReadable(config.savePerf$)
449                 sgc2.performanceTable = Read from file: config.savePerf$
450                 Rename: "Performance"
451         else
452                 # Create new table
453                 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Time
454         endif
455         initialiseSGC2.toneevaluation_table$ = "Performance"
456         call get_toneevaluation_row Total
457 endproc
459 procedure get_toneevaluation_row .tones$
460         select Table 'initialiseSGC2.toneevaluation_table$'
461     .row = Search column... Pinyin '.tones$'
462         if .row < 1
463                 Append row
464                 .row = Get number of rows
465                 Set string value... '.row' Pinyin '.tones$'
466                 Set numeric value... '.row' Correct 0
467                 Set numeric value... '.row' Wrong 0
468                 Set numeric value... '.row' Total 0
469                 Set numeric value... '.row' High 0
470                 Set numeric value... '.row' Low 0
471                 Set numeric value... '.row' Wide 0
472                 Set numeric value... '.row' Narrow 0
473                 Set numeric value... '.row' Unknown 0
474                 Set numeric value... '.row' Commented 0
475                 Set numeric value... '.row' Time 0
476                 Sort rows... Pinyin
477         .row = Search column... Pinyin '.tones$'
478         endif
479 endproc
481 procedure increment_toneevaluation_in_row .row .column$
482         select Table 'initialiseSGC2.toneevaluation_table$'
483         .tmp = Get value... '.row' '.column$'
484         .tmp += 1
485         Set numeric value... '.row' '.column$' '.tmp'
486         
487         # Set time stamp
488         .dateTime$ = date$()
489         Set string value: .row, "Time", .dateTime$
490 endproc
492 procedure increment_toneevaluation_value .tones$ .column$
493         call get_toneevaluation_row '.tones$'
494         .row = get_toneevaluation_row.row
495         call increment_toneevaluation_in_row '.row' '.column$'
496         
497         call get_toneevaluation_row Total
498         .row = get_toneevaluation_row.row
499         call increment_toneevaluation_in_row '.row' '.column$'
500 endproc
502 procedure add_feedback_to_toneevaluation .table$
503     select Table '.table$'
504     .line1$ = Get value... 1 Text
505     .line2$ = Get value... 2 Text
506     .label$ = Get value... 3 Text
507         if .label$ = "6"
508                 .label$ = "Unknown"
509         endif
510         
511         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^a-z0-9]", "", 0)
512         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
513         
514         # Set evaluation
515         call increment_toneevaluation_value '.tones$' Total
516         if .tones$ = .recognized$
517                 call increment_toneevaluation_value '.tones$' Correct
518         else
519                 call increment_toneevaluation_value '.tones$' Wrong
520         endif
521         if index_regex(.label$, "(Correct|Wrong)") <= 0
522                 call increment_toneevaluation_value '.tones$' '.label$'
523                 call increment_toneevaluation_value '.tones$' Commented
524         endif
525 endproc
527 procedure CreateSummaryToneEvaluation
528         .font$ = defaultFont$
529         # Summarize tone evaluation
530         Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
531         # Create texts
532         call get_toneevaluation_row Total
533         .row = get_toneevaluation_row.row
534         select Table 'initialiseSGC2.toneevaluation_table$'
535         .total = Get value... '.row' Total
536         .correct = Get value... '.row' Correct
537         if .total > 0
538                 .correctPerc = 100 * .correct / .total
539         else
540                 .correctPerc = 0
541         endif
542         .problematic = Get value... '.row' Commented
543         if .total > 0
544                 .problematicPerc = 100 * .problematic / .total
545         else
546                 .problematicPerc = 0
547         endif
549         select Table SummaryToneEvaluation
550         Append row
551         Set numeric value... 1 Total '.total'
552         Set numeric value... 1 Correct '.correctPerc'
553         Set numeric value... 1 Problematic '.problematicPerc'
554 endproc
556 procedure setGrade .grade$
557         select Table 'wordlist$'
558         if recordedSound$ <> "" and te.currentWord > 0 and te.currentWord <= te.numberOfWords
559                 .pinyin$ = Get value: te.currentWord, "Pinyin"
560                 
561                 call get_toneevaluation_row '.pinyin$'
562                 .row = get_toneevaluation_row.row
563                 select Table 'initialiseSGC2.toneevaluation_table$'
564                 .col = Get column index... Grade
565                 if .col <= 0
566                         Append column... Grade
567                 endif
568                         
569                 Set numeric value: .row, "Grade", '.grade$'
570                 .numRows = Get number of rows
571                 .sumGrade = 0
572                 for .r from 2 to .numRows
573                         .tmp = Get value: .r, "Grade"
574                         .sumGrade += .tmp
575                 endfor
576                 .meanGrade = .sumGrade / (.numRows - 1)
577                 Set numeric value: 1, "Grade", .meanGrade
578                 
579         endif
580 endproc
582 ###############################################################
584 # Miscelaneous procedures
586 ###############################################################
588 # Create a file 'preferencesAppDir$'/log/logPerformance
589 # This file contains the path to the target directory where logging
590 # is to be stored.
591 # Note that this procedure is nowhere used in SGC2. It has to
592 # be explicitely called in a separate script.
594 procedure activate_sgc2_logging .targetDirectory$
595         createDirectory("'preferencesAppDir$'")
596         preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
597         createDirectory("'preferencesLogDir$'")
598         preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
599         
600         if startsWith(.targetDirectory$, "~")
601                 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
602         endif
603         
604         .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
605         newline$ >> 'preferencesLogDir$'/logPerformance.txt
606 endproc
608 # TTS synthesis
609 procedure synthesize_sound .pinyin$
610         .command$ = ""
611         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
612                 .command$ < 'speakCommandFile$'
613                 .command$ = replace$(.command$, "'newline$'", " ", 0)
614                 if macintosh
615                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
616                 elsif unix
617                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
618                 elsif windows
619                         system_nocheck call '.command$' "'.pinyin$'"'
620                 endif
621         elsif sgc2.synthesizer > 0
622                 select sgc2.synthesizer
623                 Play text... '.pinyin$'
624         endif
625 endproc
627 procedure align_recordedSound .pinyin$
628         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
629                 select Sound 'recordedSound$'
630                 .recordedTextGrid = To TextGrid... pinyin
631                 Set interval text... 1 1 '.pinyin$'
632                 select sgc2.synthesizer
633                 plus Sound 'recordedSound$'
634                 plus .recordedTextGrid
635                 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
636                 Rename... AlignedTextGrid
637                 select .recordedTextGrid
638                 Remove
639         endif
640 endproc