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 28KB

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