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

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