eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

synthdata.c 25KB

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