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 17KB

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