Adapted patch to new praat version
[sgc2.git] / InitialiseSGC2.praat
blob10897d3a428ee98d2198a94f040fff9b47d980a6
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 localWordlistDir$ = "shellDirectory$/wordlists"
62 config.displayNumbers = 1
63 config.displayPinyin = 1
64 config.displayChar = 1
65 config.displayTrans = 1
66 config.useSoundExample = 1
67 config.synthesis$ = "_DISABLED_"
68 config.input$ = "Microphone"
69 config.showBackground = 1
70 sgc2.noiseThresshold = -30
71 config.strict = 0
73 if unix
74         samplingFrequency = 44100
75 elsif macintosh
76         samplingFrequency = 44100
77 elsif windows
78         samplingFrequency = 44100
79 endif
80 recordingTime = 2
82 config.language$ = "EN"
83 config.register = 249
84 config.logPerformance = 0
85 preferencesLogDir$ = "'preferencesAppDir$'/log"
86 preferencesTableDir$ = "'preferencesAppDir$'/Data"
87 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
89 # Global word lists must be installed BY THE ADMINISTRATOR
90 # This means, create the directory 'globalwordlists$' and
91 # fill it with wordlist directories containing wordlist.Table
92 # and audio files. They can be copied from a local directory.
93 globalwordlists$ = ""
94 globaltablelists$ = ""
95 globalTTSdir$ = ""
96 if unix
97         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
98         globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"     
99         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
100 elsif macintosh
101         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
102         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
103         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
104 elsif windows
105         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
106         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
107         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
108 endif
110 ##############################################################
112 config.shuffleLists = 1
113 defaultFontSize = 12
114 defaultFont$ = "Helvetica"
115 defaultLineWidth = 1
117 # Set up directories if they do not exist already
118 call set_up_directories
120 # Get saved preferences
121 call read_preferences ""
123 # Set up TTS system
124 speakCommandDir$ = "'preferencesAppDir$'/TTS"
125 speakCommandFile$ = ""
126 call set_up_TTS
128 # Set inital language
129 call set_language 'config.language$'
131 # Get the word-list
132 call load_word_list "'localWordlistDir$'" 0
134 # Set up evaluation table
135 initialiseSGC2.toneevaluation_table$ = ""
136 call initialize_toneevaluation_tables
138 # Draw inital window
139 call init_window
141 ###############################################################
143 # Obligatory procedures
145 ###############################################################
146 if not variableExists("build_SHA$")
147         build_SHA$ = "-"
148 endif
150 # Initialize Demo Window
151 procedure init_window
152     demo Erase all
153         if config.showBackground
154                 call draw_background Background
155         endif
156         demo Line width... 'defaultLineWidth'
157         demo 'defaultFont$'
158         call set_font_size 'defaultFontSize'
159         demo Black
160         call reset_viewport
161     # Define buttons in a table
162     call init_buttons
163         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
164         call set_window_title 'buttons$' '.windowTitle$'
165         # Display the word-list
166         call write_word_list
167         # Logging light
168         call paint_logging_light
169 endproc
171 # Make sure all Preferences directories are available
172 procedure set_up_directories
173         .dirPath$ = "'preferencesAppDir$'"
174         createDirectory(.dirPath$)
175         createDirectory("'.dirPath$'/wordlists")
176         createDirectory("'.dirPath$'/log")
177         createDirectory("'.dirPath$'/TTS")
178 endproc
181 # Retrieve and store setting between sessions
183 procedure read_preferences .preferencesFile$
184         if not fileReadable(.preferencesFile$)
185                 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
186         endif
187         if fileReadable(.preferencesFile$)
188                 Read from file... 'preferencesAppDir$'/sgc2rc.txt
189                 .preferenceTable$ = selected$("Table")
190                 .numPrefKeys = Get number of rows
191                 for .row to .numPrefKeys
192                         .variableName$ = Get value... '.row' Key
193                         if variableExists(.variableName$)
194                                 .variableValue = Get value... '.row' Value
195                                 if .variableValue <> undefined
196                                         '.variableName$' = '.variableValue'
197                                 endif
198                         elsif variableExists(.variableName$+"$")
199                                 .variableValue$ = Get value... '.row' Value
200                                 .variableName$ = .variableName$+"$"
201                                 '.variableName$' = "'.variableValue$'"
202                         endif
203                 endfor
204                 
205                 select Table '.preferenceTable$'
206                 Remove
207         endif
208 endproc
210 procedure write_preferences .preferencesFile$
211         Create Table with column names... Preferences 0 Key Value
212         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
213                 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
214         endif
215         .row = 0
216         
217         for .tableNum from 1 to 2
218                 if .tableNum = 1
219                         .table$ = "Config"
220                 elsif .tableNum = 2
221                         .table$ = "MainPage"
222                 endif
223                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
225                 select Table '.table$'
226                 .numTableRows = Get number of rows
227                 for .tablerow to .numTableRows
228                         select Table '.table$'
229                         .label$ = Get value... '.tablerow' Label
230                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
231                         .keyName$ = .variableName$
232                         .value$ = ""
233                         if variableExists(.variableName$)
234                                 .value = '.variableName$'
235                                 .value$ = "'.value'"
236                         elsif variableExists(.variableName$ + "$")
237                                 .variableName$ = .variableName$ + "$"
238                                 .value$ = '.variableName$'
239                         elsif index(.variableName$, "_") > 0
240                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
241                                 .keyName$ = .variableName$
242                                 select Table Preferences
243                                 .prefRow = Search column... Key '.keyName$'
244                                 if .prefRow <= 0
245                                         if variableExists(.variableName$)
246                                            .value = '.variableName$'
247                                            .value$ = "'.value'"
248                                         elsif variableExists(.variableName$ + "$")
249                                            .variableName$ = .variableName$ + "$"
250                                            .value$ = '.variableName$'
251                                         endif
252                                 endif
253                         endif
255                         if .value$ <> ""
256                                 select Table Preferences
257                                 Append row
258                                 .row += 1
259                                 Set string value... '.row' Key '.keyName$'
260                                 Set string value... '.row' Value '.value$'
261                         endif
262                 endfor
263         endfor
264         
265         # Some extra settings
266         select Table Preferences
267         Append row
268         .row += 1
269         Set string value... '.row' Key wordlistDir
270         Set string value... '.row' Value 'localWordlistDir$'
272         select Table Preferences
273         Append row
274         .row += 1
275         Set string value... '.row' Key wordlistName
276         Set string value... '.row' Value 'wordlistName$'
278         select Table Preferences
279         Write to table file... '.preferencesFile$'
280         Remove
282 endproc
284 ###############################################################
286 # TTS (speech synthesis). Place commands in user info
288 # Prerequisites
289 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
290 # speakCommandFile$ = ""
291 procedure set_up_TTS
292         if macintosh
293                 .ttscommand$ = "speak"
294                 .osName$ = "OSX"
295                 .quote$ = ""
296         elsif unix
297                 .ttscommand$ = "espeak"
298                 .osName$ = "UNIX"
299                 .quote$ = ""
300         elsif windows
301                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
302                 .osName$ = "WIN"
303                 .quote$ = """"
304         endif
305         # Global TTS command has precedence
306         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
307                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
308                 if config.synthesis$ = "_DISABLED_"
309                         # Default is ON
310                         config.synthesis$ = "eSpeak"
311                 endif
312         endif
313         
314         # Local TTS command, if there is no global one
315         # Command does not exist yet, create it if eSpeak is installed
316         # Make sure to quote the path!
317         if speakCommandFile$ = ""
318                 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
319                         if macintosh
320                                 system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
321                                 .command_path$ < 'speakCommandDir$'/command_path.txt
322                         elsif unix
323                                 system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
324                                 .command_path$ < 'speakCommandDir$'/command_path.txt
325                         elsif windows
326                                 if fileReadable(.ttscommand$)
327                                         .command_path$ = .ttscommand$
328                                 endif
329                         endif
330                         # Remove any newlines
331                         .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
333                         # Command path found
334                         if .command_path$ <> "" and fileReadable(.command_path$)
335                                 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
336                                 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
337                                 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
338                                 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
339                         endif
340                 endif
341                 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
342                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
343                 endif
344                 deleteFile("'speakCommandDir$'/command_path.txt")
345         endif
346         
347         # Close off (other TTS maybe a later option)
348         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
349                 config.synthesis$ = "eSpeak"
350         endif
351 endproc
353 ###############################################################
355 # Evaluation tables
357 # Prerequisites
358 # initialiseSGC2.toneevaluation_table$ = ""
359 procedure initialize_toneevaluation_tables
360         # Remove old table
361         if initialiseSGC2.toneevaluation_table$ != ""
362                 select Table 'initialiseSGC2.toneevaluation_table$'
363                 Remove
364         endif
365         initialiseSGC2.toneevaluation_table$ = ""
366         
367         # Create new table
368         Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
369         initialiseSGC2.toneevaluation_table$ = "Performance"
370         call get_toneevaluation_row Total
371 endproc
373 procedure get_toneevaluation_row .tones$
374         select Table 'initialiseSGC2.toneevaluation_table$'
375     .row = Search column... Tones '.tones$'
376         if .row < 1
377                 Append row
378                 .row = Get number of rows
379                 Set string value... '.row' Tones '.tones$'
380                 Set numeric value... '.row' Correct 0
381                 Set numeric value... '.row' Wrong 0
382                 Set numeric value... '.row' Total 0
383                 Set numeric value... '.row' High 0
384                 Set numeric value... '.row' Low 0
385                 Set numeric value... '.row' Wide 0
386                 Set numeric value... '.row' Narrow 0
387                 Set numeric value... '.row' Unknown 0
388                 Set numeric value... '.row' Commented 0
389                 Sort rows... Tones
390         .row = Search column... Tones '.tones$'
391         endif
392 endproc
394 procedure increment_toneevaluation_in_row .row .column$
395         select Table 'initialiseSGC2.toneevaluation_table$'
396         .tmp = Get value... '.row' '.column$'
397         .tmp += 1
398         Set numeric value... '.row' '.column$' '.tmp'
399 endproc
401 procedure increment_toneevaluation_value .tones$ .column$
402         call get_toneevaluation_row '.tones$'
403         .row = get_toneevaluation_row.row
404         call increment_toneevaluation_in_row '.row' '.column$'
405         
406         call get_toneevaluation_row Total
407         .row = get_toneevaluation_row.row
408         call increment_toneevaluation_in_row '.row' '.column$'
409 endproc
411 procedure add_feedback_to_toneevaluation .table$
412     select Table '.table$'
413     .line1$ = Get value... 1 Text
414     .line2$ = Get value... 2 Text
415     .label$ = Get value... 3 Text
416         if .label$ = "6"
417                 .label$ = "Unknown"
418         endif
419         
420         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
421         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
422         
423         # Set evaluation
424         call increment_toneevaluation_value '.tones$' Total
425         if .tones$ = .recognized$
426                 call increment_toneevaluation_value '.tones$' Correct
427         else
428                 call increment_toneevaluation_value '.tones$' Wrong
429         endif
430         if index_regex(.label$, "(Correct|Wrong)") <= 0
431                 call increment_toneevaluation_value '.tones$' '.label$'
432                 call increment_toneevaluation_value '.tones$' Commented
433         endif
434 endproc
436 procedure CreateSummaryToneEvaluation
437         .font$ = "Helvetica"
438         # Summarize tone evaluation
439         Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
440         # Create texts
441         call get_toneevaluation_row Total
442         .row = get_toneevaluation_row.row
443         select Table 'initialiseSGC2.toneevaluation_table$'
444         .total = Get value... '.row' Total
445         .correct = Get value... '.row' Correct
446         if .total > 0
447                 .correctPerc = 100 * .correct / .total
448         else
449                 .correctPerc = 0
450         endif
451         .problematic = Get value... '.row' Commented
452         if .total > 0
453                 .problematicPerc = 100 * .problematic / .total
454         else
455                 .problematicPerc = 0
456         endif
458         select Table SummaryToneEvaluation
459         Append row
460         Set numeric value... 1 Total '.total'
461         Set numeric value... 1 Correct '.correctPerc'
462         Set numeric value... 1 Problematic '.problematicPerc'
463 endproc
465 ###############################################################
467 # Miscelaneous procedures
469 ###############################################################
471 # Create a file 'preferencesAppDir$'/log/logPerformance
472 # This file contains the path to the target directory where logging
473 # is to be stored.
474 # Note that this procedure is nowhere used in SGC2. It has to
475 # be explicitely called in a separate script.
477 procedure activate_sgc2_logging .targetDirectory$
478         createDirectory("'preferencesAppDir$'")
479         createDirectory("'preferencesLogDir$'")
480         
481         if startsWith(.targetDirectory$, "~")
482                 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
483         endif
484         
485         .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
486         newline$ >> 'preferencesLogDir$'/logPerformance.txt
487 endproc
489 # TTS synthesis
490 procedure synthesize_sound .pinyin$
491         .command$ = ""
492         if fileReadable(speakCommandFile$)
493                 .command$ < 'speakCommandFile$'
494                 .command$ = replace$(.command$, "'newline$'", " ", 0)
495                 if macintosh
496                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
497                 elsif unix
498                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
499                 elsif windows
500                         system_nocheck call '.command$' "'.pinyin$'"'
501                 endif
502         endif
503 endproc