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

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