Some small changes in the scripts for Linux
[sgc2.git] / sgc2.praat
blobdbfafce790775c815025e939875b1fc3ed6c75b0
2 # SpeakGoodChinese 2.0
3
4 # Master Praat script
6 include ToneProt/ToneRecognition.praat
8 demo Select outer viewport... 0 100 0 100
9 demo Axes... 0 100 0 100
11 # Initialize parameters
12 currentWord = 1
13 pinyin$ = ""
14 sampleSound$ = ""
16 #language$ = "EN"
17 language$ = "NL"
18 soundDir$ = "./Sounds"
19 register = 180
20 precision = 3
21 logPerformance = 0
22 shuffleLists = 1
23 defaultFontSize = 12
24 defaultLineWidth = 1
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
32 if shuffleLists
33     Randomize rows
34 endif
36 call init_window
38 while demoWaitForInput()
39         .label$ = ""
40         if demoClicked()
41                 .clickX = demoX()
42                 .clickY = demoY()
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$
49         endif
50         
51         # Do things
52         if .label$ != ""
53                 # Push button down
54                 call Draw_button 'buttons$' '.label$' 1
55                 call process_label '.label$'
56                 # push button up
57                 call Draw_button 'buttons$' '.label$' 0
58         endif
59 endwhile
61 call end_program
64 ########################################################
65
66 # Definitions of procedures
67
68 ########################################################
70 # Do what is asked
71 procedure process_label .label$
72         if .label$ = "Quit"
73                 call end_program
74         elsif .label$ = "Refresh"
75                 call init_window
76         elsif .label$ = "Record"
77         call record_sound
78         call recognizeTone
79         call write_feedback Feedback
80         elsif .label$ = "Play"
81         call play_sound 'sampleSound$'
82         elsif .label$ = "Example"
83                 call generate_example
84         elsif .label$ = "Previous"
85                 call display_text Grey
86                 currentWord -= 1
87                 call display_text Black
88                 # Draw the contour
89                 call draw_tone_contour
90         elsif .label$ = "Next"
91                 call display_text Grey
92                 currentWord += 1
93                 call display_text Black
94                 # Draw the contour
95                 call draw_tone_contour
96         endif
97 endproc
101 # Initialize Demo Window
102 procedure init_window
103     demo Erase all
104         demo Line width... 'defaultLineWidth'
105         demo 'defaultFontSize'
106         demo Black
107     demo Select outer viewport... 0 100 0 100
108     demo Axes... 0 100 0 100
109     # Define buttons in a table
110     call init_buttons
111         # Write text
112         call display_text Black
113         # Draw the contour
114         call draw_tone_contour
115 endproc
117 # Intialize buttons
118 procedure init_buttons
119         call Draw_all_buttons 'buttons$'
120 endproc
122 # Draw all buttons
123 procedure Draw_all_buttons .table$
124         select Table '.table$'
125         .numRows = Get number of rows
126         
127         for .row to .numRows
128                 .label$ = Get value... '.row' Label
129                 call Draw_button '.table$' '.label$' 0
130         endfor
131 endproc
133 # Draw a tone contour
134 procedure draw_tone_contour
135     # Wipe screen
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'
142         endif   
143 endproc
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'
158     demo 
159         demo Paint rectangle... White .leftX .rightX .lowY .highY
160         
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)
166         if .newText$ = ""
167                 .newText$ = .buttonText$
168         endif
169         if .push
170                 demo Grey
171                 if .buttonColor$ = "Red"
172                         .buttonColor$ = "Pink"
173                 else
174                         .buttonColor$ = "Grey"
175                 endif
176         else
177         demo Black
178         endif
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
183     demo '.buttonColor$'
184     demo Text... '.centerX' Centre '.lowY' Bottom '.newText$'
185     .centerY += 1
186     call '.buttonDraw$' '.buttonColor$' '.centerX' '.centerY' '.radius' 
187     demo Black
188         demoShow()
189 endproc
191 # Text display
192 procedure display_text .color$
193         select Table 'wordlist$'
194         if currentWord < 0 or currentWord > numberOfWords+1
195         if shuffleLists
196                     Randomize rows
197         endif
198                 if currentWord < 0
199                         currentWord = numberOfWords
200                 else
201                         currentWord = 1
202                 endif
203         endif
204         
205         if currentWord > 0 and currentWord <= numberOfWords
206                 .displayText$ = Get value... 'currentWord' Pinyin
207         elsif currentWord = 0 or currentWord = numberOfWords+1
208                 .displayText$ = "---"
209         endif
210         demo Paint rectangle... White 20 80 20 40
211         demo '.color$'
212         demo Font size... 32
213         demo Text... 50 Centre 20 Bottom '.displayText$'
214         demo Black
215         demo 'defaultFontSize'
216 endproc
218 # The example
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$'
226                         Play
227                         Remove
228                 else
229                         execute "ToneProt/HumToneContour.praat" '.pinyin$' 'register'
230                 endif
231         endif
232 endproc
234 # A stub for buttons that do not have a drawing routine (yet)
235 procedure DrawNull .color$ .x .y .size
236 endproc
238 procedure DrawRecord .color$ .x .y .size
239         .size /= 2
240     demo Paint circle... '.color$' '.x' '.y' '.size'
241 endproc
243 procedure DrawPlay .color$ .x .y .size
244         demo '.color$'
245         call drawTriangle 1 .x .y .size
246 endproc
248 procedure DrawPrevious .color$ .x .y .size
249         demo '.color$'
250         .size *= 2/3
251         call drawTriangle -1 .x .y .size
252         .currentX = drawTriangle.currentX
253         .endX = .currentX - 0.5
254         .lowY = .y - .size
255         .highY = .y + .size
256         demo Paint rectangle... '.color$' '.currentX' '.endX' '.lowY' '.highY'
257 endproc
259 procedure DrawNext .color$ .x .y .size
260         demo '.color$'
261         .size *= 2/3
262         call drawTriangle 1 .x .y .size
263         .currentX = drawTriangle.currentX
264         .endX = .currentX + 0.5
265         .lowY = .y - .size
266         .highY = .y + .size
267         demo Paint rectangle... '.color$' '.currentX' '.endX' '.lowY' '.highY'
268 endproc
270 procedure DrawQuit .color$ .x .y .size
271         demo Line width... 4.0
272         .xstart = .x - .size
273         .ystart = .y + .size
274         .xend = .x + .size
275         .yend = .y - .size
276         demo Draw line... .xstart .ystart .xend .yend
277         .xstart = .x - .size
278         .ystart = .y - .size
279         .xend = .x + .size
280         .yend = .y + .size
281         demo Draw line... .xstart .ystart .xend .yend
282         demo Line width... 'defaultLineWidth'
283 endproc
285 procedure DrawRefresh .color$ .x .y .size
286         .size /= 2
287         demo Line width... 4.0
288         demo Draw arc... '.x' '.y' '.size' 0 270
289         demo Line width... 'defaultLineWidth'
290 endproc
292 procedure drawTriangle .direction .x .y .size
293         # Make sure direction = +/- 1
294         if .direction = 0
295                 .direction = 1 
296         endif
297         .direction /= abs(.direction)
298         
299         .offset = 0.01
300         .currentHeight = .size
301         .currentX = .x - .direction*.size
302         
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
311         endwhile
312         demo Line width... 'defaultLineWidth'
313 endproc
315 # Get the label
316 procedure buttonClicked table$ .x .y
317         .label$ = ""
318         select Table 'buttons$'
319         .bo$ = selected$("Table")
320         .bo$ = "Table_"+.bo$
321         .numRows = Get number of rows
322         for .i to .numRows
323                 if .label$ = ""
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"]
330                         endif
331                 endif
332         endfor
333 endproc
335 procedure keyPressed table$ .pressed$
336         .label$ = ""
337         .lowerPressed$ = replace_regex$(.pressed$, ".", "\L&", 0)
338         .upperPressed$ = replace_regex$(.pressed$, ".", "\U&", 0)
339         select Table 'buttons$'
340         .bo$ = selected$("Table")
341         .bo$ = "Table_"+.bo$
342         .numRows = Get number of rows
343         for .i to .numRows
344                 if .label$ = ""
345                         .key$ = '.bo$'$[.i, "Key"]
346                         if .key$ = .lowerPressed$ or .key$ = .upperPressed$
347                                 .label$ = '.bo$'$[.i, "Label"]
348                         endif
349                 endif
350         endfor
351 endproc
353 procedure record_sound
354     if sampleSound$ != ""
355         select Sound 'sampleSound$'
356         Remove
357         sampleSound$ = ""
358     endif
359     demo Paint rectangle... White 20 90 14 25
360     demo Paint circle... Red 5 85 2.5
361     demoShow()
362     Record Sound (fixed time)... Microphone 0.99 1 44100 4
363     demo Paint circle... White 5 85 3
364     demoShow()
365     Rename... Tmp
366     Resample... 10000 50
367     Rename... Source
368     sampleSound$ = selected$("Sound")
369     select Sound Tmp
370     Remove
371     select Sound 'sampleSound$'
372 endproc
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'
380         endif
381 endproc
383 procedure write_feedback .table$
384     select Table '.table$'
385     .line1$ = Get value... 1 Text
386     .line2$ = Get value... 2 Text
387     .color$ = "Red"
388     if index(.line1$, "???") > 0
389         .color$ = "Black"
390     elsif .line2$ = "Correct"
391         .color$ = "Green"
392     endif
394     demo 14
395     demo '.color$'
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'
400         
401 endproc
403 procedure play_sound .sound$
404     if .sound$ <> ""
405         select Sound '.sound$'
406         Play
407     endif
408 endproc
410 procedure end_program
411         demo Erase all
412         select all
413         Remove
414         exit
415 endproc