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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 <espeak-ng/encoding.h>
  30. #include "error.h"
  31. #include "speech.h"
  32. #include "phoneme.h"
  33. #include "voice.h"
  34. #include "synthesize.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. unsigned short *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. 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. 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. 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, bool recursing)
  286. {
  287. int ix;
  288. int includes;
  289. int ph_code;
  290. PHONEME_TAB *phtab;
  291. if (recursing == false)
  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, true);
  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, false); // 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. int stress_level;
  376. PHONEME_LIST *pl;
  377. static int condition_level[4] = { 1, 2, 4, 15 };
  378. if (phoneme_tab[plist[0].phcode]->type == phVOWEL)
  379. pl = plist;
  380. else {
  381. // consonant, get stress from the following vowel
  382. if (phoneme_tab[plist[1].phcode]->type == phVOWEL)
  383. pl = &plist[1];
  384. else
  385. return false; // no stress elevel for this consonant
  386. }
  387. stress_level = pl->stresslevel & 0xf;
  388. if (tr != NULL) {
  389. if ((control & 1) && (plist->synthflags & SFLAG_DICTIONARY) && ((tr->langopts.param[LOPT_REDUCE] & 1) == 0)) {
  390. // change phoneme. Don't change phonemes which are given for the word in the dictionary.
  391. return false;
  392. }
  393. if ((tr->langopts.param[LOPT_REDUCE] & 0x2) && (stress_level >= pl->wordstress)) {
  394. // treat the most stressed syllable in an unstressed word as stressed
  395. stress_level = STRESS_IS_PRIMARY;
  396. }
  397. }
  398. if (condition == STRESS_IS_PRIMARY)
  399. return stress_level >= pl->wordstress;
  400. if (condition == STRESS_IS_SECONDARY) {
  401. if (stress_level > STRESS_IS_SECONDARY)
  402. return true;
  403. } else {
  404. if (stress_level < condition_level[condition])
  405. return true;
  406. }
  407. return false;
  408. }
  409. static int CountVowelPosition(PHONEME_LIST *plist)
  410. {
  411. int count = 0;
  412. for (;;) {
  413. if (plist->ph->type == phVOWEL)
  414. count++;
  415. if (plist->sourceix != 0)
  416. break;
  417. plist--;
  418. }
  419. return count;
  420. }
  421. static bool InterpretCondition(Translator *tr, int control, PHONEME_LIST *plist, unsigned short *p_prog, WORD_PH_DATA *worddata)
  422. {
  423. int which;
  424. int ix;
  425. unsigned int data;
  426. int instn;
  427. int instn2;
  428. bool check_endtype = false;
  429. PHONEME_TAB *ph;
  430. PHONEME_LIST *plist_this;
  431. // instruction: 2xxx, 3xxx
  432. // bits 8-10 = 0 to 5, which phoneme, =6 the 'which' information is in the next instruction.
  433. // bit 11 = 0, bits 0-7 are a phoneme code
  434. // bit 11 = 1, bits 5-7 type of data, bits 0-4 data value
  435. // bits 8-10 = 7, other conditions
  436. instn = (*p_prog) & 0xfff;
  437. data = instn & 0xff;
  438. instn2 = instn >> 8;
  439. if (instn2 < 14) {
  440. plist_this = plist;
  441. which = (instn2) % 7;
  442. if (which == 6) {
  443. // the 'which' code is in the next instruction
  444. p_prog++;
  445. which = (*p_prog);
  446. }
  447. if (which == 4) {
  448. // nextPhW not word boundary
  449. if (plist[1].sourceix)
  450. return false;
  451. }
  452. if (which == 5) {
  453. // prevPhW, not word boundary
  454. if (plist[0].sourceix)
  455. return false;
  456. }
  457. if (which == 6) {
  458. // next2PhW, not word boundary
  459. if (plist[1].sourceix || plist[2].sourceix)
  460. return false;
  461. }
  462. switch (which)
  463. {
  464. case 0: // prevPh
  465. case 5: // prevPhW
  466. plist--;
  467. check_endtype = true;
  468. break;
  469. case 1: // thisPh
  470. break;
  471. case 2: // nextPh
  472. case 4: // nextPhW
  473. plist++;
  474. break;
  475. case 3: // next2Ph
  476. case 6: // next2PhW
  477. plist += 2;
  478. break;
  479. case 7:
  480. // nextVowel, not word boundary
  481. for (which = 1;; which++) {
  482. if (plist[which].sourceix)
  483. return false;
  484. if (phoneme_tab[plist[which].phcode]->type == phVOWEL) {
  485. plist = &plist[which];
  486. break;
  487. }
  488. }
  489. break;
  490. case 8: // prevVowel in this word
  491. if ((worddata == NULL) || (worddata->prev_vowel.ph == NULL))
  492. return false; // no previous vowel
  493. plist = &(worddata->prev_vowel);
  494. check_endtype = true;
  495. break;
  496. case 9: // next3PhW
  497. for (ix = 1; ix <= 3; ix++) {
  498. if (plist[ix].sourceix)
  499. return false;
  500. }
  501. plist = &plist[3];
  502. break;
  503. case 10: // prev2PhW
  504. if ((plist[0].sourceix) || (plist[-1].sourceix))
  505. return false;
  506. plist -= 2;
  507. check_endtype = true;
  508. break;
  509. }
  510. if ((which == 0) || (which == 5)) {
  511. if (plist->phcode == 1) {
  512. // This is a NULL phoneme, a phoneme has been deleted so look at the previous phoneme
  513. plist--;
  514. }
  515. }
  516. if (control & 0x100) {
  517. // "change phonemes" pass
  518. plist->ph = phoneme_tab[plist->phcode];
  519. }
  520. ph = plist->ph;
  521. if (instn2 < 7) {
  522. // 'data' is a phoneme number
  523. if ((phoneme_tab[data]->mnemonic == ph->mnemonic) == true)
  524. return true;
  525. // not an exact match, check for a vowel type (eg. #i )
  526. if ((check_endtype) && (ph->type == phVOWEL))
  527. return data == ph->end_type; // prevPh() match on end_type
  528. return data == ph->start_type; // thisPh() or nextPh(), match on start_type
  529. }
  530. data = instn & 0x1f;
  531. switch (instn & 0xe0)
  532. {
  533. case CONDITION_IS_PHONEME_TYPE:
  534. return ph->type == data;
  535. case CONDITION_IS_PLACE_OF_ARTICULATION:
  536. return ((ph->phflags >> 16) & 0xf) == data;
  537. case CONDITION_IS_PHFLAG_SET:
  538. return (ph->phflags & (1 << data)) != 0;
  539. case CONDITION_IS_OTHER:
  540. switch (data)
  541. {
  542. case STRESS_IS_DIMINISHED:
  543. case STRESS_IS_UNSTRESSED:
  544. case STRESS_IS_NOT_STRESSED:
  545. case STRESS_IS_SECONDARY:
  546. case STRESS_IS_PRIMARY:
  547. return StressCondition(tr, plist, data, 0);
  548. case isBreak:
  549. return (ph->type == phPAUSE) || (plist_this->synthflags & SFLAG_NEXT_PAUSE);
  550. case isWordStart:
  551. return plist->sourceix != 0;
  552. case isWordEnd:
  553. return plist[1].sourceix || (plist[1].ph->type == phPAUSE);
  554. case isAfterStress:
  555. if (plist->sourceix != 0)
  556. return false;
  557. do {
  558. plist--;
  559. if ((plist->stresslevel & 0xf) >= 4)
  560. return true;
  561. } while (plist->sourceix == 0);
  562. break;
  563. case isNotVowel:
  564. return ph->type != phVOWEL;
  565. case isFinalVowel:
  566. for (;;) {
  567. plist++;
  568. if (plist->sourceix != 0)
  569. return true; // start of next word, without finding another vowel
  570. if (plist->ph->type == phVOWEL)
  571. return false;
  572. }
  573. case isVoiced:
  574. return (ph->type == phVOWEL) || (ph->type == phLIQUID) || (ph->phflags & phVOICED);
  575. case isFirstVowel:
  576. return CountVowelPosition(plist) == 1;
  577. case isSecondVowel:
  578. return CountVowelPosition(plist) == 2;
  579. case isTranslationGiven:
  580. return (plist->synthflags & SFLAG_DICTIONARY) != 0;
  581. }
  582. break;
  583. }
  584. return false;
  585. } else if (instn2 == 0xf) {
  586. // Other conditions
  587. switch (data)
  588. {
  589. case 1: // PreVoicing
  590. return control & 1;
  591. case 2: // KlattSynth
  592. return voice->klattv[0] != 0;
  593. case 3: // MbrolaSynth
  594. return mbrola_name[0] != 0;
  595. }
  596. }
  597. return false;
  598. }
  599. static void SwitchOnVowelType(PHONEME_LIST *plist, PHONEME_DATA *phdata, unsigned short **p_prog, int instn_type)
  600. {
  601. unsigned short *prog;
  602. int voweltype;
  603. signed char x;
  604. if (instn_type == 2) {
  605. phdata->pd_control |= pd_FORNEXTPH;
  606. voweltype = plist[1].ph->start_type; // SwitchNextVowelType
  607. } else
  608. voweltype = plist[-1].ph->end_type; // SwitchPrevVowelType
  609. voweltype -= phonVOWELTYPES;
  610. if ((voweltype >= 0) && (voweltype < 6)) {
  611. prog = *p_prog + voweltype*2;
  612. phdata->sound_addr[instn_type] = (((prog[1] & 0xf) << 16) + prog[2]) * 4;
  613. x = (prog[1] >> 4) & 0xff;
  614. phdata->sound_param[instn_type] = x; // sign extend
  615. }
  616. *p_prog += 12;
  617. }
  618. int NumInstnWords(unsigned short *prog)
  619. {
  620. int instn;
  621. int instn2;
  622. int instn_type;
  623. int n;
  624. int type2;
  625. static const char n_words[16] = { 0, 1, 0, 0, 1, 1, 0, 1, 1, 2, 4, 0, 0, 0, 0, 0 };
  626. instn = *prog;
  627. instn_type = instn >> 12;
  628. if ((n = n_words[instn_type]) > 0)
  629. return n;
  630. switch (instn_type)
  631. {
  632. case 0:
  633. if (((instn & 0xf00) >> 8) == i_IPA_NAME) {
  634. n = ((instn & 0xff) + 1) / 2;
  635. return n+1;
  636. }
  637. return 1;
  638. case 6:
  639. type2 = (instn & 0xf00) >> 9;
  640. if ((type2 == 5) || (type2 == 6))
  641. return 12; // switch on vowel type
  642. return 1;
  643. case 2:
  644. case 3:
  645. // a condition, check for a 2-word instruction
  646. if (((n = instn & 0x0f00) == 0x600) || (n == 0x0d00))
  647. return 2;
  648. return 1;
  649. default:
  650. // instn_type 11 to 15, 2 words
  651. instn2 = prog[2];
  652. if ((instn2 >> 12) == 0xf) {
  653. // This instruction is followed by addWav(), 2 more words
  654. return 4;
  655. }
  656. if (instn2 == INSTN_CONTINUE)
  657. return 3;
  658. return 2;
  659. }
  660. }
  661. void InterpretPhoneme(Translator *tr, int control, PHONEME_LIST *plist, PHONEME_DATA *phdata, WORD_PH_DATA *worddata)
  662. {
  663. // control:
  664. // bit 0: PreVoicing
  665. // bit 8: change phonemes
  666. PHONEME_TAB *ph;
  667. unsigned short *prog;
  668. unsigned short instn;
  669. int instn2;
  670. int or_flag;
  671. bool truth;
  672. bool truth2;
  673. int data;
  674. int end_flag;
  675. int ix;
  676. signed char param_sc;
  677. #define N_RETURN 10
  678. int n_return = 0;
  679. unsigned short *return_addr[N_RETURN]; // return address stack
  680. ph = plist->ph;
  681. if ((worddata != NULL) && (plist->sourceix)) {
  682. // start of a word, reset word data
  683. worddata->prev_vowel.ph = NULL;
  684. }
  685. memset(phdata, 0, sizeof(PHONEME_DATA));
  686. phdata->pd_param[i_SET_LENGTH] = ph->std_length;
  687. phdata->pd_param[i_LENGTH_MOD] = ph->length_mod;
  688. if (ph->program == 0)
  689. return;
  690. end_flag = 0;
  691. for (prog = &phoneme_index[ph->program]; end_flag != 1; prog++) {
  692. instn = *prog;
  693. instn2 = (instn >> 8) & 0xf;
  694. switch (instn >> 12)
  695. {
  696. case 0: // 0xxx
  697. data = instn & 0xff;
  698. if (instn2 == 0) {
  699. // instructions with no operand
  700. switch (data)
  701. {
  702. case INSTN_RETURN:
  703. end_flag = 1;
  704. break;
  705. case INSTN_CONTINUE:
  706. break;
  707. default:
  708. InvalidInstn(ph, instn);
  709. break;
  710. }
  711. } else if (instn2 == i_APPEND_IFNEXTVOWEL) {
  712. if (phoneme_tab[plist[1].phcode]->type == phVOWEL)
  713. phdata->pd_param[i_APPEND_PHONEME] = data;
  714. } else if (instn2 == i_ADD_LENGTH) {
  715. if (data & 0x80) {
  716. // a negative value, do sign extension
  717. data = -(0x100 - data);
  718. }
  719. phdata->pd_param[i_SET_LENGTH] += data;
  720. } else if (instn2 == i_IPA_NAME) {
  721. // followed by utf-8 characters, 2 per instn word
  722. for (ix = 0; (ix < data) && (ix < 16); ix += 2) {
  723. prog++;
  724. phdata->ipa_string[ix] = prog[0] >> 8;
  725. phdata->ipa_string[ix+1] = prog[0] & 0xff;
  726. }
  727. phdata->ipa_string[ix] = 0;
  728. } else if (instn2 < N_PHONEME_DATA_PARAM) {
  729. phdata->pd_param[instn2] = data;
  730. if ((instn2 == i_CHANGE_PHONEME) && (control & 0x100)) {
  731. // found ChangePhoneme() in PhonemeList mode, exit
  732. end_flag = 1;
  733. }
  734. } else
  735. InvalidInstn(ph, instn);
  736. break;
  737. case 1:
  738. if (tr == NULL)
  739. break; // ignore if in synthesis stage
  740. if (instn2 < 8) {
  741. // ChangeIf
  742. if (StressCondition(tr, plist, instn2 & 7, 1) == true) {
  743. phdata->pd_param[i_CHANGE_PHONEME] = instn & 0xff;
  744. end_flag = 1; // change phoneme, exit
  745. }
  746. }
  747. break;
  748. case 2:
  749. case 3:
  750. // conditions
  751. or_flag = 0;
  752. truth = true;
  753. while ((instn & 0xe000) == 0x2000) {
  754. // process a sequence of conditions, using boolean accumulator
  755. truth2 = InterpretCondition(tr, control, plist, prog, worddata);
  756. prog += NumInstnWords(prog);
  757. if (*prog == i_NOT) {
  758. truth2 = truth2 ^ 1;
  759. prog++;
  760. }
  761. if (or_flag)
  762. truth = truth || truth2;
  763. else
  764. truth = truth && truth2;
  765. or_flag = instn & 0x1000;
  766. instn = *prog;
  767. }
  768. if (truth == false) {
  769. if ((instn & 0xf800) == i_JUMP_FALSE)
  770. prog += instn & 0xff;
  771. else {
  772. // instruction after a condition is not JUMP_FALSE, so skip the instruction.
  773. prog += NumInstnWords(prog);
  774. if ((prog[0] & 0xfe00) == 0x6000)
  775. prog++; // and skip ELSE jump
  776. }
  777. }
  778. prog--;
  779. break;
  780. case 6:
  781. // JUMP
  782. switch (instn2 >> 1)
  783. {
  784. case 0:
  785. prog += (instn & 0xff) - 1;
  786. break;
  787. case 4:
  788. // conditional jumps should have been processed in the Condition section
  789. break;
  790. case 5: // NexttVowelStarts
  791. SwitchOnVowelType(plist, phdata, &prog, 2);
  792. break;
  793. case 6: // PrevVowelTypeEndings
  794. SwitchOnVowelType(plist, phdata, &prog, 3);
  795. break;
  796. }
  797. break;
  798. case 9:
  799. data = ((instn & 0xf) << 16) + prog[1];
  800. prog++;
  801. switch (instn2)
  802. {
  803. case 1:
  804. // call a procedure or another phoneme
  805. if (n_return < N_RETURN) {
  806. return_addr[n_return++] = prog;
  807. prog = &phoneme_index[data] - 1;
  808. }
  809. break;
  810. case 2:
  811. // pitch envelope
  812. phdata->pitch_env = data;
  813. break;
  814. case 3:
  815. // amplitude envelope
  816. phdata->amp_env = data;
  817. break;
  818. }
  819. break;
  820. case 10: // Vowelin, Vowelout
  821. if (instn2 == 1)
  822. ix = 0;
  823. else
  824. ix = 2;
  825. phdata->vowel_transition[ix] = ((prog[0] & 0xff) << 16) + prog[1];
  826. phdata->vowel_transition[ix+1] = (prog[2] << 16) + prog[3];
  827. prog += 3;
  828. break;
  829. case 11: // FMT
  830. case 12: // WAV
  831. case 13: // VowelStart
  832. case 14: // VowelEnd
  833. case 15: // addWav
  834. instn2 = (instn >> 12) - 11;
  835. phdata->sound_addr[instn2] = ((instn & 0xf) << 18) + (prog[1] << 2);
  836. param_sc = phdata->sound_param[instn2] = (instn >> 4) & 0xff;
  837. prog++;
  838. if (prog[1] != INSTN_CONTINUE) {
  839. if (instn2 < 2) {
  840. // FMT() and WAV() imply Return
  841. end_flag = 1;
  842. if ((prog[1] >> 12) == 0xf) {
  843. // Return after the following addWav()
  844. end_flag = 2;
  845. }
  846. } else if (instn2 == pd_ADDWAV) {
  847. // addWav(), return if previous instruction was FMT() or WAV()
  848. end_flag--;
  849. }
  850. if ((instn2 == pd_VWLSTART) || (instn2 == pd_VWLEND)) {
  851. // VowelStart or VowelEnding.
  852. phdata->sound_param[instn2] = param_sc; // sign extend
  853. }
  854. }
  855. break;
  856. default:
  857. InvalidInstn(ph, instn);
  858. break;
  859. }
  860. if ((end_flag == 1) && (n_return > 0)) {
  861. // return from called procedure or phoneme
  862. end_flag = 0;
  863. prog = return_addr[--n_return];
  864. }
  865. }
  866. if ((worddata != NULL) && (plist->type == phVOWEL))
  867. memcpy(&worddata->prev_vowel, &plist[0], sizeof(PHONEME_LIST));
  868. plist->std_length = phdata->pd_param[i_SET_LENGTH];
  869. if (phdata->sound_addr[0] != 0) {
  870. plist->phontab_addr = phdata->sound_addr[0]; // FMT address
  871. plist->sound_param = phdata->sound_param[0];
  872. } else {
  873. plist->phontab_addr = phdata->sound_addr[1]; // WAV address
  874. plist->sound_param = phdata->sound_param[1];
  875. }
  876. }
  877. void InterpretPhoneme2(int phcode, PHONEME_DATA *phdata)
  878. {
  879. // Examine the program of a single isolated phoneme
  880. int ix;
  881. PHONEME_LIST plist[4];
  882. memset(plist, 0, sizeof(plist));
  883. for (ix = 0; ix < 4; ix++) {
  884. plist[ix].phcode = phonPAUSE;
  885. plist[ix].ph = phoneme_tab[phonPAUSE];
  886. }
  887. plist[1].phcode = phcode;
  888. plist[1].ph = phoneme_tab[phcode];
  889. plist[2].sourceix = 1;
  890. InterpretPhoneme(NULL, 0, &plist[1], phdata, NULL);
  891. }