Provisions for showing the feedback in the init window when recordings are available
[sgc2.git] / InitialiseSGC2.praat
blobd7ceea57c695221d148b72ffae3f2724f8127e03
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
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$ = ""
40 # Define canvas
41 viewportMargin = 5
42 yWordlist = 11
43 yFeedback = yWordlist + 5
44 yPinyin = yFeedback + 10
45 yContour = yPinyin + 15
46 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
47 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
48 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
49 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
51 # Pop-Up window colors
52 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
53 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
55 # Initialize parameters
56 alertText$ = ""
57 sgc.currentWord = 1
58 sgc.numberOfWords = 0
59 sgc.recordedSound = 0
60 sgc.saveAudioOn = 0
61 sgc.writeAll = 0
62 pinyin$ = ""
63 sgc.pinyin$ = ""
64 character$ = ""
65 buttons$ = ""
66 config$ = ""
67 wordlistNum = 1
68 wordlistName$ = ""
69 wordlist$ = ""
70 sgc.failedAttempts = 0
71 mainPage.play = -1
72 config.deleteWordlist = -1
73 config.displayNumbers = 1
74 config.displayPinyin = 1
75 config.displayChar = 0
76 config.displayTrans = 1
77 config.useSoundExample = 1
78 config.synthesis$ = "_DISABLED_"
79 config.input$ = "Microphone"
80 config.showBackground = 1
81 sgc2.noiseThresshold = -30
82 config.strict$ = "1"
83 sgc.highestLevel = 3
84 config.savePerf = 0
85 config.openPerf = 0
86 config.clearSummary = 0
87 config.audioName$ = ""
88 sgc.savePerf$ = ""
89 sgc.saveAudio$ = ""
90 mainPage.saveAudio$ = ""
92 sgc_ToneProt.minimumPitch = 40
93 sgc_ToneProt.maximumPitch = 600
95 # Platform dependent settings
96 if macintosh or windows
97         config.displayChar = 1
98         config.displayNumbers = 0
99 endif
100 if windows and endsWith(build_SHA$, " XP")
101         config.displayChar = 0
102         config.displayNumbers = 0
103 endif
104 if unix
105         config.displayChar = 0
106         config.displayNumbers = 1
107 endif
109 if unix
110         samplingFrequency = 44100
111 elsif macintosh
112         samplingFrequency = 44100
113 elsif windows
114         samplingFrequency = 44100
115 endif
116 recordingTime = 2
118 logging = 0
119 config.language$ = "EN"
120 config.register = 249
121 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
122 preferencesLogDir$ = "'preferencesAppDir$'/log"
123 preferencesTableDir$ = "'preferencesAppDir$'/Data"
124 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
125 localWordlistDir$ = sgc2wordlists$
127 # Global word lists must be installed BY THE ADMINISTRATOR
128 # This means, create the directory 'globalwordlists$' and
129 # fill it with wordlist directories containing wordlist.Table
130 # and audio files. They can be copied from a local directory.
131 globalwordlists$ = ""
132 globaltablelists$ = ""
133 globalTTSdir$ = ""
134 if unix
135         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
136         globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"     
137         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
138 elsif macintosh
139         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
140         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
141         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
142 elsif windows
143         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
144         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
145         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
146 endif
147 sgc2.synthesizer = -1
148 # Define a Praat TTS command that will set sgc2.synthesizer
149 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
150 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
151 sgc.alignedTextGrid = -1
153 ##############################################################
155 config.shuffleLists = 1
156 config.adaptiveLists = 0
157 defaultFontSize = 12
158 defaultFont$ = "Helvetica"
159 defaultLineWidth = 1
161 # Set up directories if they do not exist already
162 call set_up_directories
164 # Get saved preferences
165 call read_preferences ""
167 # Set up TTS system
168 speakCommandDir$ = "'preferencesAppDir$'/TTS"
169 speakCommandFile$ = ""
170 call set_up_TTS
171 sgc.recordCommandFile$ = ""
172 sgc.playCommandFile$ = ""
173 if windows
174         sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
175 else
176         sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
177 endif
178 sgc.useAlternativeRecorder = 0
179 call set_up_recorder
180 sgc.useAlternativePlayer = 0
181 call set_up_player
183 # Set inital language
184 call set_language 'config.language$'
186 # Get the word-list
187 call load_word_list "'localWordlistDir$'" 0
189 # Set up evaluation table
190 initialiseSGC2.toneevaluation_table$ = ""
191 call initialize_toneevaluation_tables
193 # Draw inital window
194 call init_window
196 ###############################################################
198 # Obligatory procedures
200 ###############################################################
201 # Initialize Demo Window
202 procedure init_window
203     demo Erase all
204         demo Line width... 'defaultLineWidth'
205         demo 'defaultFont$'
206         call set_font_size 'defaultFontSize'
207         demo Black
208         
209         # If there is no recorded sound, try to read stored recording
210         if recordedSound$ = "" and sgc.saveAudio$ <> ""
211                 sgc.pinyin$ = ""
212                 select Table 'wordlist$'
213                 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
214                         sgc.pinyin$ = Get value... 'sgc.currentWord' Pinyin
215                         .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
216                         if fileReadable(.outputName$)
217                                 sgc.recordedSound = Read from file: .outputName$
218                                 recordedSound$ = selected$("Sound")
219                                 call recognizeTone
220                                 # See feedback on earl;ier recorded words
221                                 if fileReadable("'sgc.saveAudio$'/TestSpeakGoodChinese2.txt")
222                                         call write_feedback Feedback
223                                 endif
224                                 select Table Feedback
225                                 Remove
226                                 
227                                 # Set config.audioName
228                                 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
229                                 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
230                                 
231                         endif
232                 endif
233         endif
234         
235         # Set mainPage.saveAudio$ grade
236         call set_grade_display 'sgc.pinyin$'
237         
238         # Update screen
239         call reset_viewport
240         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
241         call set_window_title 'buttons$' '.windowTitle$'
242         # Display the word-list
243         call write_word_list
244     # Wipe screen and draw background
245     call wipeArea 'wipeContourArea$'
246         if config.showBackground
247                 call draw_background Background
248         endif
249     # Define buttons in a table
250     call init_buttons
251     # Set Play button
252         call Set_Play_Button
253     
254         # Draw the contour
255         call draw_tone_contour
256         # SaveAudio light
257         call paint_saveAudio_light
258         # Set play button
259         
260         sgc.failedAttempts = 0
261 endproc
263 # Make sure all Preferences directories are available
264 procedure set_up_directories
265         .dirPath$ = "'preferencesAppDir$'"
266         createDirectory(.dirPath$)
267         .dirPath$ > '.dirPath$'/directory.txt
268         createDirectory("'.dirPath$'/wordlists")
269         .dirPath$ > '.dirPath$'/wordlists/directory.txt
270         createDirectory("'.dirPath$'/pitchmodels")
271         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
272         createDirectory("'.dirPath$'/log")
273         .dirPath$ > '.dirPath$'/log/directory.txt
274         createDirectory("'.dirPath$'/TTS")
275         .dirPath$ > '.dirPath$'/TTS/directory.txt
276 endproc
279 # Retrieve and store setting between sessions
281 procedure read_preferences .preferencesFile$
282         if not fileReadable(.preferencesFile$)
283                 .preferencesFile$ = preferencesAppFile$
284         endif
285         if fileReadable(.preferencesFile$)
286                 Read from file... 'preferencesAppFile$'
287                 .preferenceTable$ = selected$("Table")
288                 .numPrefKeys = Get number of rows
289                 for .row to .numPrefKeys
290                         .variableName$ = Get value... '.row' Key
291                         if variableExists(.variableName$)
292                                 .variableValue = Get value... '.row' Value
293                                 if .variableValue <> undefined
294                                         '.variableName$' = '.variableValue'
295                                 endif
296                         elsif variableExists(.variableName$+"$")
297                                 .variableValue$ = Get value... '.row' Value
298                                 .variableName$ = .variableName$+"$"
299                                 '.variableName$' = "'.variableValue$'"
300                         endif
301                 endfor
302                 
303                 select Table '.preferenceTable$'
304                 Remove
305         endif
306 endproc
308 procedure write_preferences .preferencesFile$
309         Create Table with column names... Preferences 0 Key Value
310         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
311                 .preferencesFile$ = preferencesAppFile$
312         endif
313         .row = 0
314         
315         for .tableNum from 1 to 2
316                 if .tableNum = 1
317                         .table$ = "Config"
318                 elsif .tableNum = 2
319                         .table$ = "MainPage"
320                 endif
321                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
323                 select Table '.table$'
324                 .numTableRows = Get number of rows
325                 for .tablerow to .numTableRows
326                         select Table '.table$'
327                         .label$ = Get value... '.tablerow' Label
328                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
329                         .keyName$ = .variableName$
330                         .value$ = ""
331                         if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
332                                 .value$ = ""
333                         elsif variableExists(.variableName$)
334                                 .value = '.variableName$'
335                                 .value$ = "'.value'"
336                         elsif variableExists(.variableName$ + "$")
337                                 .variableName$ = .variableName$ + "$"
338                                 .value$ = '.variableName$'
339                         elsif index(.variableName$, "_") > 0
340                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
341                                 .keyName$ = .variableName$
342                                 select Table Preferences
343                                 .prefRow = Search column... Key '.keyName$'
344                                 if .prefRow <= 0
345                                         if variableExists(.variableName$)
346                                            .value = '.variableName$'
347                                            .value$ = "'.value'"
348                                         elsif variableExists(.variableName$ + "$")
349                                            .variableName$ = .variableName$ + "$"
350                                            .value$ = '.variableName$'
351                                         endif
352                                 endif
353                         endif
355                         if .value$ <> ""
356                                 select Table Preferences
357                                 Append row
358                                 .row += 1
359                                 Set string value... '.row' Key '.keyName$'
360                                 Set string value... '.row' Value '.value$'
361                         endif
362                 endfor
363         endfor
364         
365         # Some extra settings
366         select Table Preferences
367         Append row
368         .row += 1
369         Set string value... '.row' Key wordlistDir
370         Set string value... '.row' Value 'localWordlistDir$'
372         select Table Preferences
373         Append row
374         .row += 1
375         Set string value... '.row' Key wordlistName
376         Set string value... '.row' Value 'wordlistName$'
378         select Table Preferences
379         Write to table file... '.preferencesFile$'
380         Remove
382 endproc
384 ###############################################################
386 # TTS (speech synthesis). Place commands in user info
388 # Prerequisites
389 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
390 # speakCommandFile$ = ""
392 # Make sure sgc2.synthesizer is set!
393 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
394         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
395         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
396 endproc
398 procedure set_up_TTS
399         if sgc2.synthesizer > 0
400                 select sgc2.synthesizer
401                 Remove
402                 sgc2.synthesizer = -1
403         endif
404         # Note, the TTScommand must set sgc2.synthesizer!
405         if config.strict$ = "'sgc.highestLevel'"
406                 'sgc2.advancedTTScommand$'
407         else
408                 'sgc2.normalTTScommand$'
409         endif
411         if macintosh
412                 .ttscommand$ = "speak"
413                 .osName$ = "OSX"
414                 .quote$ = ""
415         elsif unix
416                 .ttscommand$ = "espeak"
417                 .osName$ = "UNIX"
418                 .quote$ = ""
419         elsif windows
420                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
421                 .osName$ = "WIN"
422                 .quote$ = """"
423         endif
424         # Global TTS command has precedence
425         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
426                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
427                 if config.synthesis$ = "_DISABLED_"
428                         # Default is ON
429                         config.synthesis$ = "eSpeak"
430                 endif
431         endif
433         # Local TTS command, if there is no global one
434         # Command does not exist yet, create it if eSpeak is installed
435         # Make sure to quote the path!
436         if speakCommandFile$ = ""
437                 # Autodetect synthesizer command
438 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
439 #                       .command_path$ = ""
440 #                       if macintosh
441 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
442 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
443 #                       elsif unix
444 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
445 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
446 #                       elsif windows
447 #                               if fileReadable(.ttscommand$)
448 #                                       .command_path$ = .ttscommand$
449 #                               endif
450 #                       endif
451 #                       # Remove any newlines
452 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
454 #                       # Command path found
455 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
456 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
457 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
458 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
459 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
460 #                       endif
461 #               endif
462 #               if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
463                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
464                 endif
465                 deleteFile("'speakCommandDir$'/command_path.txt")
466         endif
467         
468         # Close off (other TTS maybe a later option)
469         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
470                 config.synthesis$ = "eSpeak"
471         endif
472 endproc
474 # Alternative audio recorder
475 procedure set_up_recorder
476         if macintosh
477                 .osName$ = "OSX"
478                 .quote$ = ""
479         elsif unix
480                 .osName$ = "UNIX"
481                 .quote$ = ""
482         elsif windows
483                 .osName$ = "WIN"
484                 .quote$ = """"
485         endif
486         # Global TTS command has precedence
487         sgc.useAlternativeRecorder = 0
488         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
489                 createDirectory(sgc.scratchAudioDir$)
490                 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
491                 sgc.useAlternativeRecorder = 1
492         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
493                 createDirectory(sgc.scratchAudioDir$)
494                 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
495                 sgc.useAlternativeRecorder = 1
496         endif
497 endproc
499 # Alternative audio player
500 procedure set_up_player
501         if macintosh
502                 .osName$ = "OSX"
503                 .quote$ = ""
504         elsif unix
505                 .osName$ = "UNIX"
506                 .quote$ = ""
507         elsif windows
508                 .osName$ = "WIN"
509                 .quote$ = """"
510         endif
511         # Global TTS command has precedence
512         sgc.useAlternativePlayer = 0
513         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_play_command.txt")
514                 createDirectory(sgc.scratchAudioDir$)
515                 sgc.playCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_play_command.txt"
516                 sgc.useAlternativePlayer = 1
517         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_play_command.txt")
518                 createDirectory(sgc.scratchAudioDir$)
519                 sgc.playCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_play_command.txt"
520                 sgc.useAlternativePlayer = 1
521         endif
522 endproc
524 ###############################################################
526 # Evaluation tables
528 # Prerequisites
529 # initialiseSGC2.toneevaluation_table$ = ""
530 procedure initialize_toneevaluation_tables
531         # Remove old table
532         if initialiseSGC2.toneevaluation_table$ != ""
533                 select Table 'initialiseSGC2.toneevaluation_table$'
534                 Remove
535         endif
536         initialiseSGC2.toneevaluation_table$ = ""
537         
538         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
539                 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
540                 Rename: "Performance"
541                 # Check to make sure all columns are initialized, but only if Pinyin exists!
542                 select sgc2.performanceTable
543                 .column = Get column index: "Pinyin"
544                 if .column <= 0
545                         select sgc2.performanceTable
546                         Remove
547                         sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time
548                 endif
549                 .columnNameList$ = "Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time"
550                 while index_regex(.columnNameList$, "\S")
551                         .columnNameList$ = replace_regex$(.columnNameList$, "^\s+", "", 1)
552                         .columnHeader$ = extractWord$(.columnNameList$, "")
553                         .columnNameList$ = replace_regex$(.columnNameList$, "^\s*'.columnHeader$'\s*", "", 1)
554                         # Initialize header
555                         select sgc2.performanceTable
556                         .column = Get column index: .columnHeader$
557                         if .column <= 0
558                                 select sgc2.performanceTable
559                                 Append column: .columnHeader$
560                         endif
561                         
562                 endwhile
563         else
564                 # Create new table
565                 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time
566         endif
567         initialiseSGC2.toneevaluation_table$ = "Performance"
568         call get_toneevaluation_row Total
569 endproc
571 procedure get_toneevaluation_row .pinyin$
572         select Table 'initialiseSGC2.toneevaluation_table$'
573     .row = Search column... Pinyin '.pinyin$'
574         if .row < 1
575                 Append row
576                 .row = Get number of rows
577                 Set string value... '.row' Pinyin '.pinyin$'
578                 Set numeric value... '.row' Correct 0
579                 Set numeric value... '.row' Wrong 0
580                 Set numeric value... '.row' Total 0
581                 Set numeric value... '.row' High 0
582                 Set numeric value... '.row' Low 0
583                 Set numeric value... '.row' Wide 0
584                 Set numeric value... '.row' Narrow 0
585                 Set numeric value... '.row' Unknown 0
586                 Set numeric value... '.row' Commented 0
587                 Set numeric value... '.row' Level 0
588                 Set numeric value... '.row' Time 0
589                 Sort rows... Pinyin
590         .row = Search column... Pinyin '.pinyin$'
591         endif
592 endproc
594 procedure increment_toneevaluation_in_row .row .column$
595         select Table 'initialiseSGC2.toneevaluation_table$'
596         .tmp = Get value... '.row' '.column$'
597         .tmp += 1
598         Set numeric value... '.row' '.column$' '.tmp'
599         
600         # Set Level
601         Set string value: .row, "Level", config.strict$
602         
603         # Set time stamp
604         .dateTime$ = date$()
605         Set string value: .row, "Time", .dateTime$
606         
607 endproc
609 # Update existing performance file with toneevaluation
610 procedure update_toneevaluation_file
611         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
612                 select Table 'initialiseSGC2.toneevaluation_table$'
613                 Write to table file... 'sgc.savePerf$'
614         endif
615 endproc
617 procedure increment_toneevaluation_value .pinyin$ .column$
618         call get_toneevaluation_row '.pinyin$'
619         .row = get_toneevaluation_row.row
620         call increment_toneevaluation_in_row '.row' '.column$'
621         
622         call get_toneevaluation_row Total
623         .row = get_toneevaluation_row.row
624         call increment_toneevaluation_in_row '.row' '.column$'
625 endproc
627 procedure add_feedback_to_toneevaluation .table$
628     select Table '.table$'
629     .line1$ = Get value... 1 Text
630     .line2$ = Get value... 2 Text
631     .label$ = Get value... 3 Text
632         if .label$ = "6"
633                 .label$ = "Unknown"
634         endif
635         
636         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
637         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
638         
639         # Set evaluation
640         call increment_toneevaluation_value 'sgc.pinyin$' Total
641         if .tones$ = .recognized$
642                 call increment_toneevaluation_value 'sgc.pinyin$' Correct
643                 .result = 1
644         else
645                 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
646                 .result = 0
647         endif
648         if index_regex(.label$, "(Correct|Wrong)") <= 0
649                 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
650                 call increment_toneevaluation_value 'sgc.pinyin$' Commented
651                 .result = 0
652         endif
653         
654         # Update performance table when SaveAudio is on
655         if sgc.saveAudioOn
656                 call update_toneevaluation_file
657         endif
658 endproc
660 procedure setGrade .grade$
661         select Table 'wordlist$'
662         if recordedSound$ <> "" and sgc.pinyin$ <> ""           
663                 call get_toneevaluation_row 'sgc.pinyin$'
664                 .row = get_toneevaluation_row.row
665                 select Table 'initialiseSGC2.toneevaluation_table$'
666                 .col = Get column index... Grade
667                 if .col <= 0
668                         Insert column... 2 Grade
669                 endif
670                         
671                 Set numeric value: .row, "Grade", '.grade$'
672                 .numRows = Get number of rows
673                 .sumGrade = 0
674                 .sumN = 0
675                 for .r from 2 to .numRows
676                         .tmp = Get value: .r, "Grade"
677                         if .tmp <> undefined
678                                 .sumGrade += .tmp
679                                 .sumN += 1
680                         endif
681                 endfor
682                 .meanGrade = -1
683                 if .sumN > 0
684                         .meanGrade = .sumGrade / .sumN
685                 endif
686                 Set numeric value: 1, "Grade", .meanGrade
687                 
688                 # Store current performance table
689                 call update_toneevaluation_file
690         endif
691 endproc
693 # Set mainPage.saveAudio$ to Grade
694 procedure set_grade_display .pinyin$
695         mainPage.saveAudio$ = ""
696         select Table 'initialiseSGC2.toneevaluation_table$'
697         .grade$ = ""
698         .col = Get column index: "Grade"
699         if .pinyin$ <> "" and .col > 0
700                 call get_toneevaluation_row '.pinyin$'
701                 .row = get_toneevaluation_row.row
702                 if .row > 0
703                         .grade$ = Get value: .row, "Grade"
704                         if index_regex(.grade$, "[0-9]") <= 0
705                                 .grade$ = ""
706                         endif
707                 endif
708         endif
709         if .grade$ <> ""
710                 mainPage.saveAudio$ = .grade$
711         endif
712 endproc
713         
714 ###############################################################
716 # Miscelaneous procedures
718 ###############################################################
720 # TTS synthesis
721 procedure synthesize_sound .pinyin$
722         .command$ = ""
723         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
724                 .command$ < 'speakCommandFile$'
725                 .command$ = replace$(.command$, "'newline$'", " ", 0)
726                 if macintosh
727                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
728                 elsif unix
729                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
730                 elsif windows
731                         system_nocheck call '.command$' "'.pinyin$'"'
732                 endif
733         elsif sgc2.synthesizer > 0
734                 select sgc2.synthesizer
735                 Play text... '.pinyin$'
736         endif
737 endproc
739 procedure align_recordedSound .pinyin$
740         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
741                 select Sound 'recordedSound$'
742                 .recordedTextGrid = To TextGrid... pinyin
743                 Set interval text... 1 1 '.pinyin$'
744                 select sgc2.synthesizer
745                 plus Sound 'recordedSound$'
746                 plus .recordedTextGrid
747                 sgc.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
748                 Rename... AlignedTextGrid
749                 select .recordedTextGrid
750                 Remove
751         endif
752 endproc