eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

synthdata.cpp 27KB

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