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