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

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