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.

synthdata.cpp 29KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * Copyright (C) 2015 by 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: *
  18. * <http://www.gnu.org/licenses/>. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #include <wctype.h>
  24. #include <string.h>
  25. #include "speak_lib.h"
  26. #include "speech.h"
  27. #include "phoneme.h"
  28. #include "synthesize.h"
  29. #include "voice.h"
  30. #include "translate.h"
  31. #include "wave.h"
  32. const char *version_string = "1.48.15 16.Apr.15";
  33. const int version_phdata = 0x014801;
  34. int option_device_number = -1;
  35. FILE *f_logespeak = NULL;
  36. int logging_type;
  37. // copy the current phoneme table into here
  38. int n_phoneme_tab;
  39. int current_phoneme_table;
  40. PHONEME_TAB *phoneme_tab[N_PHONEME_TAB];
  41. unsigned char phoneme_tab_flags[N_PHONEME_TAB]; // bit 0: not inherited
  42. USHORT *phoneme_index=NULL;
  43. char *phondata_ptr=NULL;
  44. unsigned char *wavefile_data=NULL;
  45. static unsigned char *phoneme_tab_data = NULL;
  46. int n_phoneme_tables;
  47. PHONEME_TAB_LIST phoneme_tab_list[N_PHONEME_TABS];
  48. int phoneme_tab_number = 0;
  49. int wavefile_ix; // a wavefile to play along with the synthesis
  50. int wavefile_amp;
  51. int wavefile_ix2;
  52. int wavefile_amp2;
  53. int seq_len_adjust;
  54. int vowel_transition[4];
  55. int vowel_transition0;
  56. int vowel_transition1;
  57. int FormantTransition2(frameref_t *seq, int *n_frames, unsigned int data1, unsigned int data2, PHONEME_TAB *other_ph, int which);
  58. static char *ReadPhFile(void *ptr, const char *fname, int *size)
  59. {//=============================================================
  60. FILE *f_in;
  61. char *p;
  62. unsigned int length;
  63. char buf[sizeof(path_home)+40];
  64. sprintf(buf,"%s%c%s",path_home,PATHSEP,fname);
  65. length = GetFileLength(buf);
  66. if((f_in = fopen(buf,"rb")) == NULL)
  67. {
  68. fprintf(stderr,"Can't read data file: '%s'\n",buf);
  69. return(NULL);
  70. }
  71. if(ptr != NULL)
  72. Free(ptr);
  73. if((p = Alloc(length)) == NULL)
  74. {
  75. fclose(f_in);
  76. return(NULL);
  77. }
  78. if(fread(p,1,length,f_in) != length)
  79. {
  80. fclose(f_in);
  81. Free(p);
  82. return(NULL);
  83. }
  84. fclose(f_in);
  85. if(size != NULL)
  86. *size = length;
  87. return(p);
  88. } // end of ReadPhFile
  89. int LoadPhData(int *srate)
  90. {//========================
  91. int ix;
  92. int n_phonemes;
  93. int version;
  94. int result = 1;
  95. int length;
  96. int rate;
  97. unsigned char *p;
  98. int *pw;
  99. if((phoneme_tab_data = (unsigned char *)ReadPhFile((void *)(phoneme_tab_data),"phontab",NULL)) == NULL)
  100. return(-1);
  101. if((phoneme_index = (USHORT *)ReadPhFile((void *)(phoneme_index),"phonindex",NULL)) == NULL)
  102. return(-1);
  103. if((phondata_ptr = ReadPhFile((void *)(phondata_ptr),"phondata",NULL)) == NULL)
  104. return(-1);
  105. if((tunes = (TUNE *)ReadPhFile((void *)(tunes),"intonations",&length)) == NULL)
  106. return(-1);
  107. wavefile_data = (unsigned char *)phondata_ptr;
  108. n_tunes = length / sizeof(TUNE);
  109. // read the version number and sample rate from the first 8 bytes of phondata
  110. version = 0; // bytes 0-3, version number
  111. rate = 0; // bytes 4-7, sample rate
  112. for(ix=0; ix<4; ix++)
  113. {
  114. version += (wavefile_data[ix] << (ix*8));
  115. rate += (wavefile_data[ix+4] << (ix*8));
  116. }
  117. if(version != version_phdata)
  118. {
  119. result = version;
  120. }
  121. // set up phoneme tables
  122. p = phoneme_tab_data;
  123. n_phoneme_tables = p[0];
  124. p+=4;
  125. for(ix=0; ix<n_phoneme_tables; ix++)
  126. {
  127. n_phonemes = p[0];
  128. phoneme_tab_list[ix].n_phonemes = p[0];
  129. phoneme_tab_list[ix].includes = p[1];
  130. pw = (int *)p;
  131. phoneme_tab_list[ix].equivalence_tables = Reverse4Bytes(pw[1]);
  132. p += 8;
  133. memcpy(phoneme_tab_list[ix].name,p,N_PHONEME_TAB_NAME);
  134. p += N_PHONEME_TAB_NAME;
  135. phoneme_tab_list[ix].phoneme_tab_ptr = (PHONEME_TAB *)p;
  136. p += (n_phonemes * sizeof(PHONEME_TAB));
  137. }
  138. if(phoneme_tab_number >= n_phoneme_tables)
  139. phoneme_tab_number = 0;
  140. if(srate != NULL)
  141. *srate = rate;
  142. return(result);
  143. } // end of LoadPhData
  144. void FreePhData(void)
  145. {//==================
  146. Free(phoneme_tab_data);
  147. Free(phoneme_index);
  148. Free(phondata_ptr);
  149. Free(tunes);
  150. phoneme_tab_data=NULL;
  151. phoneme_index=NULL;
  152. phondata_ptr=NULL;
  153. tunes=NULL;
  154. }
  155. int PhonemeCode(unsigned int mnem)
  156. {//===============================
  157. int ix;
  158. for(ix=0; ix<n_phoneme_tab; ix++)
  159. {
  160. if(phoneme_tab[ix] == NULL)
  161. continue;
  162. if(phoneme_tab[ix]->mnemonic == mnem)
  163. return(phoneme_tab[ix]->code);
  164. }
  165. return(0);
  166. }
  167. int LookupPhonemeString(const char *string)
  168. {//========================================
  169. int ix;
  170. unsigned char c;
  171. unsigned int mnem;
  172. // Pack up to 4 characters into a word
  173. mnem = 0;
  174. for(ix=0; ix<4; ix++)
  175. {
  176. if(string[ix]==0) break;
  177. c = string[ix];
  178. mnem |= (c << (ix*8));
  179. }
  180. return(PhonemeCode(mnem));
  181. }
  182. frameref_t *LookupSpect(PHONEME_TAB *this_ph, int which, FMT_PARAMS *fmt_params, int *n_frames, PHONEME_LIST *plist)
  183. {//===================================================================================================================
  184. int ix;
  185. int nf;
  186. int nf1;
  187. int seq_break;
  188. frameref_t *frames;
  189. int length1;
  190. int length_std;
  191. int length_factor;
  192. SPECT_SEQ *seq, *seq2;
  193. SPECT_SEQK *seqk, *seqk2;
  194. frame_t *frame;
  195. static frameref_t frames_buf[N_SEQ_FRAMES];
  196. seq = (SPECT_SEQ *)(&phondata_ptr[fmt_params->fmt_addr]);
  197. seqk = (SPECT_SEQK *)seq;
  198. nf = seq->n_frames;
  199. if(nf >= N_SEQ_FRAMES)
  200. nf = N_SEQ_FRAMES - 1;
  201. seq_len_adjust = fmt_params->fmt2_lenadj + fmt_params->fmt_length;
  202. seq_break = 0;
  203. for(ix=0; ix<nf; ix++)
  204. {
  205. if(seq->frame[0].frflags & FRFLAG_KLATT)
  206. frame = &seqk->frame[ix];
  207. else
  208. frame = (frame_t *)&seq->frame[ix];
  209. frames_buf[ix].frame = frame;
  210. frames_buf[ix].frflags = frame->frflags;
  211. frames_buf[ix].length = frame->length;
  212. if(frame->frflags & FRFLAG_VOWEL_CENTRE)
  213. seq_break = ix;
  214. }
  215. frames = &frames_buf[0];
  216. if(seq_break > 0)
  217. {
  218. if(which==1)
  219. {
  220. nf = seq_break + 1;
  221. }
  222. else
  223. {
  224. frames = &frames_buf[seq_break]; // body of vowel, skip past initial frames
  225. nf -= seq_break;
  226. }
  227. }
  228. // do we need to modify a frame for blending with a consonant?
  229. if((this_ph->type == phVOWEL) && (fmt_params->fmt2_addr == 0) && (fmt_params->use_vowelin))
  230. {
  231. seq_len_adjust += FormantTransition2(frames,&nf,fmt_params->transition0,fmt_params->transition1,NULL,which);
  232. }
  233. length1 = 0;
  234. nf1 = nf - 1;
  235. for(ix=0; ix<nf1; ix++)
  236. length1 += frames[ix].length;
  237. if(fmt_params->fmt2_addr != 0)
  238. {
  239. // a secondary reference has been returned, which is not a wavefile
  240. // add these spectra to the main sequence
  241. seq2 = (SPECT_SEQ *)(&phondata_ptr[fmt_params->fmt2_addr]);
  242. seqk2 = (SPECT_SEQK *)seq2;
  243. // first frame of the addition just sets the length of the last frame of the main seq
  244. nf--;
  245. for(ix=0; ix<seq2->n_frames; ix++)
  246. {
  247. if(seq2->frame[0].frflags & FRFLAG_KLATT)
  248. frame = &seqk2->frame[ix];
  249. else
  250. frame = (frame_t *)&seq2->frame[ix];
  251. frames[nf].length = frame->length;
  252. if(ix > 0)
  253. {
  254. frames[nf].frame = frame;
  255. frames[nf].frflags = frame->frflags;
  256. }
  257. nf++;
  258. }
  259. wavefile_ix = 0;
  260. }
  261. if(length1 > 0)
  262. {
  263. if(which==2)
  264. {
  265. // adjust the length of the main part to match the standard length specified for the vowel
  266. // less the front part of the vowel and any added suffix
  267. length_std = fmt_params->std_length + seq_len_adjust - 45;
  268. if(length_std < 10)
  269. length_std = 10;
  270. if(plist->synthflags & SFLAG_LENGTHEN)
  271. length_std += (phoneme_tab[phonLENGTHEN]->std_length * 2); // phoneme was followed by an extra : symbol
  272. // can adjust vowel length for stressed syllables here
  273. length_factor = (length_std * 256)/ length1;
  274. for(ix=0; ix<nf1; ix++)
  275. {
  276. frames[ix].length = (frames[ix].length * length_factor)/256;
  277. }
  278. }
  279. else
  280. {
  281. if(which == 1)
  282. {
  283. // front of a vowel
  284. if(fmt_params->fmt_control == 1)
  285. {
  286. // This is the default start of a vowel.
  287. // Allow very short vowels to have shorter front parts
  288. if(fmt_params->std_length < 130)
  289. frames[0].length = (frames[0].length * fmt_params->std_length)/130;
  290. }
  291. }
  292. else
  293. {
  294. //not a vowel
  295. if(fmt_params->std_length > 0)
  296. {
  297. seq_len_adjust += (fmt_params->std_length - length1);
  298. }
  299. }
  300. if(seq_len_adjust != 0)
  301. {
  302. length_factor = ((length1 + seq_len_adjust) * 256)/length1;
  303. for(ix=0; ix<nf1; ix++)
  304. {
  305. frames[ix].length = (frames[ix].length * length_factor)/256;
  306. }
  307. }
  308. }
  309. }
  310. *n_frames = nf;
  311. return(frames);
  312. } // end of LookupSpect
  313. unsigned char *GetEnvelope(int index)
  314. {//==================================
  315. if(index==0)
  316. {
  317. fprintf(stderr,"espeak: No envelope\n");
  318. return(envelope_data[0]); // not found, use a default envelope
  319. }
  320. return((unsigned char *)&phondata_ptr[index]);
  321. }
  322. static void SetUpPhonemeTable(int number, int recursing)
  323. {//=====================================================
  324. int ix;
  325. int includes;
  326. int ph_code;
  327. PHONEME_TAB *phtab;
  328. if(recursing==0)
  329. {
  330. memset(phoneme_tab_flags,0,sizeof(phoneme_tab_flags));
  331. }
  332. if((includes = phoneme_tab_list[number].includes) > 0)
  333. {
  334. // recursively include base phoneme tables
  335. SetUpPhonemeTable(includes-1,1);
  336. }
  337. // now add the phonemes from this table
  338. phtab = phoneme_tab_list[number].phoneme_tab_ptr;
  339. for(ix=0; ix<phoneme_tab_list[number].n_phonemes; ix++)
  340. {
  341. ph_code = phtab[ix].code;
  342. phoneme_tab[ph_code] = &phtab[ix];
  343. if(ph_code > n_phoneme_tab)
  344. n_phoneme_tab = ph_code;
  345. if(recursing == 0)
  346. phoneme_tab_flags[ph_code] |= 1; // not inherited
  347. }
  348. } // end of SetUpPhonemeTable
  349. void SelectPhonemeTable(int number)
  350. {//================================
  351. n_phoneme_tab = 0;
  352. SetUpPhonemeTable(number,0); // recursively for included phoneme tables
  353. n_phoneme_tab++;
  354. current_phoneme_table = number;
  355. } // end of SelectPhonemeTable
  356. int LookupPhonemeTable(const char *name)
  357. {//=====================================
  358. int ix;
  359. for(ix=0; ix<n_phoneme_tables; ix++)
  360. {
  361. if(strcmp(name,phoneme_tab_list[ix].name)==0)
  362. {
  363. phoneme_tab_number = ix;
  364. break;
  365. }
  366. }
  367. if(ix == n_phoneme_tables)
  368. return(-1);
  369. return(ix);
  370. }
  371. int SelectPhonemeTableName(const char *name)
  372. {//=========================================
  373. // Look up a phoneme set by name, and select it if it exists
  374. // Returns the phoneme table number
  375. int ix;
  376. if((ix = LookupPhonemeTable(name)) == -1)
  377. return(-1);
  378. SelectPhonemeTable(ix);
  379. return(ix);
  380. } // end of DelectPhonemeTableName
  381. void LoadConfig(void)
  382. {//==================
  383. // Load configuration file, if one exists
  384. char buf[sizeof(path_home)+10];
  385. FILE *f;
  386. int ix;
  387. char c1;
  388. char *p;
  389. char string[200];
  390. logging_type = 0;
  391. for(ix=0; ix<N_SOUNDICON_SLOTS; ix++)
  392. {
  393. soundicon_tab[ix].filename = NULL;
  394. soundicon_tab[ix].data = NULL;
  395. }
  396. sprintf(buf,"%s%c%s",path_home,PATHSEP,"config");
  397. if((f = fopen(buf,"r"))==NULL)
  398. {
  399. return;
  400. }
  401. while(fgets(buf,sizeof(buf),f)!=NULL)
  402. {
  403. if(buf[0] == '/') continue;
  404. if(memcmp(buf,"log",3)==0)
  405. {
  406. if(sscanf(&buf[4],"%d %s",&logging_type,string)==2)
  407. {
  408. if (f_logespeak)
  409. fclose(f_logespeak);
  410. f_logespeak = fopen(string,"w");
  411. }
  412. }
  413. else
  414. if(memcmp(buf,"tone",4)==0)
  415. {
  416. ReadTonePoints(&buf[5],tone_points);
  417. }
  418. else
  419. if(memcmp(buf,"pa_device",9)==0)
  420. {
  421. sscanf(&buf[10],"%d",&option_device_number);
  422. }
  423. else
  424. if(memcmp(buf,"soundicon",9)==0)
  425. {
  426. ix = sscanf(&buf[10],"_%c %s",&c1,string);
  427. if(ix==2)
  428. {
  429. soundicon_tab[n_soundicon_tab].name = c1;
  430. p = Alloc(strlen(string)+1);
  431. strcpy(p,string);
  432. soundicon_tab[n_soundicon_tab].filename = p;
  433. soundicon_tab[n_soundicon_tab++].length = 0;
  434. }
  435. }
  436. }
  437. fclose(f);
  438. } // end of LoadConfig
  439. PHONEME_DATA this_ph_data;
  440. static void InvalidInstn(PHONEME_TAB *ph, int instn)
  441. {//====================================================
  442. fprintf(stderr,"Invalid instruction %.4x for phoneme '%s'\n", instn, WordToString(ph->mnemonic));
  443. }
  444. static bool StressCondition(Translator *tr, PHONEME_LIST *plist, int condition, int control)
  445. {//========================================================================================
  446. // condition:
  447. // 0 if diminished, 1 if unstressed, 2 if not stressed, 3 if stressed, 4 if max stress
  448. int stress_level;
  449. PHONEME_LIST *pl;
  450. static int condition_level[4] = {1,2,4,15};
  451. if(phoneme_tab[plist[0].phcode]->type == phVOWEL)
  452. {
  453. pl = plist;
  454. }
  455. else
  456. {
  457. // consonant, get stress from the following vowel
  458. if(phoneme_tab[plist[1].phcode]->type == phVOWEL)
  459. {
  460. pl = &plist[1];
  461. }
  462. else
  463. return(false); // no stress elevel for this consonant
  464. }
  465. stress_level = pl->stresslevel & 0xf;
  466. if(tr != NULL)
  467. {
  468. if((control & 1) && (plist->synthflags & SFLAG_DICTIONARY) && ((tr->langopts.param[LOPT_REDUCE] & 1)==0))
  469. {
  470. // change phoneme. Don't change phonemes which are given for the word in the dictionary.
  471. return(false);
  472. }
  473. if((tr->langopts.param[LOPT_REDUCE] & 0x2) && (stress_level >= pl->wordstress))
  474. {
  475. // treat the most stressed syllable in an unstressed word as stressed
  476. stress_level = 4;
  477. }
  478. }
  479. if(condition == 4)
  480. {
  481. return(stress_level >= pl->wordstress);
  482. }
  483. if(condition == 3)
  484. {
  485. // if stressed
  486. if(stress_level > 3)
  487. return(true);
  488. }
  489. else
  490. {
  491. if(stress_level < condition_level[condition])
  492. return(true);
  493. }
  494. return(false);
  495. } // end of StressCondition
  496. static int CountVowelPosition(PHONEME_LIST *plist)
  497. {//===============================================
  498. int count = 0;
  499. for(;;)
  500. {
  501. if(plist->ph->type == phVOWEL)
  502. count++;
  503. if(plist->sourceix != 0)
  504. break;
  505. plist--;
  506. }
  507. return(count);
  508. } // end of CoundVowelPosition
  509. static bool InterpretCondition(Translator *tr, int control, PHONEME_LIST *plist, USHORT *p_prog, WORD_PH_DATA *worddata)
  510. {//========================================================================================================================
  511. int which;
  512. int ix;
  513. unsigned int data;
  514. int instn;
  515. int instn2;
  516. int count;
  517. int check_endtype = 0;
  518. PHONEME_TAB *ph;
  519. PHONEME_LIST *plist_this;
  520. // instruction: 2xxx, 3xxx
  521. // bits 8-10 = 0 to 5, which phoneme, =6 the 'which' information is in the next instruction.
  522. // bit 11 = 0, bits 0-7 are a phoneme code
  523. // bit 11 = 1, bits 5-7 type of data, bits 0-4 data value
  524. // bits 8-10 = 7, other conditions
  525. instn = (*p_prog) & 0xfff;
  526. data = instn & 0xff;
  527. instn2 = instn >> 8;
  528. if(instn2 < 14)
  529. {
  530. plist_this = plist;
  531. which = (instn2) % 7;
  532. if(which==6)
  533. {
  534. // the 'which' code is in the next instruction
  535. p_prog++;
  536. which = (*p_prog);
  537. }
  538. if(which==4)
  539. {
  540. // nextPhW not word boundary
  541. if(plist[1].sourceix)
  542. return(false);
  543. }
  544. if(which==5)
  545. {
  546. // prevPhW, not word boundary
  547. if(plist[0].sourceix)
  548. return(false);
  549. }
  550. if(which==6)
  551. {
  552. // next2PhW, not word boundary
  553. if(plist[1].sourceix || plist[2].sourceix)
  554. return(false);
  555. }
  556. switch(which)
  557. {
  558. case 0: // prevPh
  559. case 5: // prevPhW
  560. plist--;
  561. check_endtype = 1;
  562. break;
  563. case 1: // thisPh
  564. break;
  565. case 2: // nextPh
  566. case 4: // nextPhW
  567. plist++;
  568. break;
  569. case 3: // next2Ph
  570. case 6: // next2PhW
  571. plist += 2;
  572. break;
  573. case 7:
  574. // nextVowel, not word boundary
  575. for(which=1;;which++)
  576. {
  577. if(plist[which].sourceix)
  578. return(false);
  579. if(phoneme_tab[plist[which].phcode]->type == phVOWEL)
  580. {
  581. plist = &plist[which];
  582. break;
  583. }
  584. }
  585. break;
  586. case 8: // prevVowel in this word
  587. if((worddata==NULL) || (worddata->prev_vowel.ph == NULL))
  588. return(false); // no previous vowel
  589. plist = &(worddata->prev_vowel);
  590. check_endtype = 1;
  591. break;
  592. case 9: // next3PhW
  593. for(ix=1; ix<=3; ix++)
  594. {
  595. if(plist[ix].sourceix)
  596. return(false);
  597. }
  598. plist = &plist[3];
  599. break;
  600. case 10: // prev2PhW
  601. if((plist[0].sourceix) || (plist[-1].sourceix))
  602. return(false);
  603. plist-=2;
  604. check_endtype = 1;
  605. break;
  606. }
  607. if((which == 0) || (which == 5))
  608. {
  609. if(plist->phcode == 1)
  610. {
  611. // This is a NULL phoneme, a phoneme has been deleted so look at the previous phoneme
  612. plist--;
  613. }
  614. }
  615. if(control & 0x100)
  616. {
  617. // "change phonemes" pass
  618. plist->ph = phoneme_tab[plist->phcode];
  619. }
  620. ph = plist->ph;
  621. if(instn2 < 7)
  622. {
  623. // 'data' is a phoneme number
  624. if((phoneme_tab[data]->mnemonic == ph->mnemonic) == true)
  625. return(true);
  626. // not an exact match, check for a vowel type (eg. #i )
  627. if((check_endtype) && (ph->type == phVOWEL))
  628. return(data == ph->end_type); // prevPh() match on end_type
  629. return(data == ph->start_type); // thisPh() or nextPh(), match on start_type
  630. }
  631. data = instn & 0x1f;
  632. switch(instn & 0xe0)
  633. {
  634. case 0x00:
  635. // phoneme type, vowel, nasal, fricative, etc
  636. return(ph->type == data);
  637. break;
  638. case 0x20:
  639. // place of articulation
  640. return(((ph->phflags >> 16) & 0xf) == data);
  641. break;
  642. case 0x40:
  643. // is a bit set in phoneme flags
  644. return((ph->phflags & (1 << data)) != 0);
  645. break;
  646. case 0x80:
  647. switch(data)
  648. {
  649. case 0:
  650. case 1:
  651. case 2:
  652. case 3:
  653. case 4:
  654. return(StressCondition(tr, plist, data, 0));
  655. case 5: // isBreak, Either pause phoneme, or (stop/vstop/vfric not followed by vowel or (liquid in same word))
  656. return((ph->type == phPAUSE) || (plist_this->synthflags & SFLAG_NEXT_PAUSE));
  657. case 6: // isWordStart
  658. return(plist->sourceix != 0);
  659. case 7: // notWordStart
  660. return(plist->sourceix == 0);
  661. case 8: // isWordEnd
  662. return(plist[1].sourceix || (plist[1].ph->type == phPAUSE));
  663. break;
  664. case 9: // isAfterStress
  665. if(plist->sourceix != 0)
  666. return(false);
  667. do {
  668. plist--;
  669. if((plist->stresslevel & 0xf) >= 4)
  670. return(true);
  671. } while (plist->sourceix == 0);
  672. break;
  673. case 10: // isNotVowel
  674. return(ph->type != phVOWEL);
  675. case 11: // isFinalVowel
  676. for(;;)
  677. {
  678. plist++;
  679. // plist->ph = phoneme_tab[plist->phcode]; // Why was this line here?? It corrupts plist if we have language switching if phoneme_tab is wrong language
  680. if(plist->sourceix != 0)
  681. return(true); // start of next word, without finding another vowel
  682. if(plist->ph->type == phVOWEL)
  683. return(false);
  684. }
  685. break;
  686. case 12: // isVoiced
  687. return((ph->type == phVOWEL) || (ph->type == phLIQUID) || (ph->phflags & phVOICED));
  688. case 13: // isFirstVowel
  689. return(CountVowelPosition(plist)==1);
  690. case 14: // isSecondVowel
  691. return(CountVowelPosition(plist)==2);
  692. case 15: // isSeqFlag1
  693. // is this preceded by a sequence if 1 or more vowels which have 'flag1' ? (lang=hi)
  694. if(plist->sourceix != 0)
  695. return(false); // this is the first phoneme in the word, so no.
  696. count = 0;
  697. for(;;)
  698. {
  699. plist--;
  700. if(plist->ph->type == phVOWEL)
  701. {
  702. if(plist->ph->phflags & phFLAG1)
  703. count++;
  704. else
  705. break; // stop when we find a vowel without flag1
  706. }
  707. if(plist->sourceix != 0)
  708. break;
  709. }
  710. return(count > 0);
  711. case 0x10: // isTranslationGiven
  712. return((plist->synthflags & SFLAG_DICTIONARY) != 0);
  713. }
  714. break;
  715. }
  716. return(false);
  717. }
  718. else
  719. if(instn2 == 0xf)
  720. {
  721. // Other conditions
  722. switch(data)
  723. {
  724. case 1: // PreVoicing
  725. return(control & 1);
  726. case 2: // KlattSynth
  727. return(voice->klattv[0] != 0);
  728. case 3: // MbrolaSynth
  729. return(mbrola_name[0] != 0);
  730. }
  731. }
  732. return(false);
  733. } // end of InterpretCondition
  734. static void SwitchOnVowelType(PHONEME_LIST *plist, PHONEME_DATA *phdata, USHORT **p_prog, int instn_type)
  735. {//========================================================================================================
  736. USHORT *prog;
  737. int voweltype;
  738. signed char x;
  739. if(instn_type == 2)
  740. {
  741. phdata->pd_control |= pd_FORNEXTPH;
  742. voweltype = plist[1].ph->start_type; // SwitchNextVowelType
  743. }
  744. else
  745. {
  746. voweltype = plist[-1].ph->end_type; // SwitchPrevVowelType
  747. }
  748. voweltype -= phonVOWELTYPES;
  749. if((voweltype >= 0) && (voweltype < 6))
  750. {
  751. prog = *p_prog + voweltype*2;
  752. phdata->sound_addr[instn_type] = (((prog[1] & 0xf) << 16) + prog[2]) * 4;
  753. x = (prog[1] >> 4) & 0xff;
  754. phdata->sound_param[instn_type] = x; // sign extend
  755. }
  756. *p_prog += 12;
  757. } // end of SwitchVowelType
  758. int NumInstnWords(USHORT *prog)
  759. {//============================
  760. int instn;
  761. int instn2;
  762. int instn_type;
  763. int n;
  764. int type2;
  765. static const char n_words[16] = {0,1,0,0,1,1,0,1,1,2,4,0,0,0,0,0};
  766. instn = *prog;
  767. instn_type = instn >> 12;
  768. if((n = n_words[instn_type]) > 0)
  769. return(n);
  770. switch(instn_type)
  771. {
  772. case 0:
  773. if(((instn & 0xf00) >> 8) == i_IPA_NAME)
  774. {
  775. n = ((instn & 0xff) + 1) / 2;
  776. return(n+1);
  777. }
  778. return(1);;
  779. case 6:
  780. type2 = (instn & 0xf00) >> 9;
  781. if((type2 == 5) || (type2 == 6))
  782. return(12); // switch on vowel type
  783. return(1);
  784. case 2:
  785. case 3:
  786. // a condition, check for a 2-word instruction
  787. if(((n = instn & 0x0f00) == 0x600) || (n == 0x0d00))
  788. return(2);
  789. return(1);
  790. default:
  791. // instn_type 11 to 15, 2 words
  792. instn2 = prog[2];
  793. if((instn2 >> 12) == 0xf)
  794. {
  795. // This instruction is followed by addWav(), 2 more words
  796. return(4);
  797. }
  798. if(instn2 == i_CONTINUE)
  799. {
  800. return(3);
  801. }
  802. return(2);
  803. }
  804. } // end of NumInstnWords
  805. void InterpretPhoneme(Translator *tr, int control, PHONEME_LIST *plist, PHONEME_DATA *phdata, WORD_PH_DATA *worddata)
  806. {//===================================================================================================================
  807. // control:
  808. //bit 0: PreVoicing
  809. //bit 8: change phonemes
  810. PHONEME_TAB *ph;
  811. USHORT *prog;
  812. USHORT instn;
  813. int instn2;
  814. int or_flag;
  815. bool truth;
  816. bool truth2;
  817. int data;
  818. int end_flag;
  819. int ix;
  820. signed char param_sc;
  821. #define N_RETURN 10
  822. int n_return=0;
  823. USHORT *return_addr[N_RETURN]; // return address stack
  824. ph = plist->ph;
  825. if((worddata != NULL) && (plist->sourceix))
  826. {
  827. // start of a word, reset word data
  828. worddata->prev_vowel.ph = NULL;
  829. }
  830. memset(phdata, 0, sizeof(PHONEME_DATA));
  831. phdata->pd_param[i_SET_LENGTH] = ph->std_length;
  832. phdata->pd_param[i_LENGTH_MOD] = ph->length_mod;
  833. if(ph->program == 0)
  834. {
  835. return;
  836. }
  837. end_flag = 0;
  838. for(prog = &phoneme_index[ph->program]; end_flag != 1; prog++)
  839. {
  840. instn = *prog;
  841. instn2 = (instn >> 8) & 0xf;
  842. or_flag = 0;
  843. switch(instn >> 12)
  844. {
  845. case 0: // 0xxx
  846. data = instn & 0xff;
  847. if(instn2 == 0)
  848. {
  849. // instructions with no operand
  850. switch(data)
  851. {
  852. case i_RETURN:
  853. end_flag = 1;
  854. break;
  855. case i_CONTINUE:
  856. break;
  857. default:
  858. InvalidInstn(ph,instn);
  859. break;
  860. }
  861. }
  862. else
  863. if(instn2 == i_APPEND_IFNEXTVOWEL)
  864. {
  865. if(phoneme_tab[plist[1].phcode]->type == phVOWEL)
  866. phdata->pd_param[i_APPEND_PHONEME] = data;
  867. }
  868. else
  869. if(instn2 == i_ADD_LENGTH)
  870. {
  871. if(data & 0x80)
  872. {
  873. // a negative value, do sign extension
  874. data = -(0x100 - data);
  875. }
  876. phdata->pd_param[i_SET_LENGTH] += data;
  877. }
  878. else
  879. if(instn2 == i_IPA_NAME)
  880. {
  881. // followed by utf-8 characters, 2 per instn word
  882. for(ix=0; (ix < data) && (ix < 16); ix += 2)
  883. {
  884. prog++;
  885. phdata->ipa_string[ix] = prog[0] >> 8;
  886. phdata->ipa_string[ix+1] = prog[0] & 0xff;
  887. }
  888. phdata->ipa_string[ix] = 0;
  889. }
  890. else
  891. if(instn2 < N_PHONEME_DATA_PARAM)
  892. {
  893. if(instn2 == i_CHANGE_PHONEME2)
  894. {
  895. phdata->pd_param[i_CHANGE_PHONEME] = data; // also set ChangePhoneme
  896. }
  897. phdata->pd_param[instn2] = data;
  898. if((instn2 == i_CHANGE_PHONEME) && (control & 0x100))
  899. {
  900. // found ChangePhoneme() in PhonemeList mode, exit
  901. end_flag = 1;
  902. }
  903. }
  904. else
  905. {
  906. InvalidInstn(ph,instn);
  907. }
  908. break;
  909. case 1:
  910. if(tr == NULL)
  911. break; // ignore if in synthesis stage
  912. if(instn2 < 8)
  913. {
  914. // ChangeIf
  915. if(StressCondition(tr, plist, instn2 & 7, 1) == true)
  916. {
  917. phdata->pd_param[i_CHANGE_PHONEME] = instn & 0xff;
  918. end_flag = 1; // change phoneme, exit
  919. }
  920. }
  921. break;
  922. case 2:
  923. case 3:
  924. // conditions
  925. or_flag = 0;
  926. truth = true;
  927. while((instn & 0xe000) == 0x2000)
  928. {
  929. // process a sequence of conditions, using boolean accumulator
  930. truth2 = InterpretCondition(tr, control, plist, prog, worddata);
  931. prog += NumInstnWords(prog);
  932. if(*prog == i_NOT)
  933. {
  934. truth2 = truth2 ^ 1;
  935. prog++;
  936. }
  937. if(or_flag)
  938. truth = truth || truth2;
  939. else
  940. truth = truth && truth2;
  941. or_flag = instn & 0x1000;
  942. instn = *prog;
  943. }
  944. if(truth == false)
  945. {
  946. if((instn & 0xf800) == i_JUMP_FALSE)
  947. {
  948. prog += instn & 0xff;
  949. }
  950. else
  951. {
  952. // instruction after a condition is not JUMP_FALSE, so skip the instruction.
  953. prog += NumInstnWords(prog);
  954. if((prog[0] & 0xfe00) == 0x6000)
  955. prog++; // and skip ELSE jump
  956. }
  957. }
  958. prog--;
  959. break;
  960. case 6:
  961. // JUMP
  962. switch(instn2 >> 1)
  963. {
  964. case 0:
  965. prog += (instn & 0xff) - 1;
  966. break;
  967. case 4:
  968. // conditional jumps should have been processed in the Condition section
  969. break;
  970. case 5: // NexttVowelStarts
  971. SwitchOnVowelType(plist, phdata, &prog, 2);
  972. break;
  973. case 6: // PrevVowelTypeEndings
  974. SwitchOnVowelType(plist, phdata, &prog, 3);
  975. break;
  976. }
  977. break;
  978. case 9:
  979. data = ((instn & 0xf) << 16) + prog[1];
  980. prog++;
  981. switch(instn2)
  982. {
  983. case 1:
  984. // call a procedure or another phoneme
  985. if(n_return < N_RETURN)
  986. {
  987. return_addr[n_return++] = prog;
  988. prog = &phoneme_index[data] - 1;
  989. }
  990. break;
  991. case 2:
  992. // pitch envelope
  993. phdata->pitch_env = data;
  994. break;
  995. case 3:
  996. // amplitude envelope
  997. phdata->amp_env = data;
  998. break;
  999. }
  1000. break;
  1001. case 10: // Vowelin, Vowelout
  1002. if(instn2 == 1)
  1003. ix = 0;
  1004. else
  1005. ix = 2;
  1006. phdata->vowel_transition[ix] = ((prog[0] & 0xff) << 16) + prog[1];
  1007. phdata->vowel_transition[ix+1] = (prog[2] << 16) + prog[3];
  1008. prog += 3;
  1009. break;
  1010. case 11: // FMT
  1011. case 12: // WAV
  1012. case 13: // VowelStart
  1013. case 14: // VowelEnd
  1014. case 15: // addWav
  1015. instn2 = (instn >> 12) - 11;
  1016. phdata->sound_addr[instn2] = ((instn & 0xf) << 18) + (prog[1] << 2);
  1017. param_sc = phdata->sound_param[instn2] = (instn >> 4) & 0xff;
  1018. prog++;
  1019. if(prog[1] != i_CONTINUE)
  1020. {
  1021. if(instn2 < 2)
  1022. {
  1023. // FMT() and WAV() imply Return
  1024. end_flag = 1;
  1025. if((prog[1] >> 12) == 0xf)
  1026. {
  1027. // Return after the following addWav()
  1028. end_flag = 2;
  1029. }
  1030. }
  1031. else
  1032. if(instn2 ==pd_ADDWAV)
  1033. {
  1034. // addWav(), return if previous instruction was FMT() or WAV()
  1035. end_flag--;
  1036. }
  1037. if((instn2 == pd_VWLSTART) || (instn2 == pd_VWLEND))
  1038. {
  1039. // VowelStart or VowelEnding.
  1040. phdata->sound_param[instn2] = param_sc; // sign extend
  1041. }
  1042. }
  1043. break;
  1044. default:
  1045. InvalidInstn(ph,instn);
  1046. break;
  1047. }
  1048. if(ph->phflags & phSINGLE_INSTN)
  1049. {
  1050. end_flag = 1; // this phoneme has a one-instruction program, with an implicit Return
  1051. }
  1052. if((end_flag == 1) && (n_return > 0))
  1053. {
  1054. // return from called procedure or phoneme
  1055. end_flag = 0;
  1056. prog = return_addr[--n_return];
  1057. }
  1058. }
  1059. if((worddata != NULL) && (plist->type == phVOWEL))
  1060. {
  1061. memcpy(&worddata->prev_vowel, &plist[0], sizeof(PHONEME_LIST));
  1062. }
  1063. #ifdef _ESPEAKEDIT
  1064. plist->std_length = phdata->pd_param[i_SET_LENGTH];
  1065. if(phdata->sound_addr[0] != 0)
  1066. {
  1067. plist->phontab_addr = phdata->sound_addr[0]; // FMT address
  1068. plist->sound_param = phdata->sound_param[0];
  1069. }
  1070. else
  1071. {
  1072. plist->phontab_addr = phdata->sound_addr[1]; // WAV address
  1073. plist->sound_param = phdata->sound_param[1];
  1074. }
  1075. #endif
  1076. } // end of InterpretPhoneme
  1077. void InterpretPhoneme2(int phcode, PHONEME_DATA *phdata)
  1078. {//=====================================================
  1079. // Examine the program of a single isolated phoneme
  1080. int ix;
  1081. PHONEME_LIST plist[4];
  1082. memset(plist, 0, sizeof(plist));
  1083. for(ix=0; ix<4; ix++)
  1084. {
  1085. plist[ix].phcode = phonPAUSE;
  1086. plist[ix].ph = phoneme_tab[phonPAUSE];
  1087. }
  1088. plist[1].phcode = phcode;
  1089. plist[1].ph = phoneme_tab[phcode];
  1090. plist[2].sourceix = 1;
  1091. InterpretPhoneme(NULL, 0, &plist[1], phdata, NULL);
  1092. } // end of InterpretPhoneme2