synth_mbrola.cpp 14 KB

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