Added segmentation for third tones as part of creaky voice detection, working version
[sgc2.git] / ToneProt / ToneScript.praat
blobd2a5b6d1b33aacca6f276450c9b7370544373abe
1 #! praat
3 #     SpeakGoodChinese: toneScript.praat generates synthetic tone contours
4 #     for Mandarin Chinese
5 #     Copyright (C) 2007  R.J.J.H. van Son
6 #     The SpeakGoodChinese team are:
7 #     Guangqin Chen, Zhonyan Chen, Stefan de Konink, Eveline van Hagen, 
8 #     Rob van Son, Dennis Vierkant, David Weenink
9
10 #     This program is free software; you can redistribute it and/or modify
11 #     it under the terms of the GNU General Public License as published by
12 #     the Free Software Foundation; either version 2 of the License, or
13 #     (at your option) any later version.
14
15 #     This program is distributed in the hope that it will be useful,
16 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #     GNU General Public License for more details.
19
20 #     You should have received a copy of the GNU General Public License
21 #     along with this program; if not, write to the Free Software
22 #     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23
24 # form Enter pinyin and tone 1 frequency
25 #       word toneScript.inputWord ba1ba1
26 #       positive toneScript.upperRegister_(Hz) 300
27 #     real toneScript.range_Factor 1
28 #     real toneScript.durationScale 1
29 #     optionmenu toneScript.generate 1
30 #         option Pitch
31 #         option Sound
32 #         option CorrectPitch
33 #         option CorrectSound
34 # endform
36 # Get the rules of the tones
37 # include ToneRules.praat
39 #call toneScript 'toneScript.inputWord$' 'toneScript.upperRegister' 'toneScript.range_Factor' 'toneScript.durationScale' 'toneScript.generate$'
41 procedure toneScript toneScript.inputWord$ toneScript.upperRegister toneScript.range_Factor toneScript.durationScale toneScript.generate$
42         # To supress the ToneList, change to 0
43         toneScript.createToneList = 1
44         if rindex_regex(toneScript.generate$, "Correct") > 0
45                 toneScript.createToneList = 0
46         endif
47         
48         # First tone to check
49         .skipSyll$ = "()"
50         .startSyll = 0
51         if index_regex(toneScript.generate$, "_[\d]+$") > 0
52                 .pos = index_regex(toneScript.generate$, "_[\d]+$")
53                 .left$ = left$(toneScript.generate$, .pos)
54                 .startSyll = extractNumber(toneScript.generate$, .left$) - 1
55                 if .startSyll <> undefined and .startSyll > 0
56                         .skipSyll$ = "([^\d]+[\d]+){'.startSyll'}"
57                 else
58                         .startSyll = 0
59                 endif
60         endif
62         # Limit lowest tone
63         toneScript.absoluteMinimum = 80
65         toneScript.prevTone = -1
66         toneScript.nextTone = -1
68         toneScript.point = 0
69         toneScript.lastFrequency = 0
71         # Clean up input
72         if toneScript.inputWord$ <> ""
73         toneScript.inputWord$ = replace_regex$(toneScript.inputWord$, "^\s*(.+)\s*$", "\1", 1)
74         endif
76         # Add a tone movement. The current time toneScript.point is 'toneScript.point'
77         toneScript.delta = 0.0000001
78         if toneScript.durationScale <= 0
79         toneScript.durationScale = 1.0
80         endif
81         toneScript.segmentDuration = 0.150
82         toneScript.fixedDuration = 0.12
84         #
85         # Movements
86         # start * ?Semit is a fall
87         # start / ?Semit is a rise
88         # 1/(12 semitones)
89         toneScript.octave = 0.5
90         # 1/(9 semitones)
91         toneScript.nineSemit = 0.594603557501361
92         # 1/(6 semitones)
93         toneScript.sixSemit = 0.707106781186547
94         # 1/(3 semitones) down
95         toneScript.threeSemit = 0.840896415253715
96         # 1/(2 semitones) down
97         toneScript.twoSemit = 0.890898718140339
98         # 1/(1 semitones) down
99         toneScript.oneSemit = 0.943874313
100         # 1/(4 semitones) down
101         toneScript.fourSemit = toneScript.twoSemit * toneScript.twoSemit
102         # 1/(5 semitones) down
103         toneScript.fiveSemit = toneScript.threeSemit * toneScript.twoSemit
105         toneScript.frequency_Range = toneScript.octave
106         if toneScript.range_Factor > 0
107         toneScript.frequency_Range =  toneScript.frequency_Range * toneScript.range_Factor
108         endif
110         # Previous end frequency
111         toneScript.lastFrequency = 0
112         # Split input into syllables
113         toneScript.margin = 0.25
115         # Get a list of items
116         if toneScript.createToneList = 1
117         Create Table with column names... ToneList 36 Word
119         for .i from 1 to 36
120                 select Table ToneList
121                 Set string value... '.i' Word ------EMPTY
122         endfor
123         endif
125         toneScript.syllableCount = length(replace_regex$(toneScript.inputWord$, "[^\d]+([\d]+)", "1", 0))
126         if toneScript.syllableCount < .startSyll + 1
127                 .skipSyll$ = "()"
128         endif
129         toneScript.wordNumber = 0
130         toneScript.lowerBound = 0
131         if rindex(toneScript.generate$, "Correct") <= 0
132         for toneScript.first from toneScript.lowerBound to 4
133                 toneScript.currentWord$ = replace_regex$(toneScript.inputWord$, "^('.skipSyll$')([^\d]+)([\d]+)(.*)$", "\1\3'toneScript.first'\5", 1)
134                 for toneScript.second from 0 to 4
135                         if (toneScript.first <> 5 and toneScript.second <> 5) and (toneScript.syllableCount > 1 or toneScript.second == 1)
136                                 toneScript.currentWord$ = replace_regex$(toneScript.currentWord$, "^('.skipSyll$')([^\d]+)([\d]+)([^\d]+)([\d]+)(.*)$", "\1\3'toneScript.first'\5'toneScript.second'\7", 1)
137                 # Write name in list
138                         toneScript.wordNumber = toneScript.wordNumber+1
139                         if toneScript.createToneList = 1
140                                 select Table ToneList
141                         toneScript.listLength = Get number of rows
142                         toneScript.listLength = toneScript.listLength + 1
143                         for toneScript.currLength from toneScript.listLength to toneScript.wordNumber
144                                 Append row
145                                 Set string value... 'toneScript.currLength' Word ------EMPTY
146                         endfor
147                         Set string value... 'toneScript.wordNumber' Word 'toneScript.currentWord$'
148                         endif
149                         # Actually, generate something
150                                         call generateWord 'toneScript.generate$' 'toneScript.currentWord$' 'toneScript.upperRegister'
151                         endif
152                 endfor
153         endfor
154         
155         # 6,6
156         #toneScript.first = 6
157             #toneScript.currentWord$ = replace_regex$(toneScript.inputWord$, "^('.skipSyll$')([^\d]+)([\d]+)(.*)$", "\1\3'toneScript.first'\5", 1)
158             #toneScript.second = 6
159                 #if toneScript.syllableCount > 1
160                 #       toneScript.currentWord$ = replace_regex$(toneScript.currentWord$, "^('.skipSyll$')([^\d]+)([\d]+)([^\d]+)([\d]+)(.*)$", "\1\3'toneScript.first'\5'toneScript.second'\7", 1)
161         #    # Write name in list
162         #    toneScript.wordNumber = toneScript.wordNumber+1
163         #    if toneScript.createToneList = 1
164                 #               select Table ToneList
165         #        toneScript.listLength = Get number of rows
166         #        toneScript.listLength = toneScript.listLength + 1
167         #        for toneScript.currLength from toneScript.listLength to toneScript.wordNumber
168                 #                       Append row
169                 #                       Set string value... 'toneScript.currLength' Word ------EMPTY
170         #        endfor
171         #       Set string value... 'toneScript.wordNumber' Word 'toneScript.currentWord$'
172         #    endif
173                 #endif
174                 
175         # "broken" third tones
176         if index_regex(toneScript.inputWord$, "3") > 0
177                     toneScript.currentWord$ = replace$(toneScript.inputWord$, "3", "9", 0)
178                 # Write name in list
179                 toneScript.wordNumber = toneScript.wordNumber+1
180             if toneScript.createToneList = 1
181                                 select Table ToneList
182                 toneScript.listLength = Get number of rows
183                 toneScript.listLength = toneScript.listLength + 1
184                 Set string value... 'toneScript.wordNumber' Word 'toneScript.currentWord$'
185             endif
186                         # Actually, generate something
187                         call generateWord 'toneScript.generate$' 'toneScript.currentWord$' 'toneScript.upperRegister'
188                 endif
189                 
190         # 6 tones
191         if toneScript.syllableCount > 1
192                     toneScript.currentWord$ = replace_regex$(toneScript.inputWord$, "[\d+]", "6", 0)
193                 # Write name in list
194                 toneScript.wordNumber = toneScript.wordNumber+1
195             if toneScript.createToneList = 1
196                                 select Table ToneList
197                 toneScript.listLength = Get number of rows
198                 toneScript.listLength = toneScript.listLength + 1
199                 Set string value... 'toneScript.wordNumber' Word 'toneScript.currentWord$'
200             endif
201                         # Actually, generate something
202                         call generateWord 'toneScript.generate$' 'toneScript.currentWord$' 'toneScript.upperRegister'
203                 endif
204         else
205         call generateWord 'toneScript.generate$' 'toneScript.inputWord$' 'toneScript.upperRegister'
206         endif
207 endproc
209 procedure extractTone .syllable$
210         toneScript.toneSyllable = -1
211         .toneScript.currentToneText$ = replace_regex$(.syllable$, "^[^\d]+([\d]+)(.*)$", "\1", 0)
212         toneScript.toneSyllable = extractNumber(.toneScript.currentToneText$, "")
213 endproc
215 procedure convertVoicing toneScript.voicingSyllable$
216         # Remove tones
217         toneScript.voicingSyllable$ = replace_regex$(toneScript.voicingSyllable$, "^([^\d]+)[\d]+", "\1", 0)
218         # Convert voiced consonants
219         toneScript.voicingSyllable$ = replace_regex$(toneScript.voicingSyllable$, "(ng|[wrlmny])", "C", 0)
220         # Convert unvoiced consonants
221         toneScript.voicingSyllable$ = replace_regex$(toneScript.voicingSyllable$, "(sh|ch|zh|[fsxhktpgqdbzcj])", "U", 0)
222         # Convert vowels
223         toneScript.voicingSyllable$ = replace_regex$(toneScript.voicingSyllable$, "([aiuoe\XFC])", "V", 0)
224 endproc
226 procedure addToneMovement .syllable$ toneScript.topLine toneScript.prevTone toneScript.nextTone
227         # Get tone
228         toneScript.toneSyllable = -1
229         call extractTone '.syllable$'
230     if toneScript.toneSyllable = 3 and toneScript.nextTone = 3
231         toneScript.toneSyllable = 2
232     endif
234         # Get voicing pattern
235         toneScript.voicingSyllable$ = ""
236         call convertVoicing '.syllable$'
238         # Account for tones in duration
239         toneScript.toneFactor = 1
240     # Scale the duration of the current syllable
241     call toneDuration
242         toneScript.toneFactor = toneScript.toneFactor * toneScript.durationScale
244         # Unvoiced part
245         toneScript.unvoicedDuration = 0
246         if rindex_regex(toneScript.voicingSyllable$, "U") = 1
247                 toneScript.point = toneScript.point + toneScript.delta
248         Add point... 'toneScript.point' 0
249                 toneScript.point = toneScript.point + toneScript.segmentDuration * toneScript.toneFactor
250         Add point... 'toneScript.point' 0
251         toneScript.unvoicedDuration = toneScript.segmentDuration * toneScript.toneFactor
252         endif
253         # Voiced part
254         toneScript.voiceLength$ = replace_regex$(toneScript.voicingSyllable$, "U*([CV]+)U*", "\1", 0)
255         toneScript.voicedLength = length(toneScript.voiceLength$)
256         toneScript.voicedDuration = toneScript.toneFactor * (toneScript.segmentDuration*toneScript.voicedLength + toneScript.fixedDuration)
257         toneScript.point = toneScript.point + toneScript.delta
259     # Write contour of each tone
260     # Note that tones are influenced by the previous (tone 0) and next (tone 3)
261     # tones. Tone 6 is the Dutch intonation
262     # sqrt(toneScript.frequency_Range) is the mid toneScript.point
263     if toneScript.topLine * toneScript.frequency_Range < toneScript.absoluteMinimum
264         toneScript.frequency_Range = toneScript.absoluteMinimum / toneScript.topLine
265     endif
267     call toneRules
268         
269     toneScript.lastFrequency = toneScript.endPoint
271 endproc
273 procedure wordToTones .wordInput$ toneScript.highPitch
274         .currentRest$ = .wordInput$;
275         toneScript.syllableCount = 0
276         .length = 2 * toneScript.margin
278     # Split syllables
279         while rindex_regex(.currentRest$, "^[^\d]+[\d]+") > 0
280         toneScript.syllableCount += 1
281         syllable'toneScript.syllableCount'$ = replace_regex$(.currentRest$, "^([^\d]+[\d]+)(.*)$", "\1", 1)
282                 toneScript.currentSyllable$ = syllable'toneScript.syllableCount'$
284                 # For next round
285                 .currentRest$ = replace_regex$(.currentRest$, "^([^\d]+[\d]+)(.*)$", "\2", 1)
286                 # Get next syllable
287                 .nextSyllable$ = replace_regex$(.currentRest$, "^([^\d]+[\d]+)(.*)$", "\1", 1)
288                 call extractTone '.nextSyllable$'
289                 toneScript.nextTone = toneScript.toneSyllable
291                 # Get the current tone
292                 call extractTone 'toneScript.currentSyllable$'
293                 toneScript.toneSyllable'toneScript.syllableCount' = toneScript.toneSyllable
294                 toneScript.currentTone = toneScript.toneSyllable'toneScript.syllableCount'
296                 # Get the Voicing pattern
297                 call convertVoicing 'toneScript.currentSyllable$'
298                 voicingSyllable'toneScript.syllableCount'$ = toneScript.voicingSyllable$
299                 currentVoicing$ = voicingSyllable'toneScript.syllableCount'$
301                 # Calculate new .length
302             # Account for tones in duration
303             toneScript.toneFactor = 1
304         # Scale the duration of the current syllable
305         call toneDuration
306             toneScript.toneFactor = toneScript.toneFactor * toneScript.durationScale
308                 .length = .length + toneScript.toneFactor * (length(voicingSyllable'toneScript.syllableCount'$) * (toneScript.segmentDuration + toneScript.delta) + toneScript.fixedDuration)
310                 # Safety valve
311                 if toneScript.syllableCount > 2000
312                         exit
313                 endif
314         endwhile
315         
316         # Create tone pattern
317         toneScript.pitchTier = Create PitchTier... '.wordInput$' 0 '.length'
318         toneScript.pitchTierWord$ = .wordInput$
319         # Create TextGrid with syllables
320         toneScript.textGrid = Create TextGrid: 0, .length, "Tones", ""
321         Rename: .wordInput$
322         .int = 0;
324         # Add start toneScript.margin
325         select toneScript.pitchTier
326         toneScript.lastFrequency = 0
327     toneScript.point = 0
328         Add point... 'toneScript.point' 0
329         toneScript.point = toneScript.margin
330         Add point... 'toneScript.point' 0
331         
332     # Add interval to TextGrid
333         select toneScript.textGrid
334         Insert boundary: 1, toneScript.point
335         .int += 1
336         Set interval text: 1, .int, ""
338     toneScript.lastTone = -1
339     toneScript.followTone = -1
340         for .i from 1 to toneScript.syllableCount
341                 select toneScript.pitchTier
342                 toneScript.currentSyllable$ = syllable'.i'$
343         toneScript.currentTone = toneScript.toneSyllable'.i'
344         toneScript.followTone = -1
345         if .i < toneScript.syllableCount
346             .j = .i+1
347             toneScript.followTone = toneScript.toneSyllable'.j'
348         endif
349                 .lastPoint = toneScript.point
350                 call addToneMovement 'toneScript.currentSyllable$' 'toneScript.highPitch' 'toneScript.lastTone' 'toneScript.followTone'
352         toneScript.lastTone = toneScript.currentTone
353         
354         # Add interval to TextGrid
355                 select toneScript.textGrid
356                 if toneScript.unvoicedDuration > 0
357                         Insert boundary: 1, .lastPoint + toneScript.unvoicedDuration
358                         .int += 1
359                         Set interval text: 1, .int, "U"
360                 endif
361                 Insert boundary: 1, toneScript.point
362                 .int += 1
363                 Set interval text: 1, .int, "'toneScript.currentTone'"
364         endfor
366         # Add end toneScript.margin
367         select toneScript.pitchTier
368         toneScript.point = toneScript.point + toneScript.delta
369         Add point... 'toneScript.point' 0
370         toneScript.point = toneScript.point + toneScript.margin
371         Add point... 'toneScript.point' 0
372 endproc
374 procedure generateWord toneScript.whatToGenerate$ toneScript.theWord$ toneScript.upperRegister
376         # First generate model contour
377         call wordToTones 'toneScript.theWord$' 'toneScript.upperRegister'
378         
379         if index(toneScript.whatToGenerate$, "TextGrid") <= 0
380                 if variableExists("toneScript.textGrid") and toneScript.textGrid > 0
381                         select toneScript.textGrid
382                         Remove
383                 endif
384         endif
385         
386         # Generate pitch
387         if toneScript.pitchTierWord$ = toneScript.theWord$
388                 select toneScript.pitchTier
389         else
390                 select PitchTier 'toneScript.theWord$'
391         endif
392     toneScript.pitch = noprogress To Pitch... 0.0125 60.0 600.0
393         Rename... theOrigWord
394         Smooth... 10
395         Rename... 'toneScript.theWord$'
396         select Pitch theOrigWord
397         Remove
399         # Then look if "real" model exists, and use that
400         if index_regex(config.strict$, "[^0-9]") > 0
401                 config.strict$ = "1"    
402         endif
403         .userLevel = 'config.strict$'
404         if 0 and .userLevel >= sgc.highestLevel
405 ...    and (fileReadable("'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.Pitch")
406 ...             or fileReadable("'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.wav"))
407                 # Get mean of generated contour
408                 select toneScript.pitch
409                 Rename... GeneratedContour
410                 toneScript.generatedMean = do ("Get mean...", 0, 0, "Hertz")
411                 toneScript.generatedMaximum = do ("Get maximum...", 0, 0, "Hertz", "Parabolic")
412                 Remove
413                 if fileReadable("'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.Pitch")
414                         Read from file... 'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.Pitch
415                 else
416                         .modelSound = Read from file... 'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.wav
417                 select .modelSound
418                 # Third tones get really low
419                 if index(toneScript.theWord$, "3") > 0
420                                 call convert2Pitch 20 600
421                                 .modelPitch = Kill octave jumps
422                                 select convert2Pitch.object
423                                 Remove
424                         else
425                                 call convert2Pitch 60 600
426                                 .modelPitch = convert2Pitch.object
427                         endif
428                         select .modelPitch
429                         Rename... ModelPitch
431                         select .modelSound
432                         Remove
433                         select .modelPitch
434                 endif
435                 Rename... 'toneScript.theWord$'
436                 toneScript.mean = do ("Get mean...", 0, 0, "Hertz")
437                 toneScript.maximum = do ("Get maximum...", 0, 0, "Hertz", "Parabolic")
438                 toneScript.shiftFreq = toneScript.generatedMean - toneScript.mean
439                 # toneScript.shiftFreq = toneScript.generatedMaximum - toneScript.maximum
440                 Formula... self + toneScript.shiftFreq
441         endif
442         
443     # Generate sound if wanted
444     select Pitch 'toneScript.theWord$'
445     if rindex_regex(toneScript.whatToGenerate$, "Sound") > 0
446             noprogress To Sound (hum)
447     endif
449     # Clean up
450     select PitchTier 'toneScript.theWord$'
451     if rindex_regex(toneScript.whatToGenerate$, "(Sound|TextGrid)") > 0
452         plus Pitch 'toneScript.theWord$'
453     endif
454     Remove
455 endproc
457 procedure convert2Pitch .minimumPitch .maximumPitch
458         #.object = noprogress To Pitch (ac)... 0.01 '.minimumPitch' 25 yes 0.05 0.3 0.01 0.6 0.14 '.maximumPitch'
459         .object = noprogress To Pitch (cc)... 0.005 '.minimumPitch' 25 yes 0.03 0.50 0.045 0.35 0.14 '.maximumPitch'
460 endproc
462 # Not everyone add all the zeros for the neutral tones. Here we try to guess where
463 # they would belong.
464 procedure add_missing_neutral_tones .pinyin$
465         # Missing neutral tones
466         # Missing last tone
467         .pinyin$ = replace_regex$(.pinyin$, "([^0-9])$", "\10", 0)
468         # Easy cases V [^n]
469         .pinyin$ = replace_regex$(.pinyin$, "([euioa]+)([^0-9neuioar])", "\10\2", 0)
470         # Complex case V r V
471         .pinyin$ = replace_regex$(.pinyin$, "([euioa]+)(r[euioa]+)", "\10\2", 0)
472         # Complex case V r C
473         .pinyin$ = replace_regex$(.pinyin$, "([euioa]+r)([^0-9euioa]+)", "\10\2", 0)
474         # Vng cases
475         .pinyin$ = replace_regex$(.pinyin$, "([euioa]+ng)([^0-9])", "\10\2", 0)
476         # VnC cases C != g
477         .pinyin$ = replace_regex$(.pinyin$, "([euioa]+n)([^0-9geuioa])", "\10\2", 0)
478         # VnV cases -> Maximal onset
479         .pinyin$ = replace_regex$(.pinyin$, "([euioa])(n[euioa])", "\10\2", 0)
480 endproc