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

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