3 # SpeakGoodChinese: toneScript.praat generates synthetic tone contours
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
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.
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.
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
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
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
49 toneScript.absoluteMinimum = 80
51 toneScript.prevTone = -1
52 toneScript.nextTone = -1
55 toneScript.lastFrequency = 0
58 if toneScript.inputWord$ <> ""
59 toneScript.inputWord$ = replace_regex$(toneScript.inputWord$, "^\s*(.+)\s*$", "\1", 1)
62 # Add a tone movement. The current time toneScript.point is 'toneScript.point'
63 toneScript.delta = 0.0000001
64 if toneScript.durationScale <= 0
65 toneScript.durationScale = 1.0
67 toneScript.segmentDuration = 0.150
68 toneScript.fixedDuration = 0.12
72 # start * ?Semit is a fall
73 # start / ?Semit is a rise
75 toneScript.octave = 0.5
77 toneScript.nineSemit = 0.594603557501361
79 toneScript.sixSemit = 0.707106781186547
80 # 1/(3 semitones) down
81 toneScript.threeSemit = 0.840896415253715
82 # 1/(2 semitones) down
83 toneScript.twoSemit = 0.890898718140339
84 # 1/(1 semitones) down
85 toneScript.oneSemit = 0.943874313
86 # 1/(4 semitones) down
87 toneScript.fourSemit = toneScript.twoSemit * toneScript.twoSemit
88 # 1/(5 semitones) down
89 toneScript.fiveSemit = toneScript.threeSemit * toneScript.twoSemit
91 toneScript.frequency_Range = toneScript.octave
92 if toneScript.range_Factor > 0
93 toneScript.frequency_Range = toneScript.frequency_Range * toneScript.range_Factor
96 # Previous end frequency
97 toneScript.lastFrequency = 0
98 # Split input into syllables
99 toneScript.margin = 0.25
101 # Get a list of items
102 if toneScript.createToneList = 1
103 Create Table with column names... ToneList 36 Word
106 select Table ToneList
107 Set string value... '.i' Word ------EMPTY
111 toneScript.syllableCount = length(replace_regex$(toneScript.inputWord$, "[^\d]+([\d]+)", "1", 0))
112 toneScript.wordNumber = 0
113 toneScript.lowerBound = 1
114 if toneScript.syllableCount = 1
115 toneScript.lowerBound = 0
117 if rindex(toneScript.generate$, "Correct") <= 0
118 for toneScript.first from toneScript.lowerBound to 6
119 toneScript.currentWord$ = replace_regex$(toneScript.inputWord$, "^([^\d]+)([\d]+)(.*)$", "\1'toneScript.first'\3", 1)
120 for toneScript.second from 0 to 6
121 if (toneScript.first <> 5 and toneScript.second <> 5) and (toneScript.syllableCount > 1 or toneScript.second == 1)
122 toneScript.currentWord$ = replace_regex$(toneScript.currentWord$, "^([^\d]+)([\d]+)([^\d]+)([\d]+)$", "\1'toneScript.first'\3'toneScript.second'", 1)
124 toneScript.wordNumber = toneScript.wordNumber+1
125 if toneScript.createToneList = 1
126 select Table ToneList
127 toneScript.listLength = Get number of rows
128 toneScript.listLength = toneScript.listLength + 1
129 for toneScript.currLength from toneScript.listLength to toneScript.wordNumber
131 Set string value... 'toneScript.currLength' Word ------EMPTY
133 Set string value... 'toneScript.wordNumber' Word 'toneScript.currentWord$'
136 # Actually, generate something
137 call generateWord 'toneScript.generate$' 'toneScript.currentWord$' 'toneScript.upperRegister'
142 call generateWord 'toneScript.generate$' 'toneScript.inputWord$' 'toneScript.upperRegister'
146 procedure extractTone .syllable$
147 toneScript.toneSyllable = -1
148 .toneScript.currentToneText$ = replace_regex$(.syllable$, "^[^\d]+([\d]+)(.*)$", "\1", 0)
149 toneScript.toneSyllable = extractNumber(.toneScript.currentToneText$, "")
152 procedure convertVoicing toneScript.voicingSyllable$
154 toneScript.voicingSyllable$ = replace_regex$(toneScript.voicingSyllable$, "^([^\d]+)[\d]+", "\1", 0)
155 # Convert voiced consonants
156 toneScript.voicingSyllable$ = replace_regex$(toneScript.voicingSyllable$, "(ng|[wrlmny])", "C", 0)
157 # Convert unvoiced consonants
158 toneScript.voicingSyllable$ = replace_regex$(toneScript.voicingSyllable$, "(sh|ch|zh|[fsxhktpgqdbzcj])", "U", 0)
160 toneScript.voicingSyllable$ = replace_regex$(toneScript.voicingSyllable$, "([aiuoe\XFC])", "V", 0)
163 procedure addToneMovement .syllable$ toneScript.topLine toneScript.prevTone toneScript.nextTone
165 toneScript.toneSyllable = -1
166 call extractTone '.syllable$'
167 if toneScript.toneSyllable = 3 and toneScript.nextTone = 3
168 toneScript.toneSyllable = 2
171 # Get voicing pattern
172 toneScript.voicingSyllable$ = ""
173 call convertVoicing '.syllable$'
175 # Account for tones in duration
176 toneScript.toneFactor = 1
177 # Scale the duration of the current syllable
179 toneScript.toneFactor = toneScript.toneFactor * toneScript.durationScale
182 if rindex_regex(toneScript.voicingSyllable$, "U") = 1
183 toneScript.point = toneScript.point + toneScript.delta
184 Add point... 'toneScript.point' 0
185 toneScript.point = toneScript.point + toneScript.segmentDuration * toneScript.toneFactor
186 Add point... 'toneScript.point' 0
189 toneScript.voiceLength$ = replace_regex$(toneScript.voicingSyllable$, "U*([CV]+)U*", "\1", 0)
190 toneScript.voicedLength = length(toneScript.voiceLength$)
191 toneScript.voicedDuration = toneScript.toneFactor * (toneScript.segmentDuration*toneScript.voicedLength + toneScript.fixedDuration)
192 toneScript.point = toneScript.point + toneScript.delta
194 # Write contour of each tone
195 # Note that tones are influenced by the previous (tone 0) and next (tone 3)
196 # tones. Tone 6 is the Dutch intonation
197 # sqrt(toneScript.frequency_Range) is the mid toneScript.point
198 if toneScript.topLine * toneScript.frequency_Range < toneScript.absoluteMinimum
199 toneScript.frequency_Range = toneScript.absoluteMinimum / toneScript.topLine
204 toneScript.lastFrequency = toneScript.endPoint
208 procedure wordToTones .wordInput$ toneScript.highPitch
209 .currentRest$ = .wordInput$;
210 toneScript.syllableCount = 0
211 .length = 2 * toneScript.margin
214 while rindex_regex(.currentRest$, "^[^\d]+[\d]+") > 0
215 toneScript.syllableCount += 1
216 syllable'toneScript.syllableCount'$ = replace_regex$(.currentRest$, "^([^\d]+[\d]+)(.*)$", "\1", 1)
217 toneScript.currentSyllable$ = syllable'toneScript.syllableCount'$
220 call extractTone 'toneScript.currentSyllable$'
221 toneScript.toneSyllable'toneScript.syllableCount' = toneScript.toneSyllable
222 toneScript.currentTone = toneScript.toneSyllable'toneScript.syllableCount'
224 # Get the Voicing pattern
225 call convertVoicing 'toneScript.currentSyllable$'
226 voicingSyllable'toneScript.syllableCount'$ = toneScript.voicingSyllable$
227 currentVoicing$ = voicingSyllable'toneScript.syllableCount'$
229 # Calculate new .length
230 # Account for tones in duration
231 toneScript.toneFactor = 1
232 # Scale the duration of the current syllable
234 toneScript.toneFactor = toneScript.toneFactor * toneScript.durationScale
236 .length = .length + toneScript.toneFactor * (length(voicingSyllable'toneScript.syllableCount'$) * (toneScript.segmentDuration + toneScript.delta) + toneScript.fixedDuration)
239 .currentRest$ = replace_regex$(.currentRest$, "^([^\d]+[\d]+)(.*)$", "\2", 1)
242 if toneScript.syllableCount > 2000
247 # Create tone pattern
248 Create PitchTier... '.wordInput$' 0 '.length'
250 # Add start toneScript.margin
251 toneScript.lastFrequency = 0
253 Add point... 'toneScript.point' 0
254 toneScript.point = toneScript.margin
255 Add point... 'toneScript.point' 0
257 toneScript.lastTone = -1
258 toneScript.followTone = -1
259 for .i from 1 to toneScript.syllableCount
260 toneScript.currentSyllable$ = syllable'.i'$
261 toneScript.currentTone = toneScript.toneSyllable'.i'
262 toneScript.followTone = -1
263 if .i < toneScript.syllableCount
265 toneScript.followTone = toneScript.toneSyllable'.j'
268 call addToneMovement 'toneScript.currentSyllable$' 'toneScript.highPitch' 'toneScript.lastTone' 'toneScript.followTone'
270 toneScript.lastTone = toneScript.currentTone
273 # Add end toneScript.margin
274 toneScript.point = toneScript.point + toneScript.delta
275 Add point... 'toneScript.point' 0
276 toneScript.point = toneScript.point + toneScript.margin
277 Add point... 'toneScript.point' 0
280 procedure generateWord toneScript.whatToGenerate$ toneScript.theWord$ toneScript.upperRegister
282 # First generate model contour
283 call wordToTones 'toneScript.theWord$' 'toneScript.upperRegister'
285 select PitchTier 'toneScript.theWord$'
286 noprogress To Pitch... 0.0125 60.0 600.0
287 Rename... theOrigWord
289 Rename... 'toneScript.theWord$'
290 select Pitch theOrigWord
293 # Then look if "real" model exists, and use that
295 ... and (fileReadable("'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.Pitch")
296 ... or fileReadable("'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.wav"))
297 # Get mean of generated contour
298 select Pitch 'toneScript.theWord$'
299 Rename... GeneratedContour
300 toneScript.generatedMean = do ("Get mean...", 0, 0, "Hertz")
301 toneScript.generatedMaximum = do ("Get maximum...", 0, 0, "Hertz", "Parabolic")
303 if fileReadable("'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.Pitch")
304 Read from file... 'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.Pitch
306 .modelSound = Read from file... 'preferencesAppDir$'/pitchmodels/'toneScript.theWord$'.wav
308 # Third tones get really low
309 if index(toneScript.theWord$, "3") > 0
310 call convert2Pitch 15 600
312 call convert2Pitch 60 600
314 .modelPitch = convert2Pitch.object
320 Rename... 'toneScript.theWord$'
321 toneScript.mean = do ("Get mean...", 0, 0, "Hertz")
322 toneScript.maximum = do ("Get maximum...", 0, 0, "Hertz", "Parabolic")
323 toneScript.shiftFreq = toneScript.generatedMean - toneScript.mean
324 # toneScript.shiftFreq = toneScript.generatedMaximum - toneScript.maximum
325 Formula... self + toneScript.shiftFreq
328 # Generate sound if wanted
329 select Pitch 'toneScript.theWord$'
330 if rindex_regex(toneScript.whatToGenerate$, "Sound") > 0
331 noprogress To Sound (hum)
335 select PitchTier 'toneScript.theWord$'
336 if rindex_regex(toneScript.whatToGenerate$, "Sound") > 0
337 plus Pitch 'toneScript.theWord$'
342 procedure convert2Pitch .minimumPitch .maximumPitch
343 .object = noprogress To Pitch (ac)... 0 '.minimumPitch' 25 yes 0.05 0.3 0.01 0.6 0.14 '.maximumPitch'
344 #.object = noprogress To Pitch (cc)... 0 '.minimumPitch' 25 yes 0.05 0.3 0.01 0.6 0.14 '.maximumPitch'