Added an advanced mode in the TTS settings, faster and less extreme tones
[sgc2.git] / InitialiseSGC2.praat
blobb3b6e6151ac0acb7484de23ec80fa6b949d9b1b6
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 44100 0.01 50 100 100 yes no"
126 sgc2.advancedTTScommand$ = "Create SpeechSynthesizer... Mandarin f3 44100 0.01 50 70 125 yes no"
128 ##############################################################
130 config.shuffleLists = 1
131 defaultFontSize = 12
132 defaultFont$ = "Helvetica"
133 defaultLineWidth = 1
135 # Set up directories if they do not exist already
136 call set_up_directories
138 # Get saved preferences
139 call read_preferences ""
141 # Set up TTS system
142 speakCommandDir$ = "'preferencesAppDir$'/TTS"
143 speakCommandFile$ = ""
144 call set_up_TTS
146 # Set inital language
147 call set_language 'config.language$'
149 # Get the word-list
150 call load_word_list "'localWordlistDir$'" 0
152 # Set up evaluation table
153 initialiseSGC2.toneevaluation_table$ = ""
154 call initialize_toneevaluation_tables
156 # Draw inital window
157 call init_window
159 ###############################################################
161 # Obligatory procedures
163 ###############################################################
164 # Initialize Demo Window
165 procedure init_window
166     demo Erase all
167         if config.showBackground
168                 call draw_background Background
169         endif
170         demo Line width... 'defaultLineWidth'
171         demo 'defaultFont$'
172         call set_font_size 'defaultFontSize'
173         demo Black
174         call reset_viewport
175     # Define buttons in a table
176     call init_buttons
177         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
178         call set_window_title 'buttons$' '.windowTitle$'
179         # Display the word-list
180         call write_word_list
181         # Logging light
182         call paint_logging_light
183 endproc
185 # Make sure all Preferences directories are available
186 procedure set_up_directories
187         .dirPath$ = "'preferencesAppDir$'"
188         createDirectory(.dirPath$)
189         .dirPath$ > '.dirPath$'/directory.txt
190         createDirectory("'.dirPath$'/wordlists")
191         .dirPath$ > '.dirPath$'/wordlists/directory.txt
192         createDirectory("'.dirPath$'/log")
193         .dirPath$ > '.dirPath$'/log/directory.txt
194         createDirectory("'.dirPath$'/TTS")
195         .dirPath$ > '.dirPath$'/TTS/directory.txt
196 endproc
199 # Retrieve and store setting between sessions
201 procedure read_preferences .preferencesFile$
202         if not fileReadable(.preferencesFile$)
203                 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
204         endif
205         if fileReadable(.preferencesFile$)
206                 Read from file... 'preferencesAppDir$'/sgc2rc.txt
207                 .preferenceTable$ = selected$("Table")
208                 .numPrefKeys = Get number of rows
209                 for .row to .numPrefKeys
210                         .variableName$ = Get value... '.row' Key
211                         if variableExists(.variableName$)
212                                 .variableValue = Get value... '.row' Value
213                                 if .variableValue <> undefined
214                                         '.variableName$' = '.variableValue'
215                                 endif
216                         elsif variableExists(.variableName$+"$")
217                                 .variableValue$ = Get value... '.row' Value
218                                 .variableName$ = .variableName$+"$"
219                                 '.variableName$' = "'.variableValue$'"
220                         endif
221                 endfor
222                 
223                 select Table '.preferenceTable$'
224                 Remove
225         endif
226 endproc
228 procedure write_preferences .preferencesFile$
229         Create Table with column names... Preferences 0 Key Value
230         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
231                 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
232         endif
233         .row = 0
234         
235         for .tableNum from 1 to 2
236                 if .tableNum = 1
237                         .table$ = "Config"
238                 elsif .tableNum = 2
239                         .table$ = "MainPage"
240                 endif
241                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
243                 select Table '.table$'
244                 .numTableRows = Get number of rows
245                 for .tablerow to .numTableRows
246                         select Table '.table$'
247                         .label$ = Get value... '.tablerow' Label
248                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
249                         .keyName$ = .variableName$
250                         .value$ = ""
251                         if variableExists(.variableName$)
252                                 .value = '.variableName$'
253                                 .value$ = "'.value'"
254                         elsif variableExists(.variableName$ + "$")
255                                 .variableName$ = .variableName$ + "$"
256                                 .value$ = '.variableName$'
257                         elsif index(.variableName$, "_") > 0
258                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
259                                 .keyName$ = .variableName$
260                                 select Table Preferences
261                                 .prefRow = Search column... Key '.keyName$'
262                                 if .prefRow <= 0
263                                         if variableExists(.variableName$)
264                                            .value = '.variableName$'
265                                            .value$ = "'.value'"
266                                         elsif variableExists(.variableName$ + "$")
267                                            .variableName$ = .variableName$ + "$"
268                                            .value$ = '.variableName$'
269                                         endif
270                                 endif
271                         endif
273                         if .value$ <> ""
274                                 select Table Preferences
275                                 Append row
276                                 .row += 1
277                                 Set string value... '.row' Key '.keyName$'
278                                 Set string value... '.row' Value '.value$'
279                         endif
280                 endfor
281         endfor
282         
283         # Some extra settings
284         select Table Preferences
285         Append row
286         .row += 1
287         Set string value... '.row' Key wordlistDir
288         Set string value... '.row' Value 'localWordlistDir$'
290         select Table Preferences
291         Append row
292         .row += 1
293         Set string value... '.row' Key wordlistName
294         Set string value... '.row' Value 'wordlistName$'
296         select Table Preferences
297         Write to table file... '.preferencesFile$'
298         Remove
300 endproc
302 ###############################################################
304 # TTS (speech synthesis). Place commands in user info
306 # Prerequisites
307 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
308 # speakCommandFile$ = ""
309 procedure set_up_TTS
310         if sgc2.synthesizer > 0
311                 select sgc2.synthesizer
312                 Remove
313                 sgc2.synthesizer = -1
314         endif
315         if config.strict
316                 sgc2.synthesizer = 'sgc2.advancedTTScommand$'
317         else
318                 sgc2.synthesizer = 'sgc2.normalTTScommand$'
319         endif
321         if macintosh
322                 .ttscommand$ = "speak"
323                 .osName$ = "OSX"
324                 .quote$ = ""
325         elsif unix
326                 .ttscommand$ = "espeak"
327                 .osName$ = "UNIX"
328                 .quote$ = ""
329         elsif windows
330                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
331                 .osName$ = "WIN"
332                 .quote$ = """"
333         endif
334         # Global TTS command has precedence
335         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
336                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
337                 if config.synthesis$ = "_DISABLED_"
338                         # Default is ON
339                         config.synthesis$ = "eSpeak"
340                 endif
341         endif
343         # Local TTS command, if there is no global one
344         # Command does not exist yet, create it if eSpeak is installed
345         # Make sure to quote the path!
346         if speakCommandFile$ = ""
347                 # Autodetect synthesizer command
348                 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
349                         .command_path$ = ""
350                         if macintosh
351                                 system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
352                                 .command_path$ < 'speakCommandDir$'/command_path.txt
353                         elsif unix
354                                 system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
355                                 .command_path$ < 'speakCommandDir$'/command_path.txt
356                         elsif windows
357                                 if fileReadable(.ttscommand$)
358                                         .command_path$ = .ttscommand$
359                                 endif
360                         endif
361                         # Remove any newlines
362                         .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
364                         # Command path found
365                         if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
366                                 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
367                                 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
368                                 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
369                                 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
370                         endif
371                 endif
372                 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
373                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
374                 endif
375                 deleteFile("'speakCommandDir$'/command_path.txt")
376         endif
377         
378         # Close off (other TTS maybe a later option)
379         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
380                 config.synthesis$ = "eSpeak"
381         endif
382 endproc
384 ###############################################################
386 # Evaluation tables
388 # Prerequisites
389 # initialiseSGC2.toneevaluation_table$ = ""
390 procedure initialize_toneevaluation_tables
391         # Remove old table
392         if initialiseSGC2.toneevaluation_table$ != ""
393                 select Table 'initialiseSGC2.toneevaluation_table$'
394                 Remove
395         endif
396         initialiseSGC2.toneevaluation_table$ = ""
397         
398         # Create new table
399         Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
400         initialiseSGC2.toneevaluation_table$ = "Performance"
401         call get_toneevaluation_row Total
402 endproc
404 procedure get_toneevaluation_row .tones$
405         select Table 'initialiseSGC2.toneevaluation_table$'
406     .row = Search column... Tones '.tones$'
407         if .row < 1
408                 Append row
409                 .row = Get number of rows
410                 Set string value... '.row' Tones '.tones$'
411                 Set numeric value... '.row' Correct 0
412                 Set numeric value... '.row' Wrong 0
413                 Set numeric value... '.row' Total 0
414                 Set numeric value... '.row' High 0
415                 Set numeric value... '.row' Low 0
416                 Set numeric value... '.row' Wide 0
417                 Set numeric value... '.row' Narrow 0
418                 Set numeric value... '.row' Unknown 0
419                 Set numeric value... '.row' Commented 0
420                 Sort rows... Tones
421         .row = Search column... Tones '.tones$'
422         endif
423 endproc
425 procedure increment_toneevaluation_in_row .row .column$
426         select Table 'initialiseSGC2.toneevaluation_table$'
427         .tmp = Get value... '.row' '.column$'
428         .tmp += 1
429         Set numeric value... '.row' '.column$' '.tmp'
430 endproc
432 procedure increment_toneevaluation_value .tones$ .column$
433         call get_toneevaluation_row '.tones$'
434         .row = get_toneevaluation_row.row
435         call increment_toneevaluation_in_row '.row' '.column$'
436         
437         call get_toneevaluation_row Total
438         .row = get_toneevaluation_row.row
439         call increment_toneevaluation_in_row '.row' '.column$'
440 endproc
442 procedure add_feedback_to_toneevaluation .table$
443     select Table '.table$'
444     .line1$ = Get value... 1 Text
445     .line2$ = Get value... 2 Text
446     .label$ = Get value... 3 Text
447         if .label$ = "6"
448                 .label$ = "Unknown"
449         endif
450         
451         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
452         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
453         
454         # Set evaluation
455         call increment_toneevaluation_value '.tones$' Total
456         if .tones$ = .recognized$
457                 call increment_toneevaluation_value '.tones$' Correct
458         else
459                 call increment_toneevaluation_value '.tones$' Wrong
460         endif
461         if index_regex(.label$, "(Correct|Wrong)") <= 0
462                 call increment_toneevaluation_value '.tones$' '.label$'
463                 call increment_toneevaluation_value '.tones$' Commented
464         endif
465 endproc
467 procedure CreateSummaryToneEvaluation
468         .font$ = "Helvetica"
469         # Summarize tone evaluation
470         Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
471         # Create texts
472         call get_toneevaluation_row Total
473         .row = get_toneevaluation_row.row
474         select Table 'initialiseSGC2.toneevaluation_table$'
475         .total = Get value... '.row' Total
476         .correct = Get value... '.row' Correct
477         if .total > 0
478                 .correctPerc = 100 * .correct / .total
479         else
480                 .correctPerc = 0
481         endif
482         .problematic = Get value... '.row' Commented
483         if .total > 0
484                 .problematicPerc = 100 * .problematic / .total
485         else
486                 .problematicPerc = 0
487         endif
489         select Table SummaryToneEvaluation
490         Append row
491         Set numeric value... 1 Total '.total'
492         Set numeric value... 1 Correct '.correctPerc'
493         Set numeric value... 1 Problematic '.problematicPerc'
494 endproc
496 ###############################################################
498 # Miscelaneous procedures
500 ###############################################################
502 # Create a file 'preferencesAppDir$'/log/logPerformance
503 # This file contains the path to the target directory where logging
504 # is to be stored.
505 # Note that this procedure is nowhere used in SGC2. It has to
506 # be explicitely called in a separate script.
508 procedure activate_sgc2_logging .targetDirectory$
509         createDirectory("'preferencesAppDir$'")
510         preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
511         createDirectory("'preferencesLogDir$'")
512         preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
513         
514         if startsWith(.targetDirectory$, "~")
515                 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
516         endif
517         
518         .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
519         newline$ >> 'preferencesLogDir$'/logPerformance.txt
520 endproc
522 # TTS synthesis
523 procedure synthesize_sound .pinyin$
524         .command$ = ""
525         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
526                 .command$ < 'speakCommandFile$'
527                 .command$ = replace$(.command$, "'newline$'", " ", 0)
528                 if macintosh
529                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
530                 elsif unix
531                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
532                 elsif windows
533                         system_nocheck call '.command$' "'.pinyin$'"'
534                 endif
535         elsif sgc2.synthesizer > 0
536                 select sgc2.synthesizer
537                 Play text... '.pinyin$'
538         endif
539 endproc