Started with pronunciation alignment, not working yet
[sgc2.git] / InitialiseSGC2.praat
blob735d9260365fbdb2e658c226fe1667b1447e5325
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$ = ""
35 # Define canvas
36 viewportMargin = 5
37 yWordlist = 11
38 yFeedback = yWordlist + 5
39 yPinyin = yFeedback + 10
40 yContour = yPinyin + 15
41 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
42 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
43 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
44 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
46 # Pop-Up window colors
47 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
48 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
50 # Initialize parameters
51 alertText$ = ""
52 currentWord = 1
53 pinyin$ = ""
54 character$ = ""
55 buttons$ = ""
56 config$ = ""
57 wordlistNum = 1
58 wordlistName$ = ""
59 wordlist$ = ""
60 config.deleteWordlist = -1
61 config.displayNumbers = 1
62 config.displayPinyin = 1
63 config.displayChar = 0
64 config.displayTrans = 1
65 config.useSoundExample = 1
66 config.synthesis$ = "_DISABLED_"
67 config.input$ = "Microphone"
68 config.showBackground = 1
69 sgc2.noiseThresshold = -30
70 config.strict = 0
72 # Platform dependent settings
73 if macintosh or windows
74         config.displayChar = 1
75         config.displayNumbers = 0
76 endif
77 if windows and endsWith(build_SHA$, " XP")
78         config.displayChar = 0
79         config.displayNumbers = 0
80 endif
81 if unix
82         config.displayChar = 0
83         config.displayNumbers = 1
84 endif
86 if unix
87         samplingFrequency = 44100
88 elsif macintosh
89         samplingFrequency = 44100
90 elsif windows
91         samplingFrequency = 44100
92 endif
93 recordingTime = 2
95 logging = 0
96 config.language$ = "EN"
97 config.register = 249
98 config.logPerformance = 0
99 preferencesLogDir$ = "'preferencesAppDir$'/log"
100 preferencesTableDir$ = "'preferencesAppDir$'/Data"
101 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
102 localWordlistDir$ = sgc2wordlists$
104 # Global word lists must be installed BY THE ADMINISTRATOR
105 # This means, create the directory 'globalwordlists$' and
106 # fill it with wordlist directories containing wordlist.Table
107 # and audio files. They can be copied from a local directory.
108 globalwordlists$ = ""
109 globaltablelists$ = ""
110 globalTTSdir$ = ""
111 if unix
112         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
113         globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"     
114         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
115 elsif macintosh
116         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
117         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
118         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
119 elsif windows
120         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
121         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
122         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
123 endif
124 sgc2.synthesizer = -1
125 sgc2.normalTTScommand$ = "Create SpeechSynthesizer... Mandarin f3 10000 0.01 50 100 100 yes no"
126 sgc2.advancedTTScommand$ = "Create SpeechSynthesizer... Mandarin f3 10000 0.01 50 70 125 yes no"
127 sgc2.alignedTextGrid = -1
129 ##############################################################
131 config.shuffleLists = 1
132 defaultFontSize = 12
133 defaultFont$ = "Helvetica"
134 defaultLineWidth = 1
136 # Set up directories if they do not exist already
137 call set_up_directories
139 # Get saved preferences
140 call read_preferences ""
142 # Set up TTS system
143 speakCommandDir$ = "'preferencesAppDir$'/TTS"
144 speakCommandFile$ = ""
145 call set_up_TTS
147 # Set inital language
148 call set_language 'config.language$'
150 # Get the word-list
151 call load_word_list "'localWordlistDir$'" 0
153 # Set up evaluation table
154 initialiseSGC2.toneevaluation_table$ = ""
155 call initialize_toneevaluation_tables
157 # Draw inital window
158 call init_window
160 ###############################################################
162 # Obligatory procedures
164 ###############################################################
165 # Initialize Demo Window
166 procedure init_window
167     demo Erase all
168         if config.showBackground
169                 call draw_background Background
170         endif
171         demo Line width... 'defaultLineWidth'
172         demo 'defaultFont$'
173         call set_font_size 'defaultFontSize'
174         demo Black
175         call reset_viewport
176     # Define buttons in a table
177     call init_buttons
178         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
179         call set_window_title 'buttons$' '.windowTitle$'
180         # Display the word-list
181         call write_word_list
182         # Logging light
183         call paint_logging_light
184 endproc
186 # Make sure all Preferences directories are available
187 procedure set_up_directories
188         .dirPath$ = "'preferencesAppDir$'"
189         createDirectory(.dirPath$)
190         .dirPath$ > '.dirPath$'/directory.txt
191         createDirectory("'.dirPath$'/wordlists")
192         .dirPath$ > '.dirPath$'/wordlists/directory.txt
193         createDirectory("'.dirPath$'/log")
194         .dirPath$ > '.dirPath$'/log/directory.txt
195         createDirectory("'.dirPath$'/TTS")
196         .dirPath$ > '.dirPath$'/TTS/directory.txt
197 endproc
200 # Retrieve and store setting between sessions
202 procedure read_preferences .preferencesFile$
203         if not fileReadable(.preferencesFile$)
204                 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
205         endif
206         if fileReadable(.preferencesFile$)
207                 Read from file... 'preferencesAppDir$'/sgc2rc.txt
208                 .preferenceTable$ = selected$("Table")
209                 .numPrefKeys = Get number of rows
210                 for .row to .numPrefKeys
211                         .variableName$ = Get value... '.row' Key
212                         if variableExists(.variableName$)
213                                 .variableValue = Get value... '.row' Value
214                                 if .variableValue <> undefined
215                                         '.variableName$' = '.variableValue'
216                                 endif
217                         elsif variableExists(.variableName$+"$")
218                                 .variableValue$ = Get value... '.row' Value
219                                 .variableName$ = .variableName$+"$"
220                                 '.variableName$' = "'.variableValue$'"
221                         endif
222                 endfor
223                 
224                 select Table '.preferenceTable$'
225                 Remove
226         endif
227 endproc
229 procedure write_preferences .preferencesFile$
230         Create Table with column names... Preferences 0 Key Value
231         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
232                 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
233         endif
234         .row = 0
235         
236         for .tableNum from 1 to 2
237                 if .tableNum = 1
238                         .table$ = "Config"
239                 elsif .tableNum = 2
240                         .table$ = "MainPage"
241                 endif
242                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
244                 select Table '.table$'
245                 .numTableRows = Get number of rows
246                 for .tablerow to .numTableRows
247                         select Table '.table$'
248                         .label$ = Get value... '.tablerow' Label
249                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
250                         .keyName$ = .variableName$
251                         .value$ = ""
252                         if variableExists(.variableName$)
253                                 .value = '.variableName$'
254                                 .value$ = "'.value'"
255                         elsif variableExists(.variableName$ + "$")
256                                 .variableName$ = .variableName$ + "$"
257                                 .value$ = '.variableName$'
258                         elsif index(.variableName$, "_") > 0
259                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
260                                 .keyName$ = .variableName$
261                                 select Table Preferences
262                                 .prefRow = Search column... Key '.keyName$'
263                                 if .prefRow <= 0
264                                         if variableExists(.variableName$)
265                                            .value = '.variableName$'
266                                            .value$ = "'.value'"
267                                         elsif variableExists(.variableName$ + "$")
268                                            .variableName$ = .variableName$ + "$"
269                                            .value$ = '.variableName$'
270                                         endif
271                                 endif
272                         endif
274                         if .value$ <> ""
275                                 select Table Preferences
276                                 Append row
277                                 .row += 1
278                                 Set string value... '.row' Key '.keyName$'
279                                 Set string value... '.row' Value '.value$'
280                         endif
281                 endfor
282         endfor
283         
284         # Some extra settings
285         select Table Preferences
286         Append row
287         .row += 1
288         Set string value... '.row' Key wordlistDir
289         Set string value... '.row' Value 'localWordlistDir$'
291         select Table Preferences
292         Append row
293         .row += 1
294         Set string value... '.row' Key wordlistName
295         Set string value... '.row' Value 'wordlistName$'
297         select Table Preferences
298         Write to table file... '.preferencesFile$'
299         Remove
301 endproc
303 ###############################################################
305 # TTS (speech synthesis). Place commands in user info
307 # Prerequisites
308 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
309 # speakCommandFile$ = ""
310 procedure set_up_TTS
311         if sgc2.synthesizer > 0
312                 select sgc2.synthesizer
313                 Remove
314                 sgc2.synthesizer = -1
315         endif
316         if config.strict
317                 sgc2.synthesizer = 'sgc2.advancedTTScommand$'
318         else
319                 sgc2.synthesizer = 'sgc2.normalTTScommand$'
320         endif
322         if macintosh
323                 .ttscommand$ = "speak"
324                 .osName$ = "OSX"
325                 .quote$ = ""
326         elsif unix
327                 .ttscommand$ = "espeak"
328                 .osName$ = "UNIX"
329                 .quote$ = ""
330         elsif windows
331                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
332                 .osName$ = "WIN"
333                 .quote$ = """"
334         endif
335         # Global TTS command has precedence
336         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
337                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
338                 if config.synthesis$ = "_DISABLED_"
339                         # Default is ON
340                         config.synthesis$ = "eSpeak"
341                 endif
342         endif
344         # Local TTS command, if there is no global one
345         # Command does not exist yet, create it if eSpeak is installed
346         # Make sure to quote the path!
347         if speakCommandFile$ = ""
348                 # Autodetect synthesizer command
349                 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
350                         .command_path$ = ""
351                         if macintosh
352                                 system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
353                                 .command_path$ < 'speakCommandDir$'/command_path.txt
354                         elsif unix
355                                 system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
356                                 .command_path$ < 'speakCommandDir$'/command_path.txt
357                         elsif windows
358                                 if fileReadable(.ttscommand$)
359                                         .command_path$ = .ttscommand$
360                                 endif
361                         endif
362                         # Remove any newlines
363                         .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
365                         # Command path found
366                         if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
367                                 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
368                                 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
369                                 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
370                                 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
371                         endif
372                 endif
373                 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
374                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
375                 endif
376                 deleteFile("'speakCommandDir$'/command_path.txt")
377         endif
378         
379         # Close off (other TTS maybe a later option)
380         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
381                 config.synthesis$ = "eSpeak"
382         endif
383 endproc
385 ###############################################################
387 # Evaluation tables
389 # Prerequisites
390 # initialiseSGC2.toneevaluation_table$ = ""
391 procedure initialize_toneevaluation_tables
392         # Remove old table
393         if initialiseSGC2.toneevaluation_table$ != ""
394                 select Table 'initialiseSGC2.toneevaluation_table$'
395                 Remove
396         endif
397         initialiseSGC2.toneevaluation_table$ = ""
398         
399         # Create new table
400         Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
401         initialiseSGC2.toneevaluation_table$ = "Performance"
402         call get_toneevaluation_row Total
403 endproc
405 procedure get_toneevaluation_row .tones$
406         select Table 'initialiseSGC2.toneevaluation_table$'
407     .row = Search column... Tones '.tones$'
408         if .row < 1
409                 Append row
410                 .row = Get number of rows
411                 Set string value... '.row' Tones '.tones$'
412                 Set numeric value... '.row' Correct 0
413                 Set numeric value... '.row' Wrong 0
414                 Set numeric value... '.row' Total 0
415                 Set numeric value... '.row' High 0
416                 Set numeric value... '.row' Low 0
417                 Set numeric value... '.row' Wide 0
418                 Set numeric value... '.row' Narrow 0
419                 Set numeric value... '.row' Unknown 0
420                 Set numeric value... '.row' Commented 0
421                 Sort rows... Tones
422         .row = Search column... Tones '.tones$'
423         endif
424 endproc
426 procedure increment_toneevaluation_in_row .row .column$
427         select Table 'initialiseSGC2.toneevaluation_table$'
428         .tmp = Get value... '.row' '.column$'
429         .tmp += 1
430         Set numeric value... '.row' '.column$' '.tmp'
431 endproc
433 procedure increment_toneevaluation_value .tones$ .column$
434         call get_toneevaluation_row '.tones$'
435         .row = get_toneevaluation_row.row
436         call increment_toneevaluation_in_row '.row' '.column$'
437         
438         call get_toneevaluation_row Total
439         .row = get_toneevaluation_row.row
440         call increment_toneevaluation_in_row '.row' '.column$'
441 endproc
443 procedure add_feedback_to_toneevaluation .table$
444     select Table '.table$'
445     .line1$ = Get value... 1 Text
446     .line2$ = Get value... 2 Text
447     .label$ = Get value... 3 Text
448         if .label$ = "6"
449                 .label$ = "Unknown"
450         endif
451         
452         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
453         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
454         
455         # Set evaluation
456         call increment_toneevaluation_value '.tones$' Total
457         if .tones$ = .recognized$
458                 call increment_toneevaluation_value '.tones$' Correct
459         else
460                 call increment_toneevaluation_value '.tones$' Wrong
461         endif
462         if index_regex(.label$, "(Correct|Wrong)") <= 0
463                 call increment_toneevaluation_value '.tones$' '.label$'
464                 call increment_toneevaluation_value '.tones$' Commented
465         endif
466 endproc
468 procedure CreateSummaryToneEvaluation
469         .font$ = "Helvetica"
470         # Summarize tone evaluation
471         Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
472         # Create texts
473         call get_toneevaluation_row Total
474         .row = get_toneevaluation_row.row
475         select Table 'initialiseSGC2.toneevaluation_table$'
476         .total = Get value... '.row' Total
477         .correct = Get value... '.row' Correct
478         if .total > 0
479                 .correctPerc = 100 * .correct / .total
480         else
481                 .correctPerc = 0
482         endif
483         .problematic = Get value... '.row' Commented
484         if .total > 0
485                 .problematicPerc = 100 * .problematic / .total
486         else
487                 .problematicPerc = 0
488         endif
490         select Table SummaryToneEvaluation
491         Append row
492         Set numeric value... 1 Total '.total'
493         Set numeric value... 1 Correct '.correctPerc'
494         Set numeric value... 1 Problematic '.problematicPerc'
495 endproc
497 ###############################################################
499 # Miscelaneous procedures
501 ###############################################################
503 # Create a file 'preferencesAppDir$'/log/logPerformance
504 # This file contains the path to the target directory where logging
505 # is to be stored.
506 # Note that this procedure is nowhere used in SGC2. It has to
507 # be explicitely called in a separate script.
509 procedure activate_sgc2_logging .targetDirectory$
510         createDirectory("'preferencesAppDir$'")
511         preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
512         createDirectory("'preferencesLogDir$'")
513         preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
514         
515         if startsWith(.targetDirectory$, "~")
516                 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
517         endif
518         
519         .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
520         newline$ >> 'preferencesLogDir$'/logPerformance.txt
521 endproc
523 # TTS synthesis
524 procedure synthesize_sound .pinyin$
525         .command$ = ""
526         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
527                 .command$ < 'speakCommandFile$'
528                 .command$ = replace$(.command$, "'newline$'", " ", 0)
529                 if macintosh
530                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
531                 elsif unix
532                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
533                 elsif windows
534                         system_nocheck call '.command$' "'.pinyin$'"'
535                 endif
536         elsif sgc2.synthesizer > 0
537                 select sgc2.synthesizer
538                 Play text... '.pinyin$'
539         endif
540 endproc
542 procedure align_recordedSound .pinyin$
543         .command$ = ""
544         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
545                 select Sound 'recordedSound$'
546                 .recordedTextGrid = To TextGrid... pinyin
547                 Set interval text... 1 1 '.pinyin$'
548                 select sgc2.synthesizer
549                 plus Sound 'recordedSound$'
550                 plus .recordedTextGrid
551                 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align)... 1 1 1 -35 0.1 0.1
552                 Rename AlignedTextGrid
553                 select .recordedTextGrid
554                 Remove
555         endif
556 endproc