6 include ToneProt/ToneRecognition.praat
8 demo Select outer viewport... 0 100 0 100
9 demo Axes... 0 100 0 100
11 # Initialize parameters
18 soundDir$ = "./Sounds"
26 Read from file... Data/Buttons_'language$'.Table
27 buttons$ = selected$("Table")
29 Read from file... wordlists/Example_list/wordlist.txt
30 wordlist$ = selected$("Table")
31 numberOfWords = Get number of rows
38 while demoWaitForInput()
43 call buttonClicked 'buttons$' '.clickX' '.clickY'
44 .label$ = buttonClicked.label$
45 elsif demoKeyPressed()
46 .pressed$ = demoKey$()
47 call keyPressed 'buttons$' '.pressed$'
48 .label$ = keyPressed.label$
54 call Draw_button 'buttons$' '.label$' 1
55 call process_label '.label$'
57 call Draw_button 'buttons$' '.label$' 0
64 ########################################################
66 # Definitions of procedures
68 ########################################################
71 procedure process_label .label$
74 elsif .label$ = "Refresh"
76 elsif .label$ = "Record"
79 call write_feedback Feedback
80 elsif .label$ = "Play"
81 call play_sound 'sampleSound$'
82 elsif .label$ = "Example"
84 elsif .label$ = "Previous"
85 call display_text Grey
87 call display_text Black
89 call draw_tone_contour
90 elsif .label$ = "Next"
91 call display_text Grey
93 call display_text Black
95 call draw_tone_contour
101 # Initialize Demo Window
102 procedure init_window
104 demo Line width... 'defaultLineWidth'
105 demo 'defaultFontSize'
107 demo Select outer viewport... 0 100 0 100
108 demo Axes... 0 100 0 100
109 # Define buttons in a table
112 call display_text Black
114 call draw_tone_contour
118 procedure init_buttons
119 call Draw_all_buttons 'buttons$'
123 procedure Draw_all_buttons .table$
124 select Table '.table$'
125 .numRows = Get number of rows
128 .label$ = Get value... '.row' Label
129 call Draw_button '.table$' '.label$' 0
133 # Draw a tone contour
134 procedure draw_tone_contour
136 demo Paint rectangle... White 20 80 40 100
137 select Table 'wordlist$'
138 if currentWord > 0 and currentWord <= numberOfWords
139 .sound$ = Get value... 'currentWord' Sound
140 .pinyin$ = Get value... 'currentWord' Pinyin
141 execute "ToneProt/DrawToneContour.praat" '.pinyin$' 'register'
145 # Draw a button from a predefined button table
146 procedure Draw_button .table$ .label$ .push
147 select Table '.table$'
148 .row = Search column... Label '.label$'
150 .leftX = Get value... '.row' LeftX
151 .rightX = Get value... '.row' RightX
152 .lowY = Get value... '.row' LowY
153 .highY = Get value... '.row' HighY
154 # Reset and erase button area
155 demo Select outer viewport... 0 100 0 100
156 demo Axes... 0 100 0 100
157 demo Line width... 'defaultLineWidth'
159 demo Paint rectangle... White .leftX .rightX .lowY .highY
161 .buttonText$ = Get value... '.row' Text
162 .buttonColor$ = Get value... '.row' Color
163 .buttonDraw$ = Get value... '.row' Draw
164 .buttonKey$ = Get value... '.row' Key
165 .newText$ = replace_regex$(.buttonText$, "['.buttonKey$']", "#%&", 1)
167 .newText$ = .buttonText$
171 if .buttonColor$ = "Red"
172 .buttonColor$ = "Pink"
174 .buttonColor$ = "Grey"
179 demo Draw rounded rectangle... .leftX .rightX .lowY .highY 3
180 .centerX = (.leftX + .rightX)/2
181 .centerY = (.lowY + .highY)/2
182 .radius = (.highY - .lowY )/4
184 demo Text... '.centerX' Centre '.lowY' Bottom '.newText$'
186 call '.buttonDraw$' '.buttonColor$' '.centerX' '.centerY' '.radius'
192 procedure display_text .color$
193 select Table 'wordlist$'
194 if currentWord < 0 or currentWord > numberOfWords+1
199 currentWord = numberOfWords
205 if currentWord > 0 and currentWord <= numberOfWords
206 .displayText$ = Get value... 'currentWord' Pinyin
207 elsif currentWord = 0 or currentWord = numberOfWords+1
208 .displayText$ = "---"
210 demo Paint rectangle... White 20 80 20 40
213 demo Text... 50 Centre 20 Bottom '.displayText$'
215 demo 'defaultFontSize'
219 procedure generate_example
220 select Table 'wordlist$'
221 if currentWord > 0 and currentWord <= numberOfWords
222 .sound$ = Get value... 'currentWord' Sound
223 .pinyin$ = Get value... 'currentWord' Pinyin
224 if fileReadable(.sound$)
225 Read from file... '.sound$'
229 execute "ToneProt/HumToneContour.praat" '.pinyin$' 'register'
234 # A stub for buttons that do not have a drawing routine (yet)
235 procedure DrawNull .color$ .x .y .size
238 procedure DrawRecord .color$ .x .y .size
240 demo Paint circle... '.color$' '.x' '.y' '.size'
243 procedure DrawPlay .color$ .x .y .size
245 call drawTriangle 1 .x .y .size
248 procedure DrawPrevious .color$ .x .y .size
251 call drawTriangle -1 .x .y .size
252 .currentX = drawTriangle.currentX
253 .endX = .currentX - 0.5
256 demo Paint rectangle... '.color$' '.currentX' '.endX' '.lowY' '.highY'
259 procedure DrawNext .color$ .x .y .size
262 call drawTriangle 1 .x .y .size
263 .currentX = drawTriangle.currentX
264 .endX = .currentX + 0.5
267 demo Paint rectangle... '.color$' '.currentX' '.endX' '.lowY' '.highY'
270 procedure DrawQuit .color$ .x .y .size
271 demo Line width... 4.0
276 demo Draw line... .xstart .ystart .xend .yend
281 demo Draw line... .xstart .ystart .xend .yend
282 demo Line width... 'defaultLineWidth'
285 procedure DrawRefresh .color$ .x .y .size
287 demo Line width... 4.0
288 demo Draw arc... '.x' '.y' '.size' 0 270
289 demo Line width... 'defaultLineWidth'
292 procedure drawTriangle .direction .x .y .size
293 # Make sure direction = +/- 1
297 .direction /= abs(.direction)
300 .currentHeight = .size
301 .currentX = .x - .direction*.size
303 demo Line width... 2.0
305 while .currentHeight> 0
306 .ystart = .y + .currentHeight
307 .yend = .y - .currentHeight
308 demo Draw line... .currentX .ystart .currentX .yend
309 .currentHeight -= .offset *3/4
310 .currentX += .direction*.offset * 1.5
312 demo Line width... 'defaultLineWidth'
316 procedure buttonClicked table$ .x .y
318 select Table 'buttons$'
319 .bo$ = selected$("Table")
321 .numRows = Get number of rows
324 .leftX = '.bo$'[.i, "LeftX"]
325 .rightX = '.bo$'[.i, "RightX"]
326 .lowY = '.bo$'[.i, "LowY"]
327 .highY = '.bo$'[.i, "HighY"]
328 if .x > .leftX and .x < .rightX and .y > .lowY and .y < .highY
329 .label$ = '.bo$'$[.i, "Label"]
335 procedure keyPressed table$ .pressed$
337 .lowerPressed$ = replace_regex$(.pressed$, ".", "\L&", 0)
338 .upperPressed$ = replace_regex$(.pressed$, ".", "\U&", 0)
339 select Table 'buttons$'
340 .bo$ = selected$("Table")
342 .numRows = Get number of rows
345 .key$ = '.bo$'$[.i, "Key"]
346 if .key$ = .lowerPressed$ or .key$ = .upperPressed$
347 .label$ = '.bo$'$[.i, "Label"]
353 procedure record_sound
354 if sampleSound$ != ""
355 select Sound 'sampleSound$'
359 demo Paint rectangle... White 20 90 14 25
360 demo Paint circle... Red 5 85 2.5
362 Record Sound (fixed time)... Microphone 0.99 1 44100 4
363 demo Paint circle... White 5 85 3
368 sampleSound$ = selected$("Sound")
371 select Sound 'sampleSound$'
374 procedure recognizeTone
375 select Table 'wordlist$'
376 if currentWord > 0 and currentWord <= numberOfWords
377 .sound$ = Get value... 'currentWord' Sound
378 .pinyin$ = Get value... 'currentWord' Pinyin
379 execute "ToneProt/SGC_ToneProt.praat" 'sampleSound$' '.pinyin$' 'register' 'precision' Pictures 'logPerformance'
383 procedure write_feedback .table$
384 select Table '.table$'
385 .line1$ = Get value... 1 Text
386 .line2$ = Get value... 2 Text
388 if index(.line1$, "???") > 0
390 elsif .line2$ = "Correct"
396 demo Paint rectangle... White 20 90 14 25
397 demo Text... 50 Centre 20 Bottom '.line1$'
398 demo Text... 50 Centre 15 Bottom '.line2$'
399 demo 'defaultFontSize'
403 procedure play_sound .sound$
405 select Sound '.sound$'
410 procedure end_program