Added master SHA to expanded praat script output
[sgc2.git] / Config.praat
blob5c8133d6abe9eb092d7371076bd82e1a2ea003aa
2 # SpeakGoodChinese 2.0
3
4 # Praat script handling configuration page
6 #     SpeakGoodChinese: Config.praat loads the code needed for the 
7 #     settings and the Settings page of SpeakGoodChinese.
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 ###############################################################
31 # Button Drawing Routines
33 ###############################################################
35 procedure DrawCredits .color$ .x .y .size
36         .size *= 0.71
37         .lineheight = 2*.size
38         call adjustFontSizeOnHeight 'defaultFont$' 24 '.lineheight'
39         .currentFontSize = adjustFontSizeOnHeight.newFontSize
40     demo Paint circle... {0.2,0.2,0.8} '.x' '.y' '.size'
41         demo Colour... White
42         demo Text special... '.x' centre '.y' half Times '.currentFontSize' 0 i
43         demoShow()
44         demo Colour... Black
45         call set_font_size 'defaultFontSize'    
46 endproc
48 procedure DrawDeleteList .color$ .x .y .size
49     .y += 2*.size
50     .leftX = .x - 10
51     .rightX = .x + 10
52     .botY = .y+1
53     .topY = .botY + 5
54         call set_font_size 12
55     demo Red
56     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
58         # Adapt wide of text
59         .maxWidth = (.rightX - .leftX)
60     .currentFontSize = defaultFontSize
61         call adjustFontSizeOnWidth 'defaultFont$' '.currentFontSize' '.maxWidth' '.displayWordList$'
62         .currentFontSize = adjustFontSizeOnWidth.newFontSize
63         if adjustFontSizeOnWidth.diff > 0
64                 .rightX += adjustFontSizeOnWidth.diff/2
65                 .leftX -= adjustFontSizeOnWidth.diff/2
66         endif
67         call set_font_size '.currentFontSize'
68     demo Paint rectangle... White '.leftX' '.rightX' '.botY' '.topY'
70         demo Text... '.x' Centre '.botY' Bottom '.displayWordList$'
71     demo Black
72         demoShow()
73         call set_font_size 'defaultFontSize'
74 endproc
76 procedure DrawLogging .color$ .x .y .size
77     .size /= 2
78         .y += .size
79     if .color$ = "Blue"
80         .color$ = "{0.5,0.5,1}"
81      else
82         .color$ = "Blue"
83      endif
84     demo Paint circle... '.color$' '.x' '.y' '.size'
85 endproc
87 ###############################################################
89 # Obligatory button Drawing Routines
90
91 # These MUST be defined
93 ###############################################################
95 procedure DrawReturn .color$ .x .y .size
96     call DrawConfig '.color$' '.x' '.y' '.size'
97 endproc
99 # Set the correct button states after redrawing the window
100 procedure setConfigMainPage
101     # Handle logging buttons with forced button draw
102     if fileReadable("'preferencesLogDir$'/logPerformance.txt")
103         call Draw_button 'config$' +!Logging 'config.logPerformance'
104     endif
105 endproc
107 ###############################################################
109 # Button Processing Routines
111 ###############################################################
113 procedure processConfigShuffleLists .clickX .clickY .pressed$
114         .table$ = "Config"
115         .label$ = "ShuffleLists"
116         config.shuffleLists = not config.shuffleLists
117         .displayButton = 2*config.shuffleLists
118     call Draw_button '.table$' '.label$' '.displayButton'
119 endproc
121 procedure processConfigUseSoundExample .clickX .clickY .pressed$
122         .table$ = "Config"
123         .label$ = "UseSoundExample"
124         config.useSoundExample = not config.useSoundExample
125         .displayButton = 2*config.useSoundExample
126     call Draw_button '.table$' '.label$' '.displayButton'
127 endproc
128         
129 procedure processConfigSynthesis .tts$ .clickX .clickY .pressed$
130         .table$ = "Config"
131         .label$ = "Synthesis_'.tts$'"
132         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
133                 if config.synthesis$ = ""
134                         config.synthesis$ = .tts$
135                         .displayButton = 2
136                 else
137                         config.synthesis$ = ""
138                         .displayButton = 0
139                 endif
140         else
141                 config.synthesis$ = "_DISABLED_"
142                 .displayButton = -1
143         endif
144     call Draw_button '.table$' '.label$' '.displayButton'
145 endproc
146         
147 procedure processConfigStrict .clickX .clickY .pressed$
148         .table$ = "Config"
149         .label$ = "Strict"
150         config.strict = not config.strict
151         if config.strict > 0
152                 .displayButton = 2
153         else
154                 .displayButton = 0
155         endif
156     call Draw_button '.table$' '.label$' '.displayButton'
157 endproc
159 procedure processConfigDisplayPinyin .clickX .clickY .pressed$
160         .table$ = "Config"
161         .label$ = "DisplayPinyin"
162         config.displayPinyin = not config.displayPinyin
163         .displayButton = 2*config.displayPinyin
164     call Draw_button '.table$' '.label$' '.displayButton'
165 endproc
166                 
167 procedure processConfigDisplayChar .clickX .clickY .pressed$
168         .table$ = "Config"
169         .label$ = "DisplayChar"
170         config.displayChar = not config.displayChar
171         .displayButton = 2*config.displayChar
172     call Draw_button 'table$' '.label$' '.displayButton'
173 endproc
175 procedure processConfigDisplayTrans .clickX .clickY .pressed$
176         .table$ = "Config"
177         .label$ = "DisplayTrans"
178         config.displayTrans = not config.displayTrans
179         .displayButton = 2*config.displayTrans
180     call Draw_button 'table$' '.label$' '.displayButton'
181 endproc
183 procedure processConfigDisplayNumbers .clickX .clickY .pressed$
184         .table$ = "Config"
185         .label$ = "DisplayNumbers"
186         config.displayNumbers = not config.displayNumbers
187         .displayButton = 2*config.displayNumbers
188     call Draw_button 'table$' '.label$' '.displayButton'
189 endproc
191 procedure processConfigLanguage .language$ .clickX .clickY .pressed$
192         .table$ = "Config"
193         .label$ = "Language_'.language$'"
194         call processLanguageCodes '.table$' '.label$'
195 endproc
196         
197 procedure processConfigShowBackground .clickX .clickY .pressed$
198         .table$ = "Config"
199         .label$ = "ShowBackground"
200         config.showBackground = not config.showBackground
201         .displayButton = 2*config.showBackground
202     call Draw_button 'table$' '.label$' '.displayButton'
203 endproc
205 procedure processConfigInput .input$ .clickX .clickY .pressed$
206         .table$ = "Config"
207         .label$ = "Input_'.input$'"
208     call Draw_button '.table$' Input_'config.input$' 0
209         config.input$ = .input$
210     call Draw_button '.table$' Input_'config.input$' 2
211 endproc
213 procedure processConfigRegister .register .clickX .clickY .pressed$
214         .table$ = "Config"
215         .label$ = "Register_'.register'"
216         call setRegisterFromLabel '.table$' '.label$'
217 endproc
218         
219 procedure setRegisterFromLabel .table$ .label$
220     call Draw_button '.table$' Register_'config.register' 0
221     call Draw_button '.table$' '.label$' 2
222     # Someone might have to use more than 3 chars for the config.register code
223     .numChars = length(.label$) - length("Register_")
224         .registerText$ = right$(.label$, .numChars)
225         config.register = '.registerText$'
226 endproc
228 procedure processConfigDeleteWordlist .clickX .clickY .pressed$
229         .table$ = "Config"
230         .label$ = "DeleteWordlist"
232     # Do not process undeletable word lists, only those stored in the 
233     # preferencesDirectory$ can be deleted
234     if fileReadable("'sgc2wordlists$'/'wordlistName$'")
235         call findLabel '.table$' !'.label$'
236         if findLabel.row > 0
237         select Table '.table$'
238         .alertText$ = Get value... 'findLabel.row' Text
239         .confirmKey$ = Get value... 'findLabel.row' Key
240         .popupText$ = Get value... 'findLabel.row' Helptext
241         else
242             exit Cannot find delete directive: '.table$' !'.label$'
243         endif
244         call write_text_popup 'defaultFont$' 14 '.popupText$'
245         call Draw_button '.table$' '.label$' 2
246         alertText$ = .alertText$
247         call Draw_button '.table$' '.label$' 3
248         alertText$ = ""
249         
250                 # Wait for confirmation
251                 demoWaitForInput()
252         if demoInput(.confirmKey$)
253                 .deleteWordListDir$ = wordlistName$
254                 call load_word_list "'localWordlistDir$'" 1
255                 call removeWordlist '.deleteWordListDir$'
256         endif
257                 call Draw_button '.table$' '.label$' 0
258                 call Draw_config_page
259     endif
260 endproc
262 wordlistTag$ = "Wordlist name"        
263 procedure processConfigInstallWordlist .clickX .clickY .pressed$
264         .table$ = "Config"
265         .label$ = "InstallWordlist"
266     call Draw_button '.table$' '.label$' 1
267         if windows
268                 # Do not use the automatic sgc list option, ask for a wordlist NAME
269                 # Get help text
270                 call findLabel '.table$' '.label$'
271                 .row = findLabel.row
272                 select Table '.table$'
273                 .openDialogue$ = Get value... '.row' Helptext
274                 call convert_praat_to_utf8 '.openDialogue$'
275                 .openDialogue$ = convert_praat_to_utf8.text$
276                 .wordlistButton$ = replace$(wordlistTag$, " ", "_", 0)
277                 .wordlistButton$ = replace_regex$(.wordlistButton$, "^.", "\l&", 0)
278                 beginPause(.openDialogue$)
279                         sentence (wordlistTag$, "")
280                 clicked = endPause ("Cancel", "Open", 2)
281                 .wordlist_Name$ = ""
282                 if clicked = 2
283                         .wordlist_Name$ = '.wordlistButton$'$
284                 endif
285                 call install_wordlists_by_name '.wordlist_Name$'
286         else
287         call sgc2wordlist 'homeDirectory$'
288         call sgc2wordlist 'homeDirectory$'/Downloads
289         call sgc2wordlist 'homeDirectory$'/Documents
290         call sgc2wordlist 'homeDirectory$'/My Documents
291         call sgc2wordlist 'homeDirectory$'/My Documents/Downloads
292         call sgc2wordlist 'homeDirectory$'/Desktop
293         call sgc2wordlist 'preferencesAppDir$'
294         endif
295         call load_word_list "'localWordlistDir$'" 0
296     call Draw_button '.table$' '.label$' 0
297         call Draw_config_page
298 endproc
300 procedure processConfigOpenWordlist .clickX .clickY .pressed$
301         .table$ = "Config"
302         .label$ = "OpenWordlist"
303     call Draw_button '.table$' '.label$' 1
305         # Get help text
306         call findLabel '.table$' '.label$'
307         .row = findLabel.row
308         select Table '.table$'
309         .openDialogue$ = Get value... '.row' Helptext
310         call convert_praat_to_utf8 '.openDialogue$'
311         .openDialogue$ = convert_praat_to_utf8.text$
313         .wordlist_Name$ = chooseReadFile$ (.openDialogue$)
314         call install_wordlists_by_name '.wordlist_Name$'
315         call load_word_list "'localWordlistDir$'" 0
316     call Draw_button '.table$' '.label$' 0
317         call Draw_config_page
318 endproc
320 procedure processConfigPerfSummary .clickX .clickY .pressed$
321         .table$ = "Config"
322         .label$ = "PerfSummary"
323     call Draw_button '.table$' '.label$' 1
324         
325         call loadTable SummaryToneEvaluation
326         call write_tabbed_table SummaryToneEvaluation Evaluation_'config.language$'
327         demoWaitForInput()
328         select Table SummaryToneEvaluation
329         Remove
331         call Draw_button '.table$' '.label$' 0
332         call Draw_config_page
333 endproc
335 procedure processConfigListPerf .clickX .clickY .pressed$
336         .table$ = "Config"
337         .label$ = "ListPerf"
338     call Draw_button '.table$' '.label$' 1
339         
340         call write_tabbed_table 'initialiseSGC2.toneevaluation_table$' Evaluation_'config.language$'
341         demoWaitForInput()
342         
343     call Draw_button '.table$' '.label$' 0
344         call Draw_config_page
345 endproc
347 procedure processConfigSavePerf .clickX .clickY .pressed$
348         .table$ = "Config"
349         .label$ = "SavePerf"
350     call Draw_button '.table$' '.label$' 1
351         
352         # Get help text
353         call findLabel '.table$' '.label$'
354         .row = findLabel.row
355         select Table '.table$'
356         .openDialogue$ = Get value... '.row' Helptext
357         call convert_praat_to_utf8 '.openDialogue$'
358         .openDialogue$ = convert_praat_to_utf8.text$
360         .wordlist_Name$ = chooseWriteFile$ (.openDialogue$, "Performance.Table")
361         if .wordlist_Name$ <> ""
362                 select Table 'initialiseSGC2.toneevaluation_table$'
363                 Write to table file... '.wordlist_Name$'
364         endif
365         
366     call Draw_button '.table$' '.label$' 0
367         call Draw_config_page
368 endproc
370 ###############################################################
372 # Obligatory button Processing Routines
374 # These MUST be defined
376 ###############################################################
378 procedure processConfigReturn .clickX .clickY .pressed$
379         .table$ = "Config"
380         .label$ = "Return"
381         call Draw_button '.table$' '.label$' 1
382         call write_preferences ""
383 endproc
385 procedure processConfigRefresh .clickX .clickY .pressed$
386         .table$ = "Config"
387         .label$ = "Refresh"
388         call Draw_config_page
389 endproc
391 procedure processConfigCredits .clickX .clickY .pressed$
392         .table$ = "Config"
393         .label$ = "Credits"
394         call Draw_button '.table$' '.label$' 1
395         call write_text_table Credits_'config.language$'
396         demoWaitForInput()
397     call Draw_button '.table$' '.label$' 0
398     demo Erase all
399     call Draw_config_page
400 endproc
402 procedure processConfigHelp .clickX .clickY .pressed$
403         .table$ = "Config"
404         .label$ = "Help"
405         call help_loop  '.table$' Draw_config_page
406 endproc
408 ###############################################################
410 # Miscelaneous supporting code
412 ###############################################################
413 procedure install_wordlists_by_name .wordlist_Name$
414         if .wordlist_Name$ <> ""
415 printline '.wordlist_Name$'
416                 if index(.wordlist_Name$, "/")
417                         .sourceDir$ = left$(.wordlist_Name$, rindex(.wordlist_Name$, "/") -1)
418                         .file$ = right$(.wordlist_Name$, length(.wordlist_Name$) - rindex(.wordlist_Name$, "/"))
419                         call readWordlist "'.sourceDir$'" '.file$'
420                 else
421                         .start = 1
422                         if index(.wordlist_Name$, ".")
423                                 .start = 4
424                         endif
425                         .extension1$ = ".sgc"
426                         .extension2$ = ".Table"
427                         .extension3$ = ".txt"
428                         .extension4$ = ""
429                         for .e from .start to 4
430                                 .currentExtension$ = .extension'.e'$
431                         call readWordlist "'homeDirectory$'" '.wordlist_Name$''.currentExtension$'
432                         call readWordlist "'homeDirectory$'/Downloads" '.wordlist_Name$''.currentExtension$'
433                         call readWordlist "'homeDirectory$'/Documents" '.wordlist_Name$''.currentExtension$'
434                         call readWordlist "'homeDirectory$'/My Documents/Downloads" '.wordlist_Name$''.currentExtension$'
435                         call readWordlist "'homeDirectory$'/My Documents" '.wordlist_Name$''.currentExtension$'
436                         call readWordlist "'homeDirectory$'/Desktop" '.wordlist_Name$''.currentExtension$'
437                         call readWordlist "'preferencesAppDir$'" '.wordlist_Name$''.currentExtension$'
438                         endfor
439                 endif
440         endif
441 endproc
443 # Word lists: It is a VERY good idea to make sure that word-lists
444 # have unique names.
445 procedure load_word_list .localdir$ .relnumber
446     # Remove old word list
447     if wordlist$ <> ""
448                 select Table 'wordlist$'
449                 Remove
450                 call wipeArea 'wipeWordlistArea$'
451                 wordlist$ = ""
452     endif
453     
454         # Create Table that will recieve the wordlists and directories
455         Create Table with column names... AllWordLists 0 Name Directory
456         .currentWordlistRow = 0
457         
458         # Start with the word lists in the distribution, unless the local directory exists!
459         if not fileReadable(.localdir$)
460                 call CreateCreateWordlists
461                 select Table CreateWordlists
462         .numLists = Get number of rows
463                 for .i to .numLists
464                         select Table CreateWordlists
465            .currentName$ = Get value... '.i' Name
466                    .currentDirectory$ = "*call Create'.currentName$'"
467                         select Table AllWordLists
468                         Append row
469                         .currentWordlistRow = Get number of rows
470                         Set string value... '.currentWordlistRow' Name '.currentName$'
471                     .currentDirectory$ = "*call Create'.currentName$'"
472                         Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
473                 endfor
474                 select Table CreateWordlists
475                 Remove
476         endif
477         
478         # First the global word lists
479         if fileReadable(globalwordlists$)
480         Create Strings as directory list... WordList 'globalwordlists$'
481         .numLists = Get number of strings
482         for .i to .numLists
483             select Strings WordList
484             .currentName$ = Get string... '.i'
485                         select Table AllWordLists
486                         Append row
487                         .currentWordlistRow = Get number of rows
488                         Set string value... '.currentWordlistRow' Name '.currentName$'
489                     .currentDirectory$ = globalwordlists$+"/"+.currentName$
490                         Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
491         endfor
492         select Strings WordList
493         Remove
494         endif
495         
496         # Now the preferences word lists
497         if fileReadable(sgc2wordlists$)
498         Create Strings as directory list... WordList 'sgc2wordlists$'
499         .numLists = Get number of strings
500         for .i to .numLists
501             select Strings WordList
502             .currentName$ = Get string... '.i'
503                         select Table AllWordLists
504                         Append row
505                         .currentWordlistRow = Get number of rows
506                         Set string value... '.currentWordlistRow' Name '.currentName$'
507                     .currentDirectory$ = sgc2wordlists$+"/"+.currentName$
508                         Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
509         endfor
510         select Strings WordList
511         Remove
512         endif
513         
514         # Finally, the local word lists
515         if fileReadable(.localdir$)
516         Create Strings as directory list... WordList '.localdir$'
517         .numLists = Get number of strings
518         for .i to .numLists
519             select Strings WordList
520             .currentName$ = Get string... '.i'
521                         select Table AllWordLists
522                         Append row
523                         .currentWordlistRow = Get number of rows
524                         Set string value... '.currentWordlistRow' Name '.currentName$'
525                     .currentDirectory$ = .localdir$+"/"+.currentName$
526                         Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
527         endfor
528         select Strings WordList
529         Remove
530     endif
532         # Get the position of the current word list
533         select Table AllWordLists
534         .currentNumber = 1
535     .numLists = Get number of rows
536         if wordlistName$ <> ""
537                 select Table AllWordLists
538                 .currentNumber = Search column... Name 'wordlistName$'
539         endif
540     wordlistNum = .currentNumber + .relnumber
541     if wordlistNum > .numLists
542         wordlistNum = 1
543         elsif wordlistNum < 1
544                 wordlistNum = .numLists
545     endif
546     select Table AllWordLists
547     wordlistName$ = Get value... 'wordlistNum' Name
548         .dirWordlistName$ = Get value... 'wordlistNum' Directory
549     .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
550         
551         # Read in full tables
552         if fileReadable("'.dirString$'/wordlist.Table")
553         Read from file... '.dirString$'/wordlist.Table
554         Rename... 'wordlistName$'
555         # Praat wil change the name if it feels like it
556         wordlist$ = selected$("Table")
557         # Handle (legacy) simple word lists
558         elsif fileReadable("'.dirString$'/wordlist.txt")
559                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
560         wordlist$ = selected$("Table")
561                 Read Strings from raw text file... '.dirString$'/wordlist.txt
562                 Rename... RawWordList
563                 .numWordStrings = Get number of strings
564                 for .i to .numWordStrings
565                         select Strings RawWordList
566                         .currentLine$ = Get string... '.i'
567                         .currentPinyin$ = extractWord$(.currentLine$, "")
568                         if length(.currentLine$) > length(.currentPinyin$)+1
569                                 .currentFile$ = right$(.currentLine$, length(.currentPinyin$)+1)
570                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.spx")
571                                 .currentFile$ = .currentPinyin$+".spx"
572                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
573                                 .currentFile$ = .currentPinyin$+".flac"
574                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
575                                 .currentFile$ = .currentPinyin$+".wav"
576                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
577                                 .currentFile$ = .currentPinyin$+".mp3"
578                         else
579                                 .currentFile$ = "-"
580                         endif
581                         select Table 'wordlist$'
582                         Append row
583                         Set string value... '.i' Pinyin '.currentPinyin$'
584                         Set string value... '.i' Sound '.currentFile$'
585                 endfor
586                 select Strings RawWordList
587                 Remove
588                 
589                 select Table 'wordlist$'
590         elsif fileReadable(.dirString$)
591                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
592         wordlist$ = selected$("Table")
593                 Create Strings as file list... RawWordList '.dirString$'/*
594                 .numWordStrings = Get number of strings
595                 .i = 0
596                 for .j to .numWordStrings
597                         select Strings RawWordList
598                         .currentLine$ = Get string... '.j'
599                         .currentFile$ = extractWord$(.currentLine$, "")
600                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
601                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
602                                 select Table 'wordlist$'
603                                 Append row
604                                 .i += 1
605                                 Set string value... '.i' Pinyin '.currentPinyin$'
606                                 Set string value... '.i' Sound '.currentFile$'
607                         endif
608                 endfor
609                 select Strings RawWordList
610                 Remove
611                 
612                 select Table 'wordlist$'
613         elsif startsWith(.dirString$, "*call ")
614                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
615                 '.callProcedure$'
616                 wordlist$ = selected$("Table")
617         endif
618         
619         # Can this wordlist be deleted?
620         if fileReadable("'sgc2wordlists$'/'wordlistName$'")
621                 config.deleteWordlist = 0
622         else
623                 config.deleteWordlist = -1              
624         endif
625         
626         # Add a Character and Translation column if missing
627         select Table 'wordlist$'
628     numberOfWords = Get number of rows
629         .characterColumn = Get column index... Character
630         if not .characterColumn
631                 Append column... Character
632                 for .i to numberOfWords
633                         Set string value... '.i' Character -
634                 endfor
635         endif
636         .translationColumn = Get column index... Translation
637         if not .translationColumn
638                 Append column... Translation
639                 for .i to numberOfWords
640                         Set string value... '.i' Translation -
641                 endfor
642         endif
643         
644         # Remove all rows without Pinyin
645         .numRows = Get number of rows
646         for i to .numRows
647                 .rowNum = .numRows - i + 1
648                 .pinyinValue$ = Get value... '.rowNum' Pinyin
649                 if not index_regex(.pinyinValue$, "[a-zA-Z0-9]")
650                         Remove row... '.rowNum'
651                 endif
652         endfor
653         numberOfWords = Get number of rows
654         
655         # Shuffle words if requested
656     if config.shuffleLists
657         Randomize rows
658     endif
660         # Clean up
661     select Table AllWordLists
662     Remove
663     
664     # There were no Word Lists
665     label NOWORDLISTS
666     if .numLists <= 0
667         wordlistName$ = "No Word Lists available"
668         wordlistNum = 1
669                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Translation Sound
670         wordlist$ = selected$("Table")
671         .i = 0
672                 Append row
673                 .i += 1
674                 Set string value... '.i' Pinyin ni3hao3
675                 Set string value... '.i' Character -
676                 Set string value... '.i' Translation -
677                 Set string value... '.i' Sound -
678                 Append row
679                 .i += 1
680                 Set string value... '.i' Pinyin xie4xie0
681                 Set string value... '.i' Character -
682                 Set string value... '.i' Translation -
683                 Set string value... '.i' Sound -
684                 Append row
685                 .i += 1
686                 Set string value... '.i' Pinyin zai4jian4
687                 Set string value... '.i' Character -
688                 Set string value... '.i' Translation -
689                 Set string value... '.i' Sound -
690         numberOfWords = Get number of rows
691     endif
693         call set_window_title 'buttons$' 'wordlistName$'
694 endproc
696 procedure display_word_list_name
697     .xtext = 50
698     .ytext = 12
699    call reset_viewport
700     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
701     call wipeArea 'wipeWordlistArea$'
702         call adjustFontSizeOnHeight 'defaultFont$' 'defaultFontSize' 5
703         .currentFontSize = adjustFontSizeOnHeight.newFontSize
705     demo Blue
706         demo Text special... '.xtext' Centre '.ytext' Bottom Helvetica '.currentFontSize' 0 '.displayWordList$'
707     demo Black
708         demoShow()
709         call set_font_size 'defaultFontSize'
710 endproc
712 procedure write_word_list
713         # Draw the contour
714         call draw_tone_contour
716         # Write current Pinyin text
717         call display_text Black
718         
719         # Write the current word list name
720         call display_word_list_name
721 endproc
723 procedure start_logging
724         if fileReadable("'preferencesLogDir$'/logPerformance.txt")
725                 .logDirectory$ < 'preferencesLogDir$'/logPerformance.txt
726                 .logDirectory$ = extractWord$(.logDirectory$, "")
727                 if .logDirectory$ = "" or not fileReadable(.logDirectory$)
728                         .logDirectory$ = "'preferencesLogDir$'"
729                 endif
730                 createDirectory("'.logDirectory$'/sgc2log")
731                 currentLogDirectory$ = "'.logDirectory$'/sgc2log/log'logtimeStamp$'"
732                 createDirectory(currentLogDirectory$)
733                 if not fileReadable("'currentLogDirectory$'/wordlist.Table")
734                         .headerLine$ = "Pinyin'tab$'Character'tab$'Sound'newline$'"
735                         .headerLine$ > 'currentLogDirectory$'/wordlist.Table
736                 endif
737                         # Flip switch
738                         config.logPerformance = 1
739         endif
740 endproc
742 procedure log_performance .recordedSound$ .register .proficiency .pinyin$ .choice$
743         # Log files
744         .currentDate$ = date$()
745         .timeStamp$ = replace_regex$(.currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
746         .choiceText$ = replace_regex$(.choice$, "[^a-zA-Z0-9\-_]", "-", 0)
748         if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
749            .outfilename$ = .pinyin$+"_"+.choice$+"_'.register'_"+.timeStamp$+".wav"
750            .logtext$ = .pinyin$+tab$+.choice$+tab$+"'.register'Hz"+tab$+.currentDate$+tab$+.outfilename$+newline$
751            # Plain log text
752            fileappend 'currentLogDirectory$'/logFile.txt '.logtext$'
753            # A wordlist.Table
754            fileappend 'currentLogDirectory$'/wordlist.Table '.pinyin$''tab$''.choice$''tab$''.outfilename$''newline$'
755                 # The recorded sound
756            select Sound 'recordedSound$'
757            Write to WAV file... 'currentLogDirectory$'/'.outfilename$'
758         endif
759 endproc
761 procedure paint_logging_light
762     select Table Config
763     .row = Search column... Label !Logging
764         if .row < 1
765                 exit Button Table Config does not have a row with label !Logging
766         endif
767         # Get button values
768     .leftX = Get value... '.row' LeftX
769     .rightX = Get value... '.row' RightX
770     .lowY = Get value... '.row' LowY
771     .highY = Get value... '.row' HighY
772     .buttonColor$ = Get value... '.row' Color
773     .centerX = (.leftX + .rightX)/2
774     .centerY = (.lowY + .highY)/2
775     .radius = (.highY - .lowY )/(4*2)
776     .wipeRadius = 1.1*.radius
777     if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
778         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
779         demo Paint circle... '.buttonColor$' '.centerX' '.centerY' '.radius'
780         demoShow()
781     else
782         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
783         demoShow()
784     endif
785 endproc
787 # Uninstall word lists
788 procedure removeWordlist .deletedWordlistName$
789     .targetDir$ = ""
790     if fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'")
791         .targetDir$ = "'sgc2wordlists$'/'.deletedWordlistName$'"
792     endif
793     Create Strings as file list... DeleteList '.targetDir$'/*
794     .numdeleteFiles = Get number of strings
795     for .i to .numdeleteFiles
796         .file$ = Get string... '.i'
797         deleteFile("'.targetDir$'/'.file$'")
798     endfor
799     deleteFile("'.targetDir$'")
800         select Strings DeleteList
801         Remove
802 endproc
804 # Install word lists
805 procedure sgc2wordlist .sourceDir$
806         if startsWith(.sourceDir$, "preferencesDirectory$")
807                 .sourceDir$ = replace$(.sourceDir$, "preferencesDirectory$", preferencesDirectory$)
808         endif
810         .targetDirectory$ = "'sgc2wordlists$'"
811         if  fileReadable(.sourceDir$)
812                 Create Strings as file list... PackageList '.sourceDir$'/*.sgc
813                 .numFiles = Get number of strings
814                 for .i to .numFiles
815                         select Strings PackageList
816                         .file$ = Get string... '.i'
817                         call readWordlist '.sourceDir$' '.file$'
818                 endfor
820                 select Strings PackageList
821                 Remove
822         endif
823 endproc
825 # Debuggin remarks!!!
826 # fileReadable(<directory>) werkt niet in Windows
827 # De file paths / -> \\ must be performed on the filenames in Windows before the system_nocheck is given
828 # And yet only the 7z decompression has been implemented
829 windowsUnzipCommand$ = """C:\Program Files\7-Zip\7z"" x"
830 procedure readWordlist .sourceDir$ .file$
831         # No use doing anything if the source does not exist
832         if fileReadable("'.sourceDir$'/'.file$'")
833                 # What will be the target wordlist directory?
834                 .targetDirectory$ = "'sgc2wordlists$'"
835                 .dirname$ = left$(.file$, rindex(.file$, ".")-1)
836                 if .dirname$ = ""
837                         .dirname$ = .file$
838                 endif
839                 .wordlistDirectory$ = .targetDirectory$+"/"+.dirname$
840                 # Wordlist directory does not exist, neither locally nor in the preferences
841                 if not (fileReadable(.dirname$) or fileReadable(.wordlistDirectory$))
842                         .wasWordList = 0
843                         # Move source to destination
844                         if index(.file$, ".sgc")
845                                 # Create wordlist directory
846                                 createDirectory(.wordlistDirectory$)
847                                 .wasWordList = 1
848                                 if macintosh or unix
849                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/"'
850                                         system cd ''.wordlistDirectory$'';bash -rc -- 'unzip "'.file$'"'
851                                 elsif windows
852                                         system copy "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/"
853                                         system chdir "'.wordlistDirectory$'" && 'windowsUnzipCommand$' "'.file$'"
854                                 endif
855                                 deleteFile("'.wordlistDirectory$'/'.file$'")
856                         elsif index_regex(.file$, "\.(Table|txt)")
857                                 # Create wordlist directory
858                                 createDirectory(.wordlistDirectory$)
859                                 .wasWordList = 1
860                                 .extension$ = replace_regex$(.file$, "^.+\.(Table|txt)$", "\1", 0)
861                                 if macintosh or unix
862                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/wordlist.'.extension$'"'
863                                 elsif windows
864                                         system copy "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/wordlist.'.extension$'"
865                                 endif
866                         elsif fileReadable("'.sourceDir$'/'.file$'/wordlist.Table") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt")
867                                 # Create wordlist directory
868                                 createDirectory(.wordlistDirectory$)
869                                 .wasWordList = 1
870                                 .sourceFiles = Create Strings as file list... SourceStrings '.sourceDir$'/'.file$'/
871                                 .numFiles = Get number of strings
872                                 for .i to .numFiles
873                                         select '.sourceFiles'
874                                         .currentFile$ = Get string... '.i'
875                                         if macintosh or unix
876                                                 system bash -rc -- 'cp "'.sourceDir$'/'.file$'/'.currentFile$'" "'.wordlistDirectory$'/"'
877                                         elsif windows
878                                                 system copy "'.sourceDir$'/'.file$'/'.currentFile$'" "'.wordlistDirectory$'/"
879                                         endif
880                                 endfor
881                                 select '.sourceFiles'
882                                 Remove
883                         endif
884                         
885                         # Set current word list to read list
886                         if .wasWordList
887                                 wordlistName$ = .dirname$
888                         else
889                                 .table$ = "Config"
890                                 .label$ = "!NotAWordlist"
892                                 # Get help text
893                                 call findLabel '.table$' '.label$'
894                                 .row = findLabel.row
895                                 select Table '.table$'
896                                 .helpText$ = Get value... '.row' Helptext
897                                 .filetext$ = replace_regex$("'.sourceDir$'/'.file$'", "_", "\\_ ", 0)
898                         call write_text_popup 'defaultFont$' 14 '.helpText$' "'.filetext$'"
899                                 # Wait for confirmation
900                                 demoWaitForInput()
901                         endif
902                 endif
903         endif
904 endproc