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