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

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