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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2013 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include "StdAfx.h"
  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. // srate = getFreq_MBR();
  157. samplerate = srate;
  158. if(srate == 22050)
  159. SetParameter(espeakVOICETYPE,0,0);
  160. else
  161. SetParameter(espeakVOICETYPE,1,0);
  162. strcpy(mbrola_name,mbrola_voice);
  163. // mbrola_delay = 3800; // improve synchronization of events
  164. mbrola_delay = 1000; // improve synchronization of events
  165. return(EE_OK);
  166. } // end of LoadMbrolaTable
  167. static int GetMbrName(PHONEME_LIST *plist, PHONEME_TAB *ph, PHONEME_TAB *ph_prev, PHONEME_TAB *ph_next, int *name2, int *split, int *control)
  168. {//==========================================================================================================================================
  169. // Look up a phoneme in the mbrola phoneme name translation table
  170. // It may give none, 1, or 2 mbrola phonemes
  171. MBROLA_TAB *pr;
  172. PHONEME_TAB *other_ph;
  173. int found = 0;
  174. static int mnem;
  175. // control
  176. // bit 0 skip the next phoneme
  177. // bit 1 match this and Previous phoneme
  178. // bit 2 only at the start of a word
  179. // bit 3 don't match two phonemes across a word boundary
  180. // bit 4 add this phoneme name as a prefix to the next phoneme name (used for de4 phoneme prefix '?')
  181. // bit 5 only in stressed syllable
  182. // bit 6 only at the end of a word
  183. *name2=0;
  184. *split=0;
  185. *control=0;
  186. mnem = ph->mnemonic;
  187. pr = mbrola_tab;
  188. while(pr->name != 0)
  189. {
  190. if(mnem == pr->name)
  191. {
  192. if(pr->next_phoneme == 0)
  193. found = 1;
  194. else
  195. if((pr->next_phoneme == ':') && (plist->synthflags & SFLAG_LENGTHEN))
  196. {
  197. found = 1;
  198. }
  199. else
  200. {
  201. if(pr->control & 2)
  202. other_ph = ph_prev;
  203. else
  204. if((pr->control & 8) && ((plist+1)->newword))
  205. other_ph = phoneme_tab[phPAUSE]; // don't match the next phoneme over a word boundary
  206. else
  207. other_ph = ph_next;
  208. if((pr->next_phoneme == other_ph->mnemonic) ||
  209. ((pr->next_phoneme == 2) && (other_ph->type == phVOWEL)) ||
  210. ((pr->next_phoneme == '_') && (other_ph->type == phPAUSE)))
  211. {
  212. found = 1;
  213. }
  214. }
  215. if((pr->control & 4) && (plist->newword == 0)) // only at start of word
  216. found = 0;
  217. if((pr->control & 0x40) && (plist[1].newword == 0)) // only at the end of a word
  218. found = 0;
  219. if((pr->control & 0x20) && (plist->stresslevel < plist->wordstress))
  220. found = 0; // only in stressed syllables
  221. if(found)
  222. {
  223. *name2 = pr->mbr_name2;
  224. *split = pr->percent;
  225. *control = pr->control;
  226. if(pr->control & 0x10)
  227. {
  228. mbr_name_prefix = pr->mbr_name;
  229. return(0);
  230. }
  231. mnem = pr->mbr_name;
  232. break;
  233. }
  234. }
  235. pr++;
  236. }
  237. if(mbr_name_prefix != 0)
  238. {
  239. mnem = (mnem << 8) | (mbr_name_prefix & 0xff);
  240. }
  241. mbr_name_prefix = 0;
  242. return(mnem);
  243. }
  244. static char *WritePitch(int env, int pitch1, int pitch2, int split, int final)
  245. {//===========================================================================
  246. // final=1: only give the final pitch value.
  247. int x;
  248. int ix;
  249. int pitch_base;
  250. int pitch_range;
  251. int p1,p2,p_end;
  252. unsigned char *pitch_env;
  253. int max = -1;
  254. int min = 999;
  255. int y_max=0;
  256. int y_min=0;
  257. int env100 = 80; // apply the pitch change only over this proportion of the mbrola phoneme(s)
  258. int y2;
  259. int y[4];
  260. int env_split;
  261. char buf[50];
  262. static char output[50];
  263. output[0] = 0;
  264. pitch_env = envelope_data[env];
  265. SetPitch2(voice, pitch1, pitch2, &pitch_base, &pitch_range);
  266. env_split = (split * 128)/100;
  267. if(env_split < 0)
  268. env_split = 0-env_split;
  269. // find max and min in the pitch envelope
  270. for(x=0; x<128; x++)
  271. {
  272. if(pitch_env[x] > max)
  273. {
  274. max = pitch_env[x];
  275. y_max = x;
  276. }
  277. if(pitch_env[x] < min)
  278. {
  279. min = pitch_env[x];
  280. y_min = x;
  281. }
  282. }
  283. // set an additional pitch point half way through the phoneme.
  284. // but look for a maximum or a minimum and use that instead
  285. y[2] = 64;
  286. if((y_max > 0) && (y_max < 127))
  287. {
  288. y[2] = y_max;
  289. }
  290. if((y_min > 0) && (y_min < 127))
  291. {
  292. y[2] = y_min;
  293. }
  294. y[1] = y[2] / 2;
  295. y[3] = y[2] + (127 - y[2])/2;
  296. // set initial pitch
  297. p1 = ((pitch_env[0]*pitch_range)>>8) + pitch_base; // Hz << 12
  298. p_end = ((pitch_env[127]*pitch_range)>>8) + pitch_base;
  299. if(split >= 0)
  300. {
  301. sprintf(buf," 0 %d",p1/4096);
  302. strcat(output,buf);
  303. }
  304. // don't use intermediate pitch points for linear rise and fall
  305. if(env > 1)
  306. {
  307. for(ix=1; ix<4; ix++)
  308. {
  309. p2 = ((pitch_env[y[ix]]*pitch_range)>>8) + pitch_base;
  310. if(split > 0)
  311. {
  312. y2 = (y[ix] * env100)/env_split;
  313. }
  314. else
  315. if(split < 0)
  316. {
  317. y2 = ((y[ix]-env_split) * env100)/env_split;
  318. }
  319. else
  320. {
  321. y2 = (y[ix] * env100)/128;
  322. }
  323. if((y2 > 0) && (y2 <= env100))
  324. {
  325. sprintf(buf," %d %d",y2,p2/4096);
  326. strcat(output,buf);
  327. }
  328. }
  329. }
  330. p_end = p_end/4096;
  331. if(split <= 0)
  332. {
  333. sprintf(buf," %d %d",env100,p_end);
  334. strcat(output,buf);
  335. }
  336. if(env100 < 100)
  337. {
  338. sprintf(buf," %d %d",100,p_end);
  339. strcat(output,buf);
  340. }
  341. strcat(output,"\n");
  342. if(final)
  343. sprintf(output,"\t100 %d\n",p_end);
  344. return(output);
  345. } // end of WritePitch
  346. int MbrolaTranslate(PHONEME_LIST *plist, int n_phonemes, int resume, FILE *f_mbrola)
  347. {//=================================================================================
  348. // Generate a mbrola pho file
  349. unsigned int name;
  350. int len;
  351. int len1;
  352. PHONEME_TAB *ph;
  353. PHONEME_TAB *ph_next;
  354. PHONEME_TAB *ph_prev;
  355. PHONEME_LIST *p;
  356. PHONEME_LIST *next;
  357. PHONEME_DATA phdata;
  358. FMT_PARAMS fmtp;
  359. int pause = 0;
  360. int released;
  361. int name2;
  362. int control;
  363. int done;
  364. int len_percent;
  365. const char *final_pitch;
  366. char *ptr;
  367. char mbr_buf[120];
  368. static int phix;
  369. static int embedded_ix;
  370. static int word_count;
  371. if (!resume) {
  372. phix = 1;
  373. embedded_ix = 0;
  374. word_count = 0;
  375. }
  376. while (phix < n_phonemes)
  377. {
  378. if (WcmdqFree() < MIN_WCMDQ)
  379. return 1;
  380. ptr = mbr_buf;
  381. p = &plist[phix];
  382. next = &plist[phix+1];
  383. ph = p->ph;
  384. ph_prev = plist[phix-1].ph;
  385. ph_next = plist[phix+1].ph;
  386. if(p->synthflags & SFLAG_EMBEDDED)
  387. {
  388. DoEmbedded(&embedded_ix, p->sourceix);
  389. }
  390. if(p->newword & 4)
  391. DoMarker(espeakEVENT_SENTENCE, (p->sourceix & 0x7ff) + clause_start_char, 0, count_sentences);
  392. if(p->newword & 1)
  393. DoMarker(espeakEVENT_WORD, (p->sourceix & 0x7ff) + clause_start_char, p->sourceix >> 11, clause_start_word + word_count++);
  394. name = GetMbrName(p,ph,ph_prev,ph_next,&name2,&len_percent,&control);
  395. if(control & 1)
  396. phix++;
  397. if(name == 0) {
  398. phix++;
  399. continue; // ignore this phoneme
  400. }
  401. if((ph->type == phPAUSE) && (name == ph->mnemonic))
  402. {
  403. // a pause phoneme, which has not been changed by the translation
  404. name = '_';
  405. len = (p->length * speed.pause_factor)/256;
  406. // if(len == 0) continue;
  407. if(len == 0)
  408. len = 1;
  409. }
  410. else
  411. len = (80 * speed.wav_factor)/256;
  412. if(ph->code != phonEND_WORD)
  413. {
  414. char phoneme_name[16];
  415. WritePhMnemonic(phoneme_name, p->ph, p, option_phoneme_events & espeakINITIALIZE_PHONEME_IPA, NULL);
  416. DoPhonemeMarker(espeakEVENT_PHONEME, (p->sourceix & 0x7ff) + clause_start_char, 0, phoneme_name);
  417. }
  418. ptr += sprintf(ptr,"%s\t",WordToString(name));
  419. if(name2 == '_')
  420. {
  421. // add a pause after this phoneme
  422. pause = len_percent;
  423. name2 = 0;
  424. }
  425. done = 0;
  426. final_pitch = "";
  427. switch(ph->type)
  428. {
  429. case phVOWEL:
  430. len = ph->std_length;
  431. if(p->synthflags & SFLAG_LENGTHEN)
  432. len += phoneme_tab[phonLENGTHEN]->std_length; // phoneme was followed by an extra : symbol
  433. if(ph_next->type == phPAUSE)
  434. len += 50; // lengthen vowels before a pause
  435. len = (len * p->length)/256;
  436. if(name2 == 0)
  437. {
  438. char *pitch = WritePitch(p->env,p->pitch1,p->pitch2,0,0);
  439. ptr += sprintf(ptr,"%d\t%s", len, pitch);
  440. }
  441. else
  442. {
  443. char *pitch;
  444. pitch = WritePitch(p->env,p->pitch1,p->pitch2,len_percent,0);
  445. len1 = (len * len_percent)/100;
  446. ptr += sprintf(ptr,"%d\t%s", len1, pitch);
  447. pitch = WritePitch(p->env,p->pitch1,p->pitch2,-len_percent,0);
  448. ptr += sprintf(ptr,"%s\t%d\t%s", WordToString(name2), len-len1, pitch);
  449. }
  450. done = 1;
  451. break;
  452. case phSTOP:
  453. released = 0;
  454. if(next->type==phVOWEL) released = 1;
  455. if(next->type==phLIQUID && !next->newword) released = 1;
  456. if(released == 0)
  457. p->synthflags |= SFLAG_NEXT_PAUSE;
  458. InterpretPhoneme(NULL, 0, p, &phdata, NULL);
  459. len = DoSample3(&phdata, 0, -1);
  460. len = (len * 1000)/samplerate; // convert to mS
  461. len += PauseLength(p->prepause,1);
  462. break;
  463. case phVSTOP:
  464. len = (80 * speed.wav_factor)/256;
  465. break;
  466. case phFRICATIVE:
  467. len = 0;
  468. InterpretPhoneme(NULL, 0, p, &phdata, NULL);
  469. if(p->synthflags & SFLAG_LENGTHEN)
  470. len = DoSample3(&phdata, p->length, -1); // play it twice for [s:] etc.
  471. len += DoSample3(&phdata, p->length, -1);
  472. len = (len * 1000)/samplerate; // convert to mS
  473. break;
  474. case phNASAL:
  475. if(next->type != phVOWEL)
  476. {
  477. memset(&fmtp, 0, sizeof(fmtp));
  478. InterpretPhoneme(NULL, 0, p, &phdata, NULL);
  479. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  480. len = DoSpect2(p->ph, 0, &fmtp, p, -1);
  481. // len = DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,-1);
  482. len = (len * 1000)/samplerate;
  483. if(next->type == phPAUSE)
  484. len += 50;
  485. final_pitch = WritePitch(p->env,p->pitch1,p->pitch2,0,1);
  486. }
  487. break;
  488. case phLIQUID:
  489. if(next->type == phPAUSE)
  490. {
  491. len += 50;
  492. final_pitch = WritePitch(p->env,p->pitch1,p->pitch2,0,1);
  493. }
  494. break;
  495. }
  496. if(!done)
  497. {
  498. if(name2 != 0)
  499. {
  500. len1 = (len * len_percent)/100;
  501. ptr += sprintf(ptr,"%d\n%s\t",len1,WordToString(name2));
  502. len -= len1;
  503. }
  504. ptr += sprintf(ptr,"%d%s\n",len,final_pitch);
  505. }
  506. if(pause)
  507. {
  508. len += PauseLength(pause,0);
  509. ptr += sprintf(ptr,"_ \t%d\n",PauseLength(pause,0));
  510. pause = 0;
  511. }
  512. if(f_mbrola)
  513. {
  514. fwrite(mbr_buf,1,(ptr-mbr_buf),f_mbrola); // write .pho to a file
  515. }
  516. else
  517. {
  518. int res = write_MBR(mbr_buf);
  519. if (res < 0)
  520. return 0; /* don't get stuck on error */
  521. if (res == 0)
  522. return 1;
  523. wcmdq[wcmdq_tail][0] = WCMD_MBROLA_DATA;
  524. wcmdq[wcmdq_tail][1] = len;
  525. WcmdqInc();
  526. }
  527. phix++;
  528. }
  529. if(!f_mbrola)
  530. {
  531. flush_MBR();
  532. // flush the mbrola output buffer
  533. wcmdq[wcmdq_tail][0] = WCMD_MBROLA_DATA;
  534. wcmdq[wcmdq_tail][1] = 500;
  535. WcmdqInc();
  536. }
  537. return 0;
  538. } // end of MbrolaTranslate
  539. int MbrolaGenerate(PHONEME_LIST *phoneme_list, int *n_ph, int resume)
  540. {//==================================================================
  541. FILE *f_mbrola = NULL;
  542. if(*n_ph == 0)
  543. return(0);
  544. if(option_phonemes & espeakPHONEMES_MBROLA)
  545. {
  546. // send mbrola data to a file, not to the mbrola library
  547. f_mbrola = f_trans;
  548. }
  549. int again = MbrolaTranslate(phoneme_list, *n_ph, resume, f_mbrola);
  550. if (!again)
  551. *n_ph = 0;
  552. return again;
  553. }
  554. int MbrolaFill(int length, int resume, int amplitude)
  555. {//==================================================
  556. // Read audio data from Mbrola (length is in millisecs)
  557. static int n_samples;
  558. int req_samples, result;
  559. int ix;
  560. short value16;
  561. int value;
  562. if (!resume)
  563. n_samples = samplerate * length / 1000;
  564. req_samples = (out_end - out_ptr)/2;
  565. if (req_samples > n_samples)
  566. req_samples = n_samples;
  567. result = read_MBR((short *)out_ptr, req_samples);
  568. if (result <= 0)
  569. return 0;
  570. for(ix=0; ix < result; ix++)
  571. {
  572. value16 = out_ptr[0] + (out_ptr[1] << 8);
  573. value = value16 * amplitude;
  574. value = value / 40; // adjust this constant to give a suitable amplitude for mbrola voices
  575. if(value > 0x7fff)
  576. value = 0x7fff;
  577. if(value < -0x8000)
  578. value = 0x8000;
  579. out_ptr[0] = value;
  580. out_ptr[1] = value >> 8;
  581. out_ptr += 2;
  582. }
  583. n_samples -= result;
  584. return n_samples ? 1 : 0;
  585. }
  586. void MbrolaReset(void)
  587. {//===================
  588. // Reset the Mbrola engine and flush the pending audio
  589. reset_MBR();
  590. }
  591. #else // INCLUDE_MBROLA
  592. // mbrola interface is not compiled, provide dummy functions.
  593. espeak_ERROR LoadMbrolaTable(const char *mbrola_voice, const char *phtrans, int srate)
  594. {
  595. return(EE_INTERNAL_ERROR);
  596. }
  597. int MbrolaGenerate(PHONEME_LIST *phoneme_list, int *n_ph, int resume)
  598. {
  599. return(0);
  600. }
  601. int MbrolaFill(int length, int resume, int amplitude)
  602. {
  603. return(0);
  604. }
  605. void MbrolaReset(void)
  606. {
  607. }
  608. #endif // INCLUDE_MBROLA