Rebuild creaky voice detector for third tones, first working version
[sgc2.git] / InitialiseSGC2.praat
blob9cda8d41b6078d4d8b5823d05f40d3caa9bbca94
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 # Do not store these variables as preferences
30 sgc.nonPreferences$ = " config.savePerf config.openPerf config.clearSummary config.audioName "
32 # Information for logging activity
33 currentDate$ = date$()
34 dateYear$ = right$(currentDate$, length(currentDate$) - rindex(currentDate$, " "))
35 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
36 currentLogDirectory$ = ""
37 feedbackTablePrefix$ = "Feedback"
38 feedbackTableName$ = ""
39 evaluationTablePrefix$ = "Evaluation"
40 evaluationTableName$ = ""
41 performanceTableName$ = "Performance"
43 # Define canvas
44 viewportMargin = 5
45 yWordlist = 11
46 yFeedback = yWordlist + 5
47 yPinyin = yFeedback + 10
48 yContour = yPinyin + 15
49 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
50 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
51 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
52 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
54 # Pop-Up window colors
55 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
56 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
58 # Initialize parameters
59 alertText$ = ""
60 sgc.currentWord = 1
61 sgc.currentWordlist = -1
62 sgc.numberOfWords = 0
63 sgc.currentWordNum = 1
64 sgc.numberOfDisplayedWords = 0
65 sgc.recordedSound = 0
66 sgc.saveAudioOn = 0
67 sgc.writeAll = 0
68 sgc.customLanguage$ = ""
69 pinyin$ = ""
70 sgc.pinyin$ = ""
71 character$ = ""
72 buttons$ = ""
73 config$ = ""
74 wordlistNum = 1
75 wordlistName$ = ""
76 wordlist$ = ""
77 sgc.failedAttempts = 0
78 mainPage.play = -1
79 config.deleteWordlist = -1
80 config.displayNumbers = 1
81 config.displayPinyin = 1
82 config.displayChar = 0
83 config.displayTrans = 1
84 config.useSoundExample = 1
85 config.synthesis$ = "_DISABLED_"
86 config.input$ = "Microphone"
87 config.showBackground = 1
88 sgc2.noiseThresshold = -30
89 config.strict$ = "1"
90 sgc.highestLevel = 3
91 config.voice = 4
92 config.voice$ = "f4"
93 config.voicesTTS$ = "default f1 f2 f3 f4 f5 m1 m2 m3 m4 m5 m6 m7"
94 sgc.lastVoice = 12
95 sgc.nativeOSXTTScommand$ = "say -vTing-Ting  --rate=95"
96 sgc.nativeLinuxTTScommand$ = "spd-say -l zh -t female1 -r -60"
97 #sgc.nativeWindowsTTScommand$ = "mshta.exe vbscript:Execute(""CreateObject(""""SAPI.SpVoice"""").Speak """"<voice gender='female' xml:lang='zh-CN'><rate speed='-5'>$$$</rate></voice>"""":Close"")"
98 sgc.nativeWindowsTTScommand$ = "mshta.exe vbscript:Execute(""CreateObject(""""SAPI.SpVoice"""").Speak """"<voice gender='female' xml:lang='zh-CN'><rate speed='-5'><pron sym='@@@'/></rate></voice>"""":Close"")"
99 sgc.nativeTTScommand$ = ""
100 config.savePerf = 0
101 config.openPerf = 0
102 config.clearSummary = 0
103 config.audioName$ = ""
104 sgc.savePerf$ = ""
105 sgc.saveAudio$ = ""
106 mainPage.saveAudio$ = ""
107 sgc.allWordLists = -1
109 sgc_ToneProt.minimumPitch = 40
110 sgc_ToneProt.maximumPitch = 600
112 # Platform dependent settings
113 if macintosh or windows
114         config.displayChar = 1
115         config.displayNumbers = 0
116 endif
117 if windows and endsWith(build_SHA$, " XP")
118         config.displayChar = 0
119         config.displayNumbers = 0
120 endif
121 if unix
122         config.displayChar = 0
123         config.displayNumbers = 1
124 endif
126 if unix
127         samplingFrequency = 44100
128 elsif macintosh
129         samplingFrequency = 44100
130 elsif windows
131         samplingFrequency = 44100
132 endif
133 recordingTime = 2.5
135 # Be careful: Logging and Replaying can have side-effects
136 # sgc2.logging$ = "call saveLogOfActivity"
137 sgc2.logging$ = "# "
138 # When this is not "# ", the command 'replaySGC2Log$' will be run!
139 if not variableExists("replaySGC2Log$")
140         replaySGC2Log$ = "# "
141 endif
143 config.language$ = "EN"
144 config.register = 249
145 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
146 preferencesLogDir$ = "'preferencesAppDir$'/log"
147 preferencesTableDir$ = "'preferencesAppDir$'/GUI"
148 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
149 localWordlistDir$ = sgc2wordlists$
151 # Global word lists must be installed BY THE ADMINISTRATOR
152 # This means, create the directory 'globalwordlists$' and
153 # fill it with wordlist directories containing wordlist.Table
154 # and audio files. They can be copied from a local directory.
155 globalwordlists$ = ""
156 globaltablelists$ = ""
157 globalTTSdir$ = ""
158 if unix
159         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
160         globaltablelists$ = "/etc/'sgc2.demoAppName$'/GUI"      
161         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
162 elsif macintosh
163         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
164         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/GUI"
165         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
166 elsif windows
167         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
168         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/GUI"
169         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
170 endif
171 sgc2.synthesizer = -1
172 sgc.alignedTextGrid = -1
174 ##############################################################
176 config.shuffleLists = 1
177 config.adaptiveLists = 0
178 defaultFontSize = 12
179 defaultFont$ = "Helvetica"
180 defaultLineWidth = 1
182 # Set up directories if they do not exist already
183 call set_up_directories
185 # Get saved preferences
186 call read_preferences ""
188 # Set up TTS system
189 speakCommandDir$ = "'preferencesAppDir$'/TTS"
190 speakCommandFile$ = ""
191 # Define a Praat TTS command that will set sgc2.synthesizer
192 call set_TTS_parameters
193 call set_up_TTS
194 sgc.recordCommandFile$ = ""
195 sgc.playCommandFile$ = ""
196 if windows
197         sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
198 else
199         sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
200 endif
201 sgc.useAlternativeRecorder = 0
202 call set_up_recorder
203 sgc.useAlternativePlayer = 0
204 call set_up_player
206 # Set inital language
207 call set_language 'config.language$'
209 # Get the word-list
210 call load_word_list "'localWordlistDir$'" 0
212 # Set up evaluation table
213 initialiseSGC2.toneevaluation_table$ = ""
214 call initialize_toneevaluation_tables
216 # Draw inital window
217 call init_window
219 ###############################################################
221 # Obligatory procedures
223 ###############################################################
224 # Initialize Demo Window
225 procedure init_window
226     demo Erase all
227         demo Line width... 'defaultLineWidth'
228         demo 'defaultFont$'
229         call set_font_size 'defaultFontSize'
230         demo Black
231         
232         # If there is no recorded sound, try to read stored recording
233         if recordedSound$ = "" and sgc.saveAudio$ <> ""
234                 sgc.pinyin$ = ""
235                 select sgc.currentWordlist
236                 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
237                         call readPinyin 'sgc.currentWord'
238                         sgc.pinyin$ = readPinyin.pinyin$
239                         sgc.character$ = readPinyin.character$
240                         .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
241                         if fileReadable(.outputName$)
242                                 sgc.recordedSound = Read from file: .outputName$
243                                 recordedSound$ = selected$("Sound")
244                                 call recognizeTone
245                                 # See feedback on earlier recorded words
246                                 if fileReadable("'sgc.saveAudio$'/TestSpeakGoodChinese2.txt")
247                                         call write_feedback Feedback
248                                 else
249                                         call write_grade 'sgc.pinyin$'
250                                 endif
251                                 select Table Feedback
252                                 Remove
253                                 
254                                 # Set config.audioName
255                                 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
256                                 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
257                                 
258                         endif
259                 endif
260         endif
261         
262         # Set mainPage.saveAudio$ grade
263         call set_grade_display 'sgc.pinyin$'
264         call write_grade 'sgc.pinyin$'
265         
266         # Update screen
267         call reset_viewport
268         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
269         call set_window_title 'buttons$' '.windowTitle$'
270         # Display the word-list
271         call write_word_list
272     # Wipe screen and draw background
273     call wipeArea 'wipeContourArea$'
274         if config.showBackground
275                 call draw_background Background
276         endif
277     # Define buttons in a table
278     call init_buttons
279     # Set Play button
280         call Set_Play_Button
281     
282         # Draw the contour
283         call draw_tone_contour
284         # SaveAudio light
285         call paint_saveAudio_light
286         # Set play button
287         
288         sgc.failedAttempts = 0
289 endproc
291 # Make sure all Preferences directories are available
292 procedure set_up_directories
293         .dirPath$ = "'preferencesAppDir$'"
294         createDirectory(.dirPath$)
295         .dirPath$ > '.dirPath$'/directory.txt
296         createDirectory("'.dirPath$'/wordlists")
297         .dirPath$ > '.dirPath$'/wordlists/directory.txt
298         createDirectory("'.dirPath$'/pitchmodels")
299         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
300         createDirectory("'.dirPath$'/log")
301         .dirPath$ > '.dirPath$'/log/directory.txt
302         createDirectory("'.dirPath$'/TTS")
303         .dirPath$ > '.dirPath$'/TTS/directory.txt
304 endproc
307 # Retrieve and store setting between sessions
309 procedure read_preferences .preferencesFile$
310         if not fileReadable(.preferencesFile$)
311                 .preferencesFile$ = preferencesAppFile$
312         endif
313         if fileReadable(.preferencesFile$)
314                 Read from file... 'preferencesAppFile$'
315                 .preferenceTable$ = selected$("Table")
316                 .numPrefKeys = Get number of rows
317                 for .row to .numPrefKeys
318                         .variableName$ = Get value... '.row' Key
319                         if variableExists(.variableName$)
320                                 .variableValue = Get value... '.row' Value
321                                 if .variableValue <> undefined
322                                         '.variableName$' = '.variableValue'
323                                 endif
324                         elsif variableExists(.variableName$+"$")
325                                 .variableValue$ = Get value... '.row' Value
326                                 .variableName$ = .variableName$+"$"
327                                 '.variableName$' = "'.variableValue$'"
328                         endif
329                 endfor
330                 
331                 select Table '.preferenceTable$'
332                 Remove
333         endif
334 endproc
336 procedure write_preferences .preferencesFile$
337         Create Table with column names... Preferences 0 Key Value
338         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
339                 .preferencesFile$ = preferencesAppFile$
340         endif
341         .row = 0
342         
343         for .tableNum from 1 to 2
344                 if .tableNum = 1
345                         .table$ = "Config"
346                 elsif .tableNum = 2
347                         .table$ = "MainPage"
348                 endif
349                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
351                 select Table '.table$'
352                 .numTableRows = Get number of rows
353                 for .tablerow to .numTableRows
354                         select Table '.table$'
355                         .label$ = Get value... '.tablerow' Label
356                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
357                         .keyName$ = .variableName$
358                         .value$ = ""
359                         if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
360                                 .value$ = ""
361                         elsif variableExists(.variableName$)
362                                 .value = '.variableName$'
363                                 .value$ = "'.value'"
364                         elsif variableExists(.variableName$ + "$")
365                                 .variableName$ = .variableName$ + "$"
366                                 .value$ = '.variableName$'
367                         elsif index(.variableName$, "_") > 0
368                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
369                                 .keyName$ = .variableName$
370                                 select Table Preferences
371                                 .prefRow = Search column... Key '.keyName$'
372                                 if .prefRow <= 0
373                                         if variableExists(.variableName$)
374                                            .value = '.variableName$'
375                                            .value$ = "'.value'"
376                                         elsif variableExists(.variableName$ + "$")
377                                            .variableName$ = .variableName$ + "$"
378                                            .value$ = '.variableName$'
379                                         endif
380                                 endif
381                         endif
383                         if .value$ <> ""
384                                 select Table Preferences
385                                 Append row
386                                 .row += 1
387                                 Set string value... '.row' Key '.keyName$'
388                                 Set string value... '.row' Value '.value$'
389                         endif
390                 endfor
391         endfor
392         
393         # Some extra settings
394         select Table Preferences
395         Append row
396         .row += 1
397         Set string value... '.row' Key wordlistDir
398         Set string value... '.row' Value 'localWordlistDir$'
400         select Table Preferences
401         Append row
402         .row += 1
403         Set string value... '.row' Key wordlistName
404         Set string value... '.row' Value 'wordlistName$'
406         select Table Preferences
407         Write to table file... '.preferencesFile$'
408         Remove
410 endproc
412 ###############################################################
414 # TTS (speech synthesis). Place commands in user info
416 # Prerequisites
417 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
418 # speakCommandFile$ = ""
420 # Make sure sgc2.synthesizer is set!
421 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
422         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
423         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
424 endproc
426 procedure set_up_TTS
427         if sgc2.synthesizer > 0
428                 select sgc2.synthesizer
429                 Remove
430                 sgc2.synthesizer = -1
431         endif
432         # Note, the TTScommand must set sgc2.synthesizer!
433         if config.strict$ = "'sgc.highestLevel'"
434                 'sgc2.advancedTTScommand$'
435         else
436                 'sgc2.normalTTScommand$'
437         endif
439         if macintosh
440                 .ttscommand$ = "speak"
441                 .osName$ = "OSX"
442                 .quote$ = ""
443         elsif unix
444                 .ttscommand$ = "espeak"
445                 .osName$ = "UNIX"
446                 .quote$ = ""
447         elsif windows
448                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
449                 .osName$ = "WIN"
450                 .quote$ = """"
451         endif
452         # Global TTS command has precedence
453         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
454                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
455                 if config.synthesis$ = "_DISABLED_"
456                         # Default is ON
457                         config.synthesis$ = "eSpeak"
458                 endif
459         endif
461         # Local TTS command, if there is no global one
462         # Command does not exist yet, create it if eSpeak is installed
463         # Make sure to quote the path!
464         if speakCommandFile$ = ""
465                 # Autodetect synthesizer command
466 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
467 #                       .command_path$ = ""
468 #                       if macintosh
469 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
470 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
471 #                       elsif unix
472 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
473 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
474 #                       elsif windows
475 #                               if fileReadable(.ttscommand$)
476 #                                       .command_path$ = .ttscommand$
477 #                               endif
478 #                       endif
479 #                       # Remove any newlines
480 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
482 #                       # Command path found
483 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
484 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
485 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
486 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
487 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
488 #                       endif
489 #               endif
490                 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
491                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
492                 endif
493                 deleteFile("'speakCommandDir$'/command_path.txt")
494         endif
495         
496         # Close off (other TTS maybe a later option)
497         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
498                 config.synthesis$ = "eSpeak"
499         endif
500 endproc
502 procedure set_TTS_parameters
503         if config.voice > 0
504                 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+\s+){'config.voice'}(\S+)(\s+.*)?$", "\2", 0)
505         else
506                 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+)(\s+.*)?$", "\1", 0)
507         endif
508         if .voice$ <> "" and index_regex(.voice$, "\s") <= 0
509                 if .voice$ = "default"
510                         config.voice$ = "*"
511                 else
512                         config.voice$ = replace_regex$(.voice$, ".", "\u&", 0)
513                 endif
514                 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin '.voice$' 10000 0.01 50 100 95 no IPA"
515                 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin '.voice$' 10000 0.01 50 70 125 no IPA"
516         endif
517         if macintosh and fileReadable("/usr/bin/say")
518                 sgc.nativeTTScommand$ = sgc.nativeOSXTTScommand$
519         elsif unix and fileReadable("/usr/bin/spd-say")
520                 sgc.nativeTTScommand$ = sgc.nativeLinuxTTScommand$
521         elsif windows
522                 sgc.nativeTTScommand$ = sgc.nativeWindowsTTScommand$
523         endif
524 endproc
526 # Alternative audio recorder
527 procedure set_up_recorder
528         if macintosh
529                 .osName$ = "OSX"
530                 .quote$ = ""
531         elsif unix
532                 .osName$ = "UNIX"
533                 .quote$ = ""
534         elsif windows
535                 .osName$ = "WIN"
536                 .quote$ = """"
537         endif
538         # Global TTS command has precedence
539         sgc.useAlternativeRecorder = 0
540         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
541                 createDirectory(sgc.scratchAudioDir$)
542                 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
543                 sgc.useAlternativeRecorder = 1
544         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
545                 createDirectory(sgc.scratchAudioDir$)
546                 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
547                 sgc.useAlternativeRecorder = 1
548         endif
549 endproc
551 # Alternative audio player
552 procedure set_up_player
553         if macintosh
554                 .osName$ = "OSX"
555                 .quote$ = ""
556         elsif unix
557                 .osName$ = "UNIX"
558                 .quote$ = ""
559         elsif windows
560                 .osName$ = "WIN"
561                 .quote$ = """"
562         endif
563         # Global TTS command has precedence
564         sgc.useAlternativePlayer = 0
565         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_play_command.txt")
566                 createDirectory(sgc.scratchAudioDir$)
567                 sgc.playCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_play_command.txt"
568                 sgc.useAlternativePlayer = 1
569         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_play_command.txt")
570                 createDirectory(sgc.scratchAudioDir$)
571                 sgc.playCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_play_command.txt"
572                 sgc.useAlternativePlayer = 1
573         endif
574 endproc
576 ###############################################################
578 # Evaluation tables
580 # Prerequisites
581 # initialiseSGC2.toneevaluation_table$ = ""
582 procedure initialize_toneevaluation_tables
583         .invalidperformanceTable = 0
584         .oldPerformanceTable = -1
585         # Mark old table for removal
586         if initialiseSGC2.toneevaluation_table$ != ""
587                 .oldPerformanceTable = sgc2.performanceTable
588                 sgc2.performanceTable = -1
589         endif
590         initialiseSGC2.toneevaluation_table$ = ""
591         # Get the name of the table
592         call get_evaluation_table_labels 'config.language$'
593         performanceTableName$ = eval.performance$
594         
595         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
596                 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
597                 Rename: performanceTableName$
598                 .columnNameList$ = "'eval.pinyin$' 'eval.grade$' 'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'"
599                 .numPerfColumns = 14
600                 # Check to make sure all columns are initialized, but only if Pinyin exists!
601                 select sgc2.performanceTable
602                 .numColumns = Get number of columns
603                 # Find Pinyin column on 1
604                 if index("Pinyin 拼音 ピンイン", eval.pinyin$) > 0
605                         .column = Get column index: "Pinyin"
606                         if .column <= 0
607                                 .column = Get column index: "拼音"
608                         endif
609                         if .column <= 0
610                                 .column = Get column index: "ピンイン"
611                         endif
612                 else
613                         .column = Get column index: eval.pinyin$
614                 endif
615                 # This is not a valid Performance table, remove it
616                 if .column != 1 or .numColumns != .numPerfColumns
617                         select sgc2.performanceTable
618                         Remove
619                         
620                         # Set ERROR and remove file read
621                         .invalidperformanceTable = 1
622                         sgc2.performanceTable = .oldPerformanceTable
623                         .oldPerformanceTable = -1
624                 endif
626                 # ALWAYS make sure the column headers are translated!
627                 select sgc2.performanceTable
628                 .numColumns = Get number of columns
629                 .nameList$ = .columnNameList$
630                 for .col to .numColumns
631                         .nameList$ = replace_regex$(.nameList$, "^\s+", "", 1)
632                         .columnHeader$ = extractWord$(.nameList$, "")
633                         .nameList$ = replace_regex$(.nameList$, "^\s*'.columnHeader$'\s*", "", 1)
634                         select sgc2.performanceTable
635                         Set column label (index): .col, .columnHeader$
636                 endfor
638                 # Performance table has been read. Set the wordlist to the first row
639                 select sgc2.performanceTable
640                 .numWords = Get number of rows
641                 .row = 1
642                 wordlistName$ = ""
643                 .wordlistLabel$ = eval.wordlist$
644                 .col = Get column index: .wordlistLabel$
645                 if .col <= 0
646                         .wordlistLabel$ = "Wordlist"
647                         .col = Get column index: .wordlistLabel$
648                 endif
649                 while .col > 0 and .row <= .numWords and (wordlistName$ = "" or index_regex(wordlistName$, "[^\?\- ]") <= 0)
650                         wordlistName$ = Get value: .row, .wordlistLabel$
651                         .row += 1
652                 endwhile
653                 
654                 call load_word_list: localWordlistDir$, 0
655                 
656                 # If this is a graded list, look for the audio
657                 if config.audioName$ = ""
658                         # Check if there is a grade
659                         select sgc2.performanceTable
660                         .grade$ = Get value: get_toneevaluation_row.row, eval.grade$
661                         # It is graded! Look for the audio
662                         if index_regex(.grade$, "[\d]") > 0
663                                 .noAudio = 1
664                                 select sgc2.performanceTable
665                                 .numRows = Get number of rows
666                                 for .row to .numRows
667                                         select sgc2.performanceTable
668                                         .pinyin$ = Get value: .row, eval.pinyin$
669                                         .strings = Create Strings as file list: "Files", "'sgc.saveAudio$'/'.pinyin$'.*"
670                                         .numAudio = Get number of strings
671                                         Remove
672                                         if .numAudio > 0
673                                                 .noAudio = 0
674                                         endif
675                                 endfor
676                                 
677                                 if .noAudio > 0
678                                         .fileName$ = right$(sgc.savePerf$, length(sgc.savePerf$) - index_regex(sgc.savePerf$, "[/\\][^/\\]+$"))
679                                         .fileName$ = replace_regex$(.fileName$, "(?i\.tsv$)", "", 0)
680                                         call findLabel Config $AudioName
681                                         .row = findLabel.row
682                                         select Table Config
683                                         .openDialogue$ = Get value... '.row' Helptext
684                                         call convert_praat_to_latin1 '.openDialogue$'
685                                         .openDialogue$ = convert_praat_to_latin1.text$
686                                         .defaultName$= ""
687                                         sgc.saveAudio$ = chooseDirectory$ (.fileName$+": "+.openDialogue$)
688                                         
689                                 endif
690                         endif
691                 endif
692         else
693                 # Create new table
694                 sgc2.performanceTable = Create Table with column names... "'performanceTableName$'" 0 'eval.pinyin$' 'eval.grade$' 'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'
695         endif
696         initialiseSGC2.toneevaluation_table$ = performanceTableName$
697         call get_toneevaluation_row 'eval.total$'
698         
699         # Remove marked old table
700         if .oldPerformanceTable > 0
701                 select .oldPerformanceTable
702                 Remove
703                 .oldPerformanceTable = -1
704         endif
705 endproc
707 procedure get_toneevaluation_row .pinyin$
708         select sgc2.performanceTable
709         .colName$ = Get column label: 1
710         # Catch and rename Totals. Make sure it works when you stay within a new language
711         if index("Total Totaal 总计 合計", .pinyin$) > 0
712                 .row = Search column: .colName$, "Total"
713                 if .row < 1
714                         .row = Search column: .colName$, "Totaal"
715                 endif
716                 if .row < 1
717                         .row = Search column: .colName$, "总计"
718                 endif
719                 if .row < 1
720                         .row = Search column: .colName$, "合計"
721                 endif
722                 if .row > 0
723                         Set string value: .row,  .colName$, .pinyin$
724                 endif
725         else
726                 .row = Search column: .colName$ , .pinyin$
727         endif
728         if .row < 1
729                 Append row
730                 .row = Get number of rows
731                 Set string value: .row, eval.pinyin$, .pinyin$
732                 Set numeric value... '.row' 'eval.correct$' 0
733                 Set numeric value... '.row' 'eval.wrong$' 0
734                 Set numeric value... '.row' 'eval.total$' 0
735                 Set numeric value... '.row' 'eval.high$' 0
736                 Set numeric value... '.row' 'eval.low$' 0
737                 Set numeric value... '.row' 'eval.wide$' 0
738                 Set numeric value... '.row' 'eval.narrow$' 0
739                 Set numeric value... '.row' 'eval.unknown$' 0
740                 Set numeric value... '.row' 'eval.commented$' 0
741                 Set numeric value... '.row' 'eval.level$' 0
742                 Set numeric value... '.row' 'eval.time$' 0
743                 Set string value: .row, eval.wordlist$, wordlistName$
744                 Sort rows... 'eval.pinyin$'
745         .row = Search column: eval.pinyin$, .pinyin$
746         endif
747 endproc
749 procedure increment_toneevaluation_in_row .row .column$
750         select sgc2.performanceTable
751         .tmp = Get value... '.row' '.column$'
752         if .tmp = undefined
753                 .tmp = 0
754         endif
755         .tmp += 1
756         Set numeric value... '.row' '.column$' '.tmp'
757         
758         # Set Level
759         Set string value: .row, eval.level$, config.strict$
760         
761         # Set time stamp and wordlist
762         .dateTime$ = date$()
763         Set string value: .row, eval.time$, .dateTime$
764         Set string value: .row, eval.wordlist$, wordlistName$
765         
766 endproc
768 # Update existing performance file with toneevaluation
769 procedure update_toneevaluation_file
770         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
771                 select sgc2.performanceTable
772                 Write to table file... 'sgc.savePerf$'
773         endif
774 endproc
776 procedure increment_toneevaluation_value .pinyin$ .column$
777         call get_toneevaluation_row '.pinyin$'
778         .row = get_toneevaluation_row.row
779         call increment_toneevaluation_in_row '.row' '.column$'
780         
781         call get_toneevaluation_row 'eval.total$'
782         .row = get_toneevaluation_row.row
783         call increment_toneevaluation_in_row '.row' '.column$'
784 endproc
786 procedure add_feedback_to_toneevaluation .table$
787     select Table '.table$'
788     .line1$ = Get value... 1 Text
789     .line2$ = Get value... 2 Text
790     .label$ = Get value... 3 Text
791         
792         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
793         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
794         
795         # Set evaluation
796         .result = 0
797         call increment_toneevaluation_value 'sgc.pinyin$' 'eval.total$'
798         if .tones$ = .recognized$
799                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.correct$'
800                 .result = 1
801         else
802                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.wrong$'
803         endif
804         if index_regex(.label$, "(Correct|Wrong)") <= 0
805                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.commented$'
806         endif
807         
808         if .label$ = "6"
809                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.unknown$'
810         endif
811         if .label$ = "Low"
812                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.low$'
813         endif
814         if .label$ = "High"
815                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.high$'
816         endif
817         if .label$ = "Narrow"
818                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.narrow$'
819         endif
820         if .label$ = "Wide"
821                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.wide$'
822         endif
823         
824         
825         # Update performance table when SaveAudio is on
826         if sgc.saveAudioOn
827                 call update_toneevaluation_file
828         endif
829 endproc
831 procedure setGrade .grade$
832         select sgc.currentWordlist
833         if recordedSound$ <> "" and sgc.pinyin$ <> ""           
834                 call get_toneevaluation_row 'sgc.pinyin$'
835                 .row = get_toneevaluation_row.row
836                 select sgc2.performanceTable
837                 .col = Get column index: eval.grade$
838                 if .col <= 0
839                         Insert column: 2, eval.grade$
840                 endif
841                 .gradeNum = '.grade$'
842                 if .gradeNum = 0
843                         .gradeNum = 10
844                 endif
846                 Set numeric value: .row, eval.grade$, .gradeNum
847                 .numRows = Get number of rows
848                 .sumGrade = 0
849                 .sumN = 0
850                 for .r from 2 to .numRows
851                         .tmp = Get value: .r, eval.grade$
852                         if .tmp <> undefined
853                                 .sumGrade += .tmp
854                                 .sumN += 1
855                         endif
856                 endfor
857                 .meanGrade = -1
858                 if .sumN > 0
859                         .meanGrade = .sumGrade / .sumN
860                 endif
861                 
862                 call get_evaluation_text '.language$' Total
863                 .totalTxt$ = get_evaluation_text.text$
864                 call get_toneevaluation_row '.totalTxt$'
865                 .row = get_toneevaluation_row.row
866                 Set numeric value: .row , eval.grade$, .meanGrade
867                 
868                 # Store current performance table
869                 call update_toneevaluation_file
870         endif
871 endproc
873 procedure getGrade .pinyin$
874         .grade = -1
875         if .pinyin$ <> ""
876                 call get_toneevaluation_row '.pinyin$'
877                 .row = get_toneevaluation_row.row
878                 
879                 select sgc2.performanceTable
880                 .gradeLabel$ = eval.grade$
881                 .col = Get column index: eval.grade$
882                 if .col <= 0
883                         .col = Get column index: "Grade"
884                         .gradeLabel$ = "Grade"
885                 endif
886                 if .col <= 0
887                         Insert column... 2 Grade
888                 else
889                         .grade = Get value: .row, .gradeLabel$
890                         if .grade = undefined
891                                 .grade = -1
892                         endif   
893                 endif
894         endif
895 endproc
897 # Set mainPage.saveAudio$ to Grade
898 procedure set_grade_display .pinyin$
899         mainPage.saveAudio$ = ""
900         select sgc2.performanceTable
901         .grade$ = ""
902         .col = Get column index: eval.grade$
903         if .pinyin$ <> "" and .col > 0
904                 call get_toneevaluation_row 'eval.total$'
905                 .row = get_toneevaluation_row.row
906                 if .row > 0
907                         .grade$ = Get value: .row, eval.grade$
908                         if index_regex(.grade$, "[0-9]") <= 0
909                                 .grade$ = ""
910                         endif
911                 endif
912         endif
913         if .grade$ <> ""
914                 .gradeNum = '.grade$'
915                 mainPage.saveAudio$ = fixed$(.gradeNum, 1)
916         endif
917 endproc
918         
919 ###############################################################
921 # Miscelaneous procedures
923 ###############################################################
925 # TTS synthesis
926 procedure synthesize_sound .pinyin$ .character$
927         .pinyin$ = replace_regex$(.pinyin$, "(^""|""$)", "", 0)
928         .character$ = replace_regex$(.character$, "(^""|""$)", "", 0)
929         .pinyinText$ = replace_regex$(.pinyin$, "0", "5", 0)
930         .pinyinExpanded$ = replace_regex$(.pinyin$, "([0-5])", " \1 - ", 0)
931         .pinyinExpanded$ = replace_regex$(.pinyinExpanded$, " - $", "", 0)
932         
933         if .character$ = "-" or .character$ = "" or .character$ = "?"
934                 .character$ = .pinyinText$
935         endif
936         .currentCommand$ = ""
937         # Files need more checks!
938         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
939                 .currentCommand$ < 'speakCommandFile$'
940                 .currentCommand$ = replace$(.currentCommand$, "'newline$'", " ", 0)
941                 if index(.currentCommand$, "$$$") and .character$ <> .pinyinText$
942                         # Find the character representation
943                         .currentCommand$ = replace$(.currentCommand$, "$$$", .character$, 0)
944                 elsif index(.currentCommand$, "&&&")
945                         # Find the character representation
946                         .currentCommand$ = replace$(.currentCommand$, "&&&", .pinyinText$, 0)
947                 elsif index(sgc.nativeTTScommand$, "@@@")
948                         .currentCommand$ = replace$(sgc.nativeTTScommand$, "@@@", .pinyinExpanded$, 0)
949                 else
950                         .currentCommand$ = .currentCommand$ + " ""'.pinyinText$'"""
951                 endif
952                 if macintosh
953                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.currentCommand$''
954                 elsif unix
955                         system_nocheck bash -rc -- ''.currentCommand$''
956                 elsif windows
957                         runSystem_nocheck: .currentCommand$
958                 endif
959         elsif sgc.nativeTTScommand$ <> "" and config.voice <= 0
960                 # Get the pinyin text on the correct place
961                 if index(sgc.nativeTTScommand$, "$$$")
962                         .currentCommand$ = replace$(sgc.nativeTTScommand$, "$$$", .character$, 0)
963                 elsif index(sgc.nativeTTScommand$, "&&&")
964                         .currentCommand$ = replace$(sgc.nativeTTScommand$, "&&&", .pinyinText$, 0)
965                 elsif index(sgc.nativeTTScommand$, "@@@")
966                         .currentCommand$ = replace$(sgc.nativeTTScommand$, "@@@", .pinyinExpanded$, 0)
967                 else
968                         .currentCommand$ = sgc.nativeTTScommand$ + " ""'.pinyinText$'"""
969                 endif
970                 runSystem_nocheck: .currentCommand$
971         elsif sgc2.synthesizer > 0
972                 select sgc2.synthesizer
973                 Play text... '.pinyin$'
974         endif
975 endproc
977 procedure align_recordedSound .pinyin$
978         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
979                 select Sound 'recordedSound$'
980                 .recordedTextGrid = To TextGrid... pinyin
981                 Set interval text... 1 1 '.pinyin$'
982                 select sgc2.synthesizer
983                 plus Sound 'recordedSound$'
984                 plus .recordedTextGrid
985                 sgc.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
986                 Rename... AlignedTextGrid
987                 select .recordedTextGrid
988                 Remove
989         endif
990 endproc