4 # Intializing Praat script
6 # SpeakGoodChinese: InitializeSGC2.praat defines and sets the global variables
7 # and loads general SGC2 code
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
29 # Information for logging
30 currentDate$ = date$()
31 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
32 currentLogDirectory$ = ""
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
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_"
64 config.input$ = "Microphone"
65 config.showBackground = 1
66 sgc2.noiseThresshold = -30
69 samplingFrequency = 44100
71 samplingFrequency = 44100
73 samplingFrequency = 44100
77 config.language$ = "EN"
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.
89 globaltablelists$ = ""
92 globalwordlists$ = "/etc/praat/sgc2/wordlists"
93 globaltablelists$ = "/etc/praat/sgc2/Data"
94 globalTTSdir$ = "/etc/praat/sgc2/TTS"
96 globalwordlists$ = "/Library/Praat/sgc2/wordlists"
97 globaltablelists$ = "/Library/Praat/sgc2/Data"
98 globalTTSdir$ = "/Library/Praat/sgc2/TTS"
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"
105 ###############################################################
107 # TTS (speech synthesis). Place commands in user info
109 speakCommandDir$ = "'preferencesDirectory$'/sgc2/TTS"
110 speakCommandFile$ = ""
113 .ttscommand$ = "speak"
117 .ttscommand$ = "espeak"
121 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
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_"
130 config.synthesis$ = "eSpeak"
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")
140 system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
141 .command_path$ < 'speakCommandDir$'/command_path.txt
143 system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
144 .command_path$ < 'speakCommandDir$'/command_path.txt
146 if fileReadable(.ttscommand$)
147 .command_path$ = .ttscommand$
150 # Remove any newlines
151 .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
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"
161 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
162 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
164 deleteFile("'speakCommandDir$'/command_path.txt")
167 # Close off (other TTS maybe a later option)
168 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
169 config.synthesis$ = "eSpeak"
173 config.shuffleLists = 1
175 defaultFont$ = "Helvetica"
178 # Set up directories if they do not exist already
179 call set_up_directories
180 # Get saved preferences
181 call read_preferences ""
183 # Set inital language
184 call set_language 'config.language$'
186 call load_word_list "'localWordlistDir$'" 0
191 ###############################################################
193 # Obligatory procedures
195 ###############################################################
197 # Initialize Demo Window
198 procedure init_window
200 if config.showBackground
201 call draw_background Background
203 demo Line width... 'defaultLineWidth'
205 call set_font_size 'defaultFontSize'
208 # Define buttons in a table
210 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
211 call set_window_title 'buttons$' '.windowTitle$'
212 # Display the word-list
215 call paint_logging_light
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")
228 # Retrieve and store setting between sessions
230 procedure read_preferences .preferencesFile$
231 if not fileReadable(.preferencesFile$)
232 .preferencesFile$ = preferencesDirectory$+"/sgc2/sgc2rc.txt"
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'
245 elsif variableExists(.variableName$+"$")
246 .variableValue$ = Get value... '.row' Value
247 .variableName$ = .variableName$+"$"
248 '.variableName$' = "'.variableValue$'"
252 select Table '.preferenceTable$'
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"
264 for .tableNum from 1 to 2
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$
280 if variableExists(.variableName$)
281 .value = '.variableName$'
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$'
292 if variableExists(.variableName$)
293 .value = '.variableName$'
295 elsif variableExists(.variableName$ + "$")
296 .variableName$ = .variableName$ + "$"
297 .value$ = '.variableName$'
303 select Table Preferences
306 Set string value... '.row' Key '.keyName$'
307 Set string value... '.row' Value '.value$'
312 # Some extra settings
313 select Table Preferences
316 Set string value... '.row' Key wordlistDir
317 Set string value... '.row' Value 'localWordlistDir$'
319 select Table Preferences
322 Set string value... '.row' Key wordlistName
323 Set string value... '.row' Value 'wordlistName$'
325 select Table Preferences
326 Write to table file... '.preferencesFile$'
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
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$'")
347 if startsWith(.targetDirectory$, "~")
348 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
351 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
352 newline$ >> 'preferencesLogDir$'/logPerformance.txt
356 procedure synthesize_sound .pinyin$
358 if fileReadable(speakCommandFile$)
359 .command$ < 'speakCommandFile$'
360 .command$ = replace$(.command$, "'newline$'", " ", 0)
362 system_nocheck PATH=${PATH}:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
364 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
366 system_nocheck call '.command$' "'.pinyin$'"'