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

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