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

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