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.

synth_mbrola.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Copyright (C) 2005 to 2013 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 <ctype.h>
  21. #include <errno.h>
  22. #include <math.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <espeak-ng/espeak_ng.h>
  29. #include <espeak-ng/speak_lib.h>
  30. #include <espeak-ng/encoding.h>
  31. #include "dictionary.h"
  32. #include "mbrola.h"
  33. #include "readclause.h"
  34. #include "setlengths.h"
  35. #include "synthdata.h"
  36. #include "wavegen.h"
  37. #include "speech.h"
  38. #include "phoneme.h"
  39. #include "voice.h"
  40. #include "synthesize.h"
  41. #include "translate.h"
  42. // included here so tests can find these even without OPT_MBROLA set
  43. int mbrola_delay;
  44. char mbrola_name[20];
  45. #ifdef INCLUDE_MBROLA
  46. #if defined(_WIN32) || defined(_WIN64)
  47. #include <windows.h>
  48. #endif
  49. #include "mbrowrap.h"
  50. static MBROLA_TAB *mbrola_tab = NULL;
  51. static int mbrola_control = 0;
  52. static int mbr_name_prefix = 0;
  53. espeak_ng_STATUS LoadMbrolaTable(const char *mbrola_voice, const char *phtrans, int *srate)
  54. {
  55. // Load a phoneme name translation table from espeak-ng-data/mbrola
  56. int size;
  57. int ix;
  58. int *pw;
  59. FILE *f_in;
  60. char path[sizeof(path_home)+15];
  61. mbrola_name[0] = 0;
  62. mbrola_delay = 0;
  63. mbr_name_prefix = 0;
  64. if (mbrola_voice == NULL) {
  65. samplerate = samplerate_native;
  66. SetParameter(espeakVOICETYPE, 0, 0);
  67. return ENS_OK;
  68. }
  69. if (!load_MBR())
  70. return ENS_MBROLA_NOT_FOUND;
  71. sprintf(path, "%s/mbrola/%s", path_home, mbrola_voice);
  72. #ifdef PLATFORM_POSIX
  73. // if not found, then also look in
  74. // usr/share/mbrola/xx, /usr/share/mbrola/xx/xx, /usr/share/mbrola/voices/xx
  75. if (GetFileLength(path) <= 0) {
  76. sprintf(path, "/usr/share/mbrola/%s", mbrola_voice);
  77. if (GetFileLength(path) <= 0) {
  78. sprintf(path, "/usr/share/mbrola/%s/%s", mbrola_voice, mbrola_voice);
  79. if (GetFileLength(path) <= 0)
  80. sprintf(path, "/usr/share/mbrola/voices/%s", mbrola_voice);
  81. }
  82. }
  83. close_MBR();
  84. #endif
  85. if (init_MBR(path) != 0) // initialise the required mbrola voice
  86. return ENS_MBROLA_VOICE_NOT_FOUND;
  87. setNoError_MBR(1); // don't stop on phoneme errors
  88. // read eSpeak's mbrola phoneme translation data, eg. en1_phtrans
  89. sprintf(path, "%s/mbrola_ph/%s", path_home, phtrans);
  90. size = GetFileLength(path);
  91. if (size < 0) // size == -errno
  92. return -size;
  93. if ((f_in = fopen(path, "rb")) == NULL) {
  94. int error = errno;
  95. close_MBR();
  96. return error;
  97. }
  98. MBROLA_TAB *new_mbrola_tab = (MBROLA_TAB *)realloc(mbrola_tab, size);
  99. if (new_mbrola_tab == NULL) {
  100. fclose(f_in);
  101. close_MBR();
  102. return ENOMEM;
  103. }
  104. mbrola_tab = new_mbrola_tab;
  105. mbrola_control = Read4Bytes(f_in);
  106. pw = (int *)mbrola_tab;
  107. for (ix = 4; ix < size; ix += 4)
  108. *pw++ = Read4Bytes(f_in);
  109. fclose(f_in);
  110. setVolumeRatio_MBR((float)(mbrola_control & 0xff) /16.0f);
  111. samplerate = *srate = getFreq_MBR();
  112. if (*srate == 22050)
  113. SetParameter(espeakVOICETYPE, 0, 0);
  114. else
  115. SetParameter(espeakVOICETYPE, 1, 0);
  116. strcpy(mbrola_name, mbrola_voice);
  117. mbrola_delay = 1000; // improve synchronization of events
  118. return ENS_OK;
  119. }
  120. static int GetMbrName(PHONEME_LIST *plist, PHONEME_TAB *ph, PHONEME_TAB *ph_prev, PHONEME_TAB *ph_next, int *name2, int *split, int *control)
  121. {
  122. // Look up a phoneme in the mbrola phoneme name translation table
  123. // It may give none, 1, or 2 mbrola phonemes
  124. MBROLA_TAB *pr;
  125. PHONEME_TAB *other_ph;
  126. bool found = false;
  127. static int mnem;
  128. // control
  129. // bit 0 skip the next phoneme
  130. // bit 1 match this and Previous phoneme
  131. // bit 2 only at the start of a word
  132. // bit 3 don't match two phonemes across a word boundary
  133. // bit 4 add this phoneme name as a prefix to the next phoneme name (used for de4 phoneme prefix '?')
  134. // bit 5 only in stressed syllable
  135. // bit 6 only at the end of a word
  136. *name2 = 0;
  137. *split = 0;
  138. *control = 0;
  139. mnem = ph->mnemonic;
  140. pr = mbrola_tab;
  141. while (pr->name != 0) {
  142. if (mnem == pr->name) {
  143. if (pr->next_phoneme == 0)
  144. found = true;
  145. else if ((pr->next_phoneme == ':') && (plist->synthflags & SFLAG_LENGTHEN))
  146. found = true;
  147. else {
  148. if (pr->control & 2)
  149. other_ph = ph_prev;
  150. else if ((pr->control & 8) && ((plist+1)->newword))
  151. other_ph = phoneme_tab[phPAUSE]; // don't match the next phoneme over a word boundary
  152. else
  153. other_ph = ph_next;
  154. if ((pr->next_phoneme == other_ph->mnemonic) ||
  155. ((pr->next_phoneme == 2) && (other_ph->type == phVOWEL)) ||
  156. ((pr->next_phoneme == '_') && (other_ph->type == phPAUSE)))
  157. found = true;
  158. }
  159. if ((pr->control & 4) && (plist->newword == 0)) // only at start of word
  160. found = false;
  161. if ((pr->control & 0x40) && (plist[1].newword == 0)) // only at the end of a word
  162. found = false;
  163. if ((pr->control & 0x20) && (plist->stresslevel < plist->wordstress))
  164. found = false; // only in stressed syllables
  165. if (found) {
  166. *name2 = pr->mbr_name2;
  167. *split = pr->percent;
  168. *control = pr->control;
  169. if (pr->control & 0x10) {
  170. mbr_name_prefix = pr->mbr_name;
  171. return 0;
  172. }
  173. mnem = pr->mbr_name;
  174. break;
  175. }
  176. }
  177. pr++;
  178. }
  179. if (mbr_name_prefix != 0)
  180. mnem = (mnem << 8) | (mbr_name_prefix & 0xff);
  181. mbr_name_prefix = 0;
  182. return mnem;
  183. }
  184. static char *WritePitch(int env, int pitch1, int pitch2, int split, int final)
  185. {
  186. // final=1: only give the final pitch value.
  187. int x;
  188. int ix;
  189. int pitch_base;
  190. int pitch_range;
  191. int p1, p2, p_end;
  192. unsigned char *pitch_env;
  193. int max = -1;
  194. int min = 999;
  195. int y_max = 0;
  196. int y_min = 0;
  197. int env100 = 80; // apply the pitch change only over this proportion of the mbrola phoneme(s)
  198. int y2;
  199. int y[4];
  200. int env_split;
  201. char buf[50];
  202. static char output[50];
  203. output[0] = 0;
  204. pitch_env = envelope_data[env];
  205. SetPitch2(voice, pitch1, pitch2, &pitch_base, &pitch_range);
  206. env_split = (split * 128)/100;
  207. if (env_split < 0)
  208. env_split = 0-env_split;
  209. // find max and min in the pitch envelope
  210. for (x = 0; x < 128; x++) {
  211. if (pitch_env[x] > max) {
  212. max = pitch_env[x];
  213. y_max = x;
  214. }
  215. if (pitch_env[x] < min) {
  216. min = pitch_env[x];
  217. y_min = x;
  218. }
  219. }
  220. // set an additional pitch point half way through the phoneme.
  221. // but look for a maximum or a minimum and use that instead
  222. y[2] = 64;
  223. if ((y_max > 0) && (y_max < 127))
  224. y[2] = y_max;
  225. if ((y_min > 0) && (y_min < 127))
  226. y[2] = y_min;
  227. y[1] = y[2] / 2;
  228. y[3] = y[2] + (127 - y[2])/2;
  229. // set initial pitch
  230. p1 = ((pitch_env[0]*pitch_range)>>8) + pitch_base; // Hz << 12
  231. p_end = ((pitch_env[127]*pitch_range)>>8) + pitch_base;
  232. if (split >= 0) {
  233. sprintf(buf, " 0 %d", p1/4096);
  234. strcat(output, buf);
  235. }
  236. // don't use intermediate pitch points for linear rise and fall
  237. if (env > 1) {
  238. for (ix = 1; ix < 4; ix++) {
  239. p2 = ((pitch_env[y[ix]]*pitch_range)>>8) + pitch_base;
  240. if (split > 0)
  241. y2 = (y[ix] * env100)/env_split;
  242. else if (split < 0)
  243. y2 = ((y[ix]-env_split) * env100)/env_split;
  244. else
  245. y2 = (y[ix] * env100)/128;
  246. if ((y2 > 0) && (y2 <= env100)) {
  247. sprintf(buf, " %d %d", y2, p2/4096);
  248. strcat(output, buf);
  249. }
  250. }
  251. }
  252. p_end = p_end/4096;
  253. if (split <= 0) {
  254. sprintf(buf, " %d %d", env100, p_end);
  255. strcat(output, buf);
  256. }
  257. if (env100 < 100) {
  258. sprintf(buf, " %d %d", 100, p_end);
  259. strcat(output, buf);
  260. }
  261. strcat(output, "\n");
  262. if (final)
  263. sprintf(output, "\t100 %d\n", p_end);
  264. return output;
  265. }
  266. int MbrolaTranslate(PHONEME_LIST *plist, int n_phonemes, bool resume, FILE *f_mbrola)
  267. {
  268. // Generate a mbrola pho file
  269. unsigned int name;
  270. int len;
  271. int len1;
  272. PHONEME_TAB *ph;
  273. PHONEME_TAB *ph_next;
  274. PHONEME_TAB *ph_prev;
  275. PHONEME_LIST *p;
  276. PHONEME_LIST *next;
  277. PHONEME_DATA phdata;
  278. FMT_PARAMS fmtp;
  279. int pause = 0;
  280. bool released;
  281. int name2;
  282. int control;
  283. bool done;
  284. int len_percent;
  285. const char *final_pitch;
  286. char *ptr;
  287. char mbr_buf[120];
  288. static int phix;
  289. static int embedded_ix;
  290. static int word_count;
  291. if (!resume) {
  292. phix = 1;
  293. embedded_ix = 0;
  294. word_count = 0;
  295. }
  296. while (phix < n_phonemes) {
  297. if (WcmdqFree() < MIN_WCMDQ)
  298. return 1;
  299. ptr = mbr_buf;
  300. p = &plist[phix];
  301. next = &plist[phix+1];
  302. ph = p->ph;
  303. ph_prev = plist[phix-1].ph;
  304. ph_next = plist[phix+1].ph;
  305. if (p->synthflags & SFLAG_EMBEDDED)
  306. DoEmbedded(&embedded_ix, p->sourceix);
  307. if (p->newword & 4)
  308. DoMarker(espeakEVENT_SENTENCE, (p->sourceix & 0x7ff) + clause_start_char, 0, count_sentences);
  309. if (p->newword & 1)
  310. DoMarker(espeakEVENT_WORD, (p->sourceix & 0x7ff) + clause_start_char, p->sourceix >> 11, clause_start_word + word_count++);
  311. name = GetMbrName(p, ph, ph_prev, ph_next, &name2, &len_percent, &control);
  312. if (control & 1)
  313. phix++;
  314. if (name == 0) {
  315. phix++;
  316. continue; // ignore this phoneme
  317. }
  318. if ((ph->type == phPAUSE) && (name == ph->mnemonic)) {
  319. // a pause phoneme, which has not been changed by the translation
  320. name = '_';
  321. len = (p->length * speed.pause_factor)/256;
  322. if (len == 0)
  323. len = 1;
  324. } else
  325. len = (80 * speed.wav_factor)/256;
  326. if (ph->code != phonEND_WORD) {
  327. char phoneme_name[16];
  328. WritePhMnemonic(phoneme_name, p->ph, p, option_phoneme_events & espeakINITIALIZE_PHONEME_IPA, NULL);
  329. DoPhonemeMarker(espeakEVENT_PHONEME, (p->sourceix & 0x7ff) + clause_start_char, 0, phoneme_name);
  330. }
  331. ptr += sprintf(ptr, "%s\t", WordToString(name));
  332. if (name2 == '_') {
  333. // add a pause after this phoneme
  334. pause = len_percent;
  335. name2 = 0;
  336. }
  337. done = false;
  338. final_pitch = "";
  339. switch (ph->type)
  340. {
  341. case phVOWEL:
  342. len = ph->std_length;
  343. if (p->synthflags & SFLAG_LENGTHEN)
  344. len += phoneme_tab[phonLENGTHEN]->std_length; // phoneme was followed by an extra : symbol
  345. if (ph_next->type == phPAUSE)
  346. len += 50; // lengthen vowels before a pause
  347. len = (len * p->length)/256;
  348. if (name2 == 0) {
  349. char *pitch = WritePitch(p->env, p->pitch1, p->pitch2, 0, 0);
  350. ptr += sprintf(ptr, "%d\t%s", len, pitch);
  351. } else {
  352. char *pitch;
  353. pitch = WritePitch(p->env, p->pitch1, p->pitch2, len_percent, 0);
  354. len1 = (len * len_percent)/100;
  355. ptr += sprintf(ptr, "%d\t%s", len1, pitch);
  356. pitch = WritePitch(p->env, p->pitch1, p->pitch2, -len_percent, 0);
  357. ptr += sprintf(ptr, "%s\t%d\t%s", WordToString(name2), len-len1, pitch);
  358. }
  359. done = true;
  360. break;
  361. case phSTOP:
  362. released = false;
  363. if (next->type == phVOWEL) released = true;
  364. if (next->type == phLIQUID && !next->newword) released = true;
  365. if (released == false)
  366. p->synthflags |= SFLAG_NEXT_PAUSE;
  367. InterpretPhoneme(NULL, 0, p, &phdata, NULL);
  368. len = DoSample3(&phdata, 0, -1);
  369. len = (len * 1000)/samplerate; // convert to mS
  370. len += PauseLength(p->prepause, 1);
  371. break;
  372. case phVSTOP:
  373. len = (80 * speed.wav_factor)/256;
  374. break;
  375. case phFRICATIVE:
  376. len = 0;
  377. InterpretPhoneme(NULL, 0, p, &phdata, NULL);
  378. if (p->synthflags & SFLAG_LENGTHEN)
  379. len = DoSample3(&phdata, p->length, -1); // play it twice for [s:] etc.
  380. len += DoSample3(&phdata, p->length, -1);
  381. len = (len * 1000)/samplerate; // convert to mS
  382. break;
  383. case phNASAL:
  384. if (next->type != phVOWEL) {
  385. memset(&fmtp, 0, sizeof(fmtp));
  386. InterpretPhoneme(NULL, 0, p, &phdata, NULL);
  387. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  388. len = DoSpect2(p->ph, 0, &fmtp, p, -1);
  389. len = (len * 1000)/samplerate;
  390. if (next->type == phPAUSE)
  391. len += 50;
  392. final_pitch = WritePitch(p->env, p->pitch1, p->pitch2, 0, 1);
  393. }
  394. break;
  395. case phLIQUID:
  396. if (next->type == phPAUSE) {
  397. len += 50;
  398. final_pitch = WritePitch(p->env, p->pitch1, p->pitch2, 0, 1);
  399. }
  400. break;
  401. }
  402. if (!done) {
  403. if (name2 != 0) {
  404. len1 = (len * len_percent)/100;
  405. ptr += sprintf(ptr, "%d\n%s\t", len1, WordToString(name2));
  406. len -= len1;
  407. }
  408. ptr += sprintf(ptr, "%d%s\n", len, final_pitch);
  409. }
  410. if (pause) {
  411. len += PauseLength(pause, 0);
  412. ptr += sprintf(ptr, "_ \t%d\n", PauseLength(pause, 0));
  413. pause = 0;
  414. }
  415. if (f_mbrola)
  416. fwrite(mbr_buf, 1, (ptr-mbr_buf), f_mbrola); // write .pho to a file
  417. else {
  418. int res = write_MBR(mbr_buf);
  419. if (res < 0)
  420. return 0; // don't get stuck on error
  421. if (res == 0)
  422. return 1;
  423. wcmdq[wcmdq_tail][0] = WCMD_MBROLA_DATA;
  424. wcmdq[wcmdq_tail][1] = len;
  425. WcmdqInc();
  426. }
  427. phix++;
  428. }
  429. if (!f_mbrola) {
  430. flush_MBR();
  431. // flush the mbrola output buffer
  432. wcmdq[wcmdq_tail][0] = WCMD_MBROLA_DATA;
  433. wcmdq[wcmdq_tail][1] = 500;
  434. WcmdqInc();
  435. }
  436. return 0;
  437. }
  438. int MbrolaGenerate(PHONEME_LIST *phoneme_list, int *n_ph, bool resume)
  439. {
  440. FILE *f_mbrola = NULL;
  441. if (*n_ph == 0)
  442. return 0;
  443. if (option_phonemes & espeakPHONEMES_MBROLA) {
  444. // send mbrola data to a file, not to the mbrola library
  445. f_mbrola = f_trans;
  446. }
  447. int again = MbrolaTranslate(phoneme_list, *n_ph, resume, f_mbrola);
  448. if (!again)
  449. *n_ph = 0;
  450. return again;
  451. }
  452. int MbrolaFill(int length, bool resume, int amplitude)
  453. {
  454. // Read audio data from Mbrola (length is in millisecs)
  455. static int n_samples;
  456. int req_samples, result;
  457. int ix;
  458. short value16;
  459. int value;
  460. if (!resume)
  461. n_samples = samplerate * length / 1000;
  462. req_samples = (out_end - out_ptr)/2;
  463. if (req_samples > n_samples)
  464. req_samples = n_samples;
  465. result = read_MBR((short *)out_ptr, req_samples);
  466. if (result <= 0)
  467. return 0;
  468. for (ix = 0; ix < result; ix++) {
  469. value16 = out_ptr[0] + (out_ptr[1] << 8);
  470. value = value16 * amplitude;
  471. value = value / 40; // adjust this constant to give a suitable amplitude for mbrola voices
  472. if (value > 0x7fff)
  473. value = 0x7fff;
  474. if (value < -0x8000)
  475. value = 0x8000;
  476. out_ptr[0] = value;
  477. out_ptr[1] = value >> 8;
  478. out_ptr += 2;
  479. }
  480. n_samples -= result;
  481. return n_samples ? 1 : 0;
  482. }
  483. void MbrolaReset(void)
  484. {
  485. // Reset the Mbrola engine and flush the pending audio
  486. reset_MBR();
  487. }
  488. #else
  489. // mbrola interface is not compiled, provide dummy functions.
  490. espeak_ng_STATUS LoadMbrolaTable(const char *mbrola_voice, const char *phtrans, int *srate)
  491. {
  492. (void)mbrola_voice; // unused parameter
  493. (void)phtrans; // unused parameter
  494. (void)srate; // unused parameter
  495. return ENS_NOT_SUPPORTED;
  496. }
  497. int MbrolaGenerate(PHONEME_LIST *phoneme_list, int *n_ph, bool resume)
  498. {
  499. (void)phoneme_list; // unused parameter
  500. (void)n_ph; // unused parameter
  501. (void)resume; // unused parameter
  502. return 0;
  503. }
  504. int MbrolaFill(int length, bool resume, int amplitude)
  505. {
  506. (void)length; // unused parameter
  507. (void)resume; // unused parameter
  508. (void)amplitude; // unused parameter
  509. return 0;
  510. }
  511. void MbrolaReset(void)
  512. {
  513. }
  514. #endif