Ran tables2scripts.praat
[sgc2.git] / InitialiseSGC2.praat
blob20da5a7c460a9ecea3cbfb36edfec6c831d67f94
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
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 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
32 currentLogDirectory$ = ""
34 # Define canvas
35 viewportMargin = 5
36 yWordlist = 11
37 yFeedback = yWordlist + 5
38 yPinyin = yFeedback + 10
39 yContour = yPinyin + 15
40 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
41 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
42 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
43 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
45 # Initialize parameters
46 alertText$ = ""
47 currentWord = 1
48 pinyin$ = ""
49 character$ = ""
50 buttons$ = ""
51 config$ = ""
52 wordlistNum = 1
53 wordlistName$ = ""
54 wordlist$ = ""
55 config.deleteWordlist = -1
56 localWordlistDir$ = "shellDirectory$/wordlists"
57 config.displayNumbers = 1
58 config.displayPinyin = 1
59 config.displayChar = 1
60 config.displayTrans = 1
61 config.useSoundExample = 1
62 config.synthesis$ = "_DISABLED_"
63 config.strict = 0
64 config.input$ = "Microphone"
65 config.showBackground = 1
66 sgc2.noiseThresshold = -30
68 if unix
69         samplingFrequency = 44100
70 elsif macintosh
71         samplingFrequency = 44100
72 elsif windows
73         samplingFrequency = 44100
74 endif
75 recordingTime = 2
77 config.language$ = "EN"
78 config.register = 249
79 precision = 3
80 config.logPerformance = 0
81 preferencesLogDir$ = "'preferencesDirectory$'/sgc2/log"
82 preferencesTableDir$ = "'preferencesDirectory$'/sgc2/Data"
83 sgc2wordlists$ = "'preferencesDirectory$'/sgc2/wordlists"
84 # Global word lists must be installed BY THE ADMINISTRATOR
85 # This means, create the directory 'globalwordlists$' and
86 # fill it with wordlist directories containing wordlist.Table
87 # and audio files. They can be copied from a local directory.
88 globalwordlists$ = ""
89 globaltablelists$ = ""
90 globalTTSdir$ = ""
91 if unix
92         globalwordlists$ = "/etc/praat/sgc2/wordlists"
93         globaltablelists$ = "/etc/praat/sgc2/Data"      
94         globalTTSdir$ = "/etc/praat/sgc2/TTS"   
95 elsif macintosh
96         globalwordlists$ = "/Library/Praat/sgc2/wordlists"
97         globaltablelists$ = "/Library/Praat/sgc2/Data"
98         globalTTSdir$ = "/Library/Praat/sgc2/TTS"       
99 elsif windows
100         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/praat/sgc2/wordlists"
101         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/praat/sgc2/Data"
102         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/praat/sgc2/TTS"
103 endif
105 ###############################################################
107 # TTS (speech synthesis). Place commands in user info
109 speakCommandDir$ = "'preferencesDirectory$'/sgc2/TTS"
110 speakCommandFile$ = ""
111 procedure set_up_TTS
112         if macintosh
113                 .ttscommand$ = "speak"
114                 .osName$ = "OSX"
115                 .quote$ = ""
116         elsif unix
117                 .ttscommand$ = "espeak"
118                 .osName$ = "UNIX"
119                 .quote$ = ""
120         elsif windows
121                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
122                 .osName$ = "WIN"
123                 .quote$ = """"
124         endif
125         # Global TTS command has precedence
126         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
127                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
128                 if config.synthesis$ = "_DISABLED_"
129                         # Default is ON
130                         config.synthesis$ = "eSpeak"
131                 endif
132         endif
133         
134         # Local TTS command, if there is no global one
135         # Command does not exist yet, create it if eSpeak is installed
136         # Make sure to quote the path!
137         if speakCommandFile$ = ""
138                 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
139                         if macintosh
140                                 system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
141                                 .command_path$ < 'speakCommandDir$'/command_path.txt
142                         elsif unix
143                                 system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
144                                 .command_path$ < 'speakCommandDir$'/command_path.txt
145                         elsif windows
146                                 if fileReadable(.ttscommand$)
147                                         .command_path$ = .ttscommand$
148                                 endif
149                         endif
150                         # Remove any newlines
151                         .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
153                         # Command path found
154                         if .command_path$ <> "" and fileReadable(.command_path$)
155                                 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
156                                 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
157                                 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
158                                 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
159                         endif
160                 endif
161                 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
162                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
163                 endif
164                 deleteFile("'speakCommandDir$'/command_path.txt")
165         endif
166         
167         # Close off (other TTS maybe a later option)
168         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
169                 config.synthesis$ = "eSpeak"
170         endif
171 endproc
173 config.shuffleLists = 1
174 defaultFontSize = 12
175 defaultFont$ = "Helvetica"
176 defaultLineWidth = 1
178 # Set up directories if they do not exist already
179 call set_up_directories
180 # Get saved preferences
181 call read_preferences ""
182 call set_up_TTS
183 # Set inital language
184 call set_language 'config.language$'
185 # Get the word-list
186 call load_word_list "'localWordlistDir$'" 0
188 # Draw inital window
189 call init_window
191 ###############################################################
193 # Obligatory procedures
195 ###############################################################
197 # Initialize Demo Window
198 procedure init_window
199     demo Erase all
200         if config.showBackground
201                 call draw_background Background
202         endif
203         demo Line width... 'defaultLineWidth'
204         demo 'defaultFont$'
205         call set_font_size 'defaultFontSize'
206         demo Black
207         call reset_viewport
208     # Define buttons in a table
209     call init_buttons
210         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
211         call set_window_title 'buttons$' '.windowTitle$'
212         # Display the word-list
213         call write_word_list
214         # Logging light
215         call paint_logging_light
216 endproc
218 # Make sure all Preferences directories are available
219 procedure set_up_directories
220         .dirPath$ = "'preferencesDirectory$'/sgc2"
221         createDirectory(.dirPath$)
222         createDirectory("'.dirPath$'/wordlists")
223         createDirectory("'.dirPath$'/log")
224         createDirectory("'.dirPath$'/TTS")
225 endproc
228 # Retrieve and store setting between sessions
230 procedure read_preferences .preferencesFile$
231         if not fileReadable(.preferencesFile$)
232                 .preferencesFile$ = preferencesDirectory$+"/sgc2/sgc2rc.txt"
233         endif
234         if fileReadable(.preferencesFile$)
235                 Read from file... 'preferencesDirectory$'/sgc2/sgc2rc.txt
236                 .preferenceTable$ = selected$("Table")
237                 .numPrefKeys = Get number of rows
238                 for .row to .numPrefKeys
239                         .variableName$ = Get value... '.row' Key
240                         if variableExists(.variableName$)
241                                 .variableValue = Get value... '.row' Value
242                                 if .variableValue <> undefined
243                                         '.variableName$' = '.variableValue'
244                                 endif
245                         elsif variableExists(.variableName$+"$")
246                                 .variableValue$ = Get value... '.row' Value
247                                 .variableName$ = .variableName$+"$"
248                                 '.variableName$' = "'.variableValue$'"
249                         endif
250                 endfor
251                 
252                 select Table '.preferenceTable$'
253                 Remove
254         endif
255 endproc
257 procedure write_preferences .preferencesFile$
258         Create Table with column names... Preferences 0 Key Value
259         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
260                 .preferencesFile$ = preferencesDirectory$ + "/sgc2/sgc2rc.txt"
261         endif
262         .row = 0
263         
264         for .tableNum from 1 to 2
265                 if .tableNum = 1
266                         .table$ = "Config"
267                 elsif .tableNum = 2
268                         .table$ = "Buttons"
269                 endif
270                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
272                 select Table '.table$'
273                 .numTableRows = Get number of rows
274                 for .tablerow to .numTableRows
275                         select Table '.table$'
276                         .label$ = Get value... '.tablerow' Label
277                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
278                         .keyName$ = .variableName$
279                         .value$ = ""
280                         if variableExists(.variableName$)
281                                 .value = '.variableName$'
282                                 .value$ = "'.value'"
283                         elsif variableExists(.variableName$ + "$")
284                                 .variableName$ = .variableName$ + "$"
285                                 .value$ = '.variableName$'
286                         elsif index(.variableName$, "_") > 0
287                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
288                                 .keyName$ = .variableName$
289                                 select Table Preferences
290                                 .prefRow = Search column... Key '.keyName$'
291                                 if .prefRow <= 0
292                                         if variableExists(.variableName$)
293                                            .value = '.variableName$'
294                                            .value$ = "'.value'"
295                                         elsif variableExists(.variableName$ + "$")
296                                            .variableName$ = .variableName$ + "$"
297                                            .value$ = '.variableName$'
298                                         endif
299                                 endif
300                         endif
302                         if .value$ <> ""
303                                 select Table Preferences
304                                 Append row
305                                 .row += 1
306                                 Set string value... '.row' Key '.keyName$'
307                                 Set string value... '.row' Value '.value$'
308                         endif
309                 endfor
310         endfor
311         
312         # Some extra settings
313         select Table Preferences
314         Append row
315         .row += 1
316         Set string value... '.row' Key wordlistDir
317         Set string value... '.row' Value 'localWordlistDir$'
319         select Table Preferences
320         Append row
321         .row += 1
322         Set string value... '.row' Key wordlistName
323         Set string value... '.row' Value 'wordlistName$'
325         select Table Preferences
326         Write to table file... '.preferencesFile$'
327         Remove
329 endproc
331 ###############################################################
333 # Miscelaneous procedures
335 ###############################################################
337 # Create a file 'preferencesDirectory$'/sgc2/log/logPerformance
338 # This file contains the path to the target directory where logging
339 # is to be stored.
340 # Note that this procedure is nowhere used in SGC2. It has to
341 # be explicitely called in a separate script.
343 procedure activate_sgc2_logging .targetDirectory$
344         createDirectory("'preferencesDirectory$'/sgc2")
345         createDirectory("'preferencesLogDir$'")
346         
347         if startsWith(.targetDirectory$, "~")
348                 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
349         endif
350         
351         .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
352         newline$ >> 'preferencesLogDir$'/logPerformance.txt
353 endproc
355 # TTS synthesis
356 procedure synthesize_sound .pinyin$
357         .command$ = ""
358         if fileReadable(speakCommandFile$)
359                 .command$ < 'speakCommandFile$'
360                 .command$ = replace$(.command$, "'newline$'", " ", 0)
361                 if macintosh
362                         system_nocheck PATH=${PATH}:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
363                 elsif unix
364                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
365                 elsif windows
366                         system_nocheck call '.command$' "'.pinyin$'"'
367                 endif
368         endif
369 endproc