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.c 26KB

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