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

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