6 # SpeakGoodChinese: sgc2.praat is the master GUI of SpeakGoodChinese
7 # It is written in Praat script for the Demo window
9 # Copyright (C) 2007-2010 R.J.J.H. van Son
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 include ToneProt/ToneRecognition.praat
30 demo Select outer viewport... 0 100 0 100
31 demo Axes... 0 100 0 100
33 # Initialize parameters
43 wordlistDir$ = "wordlists"
52 logDir$ = "'preferencesDirectory$'/log"
53 sgc2wordlists$ = "'preferencesDirectory$'/sgc2/wordlists"
56 defaultFont$ = "Helvetica"
59 call set_up_directories
60 call read_preferences ""
63 call set_language 'language$'
65 call load_word_list "'preferencesDirectory$'/sgc2/wordlists" "'wordlistDir$'" 0
69 while demoWaitForInput()
74 call buttonClicked 'buttons$' '.clickX' '.clickY'
75 .label$ = buttonClicked.label$
76 elsif demoKeyPressed()
77 .pressed$ = demoKey$()
78 call keyPressed 'buttons$' '.pressed$'
79 .label$ = keyPressed.label$
85 call Draw_button 'buttons$' '.label$' 1
86 call process_label '.label$'
88 call Draw_button 'buttons$' '.label$' 0
95 ########################################################
97 # Definitions of procedures
99 ########################################################
102 procedure process_label .label$
105 elsif .label$ = "Config"
107 elsif .label$ = "Refresh"
109 elsif .label$ = "Record"
112 call write_feedback Feedback
113 elsif .label$ = "Play"
114 call play_sound 'sampleSound$'
115 elsif .label$ = "Example"
116 call generate_example
117 elsif .label$ = "Previous"
118 call display_text Grey
120 call display_text Black
122 call draw_tone_contour
123 elsif .label$ = "Next"
124 call display_text Grey
126 call display_text Black
128 call draw_tone_contour
129 elsif .label$ = "Wordlist"
130 call load_word_list "'preferencesDirectory$'/sgc2/wordlists" "'wordlistDir$'" 1
137 # Initialize Demo Window
138 procedure init_window
140 demoWindowTitle("Speak Good Chinese: Practice your tones")
141 demo Line width... 'defaultLineWidth'
143 demo 'defaultFontSize'
145 demo Select outer viewport... 0 100 0 100
146 demo Axes... 0 100 0 100
147 # Define buttons in a table
149 # Display the word-list
154 procedure init_buttons
155 call Draw_all_buttons 'buttons$'
159 procedure Draw_all_buttons .table$
160 select Table '.table$'
161 .numRows = Get number of rows
164 .label$ = Get value... '.row' Label
165 call Draw_button '.table$' '.label$' 0
169 # Draw a button from a predefined button table
170 procedure Draw_button .table$ .label$ .push
171 select Table '.table$'
172 .row = Search column... Label '.label$'
174 exit Button Table '.table$' does not have a row with label '.label$'
177 # Set drawing parameters
178 .topBackGroundColorUp$ = "{0.97,0.97,0.97}"
179 .topLineColorUp$ = "Black"
180 .topLineWidthUp = 1.5
181 .topBackGroundColorDown$ = "{0.92,0.92,0.92}"
182 .topLineColorDown$ = "Grey"
183 .topLineWidthDown = 1.5
184 .flankBackGroundColorUp$ = "Silver"
185 .flankLineColorUp$ = "Black"
186 .flankLineWidthUp = 1.5
187 .flankBackGroundColorDown$ = "White"
188 .flankLineColorDown$ = "Black"
189 .flankLineWidthDown = 1.5
190 .buttonFontSize = defaultFontSize
193 .leftX = Get value... '.row' LeftX
194 .rightX = Get value... '.row' RightX
195 .lowY = Get value... '.row' LowY
196 .highY = Get value... '.row' HighY
197 .buttonText$ = Get value... '.row' Text
198 .buttonColor$ = Get value... '.row' Color
199 .buttonDraw$ = Get value... '.row' Draw
200 .buttonKey$ = Get value... '.row' Key
202 # Adapt size of button to length of text
203 demo Font size... '.buttonFontSize'
204 .buttonTextWidth = demo Text width (wc)... '.buttonText$'
206 while (.rightX - .leftX) < .buttonTextWidth + 2
207 if .buttonFontSize > 6
209 demo Font size... '.buttonFontSize'
211 .diff = (.buttonTextWidth + 2 - (.rightX - .leftX))/2
216 .buttonTextWidth = demo Text width (wc)... '.buttonText$'
223 .rightX = round(.rightX)
224 .leftX = round(.leftX)
226 # Reset and erase button area
227 demo Select outer viewport... 0 100 0 100
228 demo Axes... 0 100 0 100
229 demo Line width... 'defaultLineWidth'
230 demo Paint rectangle... White .leftX .rightX .lowY .highY
232 # Give some depth to button: Draw flank outline
235 .shiftLeftX = .leftX + .shiftX
236 .shiftRightX = .rightX + .shiftX
237 .shiftLowY = .lowY + .shiftY
238 .shiftHighY = .highY + .shiftY
240 demo Paint rounded rectangle... '.flankBackGroundColorUp$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY 3
241 demo Colour... '.flankLineColorUp$'
242 demo Line width... '.flankLineWidthUp'
244 demo Paint rounded rectangle... '.flankBackGroundColorDown$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY 3
245 demo Colour... '.flankLineColorDown$'
246 demo Line width... '.flankLineWidthDown'
248 demo Draw rounded rectangle... .shiftLeftX .shiftRightX .shiftLowY .shiftHighY 3
250 # Draw the button top
252 demo Paint rounded rectangle... '.topBackGroundColorUp$' .leftX .rightX .lowY .highY 3
253 demo Colour... '.topLineColorUp$'
254 demo Line width... '.topLineWidthUp'
256 demo Paint rounded rectangle... '.topBackGroundColorDown$' .leftX .rightX .lowY .highY 3
257 demo Colour... '.topLineColorDown$'
258 demo Line width... '.topLineWidthDown'
260 demo Draw rounded rectangle... .leftX .rightX .lowY .highY 3
262 # The button text and symbol
263 .centerX = (.leftX + .rightX)/2
264 .centerY = (.lowY + .highY)/2
265 .radius = (.highY - .lowY )/4
266 .newText$ = replace_regex$(.buttonText$, "['.buttonKey$']", "#%&", 1)
268 .newText$ = .buttonText$
272 if .buttonColor$ = "Red"
273 .buttonColor$ = "Pink"
275 .buttonColor$ = "Grey"
278 .buttonColor$ = "Maroon"
283 demo Colour... '.buttonColor$'
284 demo Font size... '.buttonFontSize'
285 demo Text... '.centerX' Centre '.lowY' Bottom '.newText$'
286 demo Font size... 'defaultFontSize'
288 call '.buttonDraw$' '.buttonColor$' '.centerX' '.centerY' '.radius'
292 demo Line width... 'defaultLineWidth'
297 procedure display_text .color$
298 select Table 'wordlist$'
299 if currentWord < 0 or currentWord > numberOfWords+1
304 currentWord = numberOfWords
310 if currentWord > 0 and currentWord <= numberOfWords
312 .displayPinyin$ = Get value... 'currentWord' Pinyin
313 .displayChar$ = Get value... 'currentWord' Character
314 if .displayPinyin$ <> "-" and displayPinyin
315 .displayText$ = .displayText$ + .displayPinyin$
317 if .displayChar$ <> "-" and displayChar
318 .displayText$ = .displayText$ + " "+ .displayChar$
320 elsif currentWord = 0 or currentWord = numberOfWords+1
321 .displayText$ = "---"
323 demo Paint rectangle... White 20 80 25 40
324 demo Paint rectangle... White 10 90 19 30
327 demo Text... 50 Centre 25 Bottom '.displayText$'
330 demo 'defaultFontSize'
335 procedure generate_example
336 select Table 'wordlist$'
337 if currentWord > 0 and currentWord <= numberOfWords
338 select Table 'wordlist$'
339 .sound$ = Get value... 'currentWord' Sound
340 .pinyin$ = Get value... 'currentWord' Pinyin
341 .soundFilePath$ = wordlistDir$+"/"+wordlistName$+"/"+.sound$
342 if fileReadable("'sgc2wordlists$'/'wordlistName$'/'.sound$'")
343 .soundFilePath$ = "'sgc2wordlists$'/'wordlistName$'/'.sound$'"
345 if fileReadable(.soundFilePath$) and useSoundExample
346 Read from file... '.soundFilePath$'
350 execute "ToneProt/HumToneContour.praat" '.pinyin$' 'register'
357 ###############################################################
359 # Button Drawing Routines
361 ###############################################################
363 # A stub for buttons that do not have a drawing routine (yet)
364 procedure DrawNull .color$ .x .y .size
367 procedure DrawRecord .color$ .x .y .size
369 demo Paint circle... '.color$' '.x' '.y' '.size'
372 procedure DrawPlay .color$ .x .y .size
374 call drawTriangle 1 .x .y .size
377 procedure DrawPrevious .color$ .x .y .size
380 call drawTriangle -1 .x .y .size
381 .currentX = drawTriangle.currentX
382 .endX = .currentX - 0.5
385 demo Paint rectangle... '.color$' '.currentX' '.endX' '.lowY' '.highY'
388 procedure DrawNext .color$ .x .y .size
391 call drawTriangle 1 .x .y .size
392 .currentX = drawTriangle.currentX
393 .endX = .currentX + 0.5
396 demo Paint rectangle... '.color$' '.currentX' '.endX' '.lowY' '.highY'
399 procedure DrawQuit .color$ .x .y .size
400 demo Line width... 4.0
405 demo Draw line... .xstart .ystart .xend .yend
410 demo Draw line... .xstart .ystart .xend .yend
411 demo Line width... 'defaultLineWidth'
414 procedure DrawConfig .color$ .x .y .size
416 demo Line width... 3.0
419 demo Draw arrow... .xstart .y .xend .y
420 demo Line width... 'defaultLineWidth'
423 procedure DrawRefresh .color$ .x .y .size
425 demo Line width... 4.0
426 demo Draw arc... '.x' '.y' '.size' 0 270
427 demo Line width... 'defaultLineWidth'
430 procedure DrawWordList .color$ .x .y .size
434 .xmidright = .x - 0.1
439 demo Draw line... .xleft .yhigh .xmidleft .y
440 demo Draw line... .xright .yhigh .xmidright .y
441 demo Line width... 'defaultLineWidth'
445 procedure drawTriangle .direction .x .y .size
446 # Make sure direction = +/- 1
450 .direction /= abs(.direction)
453 .currentHeight = .size
454 .currentX = .x - .direction*.size
456 demo Line width... 2.0
458 while .currentHeight> 0
459 .ystart = .y + .currentHeight
460 .yend = .y - .currentHeight
461 demo Draw line... .currentX .ystart .currentX .yend
462 .currentHeight -= .offset *3/4
463 .currentX += .direction*.offset * 1.5
465 demo Line width... 'defaultLineWidth'
468 ###############################################################
470 # Button Processing Routines
472 ###############################################################
474 # Search row in table on label
475 procedure findLabel .table$ .label$
477 select Table '.table$'
478 .to$ = selected$("Table")
480 .numRows = Get number of rows
482 .currentKey$ = '.to$'$[.i, "Key"]
483 if .label$ = .currentKey$
492 procedure buttonClicked table$ .x .y
494 select Table 'table$'
495 .bo$ = selected$("Table")
497 .numRows = Get number of rows
500 .leftX = '.bo$'[.i, "LeftX"]
501 .rightX = '.bo$'[.i, "RightX"]
502 .lowY = '.bo$'[.i, "LowY"]
503 .highY = '.bo$'[.i, "HighY"]
504 if .x > .leftX and .x < .rightX and .y > .lowY and .y < .highY
505 .label$ = '.bo$'$[.i, "Label"]
511 procedure keyPressed table$ .pressed$
513 .lowerPressed$ = replace_regex$(.pressed$, ".", "\L&", 0)
514 .upperPressed$ = replace_regex$(.pressed$, ".", "\U&", 0)
515 select Table 'table$'
516 .bo$ = selected$("Table")
518 .numRows = Get number of rows
521 .key$ = '.bo$'$[.i, "Key"]
522 if .key$ = .lowerPressed$ or .key$ = .upperPressed$
523 .label$ = '.bo$'$[.i, "Label"]
529 procedure record_sound
530 if sampleSound$ != ""
531 select Sound 'sampleSound$'
536 demo Paint rectangle... White 10 90 15 25
537 demo Paint circle... Red 5 85 2.5
539 nowarn Record Sound (fixed time)... Microphone 0.99 1 44100 4
540 demo Paint circle... White 5 85 3
545 sampleSound$ = selected$("Sound")
548 select Sound 'sampleSound$'
551 # Draw a tone contour
552 procedure draw_tone_contour
554 demo Select inner viewport... 0 100 0 100
555 demo Paint rectangle... White 20 80 40 100
556 demo Select outer viewport... 0 100 0 100
557 demo Paint rectangle... White 20 80 40 100
558 demo Paint rectangle... White 0 100 16 26
559 select Table 'wordlist$'
560 if currentWord > 0 and currentWord <= numberOfWords
561 .sound$ = Get value... 'currentWord' Sound
562 .pinyin$ = Get value... 'currentWord' Pinyin
563 execute "ToneProt/DrawToneContour.praat" '.pinyin$' 'register'
567 procedure recognizeTone
568 select Table 'wordlist$'
569 if currentWord > 0 and currentWord <= numberOfWords
570 .sound$ = Get value... 'currentWord' Sound
571 .pinyin$ = Get value... 'currentWord' Pinyin
572 execute "ToneProt/SGC_ToneProt.praat" 'sampleSound$' '.pinyin$' 'register' 'precision' Pictures 'language$'
576 procedure write_feedback .table$
577 select Table '.table$'
578 .line1$ = Get value... 1 Text
579 .line2$ = Get value... 2 Text
580 .label$ = Get value... 3 Text
583 if index(.line1$, "???") > 0
585 elsif .label$ = "Correct"
591 demo Paint rectangle... White 0 100 16 26
592 demo Text... 50 Centre 21 Bottom '.line1$'
593 demo Text... 50 Centre 17 Bottom '.line2$'
595 demo 'defaultFontSize'
599 .splitIndex = index(.line1$, ": ")
600 .pinyin$ = left$(.line1$, (.splitIndex - 1))
601 .choice$ = right$(.line1$, (length(.line1$) - .splitIndex - 1))
602 call log_performance 'sampleSound$' 'register' 'precision' '.pinyin$' '.choice$'
605 procedure play_sound .sound$
607 select Sound '.sound$'
612 procedure end_program
613 call write_preferences ""
620 ######################################################
624 ######################################################
625 procedure config_page
627 demoWindowTitle("Speak Good Chinese: Change settings")
629 call Draw_config_page
631 while (.label$ <> "Return") and demoWaitForInput()
636 call buttonClicked 'config$' '.clickX' '.clickY'
637 .label$ = buttonClicked.label$
638 elsif demoKeyPressed()
639 .pressed$ = demoKey$()
640 call keyPressed 'config$' '.pressed$'
641 .label$ = keyPressed.label$
646 # Handle push button in process_config
647 call process_config '.label$'
650 if .label$ = "Return"
660 procedure Draw_config_page
661 call Draw_all_buttons 'config$'
662 # Set correct buttons (alert)
663 call Draw_button 'config$' Lang'language$' 2
664 call Draw_button 'config$' Register_'register' 2
666 call Draw_button 'config$' DisplayPinyin 2
669 call Draw_button 'config$' DisplayChar 2
672 call Draw_button 'config$' Shuffle 2
675 call Draw_button 'config$' SndExample 2
680 procedure DrawReturn .color$ .x .y .size
683 demo Line width... 4.0
684 demo Draw arrow... '.xleft' '.y' '.xright' '.y'
685 demo Line width... 'defaultLineWidth'
691 procedure process_config .label$
692 if .label$ = "Return"
693 call Draw_button 'config$' '.label$' 1
694 call write_preferences ""
695 elsif .label$ = "Shuffle"
696 shuffleLists = not shuffleLists
697 .displayButton = 2*shuffleLists
698 call Draw_button 'config$' '.label$' '.displayButton'
699 elsif .label$ = "SndExample"
700 useSoundExample = not useSoundExample
701 .displayButton = 2*useSoundExample
702 call Draw_button 'config$' '.label$' '.displayButton'
703 elsif startsWith(.label$, "Lang")
704 call Draw_button 'config$' Lang'language$' 0
705 call Draw_button 'config$' '.label$' 2
706 # Someone might have to use more than 2 chars for the language code
707 .numChars = length(.label$) - length("Lang")
708 .lang$ = right$(.label$, .numChars)
710 call set_language '.lang$'
711 elsif startsWith(.label$, "Register_")
712 call Draw_button 'config$' Register_'register' 0
713 call Draw_button 'config$' '.label$' 2
714 # Someone might have to use more than 3 chars for the register code
715 .numChars = length(.label$) - length("Register_")
716 .registerText$ = right$(.label$, .numChars)
717 register = '.registerText$'
718 elsif startsWith(.label$, "Display")
720 if .label$ = "DisplayPinyin"
721 displayPinyin = not displayPinyin
722 .displayButton = 2*displayPinyin
724 if .label$ = "DisplayChar"
725 displayChar = not displayChar
726 .displayButton = 2*displayChar
728 call Draw_button 'config$' '.label$' '.displayButton'
732 procedure set_language .lang$
736 select Table 'buttons$'
741 select Table 'config$'
749 # Load buttons tables
750 Read from file... Data/Buttons.Table
751 buttons$ = selected$("Table")
752 Append column... Text
754 .numLabels = Get number of rows
755 Read from file... Data/Buttons_'language$'.Table
756 .buttonsLang$ = selected$("Table")
757 for .row to .numLabels
758 select Table '.buttonsLang$'
759 .valueText$ = Get value... '.row' Text
760 .valueKey$ = Get value... '.row' Key
761 select Table 'buttons$'
762 Set string value... '.row' Text '.valueText$'
763 Set string value... '.row' Key '.valueKey$'
765 select Table '.buttonsLang$'
768 # Load configuration table
769 Read from file... Data/Config.Table
770 config$ = selected$("Table")
771 Append column... Text
773 .numLabels = Get number of rows
774 Read from file... Data/Config_'language$'.Table
775 .configLang$ = selected$("Table")
776 for .row to .numLabels
777 select Table '.configLang$'
778 .valueText$ = Get value... '.row' Text
779 .valueKey$ = Get value... '.row' Key
780 select Table 'config$'
781 Set string value... '.row' Text '.valueText$'
782 Set string value... '.row' Key '.valueKey$'
784 select Table '.configLang$'
787 # Make language change visible
789 call Draw_config_page
794 procedure load_word_list .dir1$ .dir2$ .relnumber
795 # Remove old word list
797 select Table 'wordlist$'
801 Create Strings as directory list... WordList1 '.dir1$'
803 .numLists = Get number of strings
805 select Strings WordList1
806 .currentName$ = Get string... '.i'
807 .currentName$ = .dir1$+"/"+.currentName$
808 select Strings DirWordList1
809 Set string... '.i' '.currentName$'
812 Create Strings as directory list... WordList2 '.dir2$'
813 .numLists = Get number of strings
817 select Strings WordList2
818 .currentName$ = Get string... '.i'
819 .currentName$ = .dir2$+"/"+.currentName$
820 select Strings DirWordList2
821 Set string... '.i' '.currentName$'
824 select Strings WordList1
825 plus Strings WordList2
828 select Strings WordList1
829 plus Strings WordList2
832 select Strings DirWordList1
833 plus Strings DirWordList2
835 Rename... DirWordList
836 select Strings DirWordList1
837 plus Strings DirWordList2
840 select Strings WordList2
842 select Strings WordList1
844 select Strings DirWordList1
845 Rename... DirWordList
848 # Get the position of the current word list
849 select Strings WordList
851 .numLists = Get number of strings
852 if wordlistName$ <> ""
854 select Strings WordList
855 .currentName$ = Get string... '.i'
856 if .currentName$ = wordlistName$
861 wordlistNum = .currentNumber + .relnumber
862 if wordlistNum > .numLists
865 select Strings WordList
866 wordlistName$ = Get string... 'wordlistNum'
867 select Strings DirWordList
868 .dirWordlistName$ = Get string... 'wordlistNum'
869 .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
871 # Read in full tables
872 if fileReadable("'.dirString$'/wordlist.Table")
873 Read from file... '.dirString$'/wordlist.Table
874 Rename... 'wordlistName$'
875 # Praat wil change the name if it feels like it
876 wordlist$ = selected$("Table")
877 # Handle (legacy) simple word lists
878 elsif fileReadable("'.dirString$'/wordlist.txt")
879 Create Table with column names... 'wordlistName$' 0 Pinyin Character Sound
880 wordlist$ = selected$("Table")
881 Read Strings from raw text file... '.dirString$'/wordlist.txt
882 Rename... RawWordList
883 .numWordStrings = Get number of strings
884 for .i to .numWordStrings
885 select Strings RawWordList
886 .currentLine$ = Get string... '.i'
887 .currentPinyin$ = extractWord$(.currentLine$, "")
888 if length(.currentLine$) > length(.currentPinyin$)+1
889 .currentFile$ = right$(.currentLine$, length(.currentPinyin$)+1)
890 elsif fileReadable("'.dirString$'/'.currentPinyin$'.spx")
891 .currentFile$ = .currentPinyin$+".spx"
892 elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
893 .currentFile$ = .currentPinyin$+".flac"
894 elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
895 .currentFile$ = .currentPinyin$+".wav"
896 elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
897 .currentFile$ = .currentPinyin$+".mp3"
901 select Table 'wordlist$'
903 Set string value... '.i' Pinyin '.currentPinyin$'
904 Set string value... '.i' Character -
905 Set string value... '.i' Sound '.currentFile$'
907 select Strings RawWordList
910 select Table 'wordlistName$'
912 Create Table with column names... 'wordlistName$' 0 Pinyin Character Sound
913 wordlist$ = selected$("Table")
914 Create Strings as file list... RawWordList '.dirString$'/*
915 .numWordStrings = Get number of strings
917 for .j to .numWordStrings
918 select Strings RawWordList
919 .currentLine$ = Get string... '.j'
920 .currentFile$ = extractWord$(.currentLine$, "")
921 if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
922 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
923 select Table 'wordlist$'
926 Set string value... '.i' Pinyin '.currentPinyin$'
927 Set string value... '.i' Character -
928 Set string value... '.i' Sound '.currentFile$'
931 select Strings RawWordList
934 select Table 'wordlistName$'
936 numberOfWords = Get number of rows
942 select Strings WordList
943 plus Strings DirWordList
947 procedure write_word_list
950 .ytextTop = .ytext + 5
951 .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
953 demo Paint rectangle... White 20 80 '.ytext' '.ytextTop'
955 demo Text... '.xtext' Centre '.ytext' Bottom '.displayWordList$'
959 # Write current Pinyin text
960 call display_text Black
963 call draw_tone_contour
967 procedure log_performance .sampleSound$ .register .precision .pinyin$ .choice$
968 if not logPerformance
969 logPerformance = fileReadable(logDir$+"/logPerformance.txt")
972 .currentDate$ = date$()
973 .timeStamp$ = replace_regex$(.currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
974 .choiceText$ = replace_regex$(.choice$, "[^a-zA-Z0-9\-_]", "-", 0)
977 .outfilename$ = .pinyin$+"_"+.choice$+"_'.register'_"+.timeStamp$+".wav"
978 .logtext$ = .pinyin$+tab$+.choice$+tab$+"'.register'Hz"+tab$+.currentDate$+tab$+.outfilename$+newline$
979 fileappend 'logDir$'/logFile.txt '.logtext$'
980 select Sound 'sampleSound$'
981 Write to WAV file... 'logDir$'/'.outfilename$'
985 # Make sure all Preferences directories are available
986 procedure set_up_directories
987 .dirPath$ = replace_regex$("'preferencesDirectory$'/sgc2", "[ ]", "\\&", 0);
989 system mkdir -p '.dirPath$'/wordlists
990 system mkdir -p '.dirPath$'/log
996 # Retrieve and store setting between sessions
998 procedure read_preferences .preferencesFile$
999 if not fileReadable(.preferencesFile$)
1000 .preferencesFile$ = preferencesDirectory$+"/sgc2/sgc2rc.txt"
1002 if fileReadable(.preferencesFile$)
1003 Read from file... 'preferencesDirectory$'/sgc2/sgc2rc.txt
1004 .preferenceTable$ = selected$("Table")
1005 .numPefKeys = Get number of rows
1007 call findLabel '.preferenceTable$' language
1008 if findLabel.row > 0
1009 select Table '.preferenceTable$'
1010 language$ = Get value... 'findLabel.row' Value
1013 call findLabel '.preferenceTable$' register
1014 if findLabel.row > 0
1015 select Table '.preferenceTable$'
1016 register = Get value... 'findLabel.row' Value
1019 call findLabel '.preferenceTable$' precision
1020 if findLabel.row > 0
1021 select Table '.preferenceTable$'
1022 precision = Get value... 'findLabel.row' Value
1025 call findLabel '.preferenceTable$' shuffleLists
1026 if findLabel.row > 0
1027 select Table '.preferenceTable$'
1028 shuffleLists = Get value... 'findLabel.row' Value
1031 call findLabel '.preferenceTable$' useSoundExample
1032 if findLabel.row > 0
1033 select Table '.preferenceTable$'
1034 useSoundExample = Get value... 'findLabel.row' Value
1037 call findLabel '.preferenceTable$' displayPinyin
1038 if findLabel.row > 0
1039 select Table '.preferenceTable$'
1040 displayPinyin = Get value... 'findLabel.row' Value
1043 call findLabel '.preferenceTable$' displayChar
1044 if findLabel.row > 0
1045 select Table '.preferenceTable$'
1046 displayChar = Get value... 'findLabel.row' Value
1049 call findLabel '.preferenceTable$' wordlistDir
1050 if findLabel.row > 0
1051 select Table '.preferenceTable$'
1052 wordlistDir$ = Get value... 'findLabel.row' Value
1055 call findLabel '.preferenceTable$' wordlistName
1056 if findLabel.row > 0
1057 select Table '.preferenceTable$'
1058 wordlistName$ = Get value... 'findLabel.row' Value
1061 call findLabel '.preferenceTable$' logdir
1062 if findLabel.row > 0
1063 select Table '.preferenceTable$'
1064 logDir$ = Get value... 'findLabel.row' Value
1067 select Table '.preferenceTable$'
1072 procedure write_preferences .preferencesFile$
1073 Create Table with column names... Preferences 0 Key Value
1074 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
1075 .preferencesFile$ = preferencesDirectory$ + "/sgc2/sgc2rc.txt"
1078 select Table Preferences
1081 Set string value... '.row' Key language
1082 Set string value... '.row' Value 'language$'
1085 select Table Preferences
1088 Set string value... '.row' Key register
1089 Set numeric value... '.row' Value 'register'
1092 select Table Preferences
1095 Set string value... '.row' Key precision
1096 Set numeric value... '.row' Value 'precision'
1098 select Table Preferences
1101 Set string value... '.row' Key shuffleLists
1102 Set numeric value... '.row' Value 'shuffleLists'
1104 select Table Preferences
1107 Set string value... '.row' Key useSoundExample
1108 Set numeric value... '.row' Value 'useSoundExample'
1110 select Table Preferences
1113 Set string value... '.row' Key displayPinyin
1114 Set numeric value... '.row' Value 'displayPinyin'
1116 select Table Preferences
1119 Set string value... '.row' Key displayChar
1120 Set numeric value... '.row' Value 'displayChar'
1122 select Table Preferences
1125 Set string value... '.row' Key wordlistDir
1126 Set string value... '.row' Value 'wordlistDir$'
1128 select Table Preferences
1131 Set string value... '.row' Key wordlistName
1132 Set string value... '.row' Value 'wordlistName$'
1134 select Table Preferences
1137 Set string value... '.row' Key logdir
1138 Set string value... '.row' Value 'logDir$'
1140 select Table Preferences
1141 Write to table file... '.preferencesFile$'