4 # Praat script handling buttons page
6 # SpeakGoodChinese: MainPage.praat loads the code needed for the
7 # main, practice, page of SGC2 and the sound handling and recognition.
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
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.
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.
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
28 # Includes at the bottom
30 ###############################################################
32 # Button Drawing Routines
34 ###############################################################
36 procedure DrawPrevious .color$ .x .y .size
39 call drawTriangle -1 .x .y .size
40 .currentX = drawTriangle.currentX
41 .endX = .currentX - 0.5
44 demo Paint rectangle... '.color$' '.currentX' '.endX' '.lowY' '.highY'
47 procedure DrawNext .color$ .x .y .size
50 call drawTriangle 1 .x .y .size
51 .currentX = drawTriangle.currentX
52 .endX = .currentX + 0.5
55 demo Paint rectangle... '.color$' '.currentX' '.endX' '.lowY' '.highY'
58 procedure DrawWordListUp .color$ .x .y .size
67 demo Draw line... .xleft .ylow .xmidleft .yhigh
68 demo Draw line... .xright .ylow .xmidright .yhigh
69 demo Line width... 'defaultLineWidth'
73 procedure DrawWordListDown .color$ .x .y .size
82 demo Draw line... .xleft .yhigh .xmidleft .ylow
83 demo Draw line... .xright .yhigh .xmidright .ylow
84 demo Line width... 'defaultLineWidth'
88 procedure drawTriangle .direction .x .y .size
89 # Make sure direction = +/- 1
93 .direction /= abs(.direction)
96 .currentHeight = .size
97 .currentX = .x - .direction*.size
99 demo Line width... 2.0
101 while .currentHeight> 0
102 .ystart = .y + .currentHeight
103 .yend = .y - .currentHeight
104 demo Draw line... .currentX .ystart .currentX .yend
105 .currentHeight -= .offset *3/4
106 .currentX += .direction*.offset * 1.5
108 demo Line width... 'defaultLineWidth'
111 ###############################################################
113 # Obligatory button Drawing Routines
115 # These MUST be defined
117 ###############################################################
119 procedure DrawRecord .color$ .x .y .size
121 demo Paint circle... '.color$' '.x' '.y' '.size'
124 procedure DrawPlay .color$ .x .y .size
126 call drawTriangle 1 .x .y .size
129 procedure DrawQuit .color$ .x .y .size
130 demo Colour... '.color$'
131 .lineWidth = 0.5*.size**2
132 demo Line width... '.lineWidth'
137 demo Draw line... .xstart .ystart .xend .yend
142 demo Draw line... .xstart .ystart .xend .yend
143 demo Line width... 'defaultLineWidth'
147 procedure DrawConfig .color$ .x .y .size
149 .lineWidth = 0.4*.size
150 demo Arrow size... '.lineWidth'
151 .lineWidth = 0.4*.size**2
152 demo Line width... '.lineWidth'
156 demo Draw arrow... .xstart .y .xend .y
157 demo Line width... 'defaultLineWidth'
160 procedure DrawRefresh .color$ .x .y .size
161 .lineWidth = 0.5*.size**2
163 demo Line width... '.lineWidth'
164 demo Draw arc... '.x' '.y' '.size' 0 270
165 demo Line width... 'defaultLineWidth'
168 ###############################################################
170 # Button Processing Routines
172 ###############################################################
174 procedure processMainPageExample .clickX .clickY .pressed$
175 call generate_example
178 procedure processMainPagePrevious .clickX .clickY .pressed$
180 call display_text Grey
184 call wipeArea 'wipeFeedbackArea$'
185 call display_text Black
187 sgc.failedAttempts = 0
190 procedure processMainPageNext .clickX .clickY .pressed$
192 call display_text Grey
196 call wipeArea 'wipeFeedbackArea$'
197 call display_text Black
200 procedure processMainPageWordlistUp .clickX .clickY .pressed$
201 call wipeArea 'wipeFeedbackArea$'
202 call load_word_list "'localWordlistDir$'" -1
204 call display_text Black
207 procedure processMainPageWordlistDown .clickX .clickY .pressed$
208 call wipeArea 'wipeFeedbackArea$'
209 call load_word_list "'localWordlistDir$'" 1
211 call display_text Black
214 procedure processMainPageGRADE .clickX .clickY .pressed$
215 call setGrade '.pressed$'
220 procedure processMainPagePinYinArea .clickX .clickY .pressed$
223 call display_text Red
227 ###############################################################
229 # Obligatory button Processing Routines
231 # These MUST be defined
233 ###############################################################
235 procedure processMainPageQuit .clickX .clickY .pressed$
239 procedure processMainPageRefresh .clickX .clickY .pressed$
244 procedure processMainPageConfig .clickX .clickY .pressed$
248 procedure processMainPageHelp .clickX .clickY .pressed$
249 call help_loop 'buttons$' init_window
252 procedure processMainPagePlay .clickX .clickY .pressed$
255 if recordedSound$ <> ""
256 call play_sound 'sgc.recordedSound'
259 te.buttonPressValue = mainPage.play
262 procedure Set_Play_Button
264 if recordedSound$ <> ""
267 call Draw_button MainPage Play 'mainPage.play'
270 procedure processMainPageRecord .clickX .clickY .pressed$
272 .recordingTime = recordingTime
273 if count_syllables.number > 2
274 .recordingTime = recordingTime + 1.0*ceiling((count_syllables.number - 2)/2)
276 call record_sound '.recordingTime'
282 call wipeArea 'wipeContourArea$'
284 call draw_tone_contour
286 call display_word_list_name
287 call display_text Black
288 call add_feedback_to_toneevaluation Feedback
289 call write_feedback Feedback
290 select Table Feedback
293 # Do not exercise words that are going well (autoSelect)
294 if add_feedback_to_toneevaluation.result > 0
295 if config.adaptiveLists > 0 and sgc.failedAttempts < 2
296 # Deselect current word
297 select Table 'wordlist$'
298 .i = Search column: "Pinyin", sgc.pinyin$
300 Set string value: .i, "Show", "-"
303 sgc.failedAttempts = 0
305 sgc.failedAttempts += 1
310 ###############################################################
312 # Miscelaneous supporting code
314 ###############################################################
317 procedure generate_example
318 select Table 'wordlist$'
319 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
320 select Table 'wordlist$'
321 .sound$ = Get value... 'sgc.currentWord' Sound
322 sgc.pinyin$ = Get value... 'sgc.currentWord' Pinyin
323 if .sound$ = "-" or .sound$ = ""
324 .sound$ = sgc.pinyin$+".wav"
326 .soundFilePath$ = localWordlistDir$+"/"+wordlistName$+"/"+.sound$
327 .wordlistDirectory$ = ""
328 if localWordlistDir$ <> "" and fileReadable("'localWordlistDir$'/'wordlistName$'")
329 .wordlistDirectory$ = "'localWordlistDir$'/'wordlistName$'"
330 elsif sgc2wordlists$ <> "" and fileReadable("'sgc2wordlists$'/'wordlistName$'")
331 .wordlistDirectory$ = "'sgc2wordlists$'/'wordlistName$'"
332 elsif globalwordlists$ <> "" and fileReadable("'globalwordlists$'/'wordlistName$'")
333 .wordlistDirectory$ = "'globalwordlists$'/'wordlistName$'"
335 if .wordlistDirectory$ <> ""
336 .audioExampleList = Create Strings as file list... AudioList '.wordlistDirectory$'/'sgc.pinyin$'.*
337 .number_of_examples = Get number of strings
338 if .number_of_examples > 0
340 .sound$ = Get string... 1
341 .soundFilePath$ = "'.wordlistDirectory$'/'.sound$'"
345 if fileReadable(.soundFilePath$) and config.useSoundExample
347 .tmp = nocheck Read from file... '.soundFilePath$'
348 if .tmp != undefined and .tmp > 0
349 call play_sound '.tmp'
353 elsif config.synthesis$ <> "" and config.synthesis$ <> "_DISABLED_"
354 call synthesize_sound 'sgc.pinyin$'
356 call humToneContour 'sgc.pinyin$' 'config.register'
363 # Draw a tone contour
364 procedure draw_tone_contour
365 select Table 'wordlist$'
366 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
367 .sound$ = Get value... 'sgc.currentWord' Sound
368 sgc.pinyin$ = Get value... 'sgc.currentWord' Pinyin
369 call drawToneContour 'sgc.pinyin$' 'config.register'
372 if te.recordedPitch > 0
373 call drawSourceToneContour te.recordedPitch
379 procedure recognizeTone
380 select Table 'wordlist$'
381 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
383 if index_regex(config.strict$, "[^0-9]") <= 0
384 .strict = ('config.strict$' >= sgc.highestLevel)
387 .sound$ = Get value... 'sgc.currentWord' Sound
388 sgc.pinyin$ = Get value... 'sgc.currentWord' Pinyin
389 call align_recordedSound 'sgc.pinyin$'
390 call sgc_ToneProt 'recordedSound$' 'sgc.pinyin$' 'config.register' '.strict' 'config.language$'
391 # sgc_ToneProt manipulates the sound given. Reconnect
392 select Sound 'recordedSound$'
393 sgc.recordedSound = selected("Sound")
398 procedure write_feedback .table$
399 select Table '.table$'
400 .line1$ = Get value... 1 Text
401 .line2$ = Get value... 2 Text
402 .label$ = Get value... 3 Text
404 # convert numbers to Pinyin if needed
405 if not config.displayNumbers
406 call numbers2pinyin '.line1$'
407 .line1$ = numbers2pinyin.pinyin$
411 if index(.line1$, "???") > 0
413 elsif .label$ = "Correct"
415 elsif config.strict$ = "'sgc.highestLevel'"
416 .line2$ = .line2$ + " *"
419 .currentFeedbackFontSize = 14
421 call adjustFontSizeOnHeight 'defaultFont$' '.currentFeedbackFontSize' '.maxHeight'
422 .currentFeedbackFontSize = adjustFontSizeOnHeight.newFontSize
424 call wipeArea 'wipeFeedbackArea$'
425 call set_font_size '.currentFeedbackFontSize'
427 demo Text... 50 Centre 21 Bottom '.line1$'
428 demo Text... 50 Centre 17 Bottom '.line2$'
431 call set_font_size 'defaultFontSize'
435 procedure display_text .color$
436 select Table 'wordlist$'
437 if sgc.currentWord < 0 or sgc.currentWord > sgc.numberOfWords+1
439 if config.shuffleLists
442 if sgc.currentWord < 0
443 sgc.currentWord = sgc.numberOfWords
449 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
451 .displayPinyin$ = Get value... 'sgc.currentWord' Pinyin
452 .displayChar$ = Get value... 'sgc.currentWord' Character
453 .displayTrans$ = Get value... 'sgc.currentWord' Translation
454 if .displayPinyin$ <> "-" and (config.displayPinyin or sgc.writeAll)
455 if not config.displayNumbers
456 call numbers2pinyin '.displayPinyin$'
457 .displayPinyin$ = numbers2pinyin.pinyin$
460 .displayPinyin$ = replace_regex$(.displayPinyin$, "v", "\\u\X22", 0)
461 .displayText$ = .displayText$ + .displayPinyin$
463 if .displayChar$ <> "-" and (config.displayChar or sgc.writeAll)
464 .displayText$ = .displayText$ + " "+ .displayChar$
466 if .displayTrans$ <> "-" and (config.displayTrans or sgc.writeAll)
467 .displayText$ = .displayText$ + " \s{%%"+ .displayTrans$ + "%}"
469 elsif sgc.currentWord = 0 or sgc.currentWord = sgc.numberOfWords+1
471 .displayText$ = "---"
475 call adjustFontSizeOnHeight 'defaultFont$' 24 15
476 .currentFontSize = adjustFontSizeOnHeight.newFontSize
477 call adjustFontSizeOnWidth 'defaultFont$' '.currentFontSize' 95 '.displayText$'
478 .currentFontSize = adjustFontSizeOnWidth.newFontSize
480 # Clear the writing area
481 call wipeArea 'wipePinyinArea$'
482 # Actually display text
484 call set_font_size '.currentFontSize'
485 demo Text... 50 Centre 26 Bottom '.displayText$'
489 call set_font_size 'defaultFontSize'
492 procedure numbers2pinyin .numberstext$
493 .intermediatePinyin$ = .numberstext$
494 # Move numbers to the nucleus vowel
496 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "([aeuiov])([^aeuiov0-9]*)([0-9])", "\1\3\2", 0)
498 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "([ae])([aeuiov]*)([0-9])", "\1\3\2", 0)
500 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "(ou)([0-9])", "o\2u", 0)
501 # or the second vowel
502 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "([uiov][aeuiov])([uiov])([0-9])", "\1\3\2", 0)
504 # Convert all to special characters
506 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "a1", "ā", 0)
507 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "e1", "ē", 0)
508 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "u1", "ū", 0)
509 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "i1", "ī", 0)
510 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "o1", "ō", 0)
511 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "v1", "ǖ", 0)
512 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "1", "\\-^", 0)
515 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "v2", "ǘ", 0)
516 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "([iaeou])2", "\\\1'", 0)
517 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "2", "\\'^", 0)
520 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "a3", "ǎ", 0)
521 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "e3", "ě", 0)
522 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "u3", "ǔ", 0)
523 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "i3", "ǐ", 0)
524 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "o3", "ǒ", 0)
525 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "v3", "ǚ", 0)
526 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "3", "\\N^", 0)
529 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "v4", "ǜ", 0)
530 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "([iaeou])4", "\\\1`", 0)
531 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "4", "\\`^", 0)
534 # Remove tone 0 symbol completely
535 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "0", "", 0)
536 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "a0", "å", 0)
537 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "e0", "e̊", 0)
538 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "u0", "ů", 0)
539 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "i0", "i̊", 0)
540 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "o0", "o̊", 0)
541 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "v0", "ü̊", 0)
542 #.intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "0", "\\0^", 0)
544 # Pick best vowel symbols available in cases not caught before
545 # Ugly clutch to get the 1, 3, 0 tone diacritics at least in the neighbourhood
546 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "i(\\[-N0]\^)", "i\\s{_ }\1", 0)
548 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "v", "\\u\X22", 0)
550 .pinyin$ = .intermediatePinyin$
553 # NEEDS WORK AND TESTING!!
554 # Convert unicode Pinyin into tone numbers
555 procedure pinyin2numbers .pinyin$
556 .intermediatePinyin$ = .pinyin$
557 # Convert all special characters to numbers
559 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ā([iaeouü]*)", "a\11", 0)
560 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ē([iaeouü]*)", "e\11", 0)
561 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ū([iaeouü]*)", "u\11", 0)
562 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ī([iaeouü]*)", "i\11", 0)
563 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ō([iaeouü]*)", "o\11", 0)
564 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ǖ([iaeouü]*)", "v\11", 0)
567 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "á([iaeouü]*)", "a\12", 0)
568 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "é([iaeouü]*)", "e\12", 0)
569 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ú([iaeouü]*)", "u\12", 0)
570 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "í([iaeouü]*)", "i\12", 0)
571 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ó([iaeouü]*)", "o\12", 0)
572 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ǘ([iaeouü]*)", "v\12", 0)
575 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ǎ([iaeouü]*)", "a\13", 0)
576 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ě([iaeouü]*)", "e\13", 0)
577 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ǔ([iaeouü]*)", "u\13", 0)
578 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ǐ([iaeouü]*)", "i\13", 0)
579 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ǒ([iaeouü]*)", "o\13", 0)
580 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ǚ([iaeouü]*)", "v\13", 0)
583 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "à([iaeouü]*)", "a\14", 0)
584 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "è([iaeouü]*)", "e\14", 0)
585 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ù([iaeouü]*)", "u\14", 0)
586 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ì([iaeouü]*)", "i\14", 0)
587 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ò([iaeouü]*)", "o\14", 0)
588 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ǜ([iaeouü]*)", "v\14", 0)
592 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "å([iaeouü]*)", "a\10", 0)
593 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "e̊([iaeouü]*)", "e\10", 0)
594 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ů([iaeouü]*)", "u\10", 0)
595 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "i̊([iaeouü]*)", "i\10", 0)
596 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "o̊([iaeouü]*)", "o\10", 0)
597 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "ü̊([iaeouü]*)", "v\10", 0)
599 # Syllables without a tone symbol are tone 0
600 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "([aeuiov]+)([^0-9aeuiov]|$)", "\10\2", 0)
602 # Move numbers to the end of the syllable
603 .intermediatePinyin$ = replace_regex$(.intermediatePinyin$, "([aeuiov])([0-9])((ng?)([^aeuiov]|$))?", "\1\3\2\4", 0)
605 .numberstext$ = .intermediatePinyin$
609 include ToneProt/SGC_ToneProt.praat
610 include ToneProt/DrawToneContour.praat
611 include ToneProt/HumToneContour.praat
612 include ToneProt/ToneRecognition.praat
613 include ToneProt/ToneScript.praat
614 include ToneProt/ToneRules.praat