Debian Codegraph @

external/espeak/phonemelist.cpp

raw
1 /*
2 * Copyright (C) 2005 to 2014 by Jonathan Duddington
3 * email: [email protected]
4 * Copyright (C) 2015-2016 Reece H. Dunn
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see: <http://www.gnu.org/licenses/>.
18 */
19
20 #include "espeak__config.h"
21
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "espeak_ng.h"
29 #include "speak_lib.h"
30 #include "encoding.h"
31
32 #include "phonemelist.h"
33 #include "phoneme.h" // for PHONEME_TAB, phVOWEL, REPLACE_PHONEMES, phPAUSE
34 #include "synthdata.h" // for InterpretPhoneme, SelectPhonemeTable
35 #include "synthesize.h" // for PHONEME_LIST, PHONEME_LIST2, phoneme_tab
36 #include "translate.h" // for Translator, LANGUAGE_OPTIONS, option_wordgap
37
38 #include "phoneme.h"
39 #include "synthesize.h"
40 #include "translate.h"
41 #include "speech.h"
42
43 static void SetRegressiveVoicing(int regression, PHONEME_LIST2 *plist2, PHONEME_TAB *ph, Translator *tr);
44 static void ReInterpretPhoneme(PHONEME_TAB *ph, PHONEME_TAB *ph2, PHONEME_LIST *plist3, Translator *tr, PHONEME_DATA *phdata, WORD_PH_DATA *worddata);
45
46 static const unsigned char pause_phonemes[8] = {
47 0, phonPAUSE_VSHORT, phonPAUSE_SHORT, phonPAUSE, phonPAUSE_LONG, phonGLOTTALSTOP, phonPAUSE_LONG, phonPAUSE_LONG
48 };
49
50 extern int n_ph_list2;
51 extern PHONEME_LIST2 ph_list2[N_PHONEME_LIST]; // first stage of text->phonemes
52
53 static int SubstitutePhonemes(PHONEME_LIST *plist_out)
54 {
55 // Copy the phonemes list and perform any substitutions that are required for the
56 // current voice
57 int ix;
58 int k;
59 int replace_flags;
60 int n_plist_out = 0;
61 bool word_end;
62 PHONEME_LIST2 *plist2;
63 PHONEME_TAB *next = NULL;
64 int deleted_sourceix = -1;
65
66 for (ix = 0; (ix < n_ph_list2) && (n_plist_out < N_PHONEME_LIST); ix++) {
67 plist2 = &ph_list2[ix];
68 if (deleted_sourceix != -1) {
69 plist2->sourceix = deleted_sourceix;
70 deleted_sourceix = -1;
71 }
72
73 if (plist2->phcode == phonSWITCH)
74 SelectPhonemeTable(plist2->tone_ph);
75
76 // don't do any substitution if the language has been temporarily changed
77 if (!(plist2->synthflags & SFLAG_SWITCHED_LANG)) {
78 if (ix < (n_ph_list2 -1))
79 next = phoneme_tab[ph_list2[ix+1].phcode];
80
81 word_end = false;
82 if (ix == n_ph_list2 -1 || (plist2+1)->sourceix || ((next != 0) && (next->type == phPAUSE)))
83 word_end = true; // this phoneme is the end of a word
84
85 // check whether a Voice has specified that we should replace this phoneme
86 for (k = 0; k < n_replace_phonemes; k++) {
87 if (plist2->phcode == replace_phonemes[k].old_ph) {
88 replace_flags = replace_phonemes[k].type;
89
90 if ((replace_flags & 1) && (word_end == false))
91 continue; // this replacement only occurs at the end of a word
92
93 if ((replace_flags & 2) && ((plist2->stresslevel & 0x7) > 3))
94 continue; // this replacement doesn't occur in stressed syllables
95
96 if ((replace_flags & 4) && (plist2->sourceix == 0))
97 continue; // this replacement only occurs at the start of a word
98
99 // substitute the replacement phoneme
100 plist2->phcode = replace_phonemes[k].new_ph;
101 if ((plist2->stresslevel > 1) && (phoneme_tab[plist2->phcode]->phflags & phUNSTRESSED))
102 plist2->stresslevel = 0; // the replacement must be unstressed
103 break;
104 }
105 }
106
107 if (plist2->phcode == 0) {
108 deleted_sourceix = plist2->sourceix;
109 continue; // phoneme has been replaced by NULL, so don't copy it
110 }
111 }
112
113 // copy phoneme into the output list
114 memcpy(&plist_out[n_plist_out], plist2, sizeof(PHONEME_LIST2));
115 plist_out[n_plist_out].ph = phoneme_tab[plist2->phcode];
116 plist_out[n_plist_out].type = plist_out[n_plist_out].ph->type;
117 n_plist_out++;
118 }
119 return n_plist_out;
120 }
121
122 void MakePhonemeList(Translator *tr, int post_pause, bool start_sentence)
123 {
124 int ix = 0;
125 int j;
126 int insert_ph = 0;
127 PHONEME_LIST *phlist;
128 PHONEME_TAB *ph = NULL;
129 PHONEME_TAB *next;
130 int unstress_count = 0;
131 int word_stress = 0;
132 int current_phoneme_tab;
133 int max_stress;
134 int end_sourceix;
135 int alternative;
136 int delete_count;
137 int word_start;
138 PHONEME_DATA phdata;
139 bool start_of_clause = true;
140
141 int n_ph_list3;
142 PHONEME_LIST *plist3;
143 PHONEME_LIST *plist3_inserted = NULL;
144 PHONEME_LIST ph_list3[N_PHONEME_LIST];
145
146 PHONEME_LIST2 *plist2;
147 WORD_PH_DATA worddata;
148
149 memset(&worddata, 0, sizeof(worddata));
150 plist2 = ph_list2;
151 phlist = phoneme_list;
152 end_sourceix = plist2[n_ph_list2-1].sourceix;
153 MAKE_MEM_UNDEFINED(&phoneme_list, sizeof(phoneme_list));
154
155 // is the last word of the clause unstressed ?
156 max_stress = 0;
157 for (j = n_ph_list2-3; j >= 0; j--) {
158 // start with the last phoneme (before the terminating pauses) and move backwards
159 if ((plist2[j].stresslevel & 0x7f) > max_stress)
160 max_stress = plist2[j].stresslevel & 0x7f;
161 if (plist2[j].sourceix != 0)
162 break;
163 }
164 if (max_stress < 4) {
165 // the last word is unstressed, look for a previous word that can be stressed
166 while (--j >= 0) {
167 if (plist2[j].synthflags & SFLAG_PROMOTE_STRESS) { // dictionary flags indicated that this stress can be promoted
168 plist2[j].stresslevel = 4; // promote to stressed
169 break;
170 }
171 if (plist2[j].stresslevel >= 4) {
172 // found a stressed syllable, so stop looking
173 break;
174 }
175 }
176 }
177
178 // look for switch of phoneme tables
179 delete_count = 0;
180 current_phoneme_tab = tr->phoneme_tab_ix;
181 int deleted_sourceix = -1;
182 for (j = 0; j < n_ph_list2; j++) {
183 if (current_phoneme_tab != tr->phoneme_tab_ix)
184 plist2[j].synthflags |= SFLAG_SWITCHED_LANG;
185
186 if (delete_count > 0) {
187 memcpy(&plist2[j-delete_count], &plist2[j], sizeof(plist2[0]));
188 if (deleted_sourceix != -1) {
189 plist2[j-delete_count].sourceix = deleted_sourceix;
190 deleted_sourceix = -1;
191 }
192 }
193
194 if (plist2[j].phcode == phonSWITCH) {
195 if ((!(plist2[j].synthflags & SFLAG_EMBEDDED)) && (
196 (plist2[j].tone_ph == current_phoneme_tab) ||
197 (plist2[j+1].phcode == phonSWITCH) ||
198 ((plist2[j+1].phcode == phonPAUSE) && (plist2[j+2].phcode == phonSWITCH))
199 )) {
200 // delete this phonSWITCH if it's switching to the current phoneme table, or
201 // delete this phonSWITCH if its followed by another phonSWITCH
202 if (deleted_sourceix == -1 && plist2[j].sourceix != 0)
203 deleted_sourceix = plist2[j].sourceix;
204 delete_count++;
205 } else
206 current_phoneme_tab = plist2[j].tone_ph;
207 }
208
209 }
210 n_ph_list2 -= delete_count;
211
212 SelectPhonemeTable(current_phoneme_tab);
213
214 int regression;
215 if ((regression = tr->langopts.param[LOPT_REGRESSIVE_VOICING]) != 0) {
216 SetRegressiveVoicing(regression, plist2, ph, tr);
217 }
218
219 SelectPhonemeTable(tr->phoneme_tab_ix);
220 n_ph_list3 = SubstitutePhonemes(ph_list3) - 2;
221
222 for (j = 0; (j < n_ph_list3) && (ix < N_PHONEME_LIST-3);) {
223 if (ph_list3[j].sourceix) {
224 // start of a word
225 int k;
226 int nextw;
227 word_stress = 0;
228
229 // find the highest stress level in this word
230 for (nextw = j; nextw < n_ph_list3;) {
231 if (ph_list3[nextw].stresslevel > word_stress)
232 word_stress = ph_list3[nextw].stresslevel;
233
234 nextw++;
235 if (ph_list3[nextw].sourceix)
236 break; // start of the next word
237 }
238 for (k = j; k < nextw; k++)
239 ph_list3[k].wordstress = word_stress;
240 j = nextw;
241 } else
242 j++;
243 }
244
245 // transfer all the phonemes of the clause into phoneme_list
246 ph = phoneme_tab[phonPAUSE];
247 ph_list3[0].ph = ph;
248 word_start = 1;
249
250 SelectPhonemeTable(tr->phoneme_tab_ix);
251 for (j = 0; insert_ph || ((j < n_ph_list3) && (ix < N_PHONEME_LIST-3)); j++) {
252 plist3 = &ph_list3[j];
253
254 bool inserted = false;
255 bool deleted = false;
256 if (insert_ph != 0) {
257 // we have a (linking) phoneme which we need to insert here
258 next = phoneme_tab[plist3->phcode]; // this phoneme, i.e. after the insert
259
260 // re-use the previous entry for the inserted phoneme.
261 // That's OK because we don't look backwards from plist3 *** but CountVowelPosition() and isAfterStress does !!!
262 j--;
263 plist3 = plist3_inserted = &ph_list3[j];
264 if (j > 0) {
265 // move all previous phonemes in the word back one place
266 int k;
267 if (word_start > 0) {
268 k = word_start;
269 word_start--;
270 } else
271 k = 2; // No more space, don't loose the start of word mark at ph_list2[word_start]
272 for (; k <= j; k++)
273 memcpy(&ph_list3[k-1], &ph_list3[k], sizeof(*plist3));
274 }
275 memset(&plist3[0], 0, sizeof(*plist3));
276 plist3->phcode = insert_ph;
277 ph = phoneme_tab[insert_ph];
278 plist3->ph = ph;
279 insert_ph = 0;
280 inserted = true; // don't insert the same phoneme repeatedly
281 } else {
282 // otherwise get the next phoneme from the list
283 if (plist3->sourceix != 0)
284 word_start = j;
285
286 ph = phoneme_tab[plist3->phcode];
287 plist3[0].ph = ph;
288
289 if (plist3->phcode == phonSWITCH) {
290 // change phoneme table
291 SelectPhonemeTable(plist3->tone_ph);
292 }
293 next = phoneme_tab[plist3[1].phcode]; // the phoneme after this one
294 plist3[1].ph = next;
295 }
296
297 if (ph == NULL) continue;
298
299 InterpretPhoneme(tr, 0x100, plist3, &phdata, &worddata);
300
301 if ((alternative = phdata.pd_param[pd_CHANGE_NEXTPHONEME]) > 0) {
302 ph_list3[j+1].ph = phoneme_tab[alternative];
303 ph_list3[j+1].phcode = alternative;
304 ph_list3[j+1].type = phoneme_tab[alternative]->type;
305 next = phoneme_tab[alternative];
306 }
307
308 if (((alternative = phdata.pd_param[pd_INSERTPHONEME]) > 0) && (inserted == false)) {
309 // PROBLEM: if we insert a phoneme before a vowel then we loose the stress.
310 PHONEME_TAB *ph2;
311 ph2 = ph;
312
313 insert_ph = plist3->phcode;
314 ph = phoneme_tab[alternative];
315 plist3->ph = ph;
316 plist3->phcode = alternative;
317
318 ReInterpretPhoneme(ph, ph2, plist3, tr, &phdata, &worddata);
319 }
320
321 if ((alternative = phdata.pd_param[pd_CHANGEPHONEME]) > 0) {
322 PHONEME_TAB *ph2;
323 ph2 = ph;
324 ph = phoneme_tab[alternative];
325 plist3->ph = ph;
326 plist3->phcode = alternative;
327
328 if (alternative == 1)
329 deleted = true; // NULL phoneme, discard
330 else {
331 ReInterpretPhoneme(ph, ph2, plist3, tr, &phdata, &worddata);
332 }
333 }
334
335 if ((ph->type == phVOWEL) && (deleted == false)) {
336 // Check for consecutive unstressed syllables, even across word boundaries.
337 // Do this after changing phonemes according to stress level.
338 if (plist3->stresslevel <= 1) {
339 // an unstressed vowel
340 unstress_count++;
341
342 if (tr->langopts.stress_flags & 0x08) {
343 // change sequences of consecutive unstressed vowels in unstressed words to diminished stress (TEST)
344 PHONEME_LIST *p;
345 for (p = plist3+1; p->type != phPAUSE; p++) {
346 if (p->type == phVOWEL) {
347 if (p->stresslevel <= 1) {
348 if (plist3->wordstress < 4)
349 plist3->stresslevel = 0;
350 if (p->wordstress < 4)
351 p->stresslevel = 0;
352 }
353 break;
354 }
355 }
356 } else {
357 if ((unstress_count > 1) && ((unstress_count & 1) == 0)) {
358 // in a sequence of unstressed syllables, reduce alternate syllables to 'diminished'
359 // stress. But not for the last phoneme of a stressed word
360 if ((tr->langopts.stress_flags & S_NO_DIM) || ((word_stress > 3) && ((plist3+1)->sourceix != 0))) {
361 // An unstressed final vowel of a stressed word
362 unstress_count = 1; // try again for next syllable
363 } else
364 plist3->stresslevel = 0; // change stress to 'diminished'
365 }
366 }
367 } else
368 unstress_count = 0;
369 }
370
371 if ((plist3+1)->synthflags & SFLAG_LENGTHEN) {
372 static const char types_double[] = { phFRICATIVE, phVFRICATIVE, phNASAL, phLIQUID, 0 };
373 if ((j > 0) && (strchr(types_double, next->type))) {
374 // lengthen this consonant by doubling it
375 // BUT, can't insert a phoneme at position plist3[0] because it crashes PrevPh()
376 insert_ph = next->code;
377 (plist3+1)->synthflags ^= SFLAG_LENGTHEN;
378 }
379 }
380
381 if ((plist3+1)->sourceix != 0) {
382 int x;
383
384 if (tr->langopts.vowel_pause && (ph->type != phPAUSE)) {
385
386 if ((ph->type != phVOWEL) && (tr->langopts.vowel_pause & 0x200)) {
387 // add a pause after a word which ends in a consonant
388 insert_ph = phonPAUSE_NOLINK;
389 }
390
391 if (next->type == phVOWEL) {
392 if ((x = tr->langopts.vowel_pause & 0x0c) != 0) {
393 // break before a word which starts with a vowel
394 if (x == 0xc)
395 insert_ph = phonPAUSE_NOLINK;
396 else
397 insert_ph = phonPAUSE_VSHORT;
398 }
399
400 if ((ph->type == phVOWEL) && ((x = tr->langopts.vowel_pause & 0x03) != 0)) {
401 // adjacent vowels over a word boundary
402 if (x == 2)
403 insert_ph = phonPAUSE_SHORT;
404 else
405 insert_ph = phonPAUSE_VSHORT;
406 }
407
408 if (((plist3+1)->stresslevel >= 4) && (tr->langopts.vowel_pause & 0x100)) {
409 // pause before a words which starts with a stressed vowel
410 insert_ph = phonPAUSE_SHORT;
411 }
412 }
413 }
414
415 if ((plist3 != plist3_inserted) && (ix > 0)) {
416 if ((x = (tr->langopts.word_gap & 0x7)) != 0) {
417 if ((x > 1) || ((insert_ph != phonPAUSE_SHORT) && (insert_ph != phonPAUSE_NOLINK))) {
418 // don't reduce the pause
419 insert_ph = pause_phonemes[x];
420 }
421 }
422 if (option_wordgap > 0)
423 insert_ph = phonPAUSE_LONG;
424 }
425 }
426
427 plist3[2].ph = phoneme_tab[plist3[2].phcode];
428
429 if ((insert_ph == 0) && (phdata.pd_param[pd_APPENDPHONEME] != 0))
430 insert_ph = phdata.pd_param[pd_APPENDPHONEME];
431
432 if (deleted == false) {
433 phlist[ix].ph = ph;
434 phlist[ix].type = ph->type;
435 phlist[ix].env = PITCHfall; // default, can be changed in the "intonation" module
436 phlist[ix].synthflags = plist3->synthflags;
437 phlist[ix].stresslevel = plist3->stresslevel & 0xf;
438 phlist[ix].wordstress = plist3->wordstress;
439 phlist[ix].tone_ph = plist3->tone_ph;
440 phlist[ix].sourceix = 0;
441 phlist[ix].phcode = ph->code;
442
443 if (plist3->sourceix != 0) {
444 phlist[ix].sourceix = plist3->sourceix;
445 phlist[ix].newword = PHLIST_START_OF_WORD;
446
447 if (start_sentence) {
448 phlist[ix].newword |= PHLIST_START_OF_SENTENCE;
449 start_sentence = false;
450 }
451
452 if (start_of_clause) {
453 phlist[ix].newword |= PHLIST_START_OF_CLAUSE;
454 start_of_clause = false;
455 }
456 } else
457 phlist[ix].newword = 0;
458
459 phlist[ix].length = phdata.pd_param[i_SET_LENGTH]*2;
460 if ((ph->code == phonPAUSE_LONG) && (option_wordgap > 0) && (plist3[1].sourceix != 0)) {
461 phlist[ix].ph = phoneme_tab[phonPAUSE_SHORT];
462 phlist[ix].length = option_wordgap*14; // 10mS per unit at the default speed
463 }
464
465 if (ph->type == phVOWEL || ph->type == phLIQUID || ph->type == phNASAL || ph->type == phVSTOP || ph->type == phVFRICATIVE || (ph->phflags & phPREVOICE)) {
466 phlist[ix].length = 128; // length_mod
467 phlist[ix].env = PITCHfall;
468 }
469
470 phlist[ix].prepause = 0;
471 phlist[ix].amp = 20; // default, will be changed later
472 phlist[ix].pitch1 = 255;
473 phlist[ix].pitch2 = 255;
474 ix++;
475 }
476 }
477
478 phlist[ix].newword = PHLIST_END_OF_CLAUSE;
479
480 phlist[ix].phcode = phonPAUSE;
481 phlist[ix].type = phPAUSE; // terminate with 2 Pause phonemes
482 phlist[ix].length = post_pause; // length of the pause, depends on the punctuation
483 phlist[ix].sourceix = end_sourceix;
484 phlist[ix].synthflags = 0;
485 phlist[ix].prepause = 0;
486 phlist[ix++].ph = phoneme_tab[phonPAUSE];
487
488 phlist[ix].newword = 0;
489
490 phlist[ix].phcode = phonPAUSE;
491 phlist[ix].type = phPAUSE;
492 phlist[ix].length = 0;
493 phlist[ix].sourceix = 0;
494 phlist[ix].synthflags = 0;
495 phlist[ix].prepause = 0;
496 phlist[ix++].ph = phoneme_tab[phonPAUSE_SHORT];
497
498 n_phoneme_list = ix;
499
500 SelectPhonemeTable(tr->phoneme_tab_ix);
501 }
502
503 static void SetRegressiveVoicing(int regression, PHONEME_LIST2 *plist2, PHONEME_TAB *ph, Translator *tr) {
504 // set consonant clusters to all voiced or all unvoiced
505 // Regressive
506 int type;
507 bool stop_propagation = false;
508 int voicing = 0;
509
510 for (int j = n_ph_list2-1; j >= 0; j--) {
511 if (plist2[j].phcode == phonSWITCH) {
512 /* Find previous phonSWITCH to determine language we're switching back to */
513 int k;
514 for (k = j-1; k >= 0; k--)
515 if (plist2[k].phcode == phonSWITCH)
516 break;
517 if (k >= 0)
518 SelectPhonemeTable(plist2[k].tone_ph);
519 else
520 SelectPhonemeTable(tr->phoneme_tab_ix);
521 }
522 ph = phoneme_tab[plist2[j].phcode];
523 if (ph == NULL)
524 continue;
525
526 if (plist2[j].synthflags & SFLAG_SWITCHED_LANG) {
527 stop_propagation = false;
528 voicing = 0;
529 if (regression & 0x100)
530 voicing = 1; // word-end devoicing
531 continue;
532 }
533
534 type = ph->type;
535
536 if (regression & 0x2) {
537 // [v] amd [v;] don't cause regression, or [R^]
538 if (((ph->mnemonic & 0xff) == 'v') || ((ph->mnemonic & 0xff) == 'R')) {
539 stop_propagation = true;
540 if (regression & 0x10)
541 voicing = 0;
542 }
543 }
544
545 if ((type == phSTOP) || type == (phFRICATIVE)) {
546 if ((voicing == 0) && (regression & 0xf))
547 voicing = 1;
548 else if ((voicing == 2) && (ph->end_type != 0)) // use end_type field for voicing_switch for consonants
549 plist2[j].phcode = ph->end_type; // change to voiced equivalent
550 } else if ((type == phVSTOP) || type == (phVFRICATIVE)) {
551 if ((voicing == 0) && (regression & 0xf))
552 voicing = 2;
553 else if ((voicing == 1) && (ph->end_type != 0))
554 plist2[j].phcode = ph->end_type; // change to unvoiced equivalent
555 } else {
556 if (regression & 0x8) {
557 // LANG=Polish, propagate through liquids and nasals
558 if ((type == phPAUSE) || (type == phVOWEL))
559 voicing = 0;
560 } else
561 voicing = 0;
562 }
563 if (stop_propagation) {
564 voicing = 0;
565 stop_propagation = false;
566 }
567
568 if (plist2[j].sourceix) {
569 if (regression & 0x04) {
570 // stop propagation at a word boundary
571 voicing = 0;
572 }
573 if (regression & 0x100) {
574 // devoice word-final consonants, unless propagating voiced
575 if (voicing == 0)
576 voicing = 1;
577 }
578 }
579 }
580 }
581
582 static void ReInterpretPhoneme(PHONEME_TAB *ph, PHONEME_TAB *ph2, PHONEME_LIST *plist3, Translator *tr, PHONEME_DATA *phdata, WORD_PH_DATA *worddata) {
583 if (ph->type == phVOWEL) {
584 plist3->synthflags |= SFLAG_SYLLABLE;
585 if (ph2->type != phVOWEL)
586 plist3->stresslevel = 0; // change from non-vowel to vowel, make sure it's unstressed
587 } else
588 plist3->synthflags &= ~SFLAG_SYLLABLE;
589
590 // re-interpret the changed phoneme
591 // But it doesn't obey a second ChangePhoneme()
592 InterpretPhoneme(tr, 0x100, plist3, phdata, worddata);
593 }