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