eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.

synthdata.cpp 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2013 by Jonathan Duddington *
  3. * email: jonsd@users.sourceforge.net *
  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.03c 23.Mar.13";
  33. const int version_phdata = 0x014701;
  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_IPA_NAME)
  860. {
  861. // followed by utf-8 characters, 2 per instn word
  862. for(ix=0; (ix < data) && (ix < 16); ix += 2)
  863. {
  864. prog++;
  865. phdata->ipa_string[ix] = prog[0] >> 8;
  866. phdata->ipa_string[ix+1] = prog[0] & 0xff;
  867. }
  868. phdata->ipa_string[ix] = 0;
  869. }
  870. else
  871. if(instn2 < N_PHONEME_DATA_PARAM)
  872. {
  873. if(instn2 == i_CHANGE_PHONEME2)
  874. {
  875. phdata->pd_param[i_CHANGE_PHONEME] = data; // also set ChangePhoneme
  876. }
  877. phdata->pd_param[instn2] = data;
  878. if((instn2 == i_CHANGE_PHONEME) && (control & 0x100))
  879. {
  880. // found ChangePhoneme() in PhonemeList mode, exit
  881. end_flag = 1;
  882. }
  883. }
  884. else
  885. {
  886. InvalidInstn(ph,instn);
  887. }
  888. break;
  889. case 1:
  890. if(tr == NULL)
  891. break; // ignore if in synthesis stage
  892. if(instn2 < 8)
  893. {
  894. // ChangeIf
  895. if(StressCondition(tr, plist, instn2 & 7, 1) == true)
  896. {
  897. phdata->pd_param[i_CHANGE_PHONEME] = instn & 0xff;
  898. end_flag = 1; // change phoneme, exit
  899. }
  900. }
  901. break;
  902. case 2:
  903. case 3:
  904. // conditions
  905. or_flag = 0;
  906. truth = true;
  907. while((instn & 0xe000) == 0x2000)
  908. {
  909. // process a sequence of conditions, using boolean accumulator
  910. truth2 = InterpretCondition(tr, control, plist, prog, worddata);
  911. prog += NumInstnWords(prog);
  912. if(*prog == i_NOT)
  913. {
  914. truth2 = truth2 ^ 1;
  915. prog++;
  916. }
  917. if(or_flag)
  918. truth = truth || truth2;
  919. else
  920. truth = truth && truth2;
  921. or_flag = instn & 0x1000;
  922. instn = *prog;
  923. }
  924. if(truth == false)
  925. {
  926. if((instn & 0xf800) == i_JUMP_FALSE)
  927. {
  928. prog += instn & 0xff;
  929. }
  930. else
  931. {
  932. // instruction after a condition is not JUMP_FALSE, so skip the instruction.
  933. prog += NumInstnWords(prog);
  934. if((prog[0] & 0xfe00) == 0x6000)
  935. prog++; // and skip ELSE jump
  936. }
  937. }
  938. prog--;
  939. break;
  940. case 6:
  941. // JUMP
  942. switch(instn2 >> 1)
  943. {
  944. case 0:
  945. prog += (instn & 0xff) - 1;
  946. break;
  947. case 4:
  948. // conditional jumps should have been processed in the Condition section
  949. break;
  950. case 5: // NexttVowelStarts
  951. SwitchOnVowelType(plist, phdata, &prog, 2);
  952. break;
  953. case 6: // PrevVowelTypeEndings
  954. SwitchOnVowelType(plist, phdata, &prog, 3);
  955. break;
  956. }
  957. break;
  958. case 9:
  959. data = ((instn & 0xf) << 16) + prog[1];
  960. prog++;
  961. switch(instn2)
  962. {
  963. case 1:
  964. // call a procedure or another phoneme
  965. if(n_return < N_RETURN)
  966. {
  967. return_addr[n_return++] = prog;
  968. prog = &phoneme_index[data] - 1;
  969. }
  970. break;
  971. case 2:
  972. // pitch envelope
  973. phdata->pitch_env = data;
  974. break;
  975. case 3:
  976. // amplitude envelope
  977. phdata->amp_env = data;
  978. break;
  979. }
  980. break;
  981. case 10: // Vowelin, Vowelout
  982. if(instn2 == 1)
  983. ix = 0;
  984. else
  985. ix = 2;
  986. phdata->vowel_transition[ix] = ((prog[0] & 0xff) << 16) + prog[1];
  987. phdata->vowel_transition[ix+1] = (prog[2] << 16) + prog[3];
  988. prog += 3;
  989. break;
  990. case 11: // FMT
  991. case 12: // WAV
  992. case 13: // VowelStart
  993. case 14: // VowelEnd
  994. case 15: // addWav
  995. instn2 = (instn >> 12) - 11;
  996. phdata->sound_addr[instn2] = ((instn & 0xf) << 18) + (prog[1] << 2);
  997. param_sc = phdata->sound_param[instn2] = (instn >> 4) & 0xff;
  998. prog++;
  999. if(prog[1] != i_CONTINUE)
  1000. {
  1001. if(instn2 < 2)
  1002. {
  1003. // FMT() and WAV() imply Return
  1004. end_flag = 1;
  1005. if((prog[1] >> 12) == 0xf)
  1006. {
  1007. // Return after the following addWav()
  1008. end_flag = 2;
  1009. }
  1010. }
  1011. else
  1012. if(instn2 ==pd_ADDWAV)
  1013. {
  1014. // addWav(), return if previous instruction was FMT() or WAV()
  1015. end_flag--;
  1016. }
  1017. if((instn2 == pd_VWLSTART) || (instn2 == pd_VWLEND))
  1018. {
  1019. // VowelStart or VowelEnding.
  1020. phdata->sound_param[instn2] = param_sc; // sign extend
  1021. }
  1022. }
  1023. break;
  1024. default:
  1025. InvalidInstn(ph,instn);
  1026. break;
  1027. }
  1028. if(ph->phflags & phSINGLE_INSTN)
  1029. {
  1030. end_flag = 1; // this phoneme has a one-instruction program, with an implicit Return
  1031. }
  1032. if((end_flag == 1) && (n_return > 0))
  1033. {
  1034. // return from called procedure or phoneme
  1035. end_flag = 0;
  1036. prog = return_addr[--n_return];
  1037. }
  1038. }
  1039. if((worddata != NULL) && (plist->type == phVOWEL))
  1040. {
  1041. memcpy(&worddata->prev_vowel, &plist[0], sizeof(PHONEME_LIST));
  1042. }
  1043. #ifdef _ESPEAKEDIT
  1044. plist->std_length = phdata->pd_param[i_SET_LENGTH];
  1045. if(phdata->sound_addr[0] != 0)
  1046. {
  1047. plist->phontab_addr = phdata->sound_addr[0]; // FMT address
  1048. plist->sound_param = phdata->sound_param[0];
  1049. }
  1050. else
  1051. {
  1052. plist->phontab_addr = phdata->sound_addr[1]; // WAV address
  1053. plist->sound_param = phdata->sound_param[1];
  1054. }
  1055. #endif
  1056. } // end of InterpretPhoneme
  1057. void InterpretPhoneme2(int phcode, PHONEME_DATA *phdata)
  1058. {//=====================================================
  1059. // Examine the program of a single isolated phoneme
  1060. int ix;
  1061. PHONEME_LIST plist[4];
  1062. memset(plist, 0, sizeof(plist));
  1063. for(ix=0; ix<4; ix++)
  1064. {
  1065. plist[ix].phcode = phonPAUSE;
  1066. plist[ix].ph = phoneme_tab[phonPAUSE];
  1067. }
  1068. plist[1].phcode = phcode;
  1069. plist[1].ph = phoneme_tab[phcode];
  1070. plist[2].sourceix = 1;
  1071. InterpretPhoneme(NULL, 0, &plist[1], phdata, NULL);
  1072. } // end of InterpretPhoneme2