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

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