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