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

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