Removed interference between SaveAudio and Read performance
[sgc2.git] / InitialiseSGC2.praat
bloba1c40dccebb50d0470c731bf71399205eaeece7e
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 # Information for logging
30 currentDate$ = date$()
31 dateYear$ = right$(currentDate$, length(currentDate$) - rindex(currentDate$, " "))
32 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
33 currentLogDirectory$ = ""
34 feedbackTablePrefix$ = "Feedback"
35 feedbackTableName$ = ""
37 # Define canvas
38 viewportMargin = 5
39 yWordlist = 11
40 yFeedback = yWordlist + 5
41 yPinyin = yFeedback + 10
42 yContour = yPinyin + 15
43 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
44 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
45 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
46 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
48 # Pop-Up window colors
49 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
50 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
52 # Initialize parameters
53 alertText$ = ""
54 te.currentWord = 1
55 te.numberOfWords = 0
56 te.recordedSound = 0
57 sgc.saveAudioOn = 0
58 pinyin$ = ""
59 sgc.pinyin$ = ""
60 character$ = ""
61 buttons$ = ""
62 config$ = ""
63 wordlistNum = 1
64 wordlistName$ = ""
65 wordlist$ = ""
66 mainPage.play = -1
67 config.deleteWordlist = -1
68 config.displayNumbers = 1
69 config.displayPinyin = 1
70 config.displayChar = 0
71 config.displayTrans = 1
72 config.useSoundExample = 1
73 config.synthesis$ = "_DISABLED_"
74 config.input$ = "Microphone"
75 config.showBackground = 1
76 sgc2.noiseThresshold = -30
77 config.strict = 0
78 sgc.savePerf$ = ""
79 sgc.saveAudio$ = ""
81 sgc_ToneProt.minimumPitch = 40
82 sgc_ToneProt.maximumPitch = 600
84 # Platform dependent settings
85 if macintosh or windows
86         config.displayChar = 1
87         config.displayNumbers = 0
88 endif
89 if windows and endsWith(build_SHA$, " XP")
90         config.displayChar = 0
91         config.displayNumbers = 0
92 endif
93 if unix
94         config.displayChar = 0
95         config.displayNumbers = 1
96 endif
98 if unix
99         samplingFrequency = 44100
100 elsif macintosh
101         samplingFrequency = 44100
102 elsif windows
103         samplingFrequency = 44100
104 endif
105 recordingTime = 2
107 logging = 0
108 config.language$ = "EN"
109 config.register = 249
110 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
111 preferencesLogDir$ = "'preferencesAppDir$'/log"
112 preferencesTableDir$ = "'preferencesAppDir$'/Data"
113 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
114 localWordlistDir$ = sgc2wordlists$
116 # Global word lists must be installed BY THE ADMINISTRATOR
117 # This means, create the directory 'globalwordlists$' and
118 # fill it with wordlist directories containing wordlist.Table
119 # and audio files. They can be copied from a local directory.
120 globalwordlists$ = ""
121 globaltablelists$ = ""
122 globalTTSdir$ = ""
123 if unix
124         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
125         globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"     
126         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
127 elsif macintosh
128         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
129         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
130         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
131 elsif windows
132         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
133         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
134         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
135 endif
136 sgc2.synthesizer = -1
137 # Define a Praat TTS command that will set sgc2.synthesizer
138 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
139 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
140 sgc2.alignedTextGrid = -1
142 ##############################################################
144 config.shuffleLists = 1
145 defaultFontSize = 12
146 defaultFont$ = "Helvetica"
147 defaultLineWidth = 1
149 # Set up directories if they do not exist already
150 call set_up_directories
152 # Get saved preferences
153 call read_preferences ""
155 # Set up TTS system
156 speakCommandDir$ = "'preferencesAppDir$'/TTS"
157 speakCommandFile$ = ""
158 call set_up_TTS
160 # Set inital language
161 call set_language 'config.language$'
163 # Get the word-list
164 call load_word_list "'localWordlistDir$'" 0
166 # Set up evaluation table
167 initialiseSGC2.toneevaluation_table$ = ""
168 call initialize_toneevaluation_tables
170 # Draw inital window
171 call init_window
173 ###############################################################
175 # Obligatory procedures
177 ###############################################################
178 # Initialize Demo Window
179 procedure init_window
180     demo Erase all
181         demo Line width... 'defaultLineWidth'
182         demo 'defaultFont$'
183         call set_font_size 'defaultFontSize'
184         demo Black
185         
186         # If there is no recorded sound, try to read stored recording
187         if recordedSound$ = "" and sgc.saveAudio$ <> ""
188                 .pinyin$ = ""
189                 select Table 'wordlist$'
190                 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
191                         .pinyin$ = Get value... 'te.currentWord' Pinyin
192                         .outputName$ = "'sgc.saveAudio$'/'.pinyin$'.wav"
193                         if fileReadable(.outputName$)
194                                 te.recordedSound = Read from file: .outputName$
195                                 recordedSound$ = selected$("Sound")
196                                 call recognizeTone
197                         endif
198                 endif
199         endif
200         
201         # Update screen
202         call reset_viewport
203         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
204         call set_window_title 'buttons$' '.windowTitle$'
205         # Display the word-list
206         call write_word_list
207     # Wipe screen and draw background
208     call wipeArea 'wipeContourArea$'
209         if config.showBackground
210                 call draw_background Background
211         endif
212     # Define buttons in a table
213     call init_buttons
214     # Set Play button
215         call Set_Play_Button
216     
217         # Draw the contour
218         call draw_tone_contour
219         # SaveAudio light
220         call paint_saveAudio_light
221         # Set play button
222         
223 endproc
225 # Make sure all Preferences directories are available
226 procedure set_up_directories
227         .dirPath$ = "'preferencesAppDir$'"
228         createDirectory(.dirPath$)
229         .dirPath$ > '.dirPath$'/directory.txt
230         createDirectory("'.dirPath$'/wordlists")
231         .dirPath$ > '.dirPath$'/wordlists/directory.txt
232         createDirectory("'.dirPath$'/pitchmodels")
233         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
234         createDirectory("'.dirPath$'/log")
235         .dirPath$ > '.dirPath$'/log/directory.txt
236         createDirectory("'.dirPath$'/TTS")
237         .dirPath$ > '.dirPath$'/TTS/directory.txt
238 endproc
241 # Retrieve and store setting between sessions
243 procedure read_preferences .preferencesFile$
244         if not fileReadable(.preferencesFile$)
245                 .preferencesFile$ = preferencesAppFile$
246         endif
247         if fileReadable(.preferencesFile$)
248                 Read from file... 'preferencesAppFile$'
249                 .preferenceTable$ = selected$("Table")
250                 .numPrefKeys = Get number of rows
251                 for .row to .numPrefKeys
252                         .variableName$ = Get value... '.row' Key
253                         if variableExists(.variableName$)
254                                 .variableValue = Get value... '.row' Value
255                                 if .variableValue <> undefined
256                                         '.variableName$' = '.variableValue'
257                                 endif
258                         elsif variableExists(.variableName$+"$")
259                                 .variableValue$ = Get value... '.row' Value
260                                 .variableName$ = .variableName$+"$"
261                                 '.variableName$' = "'.variableValue$'"
262                         endif
263                 endfor
264                 
265                 select Table '.preferenceTable$'
266                 Remove
267         endif
268 endproc
270 procedure write_preferences .preferencesFile$
271         Create Table with column names... Preferences 0 Key Value
272         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
273                 .preferencesFile$ = preferencesAppFile$
274         endif
275         .row = 0
276         
277         for .tableNum from 1 to 2
278                 if .tableNum = 1
279                         .table$ = "Config"
280                 elsif .tableNum = 2
281                         .table$ = "MainPage"
282                 endif
283                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
285                 select Table '.table$'
286                 .numTableRows = Get number of rows
287                 for .tablerow to .numTableRows
288                         select Table '.table$'
289                         .label$ = Get value... '.tablerow' Label
290                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
291                         .keyName$ = .variableName$
292                         .value$ = ""
293                         if variableExists(.variableName$)
294                                 .value = '.variableName$'
295                                 .value$ = "'.value'"
296                         elsif variableExists(.variableName$ + "$")
297                                 .variableName$ = .variableName$ + "$"
298                                 .value$ = '.variableName$'
299                         elsif index(.variableName$, "_") > 0
300                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
301                                 .keyName$ = .variableName$
302                                 select Table Preferences
303                                 .prefRow = Search column... Key '.keyName$'
304                                 if .prefRow <= 0
305                                         if variableExists(.variableName$)
306                                            .value = '.variableName$'
307                                            .value$ = "'.value'"
308                                         elsif variableExists(.variableName$ + "$")
309                                            .variableName$ = .variableName$ + "$"
310                                            .value$ = '.variableName$'
311                                         endif
312                                 endif
313                         endif
315                         if .value$ <> ""
316                                 select Table Preferences
317                                 Append row
318                                 .row += 1
319                                 Set string value... '.row' Key '.keyName$'
320                                 Set string value... '.row' Value '.value$'
321                         endif
322                 endfor
323         endfor
324         
325         # Some extra settings
326         select Table Preferences
327         Append row
328         .row += 1
329         Set string value... '.row' Key wordlistDir
330         Set string value... '.row' Value 'localWordlistDir$'
332         select Table Preferences
333         Append row
334         .row += 1
335         Set string value... '.row' Key wordlistName
336         Set string value... '.row' Value 'wordlistName$'
338         select Table Preferences
339         Write to table file... '.preferencesFile$'
340         Remove
342 endproc
344 ###############################################################
346 # TTS (speech synthesis). Place commands in user info
348 # Prerequisites
349 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
350 # speakCommandFile$ = ""
352 # Make sure sgc2.synthesizer is set!
353 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
354         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
355         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
356 endproc
358 procedure set_up_TTS
359         if sgc2.synthesizer > 0
360                 select sgc2.synthesizer
361                 Remove
362                 sgc2.synthesizer = -1
363         endif
364         # Note, the TTScommand must set sgc2.synthesizer!
365         if config.strict
366                 'sgc2.advancedTTScommand$'
367         else
368                 'sgc2.normalTTScommand$'
369         endif
371         if macintosh
372                 .ttscommand$ = "speak"
373                 .osName$ = "OSX"
374                 .quote$ = ""
375         elsif unix
376                 .ttscommand$ = "espeak"
377                 .osName$ = "UNIX"
378                 .quote$ = ""
379         elsif windows
380                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
381                 .osName$ = "WIN"
382                 .quote$ = """"
383         endif
384         # Global TTS command has precedence
385         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
386                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
387                 if config.synthesis$ = "_DISABLED_"
388                         # Default is ON
389                         config.synthesis$ = "eSpeak"
390                 endif
391         endif
393         # Local TTS command, if there is no global one
394         # Command does not exist yet, create it if eSpeak is installed
395         # Make sure to quote the path!
396         if speakCommandFile$ = ""
397                 # Autodetect synthesizer command
398 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
399 #                       .command_path$ = ""
400 #                       if macintosh
401 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
402 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
403 #                       elsif unix
404 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
405 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
406 #                       elsif windows
407 #                               if fileReadable(.ttscommand$)
408 #                                       .command_path$ = .ttscommand$
409 #                               endif
410 #                       endif
411 #                       # Remove any newlines
412 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
414 #                       # Command path found
415 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
416 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
417 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
418 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
419 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
420 #                       endif
421 #               endif
422 #               if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
423                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
424                 endif
425                 deleteFile("'speakCommandDir$'/command_path.txt")
426         endif
427         
428         # Close off (other TTS maybe a later option)
429         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
430                 config.synthesis$ = "eSpeak"
431         endif
432 endproc
434 ###############################################################
436 # Evaluation tables
438 # Prerequisites
439 # initialiseSGC2.toneevaluation_table$ = ""
440 procedure initialize_toneevaluation_tables
441         # Remove old table
442         if initialiseSGC2.toneevaluation_table$ != ""
443                 select Table 'initialiseSGC2.toneevaluation_table$'
444                 Remove
445         endif
446         initialiseSGC2.toneevaluation_table$ = ""
447         
448         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
449                 sgc2.performanceTable = Read from file: sgc.savePerf$
450                 Rename: "Performance"
451         else
452                 # Create new table
453                 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Time
454         endif
455         initialiseSGC2.toneevaluation_table$ = "Performance"
456         call get_toneevaluation_row Total
457 endproc
459 procedure get_toneevaluation_row .pinyin$
460         select Table 'initialiseSGC2.toneevaluation_table$'
461     .row = Search column... Pinyin '.pinyin$'
462         if .row < 1
463                 Append row
464                 .row = Get number of rows
465                 Set string value... '.row' Pinyin '.pinyin$'
466                 Set numeric value... '.row' Correct 0
467                 Set numeric value... '.row' Wrong 0
468                 Set numeric value... '.row' Total 0
469                 Set numeric value... '.row' High 0
470                 Set numeric value... '.row' Low 0
471                 Set numeric value... '.row' Wide 0
472                 Set numeric value... '.row' Narrow 0
473                 Set numeric value... '.row' Unknown 0
474                 Set numeric value... '.row' Commented 0
475                 Set numeric value... '.row' Time 0
476                 Sort rows... Pinyin
477         .row = Search column... Pinyin '.pinyin$'
478         endif
479 endproc
481 procedure increment_toneevaluation_in_row .row .column$
482         select Table 'initialiseSGC2.toneevaluation_table$'
483         .tmp = Get value... '.row' '.column$'
484         .tmp += 1
485         Set numeric value... '.row' '.column$' '.tmp'
486         
487         # Set time stamp
488         .dateTime$ = date$()
489         Set string value: .row, "Time", .dateTime$
490 endproc
492 procedure increment_toneevaluation_value .pinyin$ .column$
493         call get_toneevaluation_row '.pinyin$'
494         .row = get_toneevaluation_row.row
495         call increment_toneevaluation_in_row '.row' '.column$'
496         
497         call get_toneevaluation_row Total
498         .row = get_toneevaluation_row.row
499         call increment_toneevaluation_in_row '.row' '.column$'
500 endproc
502 procedure add_feedback_to_toneevaluation .table$
503     select Table '.table$'
504     .line1$ = Get value... 1 Text
505     .line2$ = Get value... 2 Text
506     .label$ = Get value... 3 Text
507         if .label$ = "6"
508                 .label$ = "Unknown"
509         endif
510         
511         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
512         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
513         
514         # Set evaluation
515         call increment_toneevaluation_value 'sgc.pinyin$' Total
516         if .tones$ = .recognized$
517                 call increment_toneevaluation_value 'sgc.pinyin$' Correct
518         else
519                 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
520         endif
521         if index_regex(.label$, "(Correct|Wrong)") <= 0
522                 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
523                 call increment_toneevaluation_value 'sgc.pinyin$' Commented
524         endif
525 endproc
527 procedure setGrade .grade$
528         select Table 'wordlist$'
529         if recordedSound$ <> "" and sgc.pinyin$ <> ""           
530                 call get_toneevaluation_row 'sgc.pinyin$'
531                 .row = get_toneevaluation_row.row
532                 select Table 'initialiseSGC2.toneevaluation_table$'
533                 .col = Get column index... Grade
534                 if .col <= 0
535                         Insert column... 2 Grade
536                 endif
537                         
538                 Set numeric value: .row, "Grade", '.grade$'
539                 .numRows = Get number of rows
540                 .sumGrade = 0
541                 .sumN = 0
542                 for .r from 2 to .numRows
543                         .tmp = Get value: .r, "Grade"
544                         if .tmp <> undefined
545                                 .sumGrade += .tmp
546                                 .sumN += 1
547                         endif
548                 endfor
549                 .meanGrade = -1
550                 if .sumN > 0
551                         .meanGrade = .sumGrade / .sumN
552                 endif
553                 Set numeric value: 1, "Grade", .meanGrade
554                 
555         endif
556 endproc
558 ###############################################################
560 # Miscelaneous procedures
562 ###############################################################
564 # TTS synthesis
565 procedure synthesize_sound .pinyin$
566         .command$ = ""
567         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
568                 .command$ < 'speakCommandFile$'
569                 .command$ = replace$(.command$, "'newline$'", " ", 0)
570                 if macintosh
571                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
572                 elsif unix
573                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
574                 elsif windows
575                         system_nocheck call '.command$' "'.pinyin$'"'
576                 endif
577         elsif sgc2.synthesizer > 0
578                 select sgc2.synthesizer
579                 Play text... '.pinyin$'
580         endif
581 endproc
583 procedure align_recordedSound .pinyin$
584         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
585                 select Sound 'recordedSound$'
586                 .recordedTextGrid = To TextGrid... pinyin
587                 Set interval text... 1 1 '.pinyin$'
588                 select sgc2.synthesizer
589                 plus Sound 'recordedSound$'
590                 plus .recordedTextGrid
591                 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
592                 Rename... AlignedTextGrid
593                 select .recordedTextGrid
594                 Remove
595         endif
596 endproc