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