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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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.40 23.Aug.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, next2PhW
  507. data = instn & 0xff;
  508. instn2 = instn >> 8;
  509. if(instn2 < 14)
  510. {
  511. which = (instn2) % 7;
  512. if(which==4)
  513. {
  514. // nextPh not word boundary
  515. if(plist[1].sourceix)
  516. return(false);
  517. }
  518. if(which==5)
  519. {
  520. // prevPh, not word boundary
  521. if(plist[0].sourceix)
  522. return(false);
  523. }
  524. if(which==7)
  525. {
  526. // nextPh2 not word boundary
  527. if((plist[1].sourceix) || (plist[2].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. }
  651. break;
  652. }
  653. return(false);
  654. }
  655. else
  656. if(instn2 == 0xf)
  657. {
  658. // Other conditions
  659. switch(data)
  660. {
  661. case 1: // PreVoicing
  662. return(control & 1);
  663. case 2: // KlattSynth
  664. return(voice->klattv[0] != 0);
  665. case 3: // MbrolaSynth
  666. return(mbrola_name[0] != 0);
  667. }
  668. }
  669. return(false);
  670. } // end of InterpretCondition
  671. static void SwitchVowelType(PHONEME_LIST *plist, PHONEME_DATA *phdata, USHORT **p_prog, int instn_type)
  672. {//=====================================================================================================
  673. USHORT *prog;
  674. int voweltype;
  675. signed char x;
  676. if(instn_type == 2)
  677. {
  678. phdata->pd_control |= pd_FORNEXTPH;
  679. voweltype = plist[1].ph->start_type; // SwitchNextVowelType
  680. }
  681. else
  682. {
  683. voweltype = plist[-1].ph->end_type; // SwitchPrevVowelType
  684. }
  685. voweltype -= phonVOWELTYPES;
  686. if((voweltype >= 0) && (voweltype < 6))
  687. {
  688. prog = *p_prog + voweltype*2;
  689. phdata->sound_addr[instn_type] = (((prog[1] & 0xf) << 16) + prog[2]) * 4;
  690. x = (prog[1] >> 4) & 0xff;
  691. phdata->sound_param[instn_type] = x; // sign extend
  692. }
  693. *p_prog += 12;
  694. } // end of SwitchVowelType
  695. static int NumInstnWords(USHORT *prog)
  696. {//===================================
  697. int instn;
  698. int instn2;
  699. int instn_type;
  700. static char n_words[11] = {1,1,1,1,1,1,1,1,1,2,4};
  701. instn = *prog;
  702. instn_type = instn >> 12;
  703. if(instn_type < 11)
  704. return(n_words[instn_type]);
  705. // 11 to 15, 2 words
  706. instn2 = prog[2];
  707. if((instn2 >> 12) == 0xf)
  708. {
  709. // addWav(), 2 more words
  710. return(4);
  711. }
  712. if(instn2 == i_CONTINUE)
  713. {
  714. return(3);
  715. }
  716. return(2);
  717. } // end of NumInstnWords
  718. void InterpretPhoneme(Translator *tr, int control, PHONEME_LIST *plist, PHONEME_DATA *phdata)
  719. {//==========================================================================================
  720. // control:
  721. //bit 0: PreVoicing
  722. //bit 8: change phonemes
  723. PHONEME_TAB *ph;
  724. USHORT *prog;
  725. USHORT instn;
  726. int instn2;
  727. int or_flag;
  728. bool truth;
  729. int data;
  730. int end_flag;
  731. int ix;
  732. signed char param_sc;
  733. #define N_RETURN 10
  734. int n_return=0;
  735. USHORT *return_addr[N_RETURN]; // return address stack
  736. ph = plist->ph;
  737. memset(phdata, 0, sizeof(PHONEME_DATA));
  738. phdata->pd_param[i_SET_LENGTH] = ph->std_length;
  739. phdata->pd_param[i_LENGTH_MOD] = ph->length_mod;
  740. if(ph->program == 0)
  741. return;
  742. end_flag = 0;
  743. for(prog = &phoneme_index[ph->program]; end_flag != 1; prog++)
  744. {
  745. instn = *prog;
  746. instn2 = (instn >> 8) & 0xf;
  747. or_flag = 0;
  748. switch(instn >> 12)
  749. {
  750. case 0:
  751. data = instn & 0xff;
  752. if(instn2 == 0)
  753. {
  754. // instructions with no operand
  755. switch(data)
  756. {
  757. case i_RETURN:
  758. end_flag = 1;
  759. break;
  760. case i_CONTINUE:
  761. break;
  762. default:
  763. InvalidInstn(ph,instn);
  764. break;
  765. }
  766. }
  767. else
  768. if(instn2 == i_APPEND_IFNEXTVOWEL)
  769. {
  770. if(phoneme_tab[plist[1].phcode]->type == phVOWEL)
  771. phdata->pd_param[i_APPEND_PHONEME] = data;
  772. }
  773. else
  774. if(instn2 == i_IPA_NAME)
  775. {
  776. // followed by utf-8 characters, 2 per instn word
  777. for(ix=0; (ix < data) && (ix < 16); ix += 2)
  778. {
  779. prog++;
  780. phdata->ipa_string[ix] = prog[0] >> 8;
  781. phdata->ipa_string[ix+1] = prog[0] & 0xff;
  782. }
  783. phdata->ipa_string[ix] = 0;
  784. }
  785. else
  786. if(instn2 < N_PHONEME_DATA_PARAM)
  787. {
  788. if(instn2 == i_CHANGE_PHONEME2)
  789. {
  790. phdata->pd_param[i_CHANGE_PHONEME] = data; // also set ChangePhoneme
  791. }
  792. phdata->pd_param[instn2] = data;
  793. if((instn2 == i_CHANGE_PHONEME) && (control & 0x100))
  794. {
  795. // found ChangePhoneme() in PhonemeList mode, exit
  796. end_flag = 1;
  797. }
  798. }
  799. else
  800. {
  801. InvalidInstn(ph,instn);
  802. }
  803. break;
  804. case 1:
  805. if(tr == NULL)
  806. break; // ignore if in synthesis stage
  807. if(instn2 < 8)
  808. {
  809. // ChangeIf
  810. if(StressCondition(tr, plist, instn2 & 7, 1))
  811. {
  812. phdata->pd_param[i_CHANGE_PHONEME] = instn & 0xff;
  813. end_flag = 1; // change phoneme, exit
  814. }
  815. }
  816. break;
  817. case 2:
  818. case 3:
  819. // conditions
  820. or_flag = 0;
  821. truth = true;
  822. while((instn & 0xe000) == 0x2000)
  823. {
  824. // process a sequence of conditions, using boolean accumulator
  825. if(or_flag)
  826. truth = (truth || InterpretCondition(tr, control, plist, instn & 0xfff));
  827. else
  828. truth = (truth && InterpretCondition(tr, control, plist, instn & 0xfff));
  829. or_flag = instn & 0x1000;
  830. instn = *(++prog);
  831. }
  832. if(truth == false)
  833. {
  834. if((instn & 0xf800) == i_JUMP_FALSE)
  835. {
  836. prog += instn & 0xff;
  837. }
  838. else
  839. {
  840. // instruction after a condition is not JUMP_FALSE, so skip the instruction.
  841. prog += NumInstnWords(prog);
  842. if((prog[0] & 0xfe00) == 0x6000)
  843. prog++; // and skip ELSE jump
  844. }
  845. }
  846. prog--;
  847. break;
  848. case 6:
  849. // JUMP
  850. switch(instn2 >> 1)
  851. {
  852. case 0:
  853. prog += (instn & 0xff) - 1;
  854. break;
  855. case 4:
  856. // conditional jumps should have been processed in the Condition section
  857. break;
  858. case 5: // NexttVowelStarts
  859. SwitchVowelType(plist, phdata, &prog, 2);
  860. break;
  861. case 6: // PrevVowelTypeEndings
  862. SwitchVowelType(plist, phdata, &prog, 3);
  863. break;
  864. }
  865. break;
  866. case 9:
  867. data = ((instn & 0xf) << 16) + prog[1];
  868. prog++;
  869. switch(instn2)
  870. {
  871. case 1:
  872. // call a procedure or another phoneme
  873. if(n_return < N_RETURN)
  874. {
  875. return_addr[n_return++] = prog;
  876. prog = &phoneme_index[data] - 1;
  877. }
  878. break;
  879. case 2:
  880. // pitch envelope
  881. phdata->pitch_env = data;
  882. break;
  883. case 3:
  884. // amplitude envelope
  885. phdata->amp_env = data;
  886. break;
  887. }
  888. break;
  889. case 10: // Vowelin, Vowelout
  890. if(instn2 == 1)
  891. ix = 0;
  892. else
  893. ix = 2;
  894. phdata->vowel_transition[ix] = ((prog[0] & 0xff) << 16) + prog[1];
  895. phdata->vowel_transition[ix+1] = (prog[2] << 16) + prog[3];
  896. prog += 3;
  897. break;
  898. case 11: // FMT
  899. case 12: // WAV
  900. case 13: // VowelStart
  901. case 14: // VowelEnd
  902. case 15: // addWav
  903. instn2 = (instn >> 12) - 11;
  904. phdata->sound_addr[instn2] = ((instn & 0xf) << 18) + (prog[1] << 2);
  905. param_sc = phdata->sound_param[instn2] = (instn >> 4) & 0xff;
  906. prog++;
  907. if(prog[1] != i_CONTINUE)
  908. {
  909. if(instn2 < 2)
  910. {
  911. // FMT() and WAV() imply Return
  912. end_flag = 1;
  913. if((prog[1] >> 12) == 0xf)
  914. {
  915. // Return after the following addWav()
  916. end_flag = 2;
  917. }
  918. }
  919. else
  920. if(instn2 ==pd_ADDWAV)
  921. {
  922. // addWav(), return if previous instruction was FMT() or WAV()
  923. end_flag--;
  924. }
  925. if((instn2 == pd_VWLSTART) || (instn2 == pd_VWLEND))
  926. {
  927. // VowelStart or VowelEnding.
  928. phdata->sound_param[instn2] = param_sc; // sign extend
  929. }
  930. }
  931. break;
  932. default:
  933. InvalidInstn(ph,instn);
  934. break;
  935. }
  936. if(ph->phflags & phSINGLE_INSTN)
  937. {
  938. end_flag = 1; // this phoneme has a one-instruction program, with an implicit Return
  939. }
  940. if((end_flag == 1) && (n_return > 0))
  941. {
  942. // return from called procedure or phoneme
  943. end_flag = 0;
  944. prog = return_addr[--n_return];
  945. }
  946. }
  947. } // end of InterpretPhoneme
  948. void InterpretPhoneme2(int phcode, PHONEME_DATA *phdata)
  949. {//=====================================================
  950. // Examine the program of a single isolated phoneme
  951. int ix;
  952. PHONEME_LIST plist[4];
  953. memset(plist, 0, sizeof(plist));
  954. for(ix=0; ix<4; ix++)
  955. {
  956. plist[ix].phcode = phonPAUSE;
  957. plist[ix].ph = phoneme_tab[phonPAUSE];
  958. }
  959. plist[1].phcode = phcode;
  960. plist[1].ph = phoneme_tab[phcode];
  961. plist[2].sourceix = 1;
  962. InterpretPhoneme(NULL, 0, &plist[1], phdata);
  963. } // end of InterpretPhoneme2