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

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