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

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