Folded sys_praat_name.patch into adaptPraat.patch, adapted patch to Praat 5.4.13
[sgc2.git] / InitialiseSGC2.praat
blobba24fdc8b3b01a0c9a91ce658ab317cf1aede33c
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                         call readPinyin 'sgc.currentWord'
215                         sgc.pinyin$ = readPinyin.pinyin$
216                         .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
217                         if fileReadable(.outputName$)
218                                 sgc.recordedSound = Read from file: .outputName$
219                                 recordedSound$ = selected$("Sound")
220                                 call recognizeTone
221                                 # See feedback on earl;ier recorded words
222                                 if fileReadable("'sgc.saveAudio$'/TestSpeakGoodChinese2.txt")
223                                         call write_feedback Feedback
224                                 endif
225                                 select Table Feedback
226                                 Remove
227                                 
228                                 # Set config.audioName
229                                 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
230                                 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
231                                 
232                         endif
233                 endif
234         endif
235         
236         # Set mainPage.saveAudio$ grade
237         call set_grade_display 'sgc.pinyin$'
238         
239         # Update screen
240         call reset_viewport
241         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
242         call set_window_title 'buttons$' '.windowTitle$'
243         # Display the word-list
244         call write_word_list
245     # Wipe screen and draw background
246     call wipeArea 'wipeContourArea$'
247         if config.showBackground
248                 call draw_background Background
249         endif
250     # Define buttons in a table
251     call init_buttons
252     # Set Play button
253         call Set_Play_Button
254     
255         # Draw the contour
256         call draw_tone_contour
257         # SaveAudio light
258         call paint_saveAudio_light
259         # Set play button
260         
261         sgc.failedAttempts = 0
262 endproc
264 # Make sure all Preferences directories are available
265 procedure set_up_directories
266         .dirPath$ = "'preferencesAppDir$'"
267         createDirectory(.dirPath$)
268         .dirPath$ > '.dirPath$'/directory.txt
269         createDirectory("'.dirPath$'/wordlists")
270         .dirPath$ > '.dirPath$'/wordlists/directory.txt
271         createDirectory("'.dirPath$'/pitchmodels")
272         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
273         createDirectory("'.dirPath$'/log")
274         .dirPath$ > '.dirPath$'/log/directory.txt
275         createDirectory("'.dirPath$'/TTS")
276         .dirPath$ > '.dirPath$'/TTS/directory.txt
277 endproc
280 # Retrieve and store setting between sessions
282 procedure read_preferences .preferencesFile$
283         if not fileReadable(.preferencesFile$)
284                 .preferencesFile$ = preferencesAppFile$
285         endif
286         if fileReadable(.preferencesFile$)
287                 Read from file... 'preferencesAppFile$'
288                 .preferenceTable$ = selected$("Table")
289                 .numPrefKeys = Get number of rows
290                 for .row to .numPrefKeys
291                         .variableName$ = Get value... '.row' Key
292                         if variableExists(.variableName$)
293                                 .variableValue = Get value... '.row' Value
294                                 if .variableValue <> undefined
295                                         '.variableName$' = '.variableValue'
296                                 endif
297                         elsif variableExists(.variableName$+"$")
298                                 .variableValue$ = Get value... '.row' Value
299                                 .variableName$ = .variableName$+"$"
300                                 '.variableName$' = "'.variableValue$'"
301                         endif
302                 endfor
303                 
304                 select Table '.preferenceTable$'
305                 Remove
306         endif
307 endproc
309 procedure write_preferences .preferencesFile$
310         Create Table with column names... Preferences 0 Key Value
311         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
312                 .preferencesFile$ = preferencesAppFile$
313         endif
314         .row = 0
315         
316         for .tableNum from 1 to 2
317                 if .tableNum = 1
318                         .table$ = "Config"
319                 elsif .tableNum = 2
320                         .table$ = "MainPage"
321                 endif
322                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
324                 select Table '.table$'
325                 .numTableRows = Get number of rows
326                 for .tablerow to .numTableRows
327                         select Table '.table$'
328                         .label$ = Get value... '.tablerow' Label
329                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
330                         .keyName$ = .variableName$
331                         .value$ = ""
332                         if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
333                                 .value$ = ""
334                         elsif variableExists(.variableName$)
335                                 .value = '.variableName$'
336                                 .value$ = "'.value'"
337                         elsif variableExists(.variableName$ + "$")
338                                 .variableName$ = .variableName$ + "$"
339                                 .value$ = '.variableName$'
340                         elsif index(.variableName$, "_") > 0
341                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
342                                 .keyName$ = .variableName$
343                                 select Table Preferences
344                                 .prefRow = Search column... Key '.keyName$'
345                                 if .prefRow <= 0
346                                         if variableExists(.variableName$)
347                                            .value = '.variableName$'
348                                            .value$ = "'.value'"
349                                         elsif variableExists(.variableName$ + "$")
350                                            .variableName$ = .variableName$ + "$"
351                                            .value$ = '.variableName$'
352                                         endif
353                                 endif
354                         endif
356                         if .value$ <> ""
357                                 select Table Preferences
358                                 Append row
359                                 .row += 1
360                                 Set string value... '.row' Key '.keyName$'
361                                 Set string value... '.row' Value '.value$'
362                         endif
363                 endfor
364         endfor
365         
366         # Some extra settings
367         select Table Preferences
368         Append row
369         .row += 1
370         Set string value... '.row' Key wordlistDir
371         Set string value... '.row' Value 'localWordlistDir$'
373         select Table Preferences
374         Append row
375         .row += 1
376         Set string value... '.row' Key wordlistName
377         Set string value... '.row' Value 'wordlistName$'
379         select Table Preferences
380         Write to table file... '.preferencesFile$'
381         Remove
383 endproc
385 ###############################################################
387 # TTS (speech synthesis). Place commands in user info
389 # Prerequisites
390 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
391 # speakCommandFile$ = ""
393 # Make sure sgc2.synthesizer is set!
394 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
395         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
396         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
397 endproc
399 procedure set_up_TTS
400         if sgc2.synthesizer > 0
401                 select sgc2.synthesizer
402                 Remove
403                 sgc2.synthesizer = -1
404         endif
405         # Note, the TTScommand must set sgc2.synthesizer!
406         if config.strict$ = "'sgc.highestLevel'"
407                 'sgc2.advancedTTScommand$'
408         else
409                 'sgc2.normalTTScommand$'
410         endif
412         if macintosh
413                 .ttscommand$ = "speak"
414                 .osName$ = "OSX"
415                 .quote$ = ""
416         elsif unix
417                 .ttscommand$ = "espeak"
418                 .osName$ = "UNIX"
419                 .quote$ = ""
420         elsif windows
421                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
422                 .osName$ = "WIN"
423                 .quote$ = """"
424         endif
425         # Global TTS command has precedence
426         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
427                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
428                 if config.synthesis$ = "_DISABLED_"
429                         # Default is ON
430                         config.synthesis$ = "eSpeak"
431                 endif
432         endif
434         # Local TTS command, if there is no global one
435         # Command does not exist yet, create it if eSpeak is installed
436         # Make sure to quote the path!
437         if speakCommandFile$ = ""
438                 # Autodetect synthesizer command
439 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
440 #                       .command_path$ = ""
441 #                       if macintosh
442 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
443 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
444 #                       elsif unix
445 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
446 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
447 #                       elsif windows
448 #                               if fileReadable(.ttscommand$)
449 #                                       .command_path$ = .ttscommand$
450 #                               endif
451 #                       endif
452 #                       # Remove any newlines
453 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
455 #                       # Command path found
456 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
457 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
458 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
459 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
460 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
461 #                       endif
462 #               endif
463 #               if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
464                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
465                 endif
466                 deleteFile("'speakCommandDir$'/command_path.txt")
467         endif
468         
469         # Close off (other TTS maybe a later option)
470         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
471                 config.synthesis$ = "eSpeak"
472         endif
473 endproc
475 # Alternative audio recorder
476 procedure set_up_recorder
477         if macintosh
478                 .osName$ = "OSX"
479                 .quote$ = ""
480         elsif unix
481                 .osName$ = "UNIX"
482                 .quote$ = ""
483         elsif windows
484                 .osName$ = "WIN"
485                 .quote$ = """"
486         endif
487         # Global TTS command has precedence
488         sgc.useAlternativeRecorder = 0
489         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
490                 createDirectory(sgc.scratchAudioDir$)
491                 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
492                 sgc.useAlternativeRecorder = 1
493         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
494                 createDirectory(sgc.scratchAudioDir$)
495                 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
496                 sgc.useAlternativeRecorder = 1
497         endif
498 endproc
500 # Alternative audio player
501 procedure set_up_player
502         if macintosh
503                 .osName$ = "OSX"
504                 .quote$ = ""
505         elsif unix
506                 .osName$ = "UNIX"
507                 .quote$ = ""
508         elsif windows
509                 .osName$ = "WIN"
510                 .quote$ = """"
511         endif
512         # Global TTS command has precedence
513         sgc.useAlternativePlayer = 0
514         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_play_command.txt")
515                 createDirectory(sgc.scratchAudioDir$)
516                 sgc.playCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_play_command.txt"
517                 sgc.useAlternativePlayer = 1
518         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_play_command.txt")
519                 createDirectory(sgc.scratchAudioDir$)
520                 sgc.playCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_play_command.txt"
521                 sgc.useAlternativePlayer = 1
522         endif
523 endproc
525 ###############################################################
527 # Evaluation tables
529 # Prerequisites
530 # initialiseSGC2.toneevaluation_table$ = ""
531 procedure initialize_toneevaluation_tables
532         # Remove old table
533         if initialiseSGC2.toneevaluation_table$ != ""
534                 select Table 'initialiseSGC2.toneevaluation_table$'
535                 Remove
536         endif
537         initialiseSGC2.toneevaluation_table$ = ""
538         
539         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
540                 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
541                 Rename: "Performance"
542                 # Check to make sure all columns are initialized, but only if Pinyin exists!
543                 select sgc2.performanceTable
544                 .column = Get column index: "Pinyin"
545                 if .column <= 0
546                         select sgc2.performanceTable
547                         Remove
548                         sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time
549                 endif
550                 .columnNameList$ = "Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time"
551                 while index_regex(.columnNameList$, "\S")
552                         .columnNameList$ = replace_regex$(.columnNameList$, "^\s+", "", 1)
553                         .columnHeader$ = extractWord$(.columnNameList$, "")
554                         .columnNameList$ = replace_regex$(.columnNameList$, "^\s*'.columnHeader$'\s*", "", 1)
555                         # Initialize header
556                         select sgc2.performanceTable
557                         .column = Get column index: .columnHeader$
558                         if .column <= 0
559                                 select sgc2.performanceTable
560                                 Append column: .columnHeader$
561                         endif
562                         
563                 endwhile
564         else
565                 # Create new table
566                 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time
567         endif
568         initialiseSGC2.toneevaluation_table$ = "Performance"
569         call get_toneevaluation_row Total
570 endproc
572 procedure get_toneevaluation_row .pinyin$
573         select Table 'initialiseSGC2.toneevaluation_table$'
574     .row = Search column... Pinyin '.pinyin$'
575         if .row < 1
576                 Append row
577                 .row = Get number of rows
578                 Set string value... '.row' Pinyin '.pinyin$'
579                 Set numeric value... '.row' Correct 0
580                 Set numeric value... '.row' Wrong 0
581                 Set numeric value... '.row' Total 0
582                 Set numeric value... '.row' High 0
583                 Set numeric value... '.row' Low 0
584                 Set numeric value... '.row' Wide 0
585                 Set numeric value... '.row' Narrow 0
586                 Set numeric value... '.row' Unknown 0
587                 Set numeric value... '.row' Commented 0
588                 Set numeric value... '.row' Level 0
589                 Set numeric value... '.row' Time 0
590                 Sort rows... Pinyin
591         .row = Search column... Pinyin '.pinyin$'
592         endif
593 endproc
595 procedure increment_toneevaluation_in_row .row .column$
596         select Table 'initialiseSGC2.toneevaluation_table$'
597         .tmp = Get value... '.row' '.column$'
598         if .tmp = undefined
599                 .tmp = 0
600         endif
601         .tmp += 1
602         Set numeric value... '.row' '.column$' '.tmp'
603         
604         # Set Level
605         Set string value: .row, "Level", config.strict$
606         
607         # Set time stamp
608         .dateTime$ = date$()
609         Set string value: .row, "Time", .dateTime$
610         
611 endproc
613 # Update existing performance file with toneevaluation
614 procedure update_toneevaluation_file
615         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
616                 select Table 'initialiseSGC2.toneevaluation_table$'
617                 Write to table file... 'sgc.savePerf$'
618         endif
619 endproc
621 procedure increment_toneevaluation_value .pinyin$ .column$
622         call get_toneevaluation_row '.pinyin$'
623         .row = get_toneevaluation_row.row
624         call increment_toneevaluation_in_row '.row' '.column$'
625         
626         call get_toneevaluation_row Total
627         .row = get_toneevaluation_row.row
628         call increment_toneevaluation_in_row '.row' '.column$'
629 endproc
631 procedure add_feedback_to_toneevaluation .table$
632     select Table '.table$'
633     .line1$ = Get value... 1 Text
634     .line2$ = Get value... 2 Text
635     .label$ = Get value... 3 Text
636         if .label$ = "6"
637                 .label$ = "Unknown"
638         endif
639         
640         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
641         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
642         
643         # Set evaluation
644         call increment_toneevaluation_value 'sgc.pinyin$' Total
645         if .tones$ = .recognized$
646                 call increment_toneevaluation_value 'sgc.pinyin$' Correct
647                 .result = 1
648         else
649                 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
650                 .result = 0
651         endif
652         if index_regex(.label$, "(Correct|Wrong)") <= 0
653                 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
654                 call increment_toneevaluation_value 'sgc.pinyin$' Commented
655                 .result = 0
656         endif
657         
658         # Update performance table when SaveAudio is on
659         if sgc.saveAudioOn
660                 call update_toneevaluation_file
661         endif
662 endproc
664 procedure setGrade .grade$
665         select Table 'wordlist$'
666         if recordedSound$ <> "" and sgc.pinyin$ <> ""           
667                 call get_toneevaluation_row 'sgc.pinyin$'
668                 .row = get_toneevaluation_row.row
669                 select Table 'initialiseSGC2.toneevaluation_table$'
670                 .col = Get column index... Grade
671                 if .col <= 0
672                         Insert column... 2 Grade
673                 endif
674                         
675                 Set numeric value: .row, "Grade", '.grade$'
676                 .numRows = Get number of rows
677                 .sumGrade = 0
678                 .sumN = 0
679                 for .r from 2 to .numRows
680                         .tmp = Get value: .r, "Grade"
681                         if .tmp <> undefined
682                                 .sumGrade += .tmp
683                                 .sumN += 1
684                         endif
685                 endfor
686                 .meanGrade = -1
687                 if .sumN > 0
688                         .meanGrade = .sumGrade / .sumN
689                 endif
690                 Set numeric value: 1, "Grade", .meanGrade
691                 
692                 # Store current performance table
693                 call update_toneevaluation_file
694         endif
695 endproc
697 # Set mainPage.saveAudio$ to Grade
698 procedure set_grade_display .pinyin$
699         mainPage.saveAudio$ = ""
700         select Table 'initialiseSGC2.toneevaluation_table$'
701         .grade$ = ""
702         .col = Get column index: "Grade"
703         if .pinyin$ <> "" and .col > 0
704                 call get_toneevaluation_row '.pinyin$'
705                 .row = get_toneevaluation_row.row
706                 if .row > 0
707                         .grade$ = Get value: .row, "Grade"
708                         if index_regex(.grade$, "[0-9]") <= 0
709                                 .grade$ = ""
710                         endif
711                 endif
712         endif
713         if .grade$ <> ""
714                 mainPage.saveAudio$ = .grade$
715         endif
716 endproc
717         
718 ###############################################################
720 # Miscelaneous procedures
722 ###############################################################
724 # TTS synthesis
725 procedure synthesize_sound .pinyin$
726         .command$ = ""
727         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
728                 .command$ < 'speakCommandFile$'
729                 .command$ = replace$(.command$, "'newline$'", " ", 0)
730                 if macintosh
731                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
732                 elsif unix
733                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
734                 elsif windows
735                         system_nocheck call '.command$' "'.pinyin$'"'
736                 endif
737         elsif sgc2.synthesizer > 0
738                 select sgc2.synthesizer
739                 Play text... '.pinyin$'
740         endif
741 endproc
743 procedure align_recordedSound .pinyin$
744         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
745                 select Sound 'recordedSound$'
746                 .recordedTextGrid = To TextGrid... pinyin
747                 Set interval text... 1 1 '.pinyin$'
748                 select sgc2.synthesizer
749                 plus Sound 'recordedSound$'
750                 plus .recordedTextGrid
751                 sgc.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
752                 Rename... AlignedTextGrid
753                 select .recordedTextGrid
754                 Remove
755         endif
756 endproc