Changed Windows TTS command
[sgc2.git] / InitialiseSGC2.praat
blob1edecc49a4267068be955dee353989a6648eb79c
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.nativeTTScommand$ = ""
99 config.savePerf = 0
100 config.openPerf = 0
101 config.clearSummary = 0
102 config.audioName$ = ""
103 sgc.savePerf$ = ""
104 sgc.saveAudio$ = ""
105 mainPage.saveAudio$ = ""
106 sgc.allWordLists = -1
108 sgc_ToneProt.minimumPitch = 40
109 sgc_ToneProt.maximumPitch = 600
111 # Platform dependent settings
112 if macintosh or windows
113         config.displayChar = 1
114         config.displayNumbers = 0
115 endif
116 if windows and endsWith(build_SHA$, " XP")
117         config.displayChar = 0
118         config.displayNumbers = 0
119 endif
120 if unix
121         config.displayChar = 0
122         config.displayNumbers = 0
123 endif
125 if unix
126         samplingFrequency = 44100
127 elsif macintosh
128         samplingFrequency = 44100
129 elsif windows
130         samplingFrequency = 44100
131 endif
132 recordingTime = 2
134 # Be careful: Logging and Replaying can have side-effects
135 # sgc2.logging$ = "call saveLogOfActivity"
136 sgc2.logging$ = "# "
137 # When this is not "# ", the command 'replaySGC2Log$' will be run!
138 if not variableExists("replaySGC2Log$")
139         replaySGC2Log$ = "# "
140 endif
142 config.language$ = "EN"
143 config.register = 249
144 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
145 preferencesLogDir$ = "'preferencesAppDir$'/log"
146 preferencesTableDir$ = "'preferencesAppDir$'/GUI"
147 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
148 localWordlistDir$ = sgc2wordlists$
150 # Global word lists must be installed BY THE ADMINISTRATOR
151 # This means, create the directory 'globalwordlists$' and
152 # fill it with wordlist directories containing wordlist.Table
153 # and audio files. They can be copied from a local directory.
154 globalwordlists$ = ""
155 globaltablelists$ = ""
156 globalTTSdir$ = ""
157 if unix
158         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
159         globaltablelists$ = "/etc/'sgc2.demoAppName$'/GUI"      
160         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
161 elsif macintosh
162         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
163         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/GUI"
164         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
165 elsif windows
166         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
167         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/GUI"
168         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
169 endif
170 sgc2.synthesizer = -1
171 sgc.alignedTextGrid = -1
173 ##############################################################
175 config.shuffleLists = 1
176 config.adaptiveLists = 0
177 defaultFontSize = 12
178 defaultFont$ = "Helvetica"
179 defaultLineWidth = 1
181 # Set up directories if they do not exist already
182 call set_up_directories
184 # Get saved preferences
185 call read_preferences ""
187 # Set up TTS system
188 speakCommandDir$ = "'preferencesAppDir$'/TTS"
189 speakCommandFile$ = ""
190 # Define a Praat TTS command that will set sgc2.synthesizer
191 call set_TTS_parameters
192 call set_up_TTS
193 sgc.recordCommandFile$ = ""
194 sgc.playCommandFile$ = ""
195 if windows
196         sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
197 else
198         sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
199 endif
200 sgc.useAlternativeRecorder = 0
201 call set_up_recorder
202 sgc.useAlternativePlayer = 0
203 call set_up_player
205 # Set inital language
206 call set_language 'config.language$'
208 # Get the word-list
209 call load_word_list "'localWordlistDir$'" 0
211 # Set up evaluation table
212 initialiseSGC2.toneevaluation_table$ = ""
213 call initialize_toneevaluation_tables
215 # Draw inital window
216 call init_window
218 ###############################################################
220 # Obligatory procedures
222 ###############################################################
223 # Initialize Demo Window
224 procedure init_window
225     demo Erase all
226         demo Line width... 'defaultLineWidth'
227         demo 'defaultFont$'
228         call set_font_size 'defaultFontSize'
229         demo Black
230         
231         # If there is no recorded sound, try to read stored recording
232         if recordedSound$ = "" and sgc.saveAudio$ <> ""
233                 sgc.pinyin$ = ""
234                 select sgc.currentWordlist
235                 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
236                         call readPinyin 'sgc.currentWord'
237                         sgc.pinyin$ = readPinyin.pinyin$
238                         .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
239                         if fileReadable(.outputName$)
240                                 sgc.recordedSound = Read from file: .outputName$
241                                 recordedSound$ = selected$("Sound")
242                                 call recognizeTone
243                                 # See feedback on earl;ier recorded words
244                                 if fileReadable("'sgc.saveAudio$'/TestSpeakGoodChinese2.txt")
245                                         call write_feedback Feedback
246                                 endif
247                                 select Table Feedback
248                                 Remove
249                                 
250                                 # Set config.audioName
251                                 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
252                                 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
253                                 
254                         endif
255                 endif
256         endif
257         
258         # Set mainPage.saveAudio$ grade
259         call set_grade_display 'sgc.pinyin$'
260         
261         # Update screen
262         call reset_viewport
263         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
264         call set_window_title 'buttons$' '.windowTitle$'
265         # Display the word-list
266         call write_word_list
267     # Wipe screen and draw background
268     call wipeArea 'wipeContourArea$'
269         if config.showBackground
270                 call draw_background Background
271         endif
272     # Define buttons in a table
273     call init_buttons
274     # Set Play button
275         call Set_Play_Button
276     
277         # Draw the contour
278         call draw_tone_contour
279         # SaveAudio light
280         call paint_saveAudio_light
281         # Set play button
282         
283         sgc.failedAttempts = 0
284 endproc
286 # Make sure all Preferences directories are available
287 procedure set_up_directories
288         .dirPath$ = "'preferencesAppDir$'"
289         createDirectory(.dirPath$)
290         .dirPath$ > '.dirPath$'/directory.txt
291         createDirectory("'.dirPath$'/wordlists")
292         .dirPath$ > '.dirPath$'/wordlists/directory.txt
293         createDirectory("'.dirPath$'/pitchmodels")
294         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
295         createDirectory("'.dirPath$'/log")
296         .dirPath$ > '.dirPath$'/log/directory.txt
297         createDirectory("'.dirPath$'/TTS")
298         .dirPath$ > '.dirPath$'/TTS/directory.txt
299 endproc
302 # Retrieve and store setting between sessions
304 procedure read_preferences .preferencesFile$
305         if not fileReadable(.preferencesFile$)
306                 .preferencesFile$ = preferencesAppFile$
307         endif
308         if fileReadable(.preferencesFile$)
309                 Read from file... 'preferencesAppFile$'
310                 .preferenceTable$ = selected$("Table")
311                 .numPrefKeys = Get number of rows
312                 for .row to .numPrefKeys
313                         .variableName$ = Get value... '.row' Key
314                         if variableExists(.variableName$)
315                                 .variableValue = Get value... '.row' Value
316                                 if .variableValue <> undefined
317                                         '.variableName$' = '.variableValue'
318                                 endif
319                         elsif variableExists(.variableName$+"$")
320                                 .variableValue$ = Get value... '.row' Value
321                                 .variableName$ = .variableName$+"$"
322                                 '.variableName$' = "'.variableValue$'"
323                         endif
324                 endfor
325                 
326                 select Table '.preferenceTable$'
327                 Remove
328         endif
329 endproc
331 procedure write_preferences .preferencesFile$
332         Create Table with column names... Preferences 0 Key Value
333         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
334                 .preferencesFile$ = preferencesAppFile$
335         endif
336         .row = 0
337         
338         for .tableNum from 1 to 2
339                 if .tableNum = 1
340                         .table$ = "Config"
341                 elsif .tableNum = 2
342                         .table$ = "MainPage"
343                 endif
344                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
346                 select Table '.table$'
347                 .numTableRows = Get number of rows
348                 for .tablerow to .numTableRows
349                         select Table '.table$'
350                         .label$ = Get value... '.tablerow' Label
351                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
352                         .keyName$ = .variableName$
353                         .value$ = ""
354                         if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
355                                 .value$ = ""
356                         elsif variableExists(.variableName$)
357                                 .value = '.variableName$'
358                                 .value$ = "'.value'"
359                         elsif variableExists(.variableName$ + "$")
360                                 .variableName$ = .variableName$ + "$"
361                                 .value$ = '.variableName$'
362                         elsif index(.variableName$, "_") > 0
363                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
364                                 .keyName$ = .variableName$
365                                 select Table Preferences
366                                 .prefRow = Search column... Key '.keyName$'
367                                 if .prefRow <= 0
368                                         if variableExists(.variableName$)
369                                            .value = '.variableName$'
370                                            .value$ = "'.value'"
371                                         elsif variableExists(.variableName$ + "$")
372                                            .variableName$ = .variableName$ + "$"
373                                            .value$ = '.variableName$'
374                                         endif
375                                 endif
376                         endif
378                         if .value$ <> ""
379                                 select Table Preferences
380                                 Append row
381                                 .row += 1
382                                 Set string value... '.row' Key '.keyName$'
383                                 Set string value... '.row' Value '.value$'
384                         endif
385                 endfor
386         endfor
387         
388         # Some extra settings
389         select Table Preferences
390         Append row
391         .row += 1
392         Set string value... '.row' Key wordlistDir
393         Set string value... '.row' Value 'localWordlistDir$'
395         select Table Preferences
396         Append row
397         .row += 1
398         Set string value... '.row' Key wordlistName
399         Set string value... '.row' Value 'wordlistName$'
401         select Table Preferences
402         Write to table file... '.preferencesFile$'
403         Remove
405 endproc
407 ###############################################################
409 # TTS (speech synthesis). Place commands in user info
411 # Prerequisites
412 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
413 # speakCommandFile$ = ""
415 # Make sure sgc2.synthesizer is set!
416 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
417         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
418         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
419 endproc
421 procedure set_up_TTS
422         if sgc2.synthesizer > 0
423                 select sgc2.synthesizer
424                 Remove
425                 sgc2.synthesizer = -1
426         endif
427         # Note, the TTScommand must set sgc2.synthesizer!
428         if config.strict$ = "'sgc.highestLevel'"
429                 'sgc2.advancedTTScommand$'
430         else
431                 'sgc2.normalTTScommand$'
432         endif
434         if macintosh
435                 .ttscommand$ = "speak"
436                 .osName$ = "OSX"
437                 .quote$ = ""
438         elsif unix
439                 .ttscommand$ = "espeak"
440                 .osName$ = "UNIX"
441                 .quote$ = ""
442         elsif windows
443                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
444                 .osName$ = "WIN"
445                 .quote$ = """"
446         endif
447         # Global TTS command has precedence
448         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
449                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
450                 if config.synthesis$ = "_DISABLED_"
451                         # Default is ON
452                         config.synthesis$ = "eSpeak"
453                 endif
454         endif
456         # Local TTS command, if there is no global one
457         # Command does not exist yet, create it if eSpeak is installed
458         # Make sure to quote the path!
459         if speakCommandFile$ = ""
460                 # Autodetect synthesizer command
461 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
462 #                       .command_path$ = ""
463 #                       if macintosh
464 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
465 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
466 #                       elsif unix
467 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
468 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
469 #                       elsif windows
470 #                               if fileReadable(.ttscommand$)
471 #                                       .command_path$ = .ttscommand$
472 #                               endif
473 #                       endif
474 #                       # Remove any newlines
475 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
477 #                       # Command path found
478 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
479 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
480 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
481 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
482 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
483 #                       endif
484 #               endif
485                 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
486                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
487                 endif
488                 deleteFile("'speakCommandDir$'/command_path.txt")
489         endif
490         
491         # Close off (other TTS maybe a later option)
492         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
493                 config.synthesis$ = "eSpeak"
494         endif
495 endproc
497 procedure set_TTS_parameters
498         if config.voice > 0
499                 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+\s+){'config.voice'}(\S+)(\s+.*)?$", "\2", 0)
500         else
501                 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+)(\s+.*)?$", "\1", 0)
502         endif
503         if .voice$ <> "" and index_regex(.voice$, "\s") <= 0
504                 if .voice$ = "default"
505                         config.voice$ = "*"
506                 else
507                         config.voice$ = replace_regex$(.voice$, ".", "\u&", 0)
508                 endif
509                 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin '.voice$' 10000 0.01 50 100 95 no IPA"
510                 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin '.voice$' 10000 0.01 50 70 125 no IPA"
511         endif
512         if macintosh and fileReadable("/usr/bin/say")
513                 sgc.nativeTTScommand$ = sgc.nativeOSXTTScommand$
514         elsif unix and fileReadable("/usr/bin/spd-say")
515                 sgc.nativeTTScommand$ = sgc.nativeLinuxTTScommand$
516         elsif windows
517                 sgc.nativeTTScommand$ = sgc.nativeWindowsTTScommand$
518         endif
519 endproc
521 # Alternative audio recorder
522 procedure set_up_recorder
523         if macintosh
524                 .osName$ = "OSX"
525                 .quote$ = ""
526         elsif unix
527                 .osName$ = "UNIX"
528                 .quote$ = ""
529         elsif windows
530                 .osName$ = "WIN"
531                 .quote$ = """"
532         endif
533         # Global TTS command has precedence
534         sgc.useAlternativeRecorder = 0
535         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
536                 createDirectory(sgc.scratchAudioDir$)
537                 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
538                 sgc.useAlternativeRecorder = 1
539         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
540                 createDirectory(sgc.scratchAudioDir$)
541                 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
542                 sgc.useAlternativeRecorder = 1
543         endif
544 endproc
546 # Alternative audio player
547 procedure set_up_player
548         if macintosh
549                 .osName$ = "OSX"
550                 .quote$ = ""
551         elsif unix
552                 .osName$ = "UNIX"
553                 .quote$ = ""
554         elsif windows
555                 .osName$ = "WIN"
556                 .quote$ = """"
557         endif
558         # Global TTS command has precedence
559         sgc.useAlternativePlayer = 0
560         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_play_command.txt")
561                 createDirectory(sgc.scratchAudioDir$)
562                 sgc.playCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_play_command.txt"
563                 sgc.useAlternativePlayer = 1
564         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_play_command.txt")
565                 createDirectory(sgc.scratchAudioDir$)
566                 sgc.playCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_play_command.txt"
567                 sgc.useAlternativePlayer = 1
568         endif
569 endproc
571 ###############################################################
573 # Evaluation tables
575 # Prerequisites
576 # initialiseSGC2.toneevaluation_table$ = ""
577 procedure initialize_toneevaluation_tables
578         # Remove old table
579         if initialiseSGC2.toneevaluation_table$ != ""
580                 select sgc2.performanceTable
581                 Remove
582         endif
583         initialiseSGC2.toneevaluation_table$ = ""
584         # Get the name of the table
585         call get_evaluation_table_labels 'config.language$'
586         performanceTableName$ = eval.performance$
587         
588         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
589                 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
590                 Rename: performanceTableName$
591                 # Check to make sure all columns are initialized, but only if Pinyin exists!
592                 select sgc2.performanceTable
593                 .column = Get column index: eval.pinyin$
594                 if .column <= 0
595                         select sgc2.performanceTable
596                         Remove
597                         sgc2.performanceTable = Create Table with column names... "'performanceTableName$'" 0 'eval.pinyin$' 'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'
598                 endif
599                 .columnNameList$ = "'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'"
600                 while index_regex(.columnNameList$, "\S")
601                         .columnNameList$ = replace_regex$(.columnNameList$, "^\s+", "", 1)
602                         .columnHeader$ = extractWord$(.columnNameList$, "")
603                         .columnNameList$ = replace_regex$(.columnNameList$, "^\s*'.columnHeader$'\s*", "", 1)
604                         # Initialize header
605                         select sgc2.performanceTable
606                         .column = Get column index: .columnHeader$
607                         if .column <= 0
608                                 select sgc2.performanceTable
609                                 Append column: .columnHeader$
610                         endif
611                         
612                 endwhile
613                 
614                 # Performance table has been read. Set the wordlist to the first row
615                 select sgc2.performanceTable
616                 wordlistName$ = Get value: 1, eval.wordlist$
617                 call load_word_list: localWordlistDir$, 0
618         else
619                 # Create new table
620                 sgc2.performanceTable = Create Table with column names... "'performanceTableName$'" 0 'eval.pinyin$' 'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'
621         endif
622         initialiseSGC2.toneevaluation_table$ = performanceTableName$
623         call get_toneevaluation_row 'eval.total$'
624 endproc
626 procedure get_toneevaluation_row .pinyin$
627         select sgc2.performanceTable
628     .row = Search column... "'eval.pinyin$'" '.pinyin$'
629         if .row < 1
630                 Append row
631                 .row = Get number of rows
632                 Set string value... '.row' "'eval.pinyin$'" '.pinyin$'
633                 Set numeric value... '.row' 'eval.correct$' 0
634                 Set numeric value... '.row' 'eval.wrong$' 0
635                 Set numeric value... '.row' 'eval.total$' 0
636                 Set numeric value... '.row' 'eval.high$' 0
637                 Set numeric value... '.row' 'eval.low$' 0
638                 Set numeric value... '.row' 'eval.wide$' 0
639                 Set numeric value... '.row' 'eval.narrow$' 0
640                 Set numeric value... '.row' 'eval.unknown$' 0
641                 Set numeric value... '.row' 'eval.commented$' 0
642                 Set numeric value... '.row' 'eval.level$' 0
643                 Set numeric value... '.row' 'eval.time$' 0
644                 Set string value... '.row' 'eval.wordlist$' 'wordlistName$'
645                 Sort rows... 'eval.pinyin$'
646         .row = Search column... "'eval.pinyin$'" '.pinyin$'
647         endif
648 endproc
650 procedure increment_toneevaluation_in_row .row .column$
651         select sgc2.performanceTable
652         .tmp = Get value... '.row' '.column$'
653         if .tmp = undefined
654                 .tmp = 0
655         endif
656         .tmp += 1
657         Set numeric value... '.row' '.column$' '.tmp'
658         
659         # Set Level
660         Set string value: .row, eval.level$, config.strict$
661         
662         # Set time stamp and wordlist
663         .dateTime$ = date$()
664         Set string value: .row, eval.time$, .dateTime$
665         Set string value: .row, eval.wordlist$, wordlistName$
666         
667 endproc
669 # Update existing performance file with toneevaluation
670 procedure update_toneevaluation_file
671         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
672                 select sgc2.performanceTable
673                 Write to table file... 'sgc.savePerf$'
674         endif
675 endproc
677 procedure increment_toneevaluation_value .pinyin$ .column$
678         call get_toneevaluation_row '.pinyin$'
679         .row = get_toneevaluation_row.row
680         call increment_toneevaluation_in_row '.row' '.column$'
681         
682         call get_toneevaluation_row 'eval.total$'
683         .row = get_toneevaluation_row.row
684         call increment_toneevaluation_in_row '.row' '.column$'
685 endproc
687 procedure add_feedback_to_toneevaluation .table$
688     select Table '.table$'
689     .line1$ = Get value... 1 Text
690     .line2$ = Get value... 2 Text
691     .label$ = Get value... 3 Text
692         if .label$ = "6"
693                 .label$ = "'eval.unknown$'"
694         endif
695         
696         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
697         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
698         
699         # Set evaluation
700         call increment_toneevaluation_value 'sgc.pinyin$' 'eval.total$'
701         if .tones$ = .recognized$
702                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.correct$'
703                 .result = 1
704         else
705                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.wrong$'
706                 .result = 0
707         endif
708         if index_regex(.label$, "(Correct|Wrong)") <= 0
709                 if .label$ = "Correct"
710                         .label$ = eval.correct$
711                 else
712                         .label$ = eval.wrong$
713                 endif
714                 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
715                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.commented$'
716                 .result = 0
717         endif
718         
719         # Update performance table when SaveAudio is on
720         if sgc.saveAudioOn
721                 call update_toneevaluation_file
722         endif
723 endproc
725 procedure setGrade .grade$
726         select sgc.currentWordlist
727         if recordedSound$ <> "" and sgc.pinyin$ <> ""           
728                 call get_toneevaluation_row 'sgc.pinyin$'
729                 .row = get_toneevaluation_row.row
730                 select sgc2.performanceTable
731                 .col = Get column index... Grade
732                 if .col <= 0
733                         Insert column... 2 Grade
734                 endif
735                         
736                 Set numeric value: .row, "Grade", '.grade$'
737                 .numRows = Get number of rows
738                 .sumGrade = 0
739                 .sumN = 0
740                 for .r from 2 to .numRows
741                         .tmp = Get value: .r, "Grade"
742                         if .tmp <> undefined
743                                 .sumGrade += .tmp
744                                 .sumN += 1
745                         endif
746                 endfor
747                 .meanGrade = -1
748                 if .sumN > 0
749                         .meanGrade = .sumGrade / .sumN
750                 endif
751                 Set numeric value: 1, "Grade", .meanGrade
752                 
753                 # Store current performance table
754                 call update_toneevaluation_file
755         endif
756 endproc
758 # Set mainPage.saveAudio$ to Grade
759 procedure set_grade_display .pinyin$
760         mainPage.saveAudio$ = ""
761         select sgc2.performanceTable
762         .grade$ = ""
763         .col = Get column index: "Grade"
764         if .pinyin$ <> "" and .col > 0
765                 call get_toneevaluation_row '.pinyin$'
766                 .row = get_toneevaluation_row.row
767                 if .row > 0
768                         .grade$ = Get value: .row, "Grade"
769                         if index_regex(.grade$, "[0-9]") <= 0
770                                 .grade$ = ""
771                         endif
772                 endif
773         endif
774         if .grade$ <> ""
775                 mainPage.saveAudio$ = .grade$
776         endif
777 endproc
778         
779 ###############################################################
781 # Miscelaneous procedures
783 ###############################################################
785 # TTS synthesis
786 procedure synthesize_sound .pinyin$
787         .command$ = ""
788         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
789                 .command$ < 'speakCommandFile$'
790                 .command$ = replace$(.command$, "'newline$'", " ", 0)
791                 if macintosh
792                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
793                 elsif unix
794                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
795                 elsif windows
796                         system_nocheck call '.command$' "'.pinyin$'"
797                 endif
798         elsif sgc.nativeTTScommand$ <> "" and config.voice <= 0
799                 .text$ = replace_regex$(.pinyin$, "0$", "''", 0)
800                 # Get the pinyin text on the correct place
801                 if index(sgc.nativeTTScommand$, "$$$")
802                         .currentCommand$ = replace$(sgc.nativeTTScommand$, "$$$", .text$, 0)
803                 else
804                         .currentCommand$ = sgc.nativeTTScommand$ + " ""'.text$'"""
805                 endif
806                 runSystem_nocheck: .currentCommand$
807         elsif sgc2.synthesizer > 0
808                 select sgc2.synthesizer
809                 Play text... '.pinyin$'
810         endif
811 endproc
813 procedure align_recordedSound .pinyin$
814         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
815                 select Sound 'recordedSound$'
816                 .recordedTextGrid = To TextGrid... pinyin
817                 Set interval text... 1 1 '.pinyin$'
818                 select sgc2.synthesizer
819                 plus Sound 'recordedSound$'
820                 plus .recordedTextGrid
821                 sgc.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
822                 Rename... AlignedTextGrid
823                 select .recordedTextGrid
824                 Remove
825         endif
826 endproc