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

synthdata.c 26KB

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