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

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