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

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