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

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