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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2014 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.48.13 04.Nov.14";
  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. 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. int check_endtype = 0;
  513. PHONEME_TAB *ph;
  514. PHONEME_LIST *plist_this;
  515. // instruction: 2xxx, 3xxx
  516. // bits 8-10 = 0 to 5, which phoneme, =6 the 'which' information is in the next instruction.
  517. // bit 11 = 0, bits 0-7 are a phoneme code
  518. // bit 11 = 1, bits 5-7 type of data, bits 0-4 data value
  519. // bits 8-10 = 7, other conditions
  520. instn = (*p_prog) & 0xfff;
  521. data = instn & 0xff;
  522. instn2 = instn >> 8;
  523. if(instn2 < 14)
  524. {
  525. plist_this = plist;
  526. which = (instn2) % 7;
  527. if(which==6)
  528. {
  529. // the 'which' code is in the next instruction
  530. p_prog++;
  531. which = (*p_prog);
  532. }
  533. if(which==4)
  534. {
  535. // nextPhW not word boundary
  536. if(plist[1].sourceix)
  537. return(false);
  538. }
  539. if(which==5)
  540. {
  541. // prevPhW, not word boundary
  542. if(plist[0].sourceix)
  543. return(false);
  544. }
  545. if(which==6)
  546. {
  547. // next2PhW, not word boundary
  548. if(plist[1].sourceix || plist[2].sourceix)
  549. return(false);
  550. }
  551. switch(which)
  552. {
  553. case 0: // prevPh
  554. case 5: // prevPhW
  555. plist--;
  556. check_endtype = 1;
  557. break;
  558. case 1: // thisPh
  559. break;
  560. case 2: // nextPh
  561. case 4: // nextPhW
  562. plist++;
  563. break;
  564. case 3: // next2Ph
  565. case 6: // next2PhW
  566. plist += 2;
  567. break;
  568. case 7:
  569. // nextVowel, not word boundary
  570. for(which=1;;which++)
  571. {
  572. if(plist[which].sourceix)
  573. return(false);
  574. if(phoneme_tab[plist[which].phcode]->type == phVOWEL)
  575. {
  576. plist = &plist[which];
  577. break;
  578. }
  579. }
  580. break;
  581. case 8: // prevVowel in this word
  582. if((worddata==NULL) || (worddata->prev_vowel.ph == NULL))
  583. return(false); // no previous vowel
  584. plist = &(worddata->prev_vowel);
  585. check_endtype = 1;
  586. break;
  587. case 9: // next3PhW
  588. for(ix=1; ix<=3; ix++)
  589. {
  590. if(plist[ix].sourceix)
  591. return(false);
  592. }
  593. plist = &plist[3];
  594. break;
  595. case 10: // prev2PhW
  596. if((plist[0].sourceix) || (plist[-1].sourceix))
  597. return(false);
  598. plist-=2;
  599. check_endtype = 1;
  600. break;
  601. }
  602. if((which == 0) || (which == 5))
  603. {
  604. if(plist->phcode == 1)
  605. {
  606. // This is a NULL phoneme, a phoneme has been deleted so look at the previous phoneme
  607. plist--;
  608. }
  609. }
  610. if(control & 0x100)
  611. {
  612. // "change phonemes" pass
  613. plist->ph = phoneme_tab[plist->phcode];
  614. }
  615. ph = plist->ph;
  616. if(instn2 < 7)
  617. {
  618. // 'data' is a phoneme number
  619. if((phoneme_tab[data]->mnemonic == ph->mnemonic) == true)
  620. return(true);
  621. // not an exact match, check for a vowel type (eg. #i )
  622. if((check_endtype) && (ph->type == phVOWEL))
  623. return(data == ph->end_type); // prevPh() match on end_type
  624. return(data == ph->start_type); // thisPh() or nextPh(), match on start_type
  625. }
  626. data = instn & 0x1f;
  627. switch(instn & 0xe0)
  628. {
  629. case 0x00:
  630. // phoneme type, vowel, nasal, fricative, etc
  631. return(ph->type == data);
  632. break;
  633. case 0x20:
  634. // place of articulation
  635. return(((ph->phflags >> 16) & 0xf) == data);
  636. break;
  637. case 0x40:
  638. // is a bit set in phoneme flags
  639. return((ph->phflags & (1 << data)) != 0);
  640. break;
  641. case 0x80:
  642. switch(data)
  643. {
  644. case 0:
  645. case 1:
  646. case 2:
  647. case 3:
  648. case 4:
  649. return(StressCondition(tr, plist, data, 0));
  650. case 5: // isBreak, Either pause phoneme, or (stop/vstop/vfric not followed by vowel or (liquid in same word))
  651. return((ph->type == phPAUSE) || (plist_this->synthflags & SFLAG_NEXT_PAUSE));
  652. case 6: // isWordStart
  653. return(plist->sourceix != 0);
  654. case 7: // notWordStart
  655. return(plist->sourceix == 0);
  656. case 8: // isWordEnd
  657. return(plist[1].sourceix || (plist[1].ph->type == phPAUSE));
  658. break;
  659. case 9: // isAfterStress
  660. if(plist->sourceix != 0)
  661. return(false);
  662. do {
  663. plist--;
  664. if((plist->stresslevel & 0xf) >= 4)
  665. return(true);
  666. } while (plist->sourceix == 0);
  667. break;
  668. case 10: // isNotVowel
  669. return(ph->type != phVOWEL);
  670. case 11: // isFinalVowel
  671. for(;;)
  672. {
  673. plist++;
  674. // 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
  675. if(plist->sourceix != 0)
  676. return(true); // start of next word, without finding another vowel
  677. if(plist->ph->type == phVOWEL)
  678. return(false);
  679. }
  680. break;
  681. case 12: // isVoiced
  682. return((ph->type == phVOWEL) || (ph->type == phLIQUID) || (ph->phflags & phVOICED));
  683. case 13: // isFirstVowel
  684. return(CountVowelPosition(plist)==1);
  685. case 14: // isSecondVowel
  686. return(CountVowelPosition(plist)==2);
  687. case 15: // isSeqFlag1
  688. // is this preceded by a sequence if 1 or more vowels which have 'flag1' ? (lang=hi)
  689. if(plist->sourceix != 0)
  690. return(false); // this is the first phoneme in the word, so no.
  691. count = 0;
  692. for(;;)
  693. {
  694. plist--;
  695. if(plist->ph->type == phVOWEL)
  696. {
  697. if(plist->ph->phflags & phFLAG1)
  698. count++;
  699. else
  700. break; // stop when we find a vowel without flag1
  701. }
  702. if(plist->sourceix != 0)
  703. break;
  704. }
  705. return(count > 0);
  706. case 0x10: // isTranslationGiven
  707. return((plist->synthflags & SFLAG_DICTIONARY) != 0);
  708. }
  709. break;
  710. }
  711. return(false);
  712. }
  713. else
  714. if(instn2 == 0xf)
  715. {
  716. // Other conditions
  717. switch(data)
  718. {
  719. case 1: // PreVoicing
  720. return(control & 1);
  721. case 2: // KlattSynth
  722. return(voice->klattv[0] != 0);
  723. case 3: // MbrolaSynth
  724. return(mbrola_name[0] != 0);
  725. }
  726. }
  727. return(false);
  728. } // end of InterpretCondition
  729. static void SwitchOnVowelType(PHONEME_LIST *plist, PHONEME_DATA *phdata, USHORT **p_prog, int instn_type)
  730. {//========================================================================================================
  731. USHORT *prog;
  732. int voweltype;
  733. signed char x;
  734. if(instn_type == 2)
  735. {
  736. phdata->pd_control |= pd_FORNEXTPH;
  737. voweltype = plist[1].ph->start_type; // SwitchNextVowelType
  738. }
  739. else
  740. {
  741. voweltype = plist[-1].ph->end_type; // SwitchPrevVowelType
  742. }
  743. voweltype -= phonVOWELTYPES;
  744. if((voweltype >= 0) && (voweltype < 6))
  745. {
  746. prog = *p_prog + voweltype*2;
  747. phdata->sound_addr[instn_type] = (((prog[1] & 0xf) << 16) + prog[2]) * 4;
  748. x = (prog[1] >> 4) & 0xff;
  749. phdata->sound_param[instn_type] = x; // sign extend
  750. }
  751. *p_prog += 12;
  752. } // end of SwitchVowelType
  753. int NumInstnWords(USHORT *prog)
  754. {//============================
  755. int instn;
  756. int instn2;
  757. int instn_type;
  758. int n;
  759. int type2;
  760. static const char n_words[16] = {0,1,0,0,1,1,0,1,1,2,4,0,0,0,0,0};
  761. instn = *prog;
  762. instn_type = instn >> 12;
  763. if((n = n_words[instn_type]) > 0)
  764. return(n);
  765. switch(instn_type)
  766. {
  767. case 0:
  768. if(((instn & 0xf00) >> 8) == i_IPA_NAME)
  769. {
  770. n = ((instn & 0xff) + 1) / 2;
  771. return(n+1);
  772. }
  773. return(1);;
  774. case 6:
  775. type2 = (instn & 0xf00) >> 9;
  776. if((type2 == 5) || (type2 == 6))
  777. return(12); // switch on vowel type
  778. return(1);
  779. case 2:
  780. case 3:
  781. // a condition, check for a 2-word instruction
  782. if(((n = instn & 0x0f00) == 0x600) || (n == 0x0d00))
  783. return(2);
  784. return(1);
  785. default:
  786. // instn_type 11 to 15, 2 words
  787. instn2 = prog[2];
  788. if((instn2 >> 12) == 0xf)
  789. {
  790. // This instruction is followed by addWav(), 2 more words
  791. return(4);
  792. }
  793. if(instn2 == i_CONTINUE)
  794. {
  795. return(3);
  796. }
  797. return(2);
  798. }
  799. } // end of NumInstnWords
  800. void InterpretPhoneme(Translator *tr, int control, PHONEME_LIST *plist, PHONEME_DATA *phdata, WORD_PH_DATA *worddata)
  801. {//===================================================================================================================
  802. // control:
  803. //bit 0: PreVoicing
  804. //bit 8: change phonemes
  805. PHONEME_TAB *ph;
  806. USHORT *prog;
  807. USHORT instn;
  808. int instn2;
  809. int or_flag;
  810. bool truth;
  811. bool truth2;
  812. int data;
  813. int end_flag;
  814. int ix;
  815. signed char param_sc;
  816. #define N_RETURN 10
  817. int n_return=0;
  818. USHORT *return_addr[N_RETURN]; // return address stack
  819. ph = plist->ph;
  820. if((worddata != NULL) && (plist->sourceix))
  821. {
  822. // start of a word, reset word data
  823. worddata->prev_vowel.ph = NULL;
  824. }
  825. memset(phdata, 0, sizeof(PHONEME_DATA));
  826. phdata->pd_param[i_SET_LENGTH] = ph->std_length;
  827. phdata->pd_param[i_LENGTH_MOD] = ph->length_mod;
  828. if(ph->program == 0)
  829. {
  830. return;
  831. }
  832. end_flag = 0;
  833. for(prog = &phoneme_index[ph->program]; end_flag != 1; prog++)
  834. {
  835. instn = *prog;
  836. instn2 = (instn >> 8) & 0xf;
  837. or_flag = 0;
  838. switch(instn >> 12)
  839. {
  840. case 0: // 0xxx
  841. data = instn & 0xff;
  842. if(instn2 == 0)
  843. {
  844. // instructions with no operand
  845. switch(data)
  846. {
  847. case i_RETURN:
  848. end_flag = 1;
  849. break;
  850. case i_CONTINUE:
  851. break;
  852. default:
  853. InvalidInstn(ph,instn);
  854. break;
  855. }
  856. }
  857. else
  858. if(instn2 == i_APPEND_IFNEXTVOWEL)
  859. {
  860. if(phoneme_tab[plist[1].phcode]->type == phVOWEL)
  861. phdata->pd_param[i_APPEND_PHONEME] = data;
  862. }
  863. else
  864. if(instn2 == i_ADD_LENGTH)
  865. {
  866. if(data & 0x80)
  867. {
  868. // a negative value, do sign extension
  869. data = -(0x100 - data);
  870. }
  871. phdata->pd_param[i_SET_LENGTH] += data;
  872. }
  873. else
  874. if(instn2 == i_IPA_NAME)
  875. {
  876. // followed by utf-8 characters, 2 per instn word
  877. for(ix=0; (ix < data) && (ix < 16); ix += 2)
  878. {
  879. prog++;
  880. phdata->ipa_string[ix] = prog[0] >> 8;
  881. phdata->ipa_string[ix+1] = prog[0] & 0xff;
  882. }
  883. phdata->ipa_string[ix] = 0;
  884. }
  885. else
  886. if(instn2 < N_PHONEME_DATA_PARAM)
  887. {
  888. if(instn2 == i_CHANGE_PHONEME2)
  889. {
  890. phdata->pd_param[i_CHANGE_PHONEME] = data; // also set ChangePhoneme
  891. }
  892. phdata->pd_param[instn2] = data;
  893. if((instn2 == i_CHANGE_PHONEME) && (control & 0x100))
  894. {
  895. // found ChangePhoneme() in PhonemeList mode, exit
  896. end_flag = 1;
  897. }
  898. }
  899. else
  900. {
  901. InvalidInstn(ph,instn);
  902. }
  903. break;
  904. case 1:
  905. if(tr == NULL)
  906. break; // ignore if in synthesis stage
  907. if(instn2 < 8)
  908. {
  909. // ChangeIf
  910. if(StressCondition(tr, plist, instn2 & 7, 1) == true)
  911. {
  912. phdata->pd_param[i_CHANGE_PHONEME] = instn & 0xff;
  913. end_flag = 1; // change phoneme, exit
  914. }
  915. }
  916. break;
  917. case 2:
  918. case 3:
  919. // conditions
  920. or_flag = 0;
  921. truth = true;
  922. while((instn & 0xe000) == 0x2000)
  923. {
  924. // process a sequence of conditions, using boolean accumulator
  925. truth2 = InterpretCondition(tr, control, plist, prog, worddata);
  926. prog += NumInstnWords(prog);
  927. if(*prog == i_NOT)
  928. {
  929. truth2 = truth2 ^ 1;
  930. prog++;
  931. }
  932. if(or_flag)
  933. truth = truth || truth2;
  934. else
  935. truth = truth && truth2;
  936. or_flag = instn & 0x1000;
  937. instn = *prog;
  938. }
  939. if(truth == false)
  940. {
  941. if((instn & 0xf800) == i_JUMP_FALSE)
  942. {
  943. prog += instn & 0xff;
  944. }
  945. else
  946. {
  947. // instruction after a condition is not JUMP_FALSE, so skip the instruction.
  948. prog += NumInstnWords(prog);
  949. if((prog[0] & 0xfe00) == 0x6000)
  950. prog++; // and skip ELSE jump
  951. }
  952. }
  953. prog--;
  954. break;
  955. case 6:
  956. // JUMP
  957. switch(instn2 >> 1)
  958. {
  959. case 0:
  960. prog += (instn & 0xff) - 1;
  961. break;
  962. case 4:
  963. // conditional jumps should have been processed in the Condition section
  964. break;
  965. case 5: // NexttVowelStarts
  966. SwitchOnVowelType(plist, phdata, &prog, 2);
  967. break;
  968. case 6: // PrevVowelTypeEndings
  969. SwitchOnVowelType(plist, phdata, &prog, 3);
  970. break;
  971. }
  972. break;
  973. case 9:
  974. data = ((instn & 0xf) << 16) + prog[1];
  975. prog++;
  976. switch(instn2)
  977. {
  978. case 1:
  979. // call a procedure or another phoneme
  980. if(n_return < N_RETURN)
  981. {
  982. return_addr[n_return++] = prog;
  983. prog = &phoneme_index[data] - 1;
  984. }
  985. break;
  986. case 2:
  987. // pitch envelope
  988. phdata->pitch_env = data;
  989. break;
  990. case 3:
  991. // amplitude envelope
  992. phdata->amp_env = data;
  993. break;
  994. }
  995. break;
  996. case 10: // Vowelin, Vowelout
  997. if(instn2 == 1)
  998. ix = 0;
  999. else
  1000. ix = 2;
  1001. phdata->vowel_transition[ix] = ((prog[0] & 0xff) << 16) + prog[1];
  1002. phdata->vowel_transition[ix+1] = (prog[2] << 16) + prog[3];
  1003. prog += 3;
  1004. break;
  1005. case 11: // FMT
  1006. case 12: // WAV
  1007. case 13: // VowelStart
  1008. case 14: // VowelEnd
  1009. case 15: // addWav
  1010. instn2 = (instn >> 12) - 11;
  1011. phdata->sound_addr[instn2] = ((instn & 0xf) << 18) + (prog[1] << 2);
  1012. param_sc = phdata->sound_param[instn2] = (instn >> 4) & 0xff;
  1013. prog++;
  1014. if(prog[1] != i_CONTINUE)
  1015. {
  1016. if(instn2 < 2)
  1017. {
  1018. // FMT() and WAV() imply Return
  1019. end_flag = 1;
  1020. if((prog[1] >> 12) == 0xf)
  1021. {
  1022. // Return after the following addWav()
  1023. end_flag = 2;
  1024. }
  1025. }
  1026. else
  1027. if(instn2 ==pd_ADDWAV)
  1028. {
  1029. // addWav(), return if previous instruction was FMT() or WAV()
  1030. end_flag--;
  1031. }
  1032. if((instn2 == pd_VWLSTART) || (instn2 == pd_VWLEND))
  1033. {
  1034. // VowelStart or VowelEnding.
  1035. phdata->sound_param[instn2] = param_sc; // sign extend
  1036. }
  1037. }
  1038. break;
  1039. default:
  1040. InvalidInstn(ph,instn);
  1041. break;
  1042. }
  1043. if(ph->phflags & phSINGLE_INSTN)
  1044. {
  1045. end_flag = 1; // this phoneme has a one-instruction program, with an implicit Return
  1046. }
  1047. if((end_flag == 1) && (n_return > 0))
  1048. {
  1049. // return from called procedure or phoneme
  1050. end_flag = 0;
  1051. prog = return_addr[--n_return];
  1052. }
  1053. }
  1054. if((worddata != NULL) && (plist->type == phVOWEL))
  1055. {
  1056. memcpy(&worddata->prev_vowel, &plist[0], sizeof(PHONEME_LIST));
  1057. }
  1058. #ifdef _ESPEAKEDIT
  1059. plist->std_length = phdata->pd_param[i_SET_LENGTH];
  1060. if(phdata->sound_addr[0] != 0)
  1061. {
  1062. plist->phontab_addr = phdata->sound_addr[0]; // FMT address
  1063. plist->sound_param = phdata->sound_param[0];
  1064. }
  1065. else
  1066. {
  1067. plist->phontab_addr = phdata->sound_addr[1]; // WAV address
  1068. plist->sound_param = phdata->sound_param[1];
  1069. }
  1070. #endif
  1071. } // end of InterpretPhoneme
  1072. void InterpretPhoneme2(int phcode, PHONEME_DATA *phdata)
  1073. {//=====================================================
  1074. // Examine the program of a single isolated phoneme
  1075. int ix;
  1076. PHONEME_LIST plist[4];
  1077. memset(plist, 0, sizeof(plist));
  1078. for(ix=0; ix<4; ix++)
  1079. {
  1080. plist[ix].phcode = phonPAUSE;
  1081. plist[ix].ph = phoneme_tab[phonPAUSE];
  1082. }
  1083. plist[1].phcode = phcode;
  1084. plist[1].ph = phoneme_tab[phcode];
  1085. plist[2].sourceix = 1;
  1086. InterpretPhoneme(NULL, 0, &plist[1], phdata, NULL);
  1087. } // end of InterpretPhoneme2