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.cpp 15KB

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