eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

phonemelist.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. #include "config.h"
  20. #include <stdbool.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <espeak-ng/espeak_ng.h>
  26. #include <espeak-ng/speak_lib.h>
  27. #include <espeak-ng/encoding.h>
  28. #include "phonemelist.h"
  29. #include "synthdata.h"
  30. #include "phoneme.h"
  31. #include "voice.h"
  32. #include "synthesize.h"
  33. #include "translate.h"
  34. const unsigned char pause_phonemes[8] = {
  35. 0, phonPAUSE_VSHORT, phonPAUSE_SHORT, phonPAUSE, phonPAUSE_LONG, phonGLOTTALSTOP, phonPAUSE_LONG, phonPAUSE_LONG
  36. };
  37. extern int n_ph_list2;
  38. extern PHONEME_LIST2 ph_list2[N_PHONEME_LIST]; // first stage of text->phonemes
  39. static int SubstitutePhonemes(PHONEME_LIST *plist_out)
  40. {
  41. // Copy the phonemes list and perform any substitutions that are required for the
  42. // current voice
  43. int ix;
  44. int k;
  45. int replace_flags;
  46. int n_plist_out = 0;
  47. bool word_end;
  48. PHONEME_LIST2 *plist2;
  49. PHONEME_TAB *next = NULL;
  50. for (ix = 0; (ix < n_ph_list2) && (n_plist_out < N_PHONEME_LIST); ix++) {
  51. plist2 = &ph_list2[ix];
  52. // don't do any substitution if the language has been temporarily changed
  53. if (!(plist2->synthflags & SFLAG_SWITCHED_LANG)) {
  54. if (ix < (n_ph_list2 -1))
  55. next = phoneme_tab[ph_list2[ix+1].phcode];
  56. word_end = false;
  57. if ((plist2+1)->sourceix || ((next != 0) && (next->type == phPAUSE)))
  58. word_end = true; // this phoneme is the end of a word
  59. // check whether a Voice has specified that we should replace this phoneme
  60. for (k = 0; k < n_replace_phonemes; k++) {
  61. if (plist2->phcode == replace_phonemes[k].old_ph) {
  62. replace_flags = replace_phonemes[k].type;
  63. if ((replace_flags & 1) && (word_end == false))
  64. continue; // this replacement only occurs at the end of a word
  65. if ((replace_flags & 2) && ((plist2->stresslevel & 0x7) > 3))
  66. continue; // this replacement doesn't occur in stressed syllables
  67. if ((replace_flags & 4) && (plist2->sourceix == 0))
  68. continue; // this replacement only occurs at the start of a word
  69. // substitute the replacement phoneme
  70. plist2->phcode = replace_phonemes[k].new_ph;
  71. if ((plist2->stresslevel > 1) && (phoneme_tab[plist2->phcode]->phflags & phUNSTRESSED))
  72. plist2->stresslevel = 0; // the replacement must be unstressed
  73. break;
  74. }
  75. }
  76. if (plist2->phcode == 0)
  77. continue; // phoneme has been replaced by NULL, so don't copy it
  78. }
  79. // copy phoneme into the output list
  80. memcpy(&plist_out[n_plist_out], plist2, sizeof(PHONEME_LIST2));
  81. plist_out[n_plist_out].ph = phoneme_tab[plist2->phcode];
  82. plist_out[n_plist_out].type = plist_out[n_plist_out].ph->type;
  83. n_plist_out++;
  84. }
  85. return n_plist_out;
  86. }
  87. void MakePhonemeList(Translator *tr, int post_pause, bool start_sentence)
  88. {
  89. int ix = 0;
  90. int j;
  91. int insert_ph = 0;
  92. PHONEME_LIST *phlist;
  93. PHONEME_TAB *ph;
  94. PHONEME_TAB *next, *next2;
  95. int unstress_count = 0;
  96. int word_stress = 0;
  97. int current_phoneme_tab;
  98. int max_stress;
  99. int voicing;
  100. int regression;
  101. int end_sourceix;
  102. int alternative;
  103. int delete_count;
  104. int word_start;
  105. bool inserted;
  106. bool deleted;
  107. PHONEME_DATA phdata;
  108. int n_ph_list3;
  109. PHONEME_LIST *plist3;
  110. PHONEME_LIST *plist3_inserted = NULL;
  111. PHONEME_LIST ph_list3[N_PHONEME_LIST];
  112. PHONEME_LIST2 *plist2;
  113. WORD_PH_DATA worddata;
  114. memset(&worddata, 0, sizeof(worddata));
  115. plist2 = ph_list2;
  116. phlist = phoneme_list;
  117. end_sourceix = plist2[n_ph_list2-1].sourceix;
  118. // is the last word of the clause unstressed ?
  119. max_stress = 0;
  120. for (j = n_ph_list2-3; j >= 0; j--) {
  121. // start with the last phoneme (before the terminating pauses) and move backwards
  122. if ((plist2[j].stresslevel & 0x7f) > max_stress)
  123. max_stress = plist2[j].stresslevel & 0x7f;
  124. if (plist2[j].sourceix != 0)
  125. break;
  126. }
  127. if (max_stress < 4) {
  128. // the last word is unstressed, look for a previous word that can be stressed
  129. while (--j >= 0) {
  130. if (plist2[j].synthflags & SFLAG_PROMOTE_STRESS) { // dictionary flags indicated that this stress can be promoted
  131. plist2[j].stresslevel = 4; // promote to stressed
  132. break;
  133. }
  134. if (plist2[j].stresslevel >= 4) {
  135. // found a stressed syllable, so stop looking
  136. break;
  137. }
  138. }
  139. }
  140. // look for switch of phoneme tables
  141. delete_count = 0;
  142. current_phoneme_tab = tr->phoneme_tab_ix;
  143. for (j = 0; j < n_ph_list2; j++) {
  144. if (current_phoneme_tab != tr->phoneme_tab_ix)
  145. plist2[j].synthflags |= SFLAG_SWITCHED_LANG;
  146. if (delete_count > 0)
  147. memcpy(&plist2[j-delete_count], &plist2[j], sizeof(plist2[0]));
  148. if (plist2[j].phcode == phonSWITCH) {
  149. if ((!(plist2[j].synthflags & SFLAG_EMBEDDED)) && (
  150. (plist2[j].tone_ph == current_phoneme_tab) ||
  151. (plist2[j+1].phcode == phonSWITCH) ||
  152. ((plist2[j+1].phcode == phonPAUSE) && (plist2[j+2].phcode == phonSWITCH))
  153. )) {
  154. // delete this phonSWITCH if it's switching to the current phoneme table, or
  155. // delete this phonSWITCH if its followed by another phonSWITCH
  156. delete_count++;
  157. } else
  158. current_phoneme_tab = plist2[j].tone_ph;
  159. }
  160. }
  161. n_ph_list2 -= delete_count;
  162. if ((regression = tr->langopts.param[LOPT_REGRESSIVE_VOICING]) != 0) {
  163. // set consonant clusters to all voiced or all unvoiced
  164. // Regressive
  165. int type;
  166. bool stop_propagation = false;
  167. voicing = 0;
  168. for (j = n_ph_list2-1; j >= 0; j--) {
  169. ph = phoneme_tab[plist2[j].phcode];
  170. if (ph == NULL)
  171. continue;
  172. if (plist2[j].synthflags & SFLAG_SWITCHED_LANG) {
  173. stop_propagation = false;
  174. voicing = 0;
  175. if (regression & 0x100)
  176. voicing = 1; // word-end devoicing
  177. continue;
  178. }
  179. type = ph->type;
  180. if (regression & 0x2) {
  181. // [v] amd [v;] don't cause regression, or [R^]
  182. if (((ph->mnemonic & 0xff) == 'v') || ((ph->mnemonic & 0xff) == 'R')) {
  183. stop_propagation = true;
  184. if (regression & 0x10)
  185. voicing = 0;
  186. }
  187. }
  188. if ((type == phSTOP) || type == (phFRICATIVE)) {
  189. if ((voicing == 0) && (regression & 0xf))
  190. voicing = 1;
  191. else if ((voicing == 2) && (ph->end_type != 0)) // use end_type field for voicing_switch for consonants
  192. plist2[j].phcode = ph->end_type; // change to voiced equivalent
  193. } else if ((type == phVSTOP) || type == (phVFRICATIVE)) {
  194. if ((voicing == 0) && (regression & 0xf))
  195. voicing = 2;
  196. else if ((voicing == 1) && (ph->end_type != 0))
  197. plist2[j].phcode = ph->end_type; // change to unvoiced equivalent
  198. } else {
  199. if (regression & 0x8) {
  200. // LANG=Polish, propagate through liquids and nasals
  201. if ((type == phPAUSE) || (type == phVOWEL))
  202. voicing = 0;
  203. } else
  204. voicing = 0;
  205. }
  206. if (stop_propagation) {
  207. voicing = 0;
  208. stop_propagation = false;
  209. }
  210. if (plist2[j].sourceix) {
  211. if (regression & 0x04) {
  212. // stop propagation at a word boundary
  213. voicing = 0;
  214. }
  215. if (regression & 0x100) {
  216. // devoice word-final consonants, unless propagating voiced
  217. if (voicing == 0)
  218. voicing = 1;
  219. }
  220. }
  221. }
  222. }
  223. n_ph_list3 = SubstitutePhonemes(ph_list3) - 2;
  224. for (j = 0; (j < n_ph_list3) && (ix < N_PHONEME_LIST-3);) {
  225. if (ph_list3[j].sourceix) {
  226. // start of a word
  227. int k;
  228. int nextw;
  229. word_stress = 0;
  230. // find the highest stress level in this word
  231. for (nextw = j; nextw < n_ph_list3;) {
  232. if (ph_list3[nextw].stresslevel > word_stress)
  233. word_stress = ph_list3[nextw].stresslevel;
  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. // transfer all the phonemes of the clause into phoneme_list
  245. ph = phoneme_tab[phonPAUSE];
  246. ph_list3[0].ph = ph;
  247. word_start = 1;
  248. for (j = 0; insert_ph || ((j < n_ph_list3) && (ix < N_PHONEME_LIST-3)); j++) {
  249. plist3 = &ph_list3[j];
  250. inserted = false;
  251. deleted = false;
  252. if (insert_ph != 0) {
  253. // we have a (linking) phoneme which we need to insert here
  254. next = phoneme_tab[plist3->phcode]; // this phoneme, i.e. after the insert
  255. // re-use the previous entry for the inserted phoneme.
  256. // That's OK because we don't look backwards from plist3 *** but CountVowelPosition() and isAfterStress does !!!
  257. j--;
  258. plist3 = plist3_inserted = &ph_list3[j];
  259. if (j > 0) {
  260. // move all previous phonemes in the word back one place
  261. int k;
  262. if (word_start > 0) {
  263. k = word_start;
  264. word_start--;
  265. } else
  266. k = 2; // No more space, don't loose the start of word mark at ph_list2[word_start]
  267. for (; k <= j; k++)
  268. memcpy(&ph_list3[k-1], &ph_list3[k], sizeof(*plist3));
  269. }
  270. memset(&plist3[0], 0, sizeof(*plist3));
  271. plist3->phcode = insert_ph;
  272. ph = phoneme_tab[insert_ph];
  273. plist3->ph = ph;
  274. insert_ph = 0;
  275. inserted = true; // don't insert the same phoneme repeatedly
  276. } else {
  277. // otherwise get the next phoneme from the list
  278. if (plist3->sourceix != 0)
  279. word_start = j;
  280. ph = phoneme_tab[plist3->phcode];
  281. plist3[0].ph = ph;
  282. if (plist3->phcode == phonSWITCH) {
  283. // change phoneme table
  284. SelectPhonemeTable(plist3->tone_ph);
  285. }
  286. next = phoneme_tab[plist3[1].phcode]; // the phoneme after this one
  287. plist3[1].ph = next;
  288. }
  289. if (ph == NULL) continue;
  290. InterpretPhoneme(tr, 0x100, plist3, &phdata, &worddata);
  291. if ((alternative = phdata.pd_param[pd_CHANGE_NEXTPHONEME]) > 0) {
  292. ph_list3[j+1].ph = phoneme_tab[alternative];
  293. ph_list3[j+1].phcode = alternative;
  294. ph_list3[j+1].type = phoneme_tab[alternative]->type;
  295. next = phoneme_tab[alternative];
  296. }
  297. if (((alternative = phdata.pd_param[pd_INSERTPHONEME]) > 0) && (inserted == false)) {
  298. // PROBLEM: if we insert a phoneme before a vowel then we loose the stress.
  299. PHONEME_TAB *ph2;
  300. ph2 = ph;
  301. insert_ph = plist3->phcode;
  302. ph = phoneme_tab[alternative];
  303. plist3->ph = ph;
  304. plist3->phcode = alternative;
  305. if (ph->type == phVOWEL) {
  306. plist3->synthflags |= SFLAG_SYLLABLE;
  307. if (ph2->type != phVOWEL)
  308. plist3->stresslevel = 0; // change from non-vowel to vowel, make sure it's unstressed
  309. } else
  310. plist3->synthflags &= ~SFLAG_SYLLABLE;
  311. // re-interpret the changed phoneme
  312. // But it doesn't obey a second ChangePhoneme()
  313. InterpretPhoneme(tr, 0x100, plist3, &phdata, &worddata);
  314. }
  315. if ((alternative = phdata.pd_param[pd_CHANGEPHONEME]) > 0) {
  316. PHONEME_TAB *ph2;
  317. ph2 = ph;
  318. ph = phoneme_tab[alternative];
  319. plist3->ph = ph;
  320. plist3->phcode = alternative;
  321. if (alternative == 1)
  322. deleted = true; // NULL phoneme, discard
  323. else {
  324. if (ph->type == phVOWEL) {
  325. plist3->synthflags |= SFLAG_SYLLABLE;
  326. if (ph2->type != phVOWEL)
  327. plist3->stresslevel = 0; // change from non-vowel to vowel, make sure it's unstressed
  328. } else
  329. plist3->synthflags &= ~SFLAG_SYLLABLE;
  330. // re-interpret the changed phoneme
  331. // But it doesn't obey a second ChangePhoneme()
  332. InterpretPhoneme(tr, 0x100, plist3, &phdata, &worddata);
  333. }
  334. }
  335. if ((ph->type == phVOWEL) && (deleted == false)) {
  336. PHONEME_LIST *p;
  337. // Check for consecutive unstressed syllables, even across word boundaries.
  338. // Do this after changing phonemes according to stress level.
  339. if (plist3->stresslevel <= 1) {
  340. // an unstressed vowel
  341. unstress_count++;
  342. if (tr->langopts.stress_flags & 0x08) {
  343. // change sequences of consecutive unstressed vowels in unstressed words to diminished stress (TEST)
  344. for (p = plist3+1; p->type != phPAUSE; p++) {
  345. if (p->type == phVOWEL) {
  346. if (p->stresslevel <= 1) {
  347. if (plist3->wordstress < 4)
  348. plist3->stresslevel = 0;
  349. if (p->wordstress < 4)
  350. p->stresslevel = 0;
  351. }
  352. break;
  353. }
  354. }
  355. } else {
  356. if ((unstress_count > 1) && ((unstress_count & 1) == 0)) {
  357. // in a sequence of unstressed syllables, reduce alternate syllables to 'diminished'
  358. // stress. But not for the last phoneme of a stressed word
  359. if ((tr->langopts.stress_flags & S_NO_DIM) || ((word_stress > 3) && ((plist3+1)->sourceix != 0))) {
  360. // An unstressed final vowel of a stressed word
  361. unstress_count = 1; // try again for next syllable
  362. } else
  363. plist3->stresslevel = 0; // change stress to 'diminished'
  364. }
  365. }
  366. } else
  367. unstress_count = 0;
  368. }
  369. if ((plist3+1)->synthflags & SFLAG_LENGTHEN) {
  370. static char types_double[] = { phFRICATIVE, phVFRICATIVE, phNASAL, phLIQUID, 0 };
  371. if ((j > 0) && (strchr(types_double, next->type))) {
  372. // lengthen this consonant by doubling it
  373. // BUT, can't insert a phoneme at position plist3[0] because it crashes PrevPh()
  374. insert_ph = next->code;
  375. (plist3+1)->synthflags ^= SFLAG_LENGTHEN;
  376. }
  377. }
  378. if ((plist3+1)->sourceix != 0) {
  379. int x;
  380. if (tr->langopts.vowel_pause && (ph->type != phPAUSE)) {
  381. if ((ph->type != phVOWEL) && (tr->langopts.vowel_pause & 0x200)) {
  382. // add a pause after a word which ends in a consonant
  383. insert_ph = phonPAUSE_NOLINK;
  384. }
  385. if (next->type == phVOWEL) {
  386. if ((x = tr->langopts.vowel_pause & 0x0c) != 0) {
  387. // break before a word which starts with a vowel
  388. if (x == 0xc)
  389. insert_ph = phonPAUSE_NOLINK;
  390. else
  391. insert_ph = phonPAUSE_VSHORT;
  392. }
  393. if ((ph->type == phVOWEL) && ((x = tr->langopts.vowel_pause & 0x03) != 0)) {
  394. // adjacent vowels over a word boundary
  395. if (x == 2)
  396. insert_ph = phonPAUSE_SHORT;
  397. else
  398. insert_ph = phonPAUSE_VSHORT;
  399. }
  400. if (((plist3+1)->stresslevel >= 4) && (tr->langopts.vowel_pause & 0x100)) {
  401. // pause before a words which starts with a stressed vowel
  402. insert_ph = phonPAUSE_SHORT;
  403. }
  404. }
  405. }
  406. if ((plist3 != plist3_inserted) && (ix > 0)) {
  407. if ((x = (tr->langopts.word_gap & 0x7)) != 0) {
  408. if ((x > 1) || ((insert_ph != phonPAUSE_SHORT) && (insert_ph != phonPAUSE_NOLINK))) {
  409. // don't reduce the pause
  410. insert_ph = pause_phonemes[x];
  411. }
  412. }
  413. if (option_wordgap > 0)
  414. insert_ph = phonPAUSE_LONG;
  415. }
  416. }
  417. next2 = phoneme_tab[plist3[2].phcode];
  418. plist3[2].ph = next2;
  419. if ((insert_ph == 0) && (phdata.pd_param[pd_APPENDPHONEME] != 0))
  420. insert_ph = phdata.pd_param[pd_APPENDPHONEME];
  421. if (deleted == false) {
  422. phlist[ix].ph = ph;
  423. phlist[ix].type = ph->type;
  424. phlist[ix].env = PITCHfall; // default, can be changed in the "intonation" module
  425. phlist[ix].synthflags = plist3->synthflags;
  426. phlist[ix].stresslevel = plist3->stresslevel & 0xf;
  427. phlist[ix].wordstress = plist3->wordstress;
  428. phlist[ix].tone_ph = plist3->tone_ph;
  429. phlist[ix].sourceix = 0;
  430. phlist[ix].phcode = ph->code;
  431. if (plist3->sourceix != 0) {
  432. phlist[ix].sourceix = plist3->sourceix;
  433. phlist[ix].newword = 1; // this phoneme is the start of a word
  434. if (start_sentence) {
  435. phlist[ix].newword = 5; // start of sentence + start of word
  436. start_sentence = false;
  437. }
  438. } else
  439. phlist[ix].newword = 0;
  440. phlist[ix].length = phdata.pd_param[i_SET_LENGTH]*2;
  441. if ((ph->code == phonPAUSE_LONG) && (option_wordgap > 0) && (plist3[1].sourceix != 0)) {
  442. phlist[ix].ph = phoneme_tab[phonPAUSE_SHORT];
  443. phlist[ix].length = option_wordgap*14; // 10mS per unit at the default speed
  444. }
  445. if (ph->type == phVOWEL || ph->type == phLIQUID || ph->type == phNASAL || ph->type == phVSTOP || ph->type == phVFRICATIVE || (ph->phflags & phPREVOICE)) {
  446. phlist[ix].length = 128; // length_mod
  447. phlist[ix].env = PITCHfall;
  448. }
  449. phlist[ix].prepause = 0;
  450. phlist[ix].amp = 20; // default, will be changed later
  451. phlist[ix].pitch1 = 255;
  452. phlist[ix].pitch2 = 255;
  453. ix++;
  454. }
  455. }
  456. phlist[ix].newword = 2; // end of clause
  457. phlist[ix].phcode = phonPAUSE;
  458. phlist[ix].type = phPAUSE; // terminate with 2 Pause phonemes
  459. phlist[ix].length = post_pause; // length of the pause, depends on the punctuation
  460. phlist[ix].sourceix = end_sourceix;
  461. phlist[ix].synthflags = 0;
  462. phlist[ix++].ph = phoneme_tab[phonPAUSE];
  463. phlist[ix].phcode = phonPAUSE;
  464. phlist[ix].type = phPAUSE;
  465. phlist[ix].length = 0;
  466. phlist[ix].sourceix = 0;
  467. phlist[ix].synthflags = 0;
  468. phlist[ix++].ph = phoneme_tab[phonPAUSE_SHORT];
  469. n_phoneme_list = ix;
  470. }