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.

voices.cpp 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2015 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * Copyright (C) 2015 by 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 "StdAfx.h"
  21. #include "stdio.h"
  22. #include "ctype.h"
  23. #include "wctype.h"
  24. #include "string.h"
  25. #include "stdlib.h"
  26. #include "speech.h"
  27. #include "errno.h"
  28. #ifdef PLATFORM_WINDOWS
  29. #include "windows.h"
  30. #else
  31. #ifdef PLATFORM_RISCOS
  32. #include "kernel.h"
  33. #else
  34. #include "dirent.h"
  35. #endif
  36. #endif
  37. #include "speak_lib.h"
  38. #include "phoneme.h"
  39. #include "synthesize.h"
  40. #include "voice.h"
  41. #include "translate.h"
  42. #include "debug.h"
  43. MNEM_TAB genders [] = {
  44. {"unknown", 0},
  45. {"male", 1},
  46. {"female", 2},
  47. {NULL, 0 }
  48. };
  49. int tone_points[12] = {600,170, 1200,135, 2000,110, 3000,110, -1,0};
  50. //int tone_points[12] = {250,200, 400,170, 600,170, 1200,135, 2000,110, -1,0};
  51. // limit the rate of change for each formant number
  52. //static int formant_rate_22050[9] = {50, 104, 165, 230, 220, 220, 220, 220, 220}; // values for 22kHz sample rate
  53. //static int formant_rate_22050[9] = {240, 180, 180, 180, 180, 180, 180, 180, 180}; // values for 22kHz sample rate
  54. static int formant_rate_22050[9] = {240, 170, 170, 170, 170, 170, 170, 170, 170}; // values for 22kHz sample rate
  55. int formant_rate[9]; // values adjusted for actual sample rate
  56. #define DEFAULT_LANGUAGE_PRIORITY 5
  57. #define N_VOICES_LIST 250
  58. static int n_voices_list = 0;
  59. static espeak_VOICE *voices_list[N_VOICES_LIST];
  60. static int len_path_voices;
  61. espeak_VOICE current_voice_selected;
  62. enum {
  63. V_NAME = 1,
  64. V_LANGUAGE,
  65. V_GENDER,
  66. V_TRANSLATOR,
  67. V_PHONEMES,
  68. V_DICTIONARY,
  69. // these affect voice quality, are independent of language
  70. V_FORMANT,
  71. V_PITCH,
  72. V_ECHO,
  73. V_FLUTTER,
  74. V_ROUGHNESS,
  75. V_CLARITY,
  76. V_TONE,
  77. V_VOICING,
  78. V_BREATH,
  79. V_BREATHW,
  80. // these override defaults set by the translator
  81. V_WORDGAP,
  82. V_INTONATION,
  83. V_TUNES,
  84. V_STRESSLENGTH,
  85. V_STRESSAMP,
  86. V_STRESSADD,
  87. V_DICTRULES,
  88. V_STRESSRULE,
  89. V_STRESSOPT,
  90. V_CHARSET,
  91. V_NUMBERS,
  92. V_OPTION,
  93. V_MBROLA,
  94. V_KLATT,
  95. V_FAST,
  96. V_SPEED,
  97. V_DICTMIN,
  98. V_ALPHABET2,
  99. V_DICTDIALECT,
  100. // these need a phoneme table to have been specified
  101. V_REPLACE,
  102. V_CONSONANTS
  103. };
  104. static MNEM_TAB options_tab[] = {
  105. {"reduce_t", LOPT_REDUCE_T},
  106. {"bracket", LOPT_BRACKET_PAUSE},
  107. {NULL, -1}
  108. };
  109. static MNEM_TAB keyword_tab[] = {
  110. {"name", V_NAME},
  111. {"language", V_LANGUAGE},
  112. {"gender", V_GENDER},
  113. {"formant", V_FORMANT},
  114. {"pitch", V_PITCH},
  115. {"phonemes", V_PHONEMES},
  116. {"translator", V_TRANSLATOR},
  117. {"dictionary", V_DICTIONARY},
  118. {"stressLength", V_STRESSLENGTH},
  119. {"stressAmp", V_STRESSAMP},
  120. {"stressAdd", V_STRESSADD},
  121. {"intonation", V_INTONATION},
  122. {"tunes", V_TUNES},
  123. {"dictrules", V_DICTRULES},
  124. {"stressrule", V_STRESSRULE},
  125. {"stressopt", V_STRESSOPT},
  126. {"charset", V_CHARSET},
  127. {"replace", V_REPLACE},
  128. {"words", V_WORDGAP},
  129. {"echo", V_ECHO},
  130. {"flutter", V_FLUTTER},
  131. {"roughness", V_ROUGHNESS},
  132. {"clarity", V_CLARITY},
  133. {"tone", V_TONE},
  134. {"voicing", V_VOICING},
  135. {"breath", V_BREATH},
  136. {"breathw", V_BREATHW},
  137. {"numbers", V_NUMBERS},
  138. {"option", V_OPTION},
  139. {"mbrola", V_MBROLA},
  140. {"consonants", V_CONSONANTS},
  141. {"klatt", V_KLATT},
  142. {"fast_test2", V_FAST},
  143. {"speed", V_SPEED},
  144. {"dict_min", V_DICTMIN},
  145. {"alphabet2", V_ALPHABET2},
  146. {"dictdialect", V_DICTDIALECT},
  147. // these just set a value in langopts.param[]
  148. {"l_dieresis", 0x100+LOPT_DIERESES},
  149. // {"l_lengthen", 0x100+LOPT_IT_LENGTHEN},
  150. {"l_prefix", 0x100+LOPT_PREFIXES},
  151. {"l_regressive_v", 0x100+LOPT_REGRESSIVE_VOICING},
  152. {"l_unpronouncable", 0x100+LOPT_UNPRONOUNCABLE},
  153. {"l_sonorant_min", 0x100+LOPT_SONORANT_MIN},
  154. {"l_length_mods", 0x100+LOPT_LENGTH_MODS},
  155. {"apostrophe", 0x100+LOPT_APOSTROPHE},
  156. {NULL, 0}
  157. };
  158. static MNEM_TAB dict_dialects[] = {
  159. {"en-us", DICTDIALECT_EN_US},
  160. {"es-la", DICTDIALECT_ES_LA},
  161. {NULL, 0}
  162. };
  163. #define N_VOICE_VARIANTS 12
  164. const char variants_either[N_VOICE_VARIANTS] = {1,2,12,3,13,4,14,5,11,0};
  165. const char variants_male[N_VOICE_VARIANTS] = {1,2,3,4,5,6,0};
  166. const char variants_female[N_VOICE_VARIANTS] = {11,12,13,14,0};
  167. const char *variant_lists[3] = {variants_either, variants_male, variants_female};
  168. static voice_t voicedata;
  169. voice_t *voice = &voicedata;
  170. static char *fgets_strip(char *buf, int size, FILE *f_in)
  171. {//======================================================
  172. // strip trailing spaces, and truncate lines at // comment
  173. int len;
  174. char *p;
  175. if(fgets(buf,size,f_in) == NULL)
  176. return(NULL);
  177. if(buf[0] == '#')
  178. {
  179. buf[0] = 0;
  180. return(buf);
  181. }
  182. len = strlen(buf);
  183. while((--len > 0) && isspace(buf[len]))
  184. buf[len] = 0;
  185. if((p = strstr(buf,"//")) != NULL)
  186. *p = 0;
  187. return(buf);
  188. }
  189. static int LookupTune(const char *name)
  190. {//====================================
  191. int ix;
  192. for(ix=0; ix<n_tunes; ix++)
  193. {
  194. if(strcmp(name, tunes[ix].name) == 0)
  195. return(ix);
  196. }
  197. return(-1);
  198. } // end of LookupTune
  199. static void SetToneAdjust(voice_t *voice, int *tone_pts)
  200. {//=====================================================
  201. int ix;
  202. int pt;
  203. int y;
  204. int freq1=0;
  205. int freq2;
  206. int height1 = tone_pts[1];
  207. int height2;
  208. double rate;
  209. for(pt=0; pt<12; pt+=2)
  210. {
  211. if(tone_pts[pt] == -1)
  212. {
  213. tone_pts[pt] = N_TONE_ADJUST*8;
  214. if(pt > 0)
  215. tone_pts[pt+1] = tone_pts[pt-1];
  216. }
  217. freq2 = tone_pts[pt] / 8; // 8Hz steps
  218. height2 = tone_pts[pt+1];
  219. if((freq2 - freq1) > 0)
  220. {
  221. rate = (double)(height2-height1)/(freq2-freq1);
  222. for(ix=freq1; ix<freq2; ix++)
  223. {
  224. y = height1 + (int)(rate * (ix-freq1));
  225. if(y > 255)
  226. y = 255;
  227. voice->tone_adjust[ix] = y;
  228. }
  229. }
  230. freq1 = freq2;
  231. height1 = height2;
  232. }
  233. }
  234. void ReadTonePoints(char *string, int *tone_pts)
  235. {//=============================================
  236. // tone_pts[] is int[12]
  237. int ix;
  238. for(ix=0; ix<12; ix++)
  239. tone_pts[ix] = -1;
  240. sscanf(string,"%d %d %d %d %d %d %d %d %d %d",
  241. &tone_pts[0],&tone_pts[1],&tone_pts[2],&tone_pts[3],
  242. &tone_pts[4],&tone_pts[5],&tone_pts[6],&tone_pts[7],
  243. &tone_pts[8],&tone_pts[9]);
  244. }
  245. static espeak_VOICE *ReadVoiceFile(FILE *f_in, const char *fname, const char*leafname)
  246. {//===================================================================================
  247. // Read a Voice file, allocate a VOICE_DATA and set data from the
  248. // file's language, gender, name lines
  249. char linebuf[120];
  250. char vname[80];
  251. char vgender[80];
  252. char vlanguage[80];
  253. char languages[300]; // allow space for several alternate language names and priorities
  254. unsigned int len;
  255. int langix = 0;
  256. int n_languages = 0;
  257. char *p;
  258. espeak_VOICE *voice_data;
  259. int priority;
  260. int age;
  261. int n_variants = 4; // default, number of variants of this voice before using another voice
  262. int gender;
  263. #ifdef PLATFORM_WINDOWS
  264. char fname_buf[sizeof(path_home)+15];
  265. if(memcmp(leafname,"mb-",3) == 0)
  266. {
  267. // check whether the mbrola speech data is present for this voice
  268. memcpy(vname,&leafname[3],3);
  269. vname[3] = 0;
  270. sprintf(fname_buf,"%s/mbrola/%s",path_home,vname);
  271. if(GetFileLength(fname_buf) <= 0)
  272. return(0);
  273. }
  274. #endif
  275. vname[0] = 0;
  276. vgender[0] = 0;
  277. age = 0;
  278. while(fgets_strip(linebuf,sizeof(linebuf),f_in) != NULL)
  279. {
  280. if(memcmp(linebuf,"name",4)==0)
  281. {
  282. p = &linebuf[4];
  283. while(isspace(*p)) p++;
  284. strncpy0(vname,p,sizeof(vname));
  285. }
  286. else if(memcmp(linebuf,"language",8)==0)
  287. {
  288. priority = DEFAULT_LANGUAGE_PRIORITY;
  289. vlanguage[0] = 0;
  290. sscanf(&linebuf[8],"%s %d",vlanguage,&priority);
  291. len = strlen(vlanguage) + 2;
  292. // check for space in languages[]
  293. if(len < (sizeof(languages)-langix-1))
  294. {
  295. languages[langix] = priority;
  296. strcpy(&languages[langix+1],vlanguage);
  297. langix += len;
  298. n_languages++;
  299. }
  300. }
  301. else if(memcmp(linebuf,"gender",6)==0)
  302. {
  303. sscanf(&linebuf[6],"%s %d",vgender,&age);
  304. }
  305. else if(memcmp(linebuf,"variants",8)==0)
  306. {
  307. sscanf(&linebuf[8],"%d",&n_variants);
  308. }
  309. }
  310. languages[langix++] = 0;
  311. gender = LookupMnem(genders,vgender);
  312. if(n_languages == 0)
  313. {
  314. return(NULL); // no language lines in the voice file
  315. }
  316. p = (char *)calloc(sizeof(espeak_VOICE) + langix + strlen(fname) + strlen(vname) + 3, 1);
  317. voice_data = (espeak_VOICE *)p;
  318. p = &p[sizeof(espeak_VOICE)];
  319. memcpy(p,languages,langix);
  320. voice_data->languages = p;
  321. strcpy(&p[langix],fname);
  322. voice_data->identifier = &p[langix];
  323. voice_data->name = &p[langix];
  324. if(vname[0] != 0)
  325. {
  326. langix += strlen(fname)+1;
  327. strcpy(&p[langix],vname);
  328. voice_data->name = &p[langix];
  329. }
  330. voice_data->age = age;
  331. voice_data->gender = gender;
  332. voice_data->variant = 0;
  333. voice_data->xx1 = n_variants;
  334. return(voice_data);
  335. } // end of ReadVoiceFile
  336. void VoiceReset(int tone_only)
  337. {//===========================
  338. // Set voice to the default values
  339. int pk;
  340. static unsigned char default_heights[N_PEAKS] = {130,128,120,116,100,100,128,128,128}; // changed for v.1.47
  341. static unsigned char default_widths[N_PEAKS] = {140,128,128,160,171,171,128,128,128};
  342. // static unsigned char default_heights[N_PEAKS] = {128,128,120,120,110,110,128,128,128}; // previous version
  343. // static unsigned char default_widths[N_PEAKS] = {128,128,128,160,171,171,128,128,128};
  344. static int breath_widths[N_PEAKS] = {0,200,200,400,400,400,600,600,600};
  345. // default is: pitch 80,118
  346. voice->pitch_base = 0x47000;
  347. voice->pitch_range = 4104;
  348. // default is: pitch 80,117
  349. // voice->pitch_base = 0x47000;
  350. // voice->pitch_range = 3996;
  351. voice->formant_factor = 256;
  352. voice->speed_percent = 100;
  353. voice->echo_delay = 0;
  354. voice->echo_amp = 0;
  355. voice->flutter = 64;
  356. voice->n_harmonic_peaks = 5;
  357. voice->peak_shape = 0;
  358. voice->voicing = 64;
  359. voice->consonant_amp = 90; // change from 100 to 90 for v.1.47
  360. voice->consonant_ampv = 100;
  361. voice->samplerate = samplerate_native;
  362. memset(voice->klattv,0,sizeof(voice->klattv));
  363. speed.fast_settings[0] = 450;
  364. speed.fast_settings[1] = 800;
  365. speed.fast_settings[2] = 175;
  366. #ifdef PLATFORM_RISCOS
  367. voice->roughness = 1;
  368. #else
  369. voice->roughness = 2;
  370. #endif
  371. InitBreath();
  372. for(pk=0; pk<N_PEAKS; pk++)
  373. {
  374. voice->freq[pk] = 256;
  375. voice->height[pk] = default_heights[pk]*2;
  376. voice->width[pk] = default_widths[pk]*2;
  377. voice->breath[pk] = 0;
  378. voice->breathw[pk] = breath_widths[pk]; // default breath formant woidths
  379. voice->freqadd[pk] = 0;
  380. // adjust formant smoothing depending on sample rate
  381. formant_rate[pk] = (formant_rate_22050[pk] * 22050)/samplerate;
  382. }
  383. // This table provides the opportunity for tone control.
  384. // Adjustment of harmonic amplitudes, steps of 8Hz
  385. // value of 128 means no change
  386. // memset(voice->tone_adjust,128,sizeof(voice->tone_adjust));
  387. SetToneAdjust(voice,tone_points);
  388. // default values of speed factors
  389. voice->speedf1 = 256;
  390. voice->speedf2 = 238;
  391. voice->speedf3 = 232;
  392. if(tone_only == 0)
  393. {
  394. n_replace_phonemes = 0;
  395. option_quiet = 0;
  396. LoadMbrolaTable(NULL,NULL,0);
  397. }
  398. } // end of VoiceReset
  399. static void VoiceFormant(char *p)
  400. {//==============================
  401. // Set parameters for a formant
  402. int ix;
  403. int formant;
  404. int freq = 100;
  405. int height = 100;
  406. int width = 100;
  407. int freqadd = 0;
  408. ix = sscanf(p,"%d %d %d %d %d",&formant,&freq,&height,&width,&freqadd);
  409. if(ix < 2)
  410. return;
  411. if((formant < 0) || (formant > 8))
  412. return;
  413. if(freq >= 0)
  414. voice->freq[formant] = (int)(freq * 2.56001);
  415. if(height >= 0)
  416. voice->height[formant] = (int)(height * 2.56001);
  417. if(width >= 0)
  418. voice->width[formant] = (int)(width * 2.56001);
  419. voice->freqadd[formant] = freqadd;
  420. }
  421. static void PhonemeReplacement(int type, char *p)
  422. {//==============================================
  423. int n;
  424. int phon;
  425. int flags = 0;
  426. char phon_string1[12];
  427. char phon_string2[12];
  428. strcpy(phon_string2,"NULL");
  429. n = sscanf(p,"%d %s %s",&flags,phon_string1,phon_string2);
  430. if((n < 2) || (n_replace_phonemes >= N_REPLACE_PHONEMES))
  431. return;
  432. if((phon = LookupPhonemeString(phon_string1)) == 0)
  433. return; // not recognised
  434. replace_phonemes[n_replace_phonemes].old_ph = phon;
  435. replace_phonemes[n_replace_phonemes].new_ph = LookupPhonemeString(phon_string2);
  436. replace_phonemes[n_replace_phonemes++].type = flags;
  437. } // end of PhonemeReplacement
  438. static int Read8Numbers(char *data_in,int *data)
  439. {//=============================================
  440. // Read 8 integer numbers
  441. memset(data, 0, 8+sizeof(int));
  442. return(sscanf(data_in,"%d %d %d %d %d %d %d %d",
  443. &data[0],&data[1],&data[2],&data[3],&data[4],&data[5],&data[6],&data[7]));
  444. }
  445. static unsigned int StringToWord2(const char *string)
  446. {//======================================================
  447. // Convert a language name string to a word such as L('e','n')
  448. int ix;
  449. int c;
  450. unsigned int value = 0;
  451. for(ix=0; (ix<4) && ((c = string[ix]) != 0); ix++)
  452. {
  453. value = (value << 8) | (c & 0xff);
  454. }
  455. return(value);
  456. }
  457. voice_t *LoadVoice(const char *vname, int control)
  458. {//===============================================
  459. // control, bit 0 1= no_default
  460. // bit 1 1 = change tone only, not language
  461. // bit 2 1 = don't report error on LoadDictionary
  462. // bit 4 1 = vname = full path
  463. FILE *f_voice = NULL;
  464. char *p;
  465. int key;
  466. int ix;
  467. int n;
  468. int value;
  469. int value2;
  470. int langix = 0;
  471. int tone_only = control & 2;
  472. int language_set = 0;
  473. int phonemes_set = 0;
  474. int stress_amps_set = 0;
  475. int stress_lengths_set = 0;
  476. int stress_add_set = 0;
  477. int conditional_rules = 0;
  478. LANGUAGE_OPTIONS *langopts = NULL;
  479. Translator *new_translator = NULL;
  480. char voicename[40];
  481. char language_name[40];
  482. char translator_name[40];
  483. char new_dictionary[40];
  484. char phonemes_name[40];
  485. char option_name[40];
  486. const char *language_type;
  487. char buf[sizeof(path_home)+30];
  488. char path_voices[sizeof(path_home)+12];
  489. int dict_min = 0;
  490. int stress_amps[8];
  491. int stress_lengths[8];
  492. int stress_add[8];
  493. char names[8][40];
  494. char name1[40];
  495. char name2[80];
  496. const char *voice_dir;
  497. int pitch1;
  498. int pitch2;
  499. static char voice_identifier[40]; // file name for current_voice_selected
  500. static char voice_name[40]; // voice name for current_voice_selected
  501. static char voice_languages[100]; // list of languages and priorities for current_voice_selected
  502. // which directory to look for a named voice. List of voice names, must end in a space.
  503. static const char *voices_asia =
  504. "az bn fa fa-pin gu hi hy hy-west id ka kn ku ml ms ne pa ta te tr vi vi-hue vi-sgn zh zh-yue ";
  505. static const char *voices_europe =
  506. "an bg bs ca cs cy da de el en en-us es et eu fi fr fr-be ga hr hu is it lt lv mk nl no pl pt-pt ro ru sk sq sr sv ";
  507. strncpy0(voicename, vname, sizeof(voicename));
  508. if(control & 0x10)
  509. {
  510. strcpy(buf,vname);
  511. if(GetFileLength(buf) <= 0)
  512. return(NULL);
  513. }
  514. else
  515. {
  516. if(voicename[0]==0)
  517. strcpy(voicename,"default");
  518. sprintf(path_voices,"%s%cvoices%c",path_home,PATHSEP,PATHSEP);
  519. sprintf(buf,"%s%s",path_voices,voicename); // first, look in the main voices directory
  520. if(GetFileLength(buf) <= 0)
  521. {
  522. // then look in the appropriate subdirectory
  523. if((voicename[0]=='m') && (voicename[1]=='b'))
  524. {
  525. voice_dir = "mb"; // mbrola voices
  526. }
  527. else
  528. {
  529. sprintf(name2, "%s ", voicename);
  530. if(strstr(voices_europe, voicename) != NULL)
  531. voice_dir = "europe";
  532. else if(strstr(voices_asia, voicename) != NULL)
  533. voice_dir = "asia";
  534. else
  535. voice_dir = "other";
  536. sprintf(buf,"%s%s%c%s", path_voices,voice_dir,PATHSEP,voicename);
  537. if(GetFileLength(buf) <= 0)
  538. {
  539. // if not found, look in "test" sub-directory
  540. sprintf(buf,"%stest%c%s",path_voices,PATHSEP,voicename);
  541. }
  542. }
  543. }
  544. }
  545. f_voice = fopen(buf,"r");
  546. language_type = "en"; // default
  547. if(f_voice == NULL)
  548. {
  549. if(control & 3)
  550. return(NULL); // can't open file
  551. if(SelectPhonemeTableName(voicename) >= 0)
  552. language_type = voicename;
  553. }
  554. if(!tone_only && (translator != NULL))
  555. {
  556. DeleteTranslator(translator);
  557. translator = NULL;
  558. }
  559. strcpy(translator_name,language_type);
  560. strcpy(new_dictionary,language_type);
  561. strcpy(phonemes_name,language_type);
  562. if(!tone_only)
  563. {
  564. voice = &voicedata;
  565. strncpy0(voice_identifier,vname,sizeof(voice_identifier));
  566. voice_name[0] = 0;
  567. voice_languages[0] = 0;
  568. current_voice_selected.identifier = voice_identifier;
  569. current_voice_selected.name = voice_name;
  570. current_voice_selected.languages = voice_languages;
  571. }
  572. else
  573. {
  574. // append the variant file name to the voice identifier
  575. if((p = strchr(voice_identifier,'+')) != NULL)
  576. *p = 0; // remove previous variant name
  577. sprintf(buf,"+%s",&vname[3]); // omit !v/ from the variant filename
  578. strcat(voice_identifier,buf);
  579. langopts = &translator->langopts;
  580. }
  581. VoiceReset(tone_only);
  582. if(!tone_only)
  583. SelectPhonemeTableName(phonemes_name); // set up phoneme_tab
  584. while((f_voice != NULL) && (fgets_strip(buf,sizeof(buf),f_voice) != NULL))
  585. {
  586. // isolate the attribute name
  587. for(p=buf; (*p != 0) && !isspace(*p); p++);
  588. *p++ = 0;
  589. if(buf[0] == 0) continue;
  590. key = LookupMnem(keyword_tab, buf);
  591. switch(key)
  592. {
  593. case V_LANGUAGE:
  594. {
  595. unsigned int len;
  596. int priority;
  597. if(tone_only)
  598. break;
  599. priority = DEFAULT_LANGUAGE_PRIORITY;
  600. language_name[0] = 0;
  601. sscanf(p,"%s %d",language_name,&priority);
  602. if(strcmp(language_name,"variant") == 0)
  603. break;
  604. len = strlen(language_name) + 2;
  605. // check for space in languages[]
  606. if(len < (sizeof(voice_languages)-langix-1))
  607. {
  608. voice_languages[langix] = priority;
  609. strcpy(&voice_languages[langix+1],language_name);
  610. langix += len;
  611. }
  612. // only act on the first language line
  613. if(language_set == 0)
  614. {
  615. language_type = strtok(language_name,"-");
  616. language_set = 1;
  617. strcpy(translator_name,language_type);
  618. strcpy(new_dictionary,language_type);
  619. strcpy(phonemes_name,language_type);
  620. SelectPhonemeTableName(phonemes_name);
  621. if(new_translator != NULL)
  622. DeleteTranslator(new_translator);
  623. new_translator = SelectTranslator(translator_name);
  624. langopts = &new_translator->langopts;
  625. strncpy0(voice->language_name, language_name, sizeof(voice->language_name));
  626. }
  627. }
  628. break;
  629. case V_NAME:
  630. if(tone_only == 0)
  631. {
  632. while(isspace(*p)) p++;
  633. strncpy0(voice_name,p,sizeof(voice_name));
  634. }
  635. break;
  636. case V_GENDER:
  637. {
  638. int age = 0;
  639. char vgender[80];
  640. sscanf(p,"%s %d",vgender,&age);
  641. current_voice_selected.gender = LookupMnem(genders,vgender);
  642. current_voice_selected.age = age;
  643. }
  644. break;
  645. case V_TRANSLATOR:
  646. if(tone_only) break;
  647. sscanf(p,"%s",translator_name);
  648. if(new_translator != NULL)
  649. DeleteTranslator(new_translator);
  650. new_translator = SelectTranslator(translator_name);
  651. langopts = &new_translator->langopts;
  652. break;
  653. case V_DICTIONARY: // dictionary
  654. sscanf(p,"%s",new_dictionary);
  655. break;
  656. case V_PHONEMES: // phoneme table
  657. sscanf(p,"%s",phonemes_name);
  658. break;
  659. case V_FORMANT:
  660. VoiceFormant(p);
  661. break;
  662. case V_PITCH:
  663. {
  664. double factor;
  665. // default is pitch 82 118
  666. n = sscanf(p,"%d %d",&pitch1,&pitch2);
  667. voice->pitch_base = (pitch1 - 9) << 12;
  668. voice->pitch_range = (pitch2 - pitch1) * 108;
  669. factor = (double)(pitch1 - 82)/82;
  670. voice->formant_factor = (int)((1+factor/4) * 256); // nominal formant shift for a different voice pitch
  671. }
  672. break;
  673. case V_STRESSLENGTH: // stressLength
  674. stress_lengths_set = Read8Numbers(p,stress_lengths);
  675. break;
  676. case V_STRESSAMP: // stressAmp
  677. stress_amps_set = Read8Numbers(p,stress_amps);
  678. break;
  679. case V_STRESSADD: // stressAdd
  680. stress_add_set = Read8Numbers(p,stress_add);
  681. break;
  682. case V_INTONATION: // intonation
  683. sscanf(p,"%d %d",&option_tone_flags,&option_tone2);
  684. if((option_tone_flags & 0xff) != 0)
  685. langopts->intonation_group = option_tone_flags & 0xff;
  686. break;
  687. case V_TUNES:
  688. n = sscanf(p,"%s %s %s %s %s %s",names[0],names[1],names[2],names[3],names[4],names[5]);
  689. langopts->intonation_group = 0;
  690. for(ix=0; ix<n; ix++)
  691. {
  692. if(strcmp(names[ix],"NULL")==0)
  693. continue;
  694. if((value = LookupTune(names[ix])) < 0)
  695. fprintf(stderr,"Unknown tune '%s'\n",names[ix]);
  696. else
  697. langopts->tunes[ix] = value;
  698. }
  699. break;
  700. case V_DICTRULES: // conditional dictionary rules and list entries
  701. case V_NUMBERS:
  702. case V_STRESSOPT:
  703. // expect a list of numbers
  704. while(*p != 0)
  705. {
  706. while(isspace(*p)) p++;
  707. n = -1;
  708. if((n = atoi(p)) > 0)
  709. {
  710. p++;
  711. if(n < 32)
  712. {
  713. if(key==V_DICTRULES)
  714. conditional_rules |= (1 << n);
  715. else if(key==V_NUMBERS)
  716. langopts->numbers |= (1 << n);
  717. else if(key==V_STRESSOPT)
  718. langopts->stress_flags |= (1 << n);
  719. }
  720. else
  721. {
  722. if((key==V_NUMBERS) && (n < 64))
  723. langopts->numbers2 |= (1 << (n-32));
  724. else
  725. fprintf(stderr,"Bad option number %d\n", n);
  726. }
  727. }
  728. while(isalnum(*p)) p++;
  729. }
  730. ProcessLanguageOptions(langopts);
  731. break;
  732. case V_REPLACE:
  733. if(phonemes_set == 0)
  734. {
  735. // must set up a phoneme table before we can lookup phoneme mnemonics
  736. SelectPhonemeTableName(phonemes_name);
  737. phonemes_set = 1;
  738. }
  739. PhonemeReplacement(key,p);
  740. break;
  741. case V_WORDGAP: // words
  742. sscanf(p,"%d %d",&langopts->word_gap, &langopts->vowel_pause);
  743. break;
  744. case V_STRESSRULE:
  745. sscanf(p,"%d %d %d %d",&langopts->stress_rule,
  746. &langopts->stress_flags,
  747. &langopts->unstressed_wd1,
  748. &langopts->unstressed_wd2);
  749. break;
  750. case V_CHARSET:
  751. if((sscanf(p,"%d",&value)==1) && (value < N_CHARSETS))
  752. new_translator->charset_a0 = charsets[value];
  753. break;
  754. case V_OPTION:
  755. value2 = 0;
  756. if(((sscanf(p,"%s %d %d",option_name,&value,&value2) >= 2) && ((ix = LookupMnem(options_tab, option_name)) >= 0)) ||
  757. ((sscanf(p,"%d %d %d",&ix,&value,&value2) >= 2) && (ix < N_LOPTS)))
  758. {
  759. langopts->param[ix] = value;
  760. langopts->param2[ix] = value2;
  761. }
  762. else
  763. {
  764. fprintf(stderr,"Bad voice option: %s %s\n",buf,p);
  765. }
  766. break;
  767. case V_ECHO:
  768. // echo. suggest: 135mS 11%
  769. value = 0;
  770. voice->echo_amp = 0;
  771. sscanf(p,"%d %d",&voice->echo_delay,&voice->echo_amp);
  772. break;
  773. case V_FLUTTER: // flutter
  774. if(sscanf(p,"%d",&value)==1)
  775. voice->flutter = value * 32;
  776. break;
  777. case V_ROUGHNESS: // roughness
  778. if(sscanf(p,"%d",&value)==1)
  779. voice->roughness = value;
  780. break;
  781. case V_CLARITY: // formantshape
  782. if(sscanf(p,"%d",&value)==1)
  783. {
  784. if(value > 4)
  785. {
  786. voice->peak_shape = 1; // squarer formant peaks
  787. value = 4;
  788. }
  789. voice->n_harmonic_peaks = 1+value;
  790. }
  791. break;
  792. case V_TONE:
  793. {
  794. int tone_data[12];
  795. ReadTonePoints(p,tone_data);
  796. SetToneAdjust(voice,tone_data);
  797. }
  798. break;
  799. case V_VOICING:
  800. if(sscanf(p,"%d",&value)==1)
  801. voice->voicing = (value * 64)/100;
  802. break;
  803. case V_BREATH:
  804. voice->breath[0] = Read8Numbers(p,&voice->breath[1]);
  805. for(ix=1; ix<8; ix++)
  806. {
  807. if(ix % 2)
  808. voice->breath[ix] = -voice->breath[ix];
  809. }
  810. break;
  811. case V_BREATHW:
  812. voice->breathw[0] = Read8Numbers(p,&voice->breathw[1]);
  813. break;
  814. case V_CONSONANTS:
  815. value = sscanf(p,"%d %d",&voice->consonant_amp, &voice->consonant_ampv);
  816. break;
  817. case V_SPEED:
  818. sscanf(p,"%d",&voice->speed_percent);
  819. break;
  820. case V_MBROLA:
  821. {
  822. int srate = 16000;
  823. name2[0] = 0;
  824. sscanf(p,"%s %s %d",name1,name2,&srate);
  825. if(LoadMbrolaTable(name1,name2,srate) != EE_OK)
  826. {
  827. fprintf(stderr,"mbrola voice not found\n");
  828. }
  829. voice->samplerate = srate;
  830. }
  831. break;
  832. case V_KLATT:
  833. voice->klattv[0] = 1; // default source: IMPULSIVE
  834. Read8Numbers(p,voice->klattv);
  835. voice->klattv[KLATT_Kopen] -= 40;
  836. break;
  837. case V_FAST:
  838. Read8Numbers(p,speed.fast_settings);
  839. SetSpeed(3);
  840. break;
  841. case V_DICTMIN:
  842. sscanf(p,"%d",&dict_min);
  843. break;
  844. case V_ALPHABET2:
  845. {
  846. ALPHABET *alphabet;
  847. name1[0] = name2[0] = 0;
  848. sscanf(p, "%s %s", name1, name2);
  849. if(strcmp(name1, "latin") == 0)
  850. {
  851. strncpy0(langopts->ascii_language,name2,sizeof(langopts->ascii_language));
  852. }
  853. else if((alphabet = AlphabetFromName(name1)) != 0)
  854. {
  855. langopts->alt_alphabet = alphabet->offset;
  856. langopts->alt_alphabet_lang = StringToWord2(name2);
  857. }
  858. else
  859. {
  860. fprintf(stderr,"alphabet name '%s' not found\n", name1);
  861. }
  862. }
  863. break;
  864. case V_DICTDIALECT:
  865. // specify a dialect to use for foreign words, eg, en-us for _^_EN
  866. if(sscanf(p, "%s", name1) == 1)
  867. {
  868. if((ix = LookupMnem(dict_dialects, name1)) > 0)
  869. {
  870. langopts->dict_dialect |= (1 << ix);
  871. }
  872. else
  873. {
  874. fprintf(stderr, "dictdialect name '%s' not recognized\n", name1);
  875. }
  876. }
  877. break;
  878. default:
  879. if((key & 0xff00) == 0x100)
  880. {
  881. sscanf(p,"%d",&langopts->param[key &0xff]);
  882. }
  883. else
  884. {
  885. fprintf(stderr,"Bad voice attribute: %s\n",buf);
  886. }
  887. break;
  888. }
  889. }
  890. if(f_voice != NULL)
  891. fclose(f_voice);
  892. if((new_translator == NULL) && (!tone_only))
  893. {
  894. // not set by language attribute
  895. new_translator = SelectTranslator(translator_name);
  896. }
  897. SetSpeed(3); // for speed_percent
  898. for(ix=0; ix<N_PEAKS; ix++)
  899. {
  900. voice->freq2[ix] = voice->freq[ix];
  901. voice->height2[ix] = voice->height[ix];
  902. voice->width2[ix] = voice->width[ix];
  903. }
  904. if(tone_only)
  905. {
  906. new_translator = translator;
  907. }
  908. else
  909. {
  910. if((ix = SelectPhonemeTableName(phonemes_name)) < 0)
  911. {
  912. fprintf(stderr,"Unknown phoneme table: '%s'\n",phonemes_name);
  913. ix = 0;
  914. }
  915. voice->phoneme_tab_ix = ix;
  916. new_translator->phoneme_tab_ix = ix;
  917. new_translator->dict_min_size = dict_min;
  918. LoadDictionary(new_translator, new_dictionary, control & 4);
  919. if(dictionary_name[0]==0)
  920. {
  921. DeleteTranslator(new_translator);
  922. return(NULL); // no dictionary loaded
  923. }
  924. new_translator->dict_condition = conditional_rules;
  925. voice_languages[langix] = 0;
  926. }
  927. langopts = &new_translator->langopts;
  928. if((value = langopts->param[LOPT_LENGTH_MODS]) != 0)
  929. {
  930. SetLengthMods(new_translator,value);
  931. }
  932. voice->width[0] = (voice->width[0] * 105)/100;
  933. if(!tone_only)
  934. {
  935. translator = new_translator;
  936. }
  937. // relative lengths of different stress syllables
  938. for(ix=0; ix<stress_lengths_set; ix++)
  939. {
  940. translator->stress_lengths[ix] = stress_lengths[ix];
  941. }
  942. for(ix=0; ix<stress_add_set; ix++)
  943. {
  944. translator->stress_lengths[ix] += stress_add[ix];
  945. }
  946. for(ix=0; ix<stress_amps_set; ix++)
  947. {
  948. translator->stress_amps[ix] = stress_amps[ix];
  949. translator->stress_amps_r[ix] = stress_amps[ix] -1;
  950. }
  951. return(voice);
  952. } // end of LoadVoice
  953. static char *ExtractVoiceVariantName(char *vname, int variant_num, int add_dir)
  954. {//===========================================================================
  955. // Remove any voice variant suffix (name or number) from a voice name
  956. // Returns the voice variant name
  957. char *p;
  958. static char variant_name[40];
  959. char variant_prefix[5];
  960. variant_name[0] = 0;
  961. sprintf(variant_prefix,"!v%c",PATHSEP);
  962. if(add_dir == 0)
  963. variant_prefix[0] = 0;
  964. if(vname != NULL)
  965. {
  966. if((p = strchr(vname,'+')) != NULL)
  967. {
  968. // The voice name has a +variant suffix
  969. variant_num = 0;
  970. *p++ = 0; // delete the suffix from the voice name
  971. if(IsDigit09(*p))
  972. {
  973. variant_num = atoi(p); // variant number
  974. }
  975. else
  976. {
  977. // voice variant name, not number
  978. sprintf(variant_name, "%s%s", variant_prefix, p);
  979. }
  980. }
  981. }
  982. if(variant_num > 0)
  983. {
  984. if(variant_num < 10)
  985. sprintf(variant_name,"%sm%d",variant_prefix, variant_num); // male
  986. else
  987. sprintf(variant_name,"%sf%d",variant_prefix, variant_num-10); // female
  988. }
  989. return(variant_name);
  990. } // end of ExtractVoiceVariantName
  991. voice_t *LoadVoiceVariant(const char *vname, int variant_num)
  992. {//==========================================================
  993. // Load a voice file.
  994. // Also apply a voice variant if specified by "variant", or by "+number" or "+name" in the "vname"
  995. voice_t *v;
  996. char *variant_name;
  997. char buf[60];
  998. strncpy0(buf,vname,sizeof(buf));
  999. variant_name = ExtractVoiceVariantName(buf,variant_num, 1);
  1000. if((v = LoadVoice(buf,0)) == NULL)
  1001. return(NULL);
  1002. if(variant_name[0] != 0)
  1003. {
  1004. v = LoadVoice(variant_name,2);
  1005. }
  1006. return(v);
  1007. }
  1008. static int __cdecl VoiceNameSorter(const void *p1, const void *p2)
  1009. {//=======================================================
  1010. int ix;
  1011. espeak_VOICE *v1 = *(espeak_VOICE **)p1;
  1012. espeak_VOICE *v2 = *(espeak_VOICE **)p2;
  1013. if((ix = strcmp(&v1->languages[1],&v2->languages[1])) != 0) // primary language name
  1014. return(ix);
  1015. if((ix = v1->languages[0] - v2->languages[0]) != 0) // priority number
  1016. return(ix);
  1017. return(strcmp(v1->name,v2->name));
  1018. }
  1019. static int __cdecl VoiceScoreSorter(const void *p1, const void *p2)
  1020. {//========================================================
  1021. int ix;
  1022. espeak_VOICE *v1 = *(espeak_VOICE **)p1;
  1023. espeak_VOICE *v2 = *(espeak_VOICE **)p2;
  1024. if((ix = v2->score - v1->score) != 0)
  1025. return(ix);
  1026. return(strcmp(v1->name,v2->name));
  1027. }
  1028. static int ScoreVoice(espeak_VOICE *voice_spec, const char *spec_language, int spec_n_parts, int spec_lang_len, espeak_VOICE *voice)
  1029. {//=========================================================================================================================
  1030. int ix;
  1031. const char *p;
  1032. int c1, c2;
  1033. int language_priority;
  1034. int n_parts;
  1035. int matching;
  1036. int matching_parts;
  1037. int score = 0;
  1038. int x;
  1039. int ratio;
  1040. int required_age;
  1041. int diff;
  1042. p = voice->languages; // list of languages+dialects for which this voice is suitable
  1043. if(spec_n_parts < 0)
  1044. {
  1045. // match on the subdirectory
  1046. if(memcmp(voice->identifier, spec_language, spec_lang_len) == 0)
  1047. return(100);
  1048. return(0);
  1049. }
  1050. if(spec_n_parts == 0)
  1051. {
  1052. score = 100;
  1053. }
  1054. else
  1055. {
  1056. if((*p == 0) && (strcmp(spec_language,"variants")==0))
  1057. {
  1058. // match on a voice with no languages if the required language is "variants"
  1059. score = 100;
  1060. }
  1061. // compare the required language with each of the languages of this voice
  1062. while(*p != 0)
  1063. {
  1064. language_priority = *p++;
  1065. matching = 1;
  1066. matching_parts = 0;
  1067. n_parts = 1;
  1068. for(ix=0; ; ix++)
  1069. {
  1070. if((ix >= spec_lang_len) || ((c1 = spec_language[ix]) == '-'))
  1071. c1 = 0;
  1072. if((c2 = p[ix]) == '-')
  1073. c2 = 0;
  1074. if(c1 != c2)
  1075. {
  1076. matching = 0;
  1077. }
  1078. if(p[ix] == '-')
  1079. {
  1080. n_parts++;
  1081. if(matching)
  1082. matching_parts++;
  1083. }
  1084. if(p[ix] == 0)
  1085. break;
  1086. }
  1087. p += (ix+1);
  1088. matching_parts += matching; // number of parts which match
  1089. if(matching_parts == 0)
  1090. continue; // no matching parts for this language
  1091. x = 5;
  1092. // reduce the score if not all parts of the required language match
  1093. if((diff = (spec_n_parts - matching_parts)) > 0)
  1094. x -= diff;
  1095. // reduce score if the language is more specific than required
  1096. if((diff = (n_parts - matching_parts)) > 0)
  1097. x -= diff;
  1098. x = x*100 - (language_priority * 2);
  1099. if(x > score)
  1100. score = x;
  1101. }
  1102. }
  1103. if(score == 0)
  1104. return(0);
  1105. if(voice_spec->name != NULL)
  1106. {
  1107. if(strcmp(voice_spec->name,voice->name)==0)
  1108. {
  1109. // match on voice name
  1110. score += 500;
  1111. }
  1112. else if(strcmp(voice_spec->name,voice->identifier)==0)
  1113. {
  1114. score += 400;
  1115. }
  1116. }
  1117. if(((voice_spec->gender == 1) || (voice_spec->gender == 2)) &&
  1118. ((voice->gender == 1) || (voice->gender == 2)))
  1119. {
  1120. if(voice_spec->gender == voice->gender)
  1121. score += 50;
  1122. else
  1123. score -= 50;
  1124. }
  1125. if((voice_spec->age <= 12) && (voice->gender == 2) && (voice->age > 12))
  1126. {
  1127. score += 5; // give some preference for non-child female voice if a child is requested
  1128. }
  1129. if(voice->age != 0)
  1130. {
  1131. if(voice_spec->age == 0)
  1132. required_age = 30;
  1133. else
  1134. required_age = voice_spec->age;
  1135. ratio = (required_age*100)/voice->age;
  1136. if(ratio < 100)
  1137. ratio = 10000/ratio;
  1138. ratio = (ratio - 100)/10; // 0=exact match, 10=out by factor of 2
  1139. x = 5 - ratio;
  1140. if(x > 0) x = 0;
  1141. score = score + x;
  1142. if(voice_spec->age > 0)
  1143. score += 10; // required age specified, favour voices with a specified age (near it)
  1144. }
  1145. if(score < 1)
  1146. score = 1;
  1147. return(score);
  1148. } // end of ScoreVoice
  1149. static int SetVoiceScores(espeak_VOICE *voice_select, espeak_VOICE **voices, int control)
  1150. {//======================================================================================
  1151. // control: bit0=1 include mbrola voices
  1152. int ix;
  1153. int score;
  1154. int nv; // number of candidates
  1155. int n_parts=0;
  1156. int lang_len=0;
  1157. espeak_VOICE *vp;
  1158. char language[80];
  1159. char buf[sizeof(path_home)+80];
  1160. // count number of parts in the specified language
  1161. if((voice_select->languages != NULL) && (voice_select->languages[0] != 0))
  1162. {
  1163. n_parts = 1;
  1164. lang_len = strlen(voice_select->languages);
  1165. for(ix=0; (ix<=lang_len) && ((unsigned)ix < sizeof(language)); ix++)
  1166. {
  1167. if((language[ix] = tolower(voice_select->languages[ix])) == '-')
  1168. n_parts++;
  1169. }
  1170. }
  1171. if((n_parts == 1) && (control & 1))
  1172. {
  1173. if(strcmp(language, "mbrola") == 0)
  1174. {
  1175. language[2] = 0; // truncate to "mb"
  1176. lang_len = 2;
  1177. }
  1178. sprintf(buf, "%s/voices/%s", path_home, language);
  1179. if(GetFileLength(buf) == -2)
  1180. {
  1181. // A subdirectory name has been specified. List all the voices in that subdirectory
  1182. language[lang_len++] = PATHSEP;
  1183. language[lang_len] = 0;
  1184. n_parts = -1;
  1185. }
  1186. }
  1187. // select those voices which match the specified language
  1188. nv = 0;
  1189. for(ix=0; ix<n_voices_list; ix++)
  1190. {
  1191. vp = voices_list[ix];
  1192. if(((control & 1) == 0) && (memcmp(vp->identifier,"mb/",3) == 0))
  1193. continue;
  1194. if((score = ScoreVoice(voice_select, language, n_parts, lang_len, voices_list[ix])) > 0)
  1195. {
  1196. voices[nv++] = vp;
  1197. vp->score = score;
  1198. }
  1199. }
  1200. voices[nv] = NULL; // list terminator
  1201. if(nv==0)
  1202. return(0);
  1203. // sort the selected voices by their score
  1204. qsort(voices,nv,sizeof(espeak_VOICE *),(int (__cdecl *)(const void *,const void *))VoiceScoreSorter);
  1205. return(nv);
  1206. } // end of SetVoiceScores
  1207. espeak_VOICE *SelectVoiceByName(espeak_VOICE **voices, const char *name2)
  1208. {//======================================================================
  1209. int ix;
  1210. int match_fname = -1;
  1211. int match_fname2 = -1;
  1212. int match_name = -1;
  1213. const char *id; // this is the filename within espeak-data/voices
  1214. char *variant_name;
  1215. int last_part_len;
  1216. char last_part[41];
  1217. char name[40];
  1218. if(voices == NULL)
  1219. {
  1220. if(n_voices_list == 0)
  1221. espeak_ListVoices(NULL); // create the voices list
  1222. voices = voices_list;
  1223. }
  1224. strncpy0(name, name2, sizeof(name));
  1225. if((variant_name = strchr(name, '+')) != NULL)
  1226. {
  1227. *variant_name = 0;
  1228. variant_name++;
  1229. }
  1230. sprintf(last_part,"%c%s",PATHSEP,name);
  1231. last_part_len = strlen(last_part);
  1232. for(ix=0; voices[ix] != NULL; ix++)
  1233. {
  1234. if(strcmp(name,voices[ix]->name)==0)
  1235. {
  1236. match_name = ix; // found matching voice name
  1237. break;
  1238. }
  1239. else
  1240. {
  1241. id = voices[ix]->identifier;
  1242. if(strcmp(name, id)==0)
  1243. {
  1244. match_fname = ix; // matching identifier, use this if no matching name
  1245. }
  1246. else if(strcmp(last_part,&id[strlen(id)-last_part_len])==0)
  1247. {
  1248. match_fname2 = ix;
  1249. }
  1250. }
  1251. }
  1252. if(match_name < 0)
  1253. {
  1254. match_name = match_fname; // no matching name, try matching filename
  1255. if(match_name < 0)
  1256. match_name = match_fname2; // try matching just the last part of the filename
  1257. }
  1258. if(match_name < 0)
  1259. return(NULL);
  1260. return(voices[match_name]);
  1261. } // end of SelectVoiceByName
  1262. char const *SelectVoice(espeak_VOICE *voice_select, int *found)
  1263. {//============================================================
  1264. // Returns a path within espeak-voices, with a possible +variant suffix
  1265. // variant is an output-only parameter
  1266. int nv; // number of candidates
  1267. int ix, ix2;
  1268. int j;
  1269. int n_variants;
  1270. int variant_number;
  1271. int gender;
  1272. int skip;
  1273. int aged=1;
  1274. char *variant_name;
  1275. const char *p, *p_start;
  1276. espeak_VOICE *vp = NULL;
  1277. espeak_VOICE *vp2;
  1278. espeak_VOICE voice_select2;
  1279. espeak_VOICE *voices[N_VOICES_LIST]; // list of candidates
  1280. espeak_VOICE *voices2[N_VOICES_LIST+N_VOICE_VARIANTS];
  1281. static espeak_VOICE voice_variants[N_VOICE_VARIANTS];
  1282. static char voice_id[50];
  1283. *found = 1;
  1284. memcpy(&voice_select2,voice_select,sizeof(voice_select2));
  1285. if(n_voices_list == 0)
  1286. espeak_ListVoices(NULL); // create the voices list
  1287. if((voice_select2.languages == NULL) || (voice_select2.languages[0] == 0))
  1288. {
  1289. // no language is specified. Get language from the named voice
  1290. static char buf[60];
  1291. if(voice_select2.name == NULL)
  1292. {
  1293. if((voice_select2.name = voice_select2.identifier) == NULL)
  1294. voice_select2.name = "default";
  1295. }
  1296. strncpy0(buf,voice_select2.name,sizeof(buf));
  1297. variant_name = ExtractVoiceVariantName(buf,0,0);
  1298. vp = SelectVoiceByName(voices_list,buf);
  1299. if(vp != NULL)
  1300. {
  1301. voice_select2.languages = &(vp->languages[1]);
  1302. if((voice_select2.gender==0) && (voice_select2.age==0) && (voice_select2.variant==0))
  1303. {
  1304. if(variant_name[0] != 0)
  1305. {
  1306. sprintf(voice_id,"%s+%s", vp->identifier, variant_name);
  1307. return(voice_id);
  1308. }
  1309. return(vp->identifier);
  1310. }
  1311. }
  1312. }
  1313. // select and sort voices for the required language
  1314. nv = SetVoiceScores(&voice_select2,voices,0);
  1315. if(nv == 0)
  1316. {
  1317. // no matching voice, choose the default
  1318. *found = 0;
  1319. if((voices[0] = SelectVoiceByName(voices_list,"default")) != NULL)
  1320. nv = 1;
  1321. }
  1322. gender = 0;
  1323. if((voice_select2.gender == 2) || ((voice_select2.age > 0) && (voice_select2.age < 13)))
  1324. gender = 2;
  1325. else if(voice_select2.gender == 1)
  1326. gender = 1;
  1327. #define AGE_OLD 60
  1328. if(voice_select2.age < AGE_OLD)
  1329. aged = 0;
  1330. p = p_start = variant_lists[gender];
  1331. if(aged == 0)
  1332. p++; // the first voice in the variants list is older
  1333. // add variants for the top voices
  1334. n_variants = 0;
  1335. for(ix=0, ix2=0; ix<nv; ix++)
  1336. {
  1337. vp = voices[ix];
  1338. // is the main voice the required gender?
  1339. skip=0;
  1340. if((gender != 0) && (vp->gender != gender))
  1341. {
  1342. skip=1;
  1343. }
  1344. if((ix2==0) && aged && (vp->age < AGE_OLD))
  1345. {
  1346. skip=1;
  1347. }
  1348. if(skip==0)
  1349. {
  1350. voices2[ix2++] = vp;
  1351. }
  1352. for(j=0; (j < vp->xx1) && (n_variants < N_VOICE_VARIANTS);)
  1353. {
  1354. if((variant_number = *p) == 0)
  1355. {
  1356. p = p_start;
  1357. continue;
  1358. }
  1359. vp2 = &voice_variants[n_variants++]; // allocate space for voice variant
  1360. memcpy(vp2,vp,sizeof(espeak_VOICE)); // copy from the original voice
  1361. vp2->variant = variant_number;
  1362. voices2[ix2++] = vp2;
  1363. p++;
  1364. j++;
  1365. }
  1366. }
  1367. // add any more variants to the end of the list
  1368. while((vp != NULL) && ((variant_number = *p++) != 0) && (n_variants < N_VOICE_VARIANTS))
  1369. {
  1370. vp2 = &voice_variants[n_variants++]; // allocate space for voice variant
  1371. memcpy(vp2,vp,sizeof(espeak_VOICE)); // copy from the original voice
  1372. vp2->variant = variant_number;
  1373. voices2[ix2++] = vp2;
  1374. }
  1375. // index the sorted list by the required variant number
  1376. if(ix2 == 0)
  1377. return(NULL);
  1378. vp = voices2[voice_select2.variant % ix2];
  1379. if(vp->variant != 0)
  1380. {
  1381. variant_name = ExtractVoiceVariantName(NULL, vp->variant, 0);
  1382. sprintf(voice_id,"%s+%s", vp->identifier, variant_name);
  1383. return(voice_id);
  1384. }
  1385. return(vp->identifier);
  1386. } // end of SelectVoice
  1387. static void GetVoices(const char *path)
  1388. {//====================================
  1389. FILE *f_voice;
  1390. espeak_VOICE *voice_data;
  1391. int ftype;
  1392. char fname[sizeof(path_home)+100];
  1393. #ifdef PLATFORM_RISCOS
  1394. int len;
  1395. int *type;
  1396. char *p;
  1397. _kernel_swi_regs regs;
  1398. _kernel_oserror *error;
  1399. char buf[80];
  1400. char directory2[sizeof(path_home)+100];
  1401. regs.r[0] = 10;
  1402. regs.r[1] = (int)path;
  1403. regs.r[2] = (int)buf;
  1404. regs.r[3] = 1;
  1405. regs.r[4] = 0;
  1406. regs.r[5] = sizeof(buf);
  1407. regs.r[6] = 0;
  1408. while(regs.r[3] > 0)
  1409. {
  1410. error = _kernel_swi(0x0c+0x20000,&regs,&regs); /* OS_GBPB 10, read directory entries */
  1411. if((error != NULL) || (regs.r[3] == 0))
  1412. {
  1413. break;
  1414. }
  1415. type = (int *)(&buf[16]);
  1416. len = strlen(&buf[20]);
  1417. sprintf(fname,"%s.%s",path,&buf[20]);
  1418. if(*type == 2)
  1419. {
  1420. // a sub-directory
  1421. GetVoices(fname);
  1422. }
  1423. else
  1424. {
  1425. // a regular line, add it to the voices list
  1426. if((f_voice = fopen(fname,"r")) == NULL)
  1427. continue;
  1428. // pass voice file name within the voices directory
  1429. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, &buf[20]);
  1430. fclose(f_voice);
  1431. if(voice_data != NULL)
  1432. {
  1433. voices_list[n_voices_list++] = voice_data;
  1434. }
  1435. }
  1436. }
  1437. #else
  1438. #ifdef PLATFORM_WINDOWS
  1439. WIN32_FIND_DATAA FindFileData;
  1440. HANDLE hFind = INVALID_HANDLE_VALUE;
  1441. #undef UNICODE // we need FindFirstFileA() which takes an 8-bit c-string
  1442. sprintf(fname,"%s\\*",path);
  1443. hFind = FindFirstFileA(fname, &FindFileData);
  1444. if(hFind == INVALID_HANDLE_VALUE)
  1445. return;
  1446. do {
  1447. if(n_voices_list >= (N_VOICES_LIST-2))
  1448. break; // voices list is full
  1449. if(FindFileData.cFileName[0] != '.')
  1450. {
  1451. sprintf(fname,"%s%c%s",path,PATHSEP,FindFileData.cFileName);
  1452. ftype = GetFileLength(fname);
  1453. if(ftype == -2)
  1454. {
  1455. // a sub-sirectory
  1456. GetVoices(fname);
  1457. }
  1458. else if(ftype > 0)
  1459. {
  1460. // a regular line, add it to the voices list
  1461. if((f_voice = fopen(fname,"r")) == NULL)
  1462. continue;
  1463. // pass voice file name within the voices directory
  1464. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, FindFileData.cFileName);
  1465. fclose(f_voice);
  1466. if(voice_data != NULL)
  1467. {
  1468. voices_list[n_voices_list++] = voice_data;
  1469. }
  1470. }
  1471. }
  1472. } while(FindNextFileA(hFind, &FindFileData) != 0);
  1473. FindClose(hFind);
  1474. #else
  1475. DIR *dir;
  1476. struct dirent *ent;
  1477. if((dir = opendir((char *)path)) == NULL) // note: (char *) is needed for WINCE
  1478. return;
  1479. while((ent = readdir(dir)) != NULL)
  1480. {
  1481. if(n_voices_list >= (N_VOICES_LIST-2))
  1482. {
  1483. LOGE("The voice list is full.");
  1484. break; // voices list is full
  1485. }
  1486. if(ent->d_name[0] == '.')
  1487. continue;
  1488. sprintf(fname,"%s%c%s",path,PATHSEP,ent->d_name);
  1489. ftype = GetFileLength(fname);
  1490. if(ftype == -2)
  1491. {
  1492. // a sub-sirectory
  1493. GetVoices(fname);
  1494. }
  1495. else if(ftype > 0)
  1496. {
  1497. // a regular line, add it to the voices list
  1498. if((f_voice = fopen(fname,"r")) == NULL)
  1499. {
  1500. LOGE("Unble to load voice file '%s': %s", fname, strerror(errno));
  1501. continue;
  1502. }
  1503. // pass voice file name within the voices directory
  1504. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, ent->d_name);
  1505. fclose(f_voice);
  1506. if(voice_data != NULL)
  1507. {
  1508. voices_list[n_voices_list++] = voice_data;
  1509. }
  1510. }
  1511. else if (ftype == 0)
  1512. {
  1513. LOGE("Unble to load voice file '%s': %s", fname, strerror(errno));
  1514. }
  1515. }
  1516. closedir(dir);
  1517. #endif
  1518. #endif
  1519. } // end of GetVoices
  1520. espeak_ERROR SetVoiceByName(const char *name)
  1521. {//=========================================
  1522. espeak_VOICE *v;
  1523. int ix;
  1524. espeak_VOICE voice_selector;
  1525. char *variant_name;
  1526. static char buf[60];
  1527. strncpy0(buf,name,sizeof(buf));
  1528. variant_name = ExtractVoiceVariantName(buf, 0, 1);
  1529. for(ix=0; ; ix++)
  1530. {
  1531. // convert voice name to lower case (ascii)
  1532. if((buf[ix] = tolower(buf[ix])) == 0)
  1533. break;
  1534. }
  1535. memset(&voice_selector,0,sizeof(voice_selector));
  1536. voice_selector.name = (char *)name; // include variant name in voice stack ??
  1537. // first check for a voice with this filename
  1538. // This may avoid the need to call espeak_ListVoices().
  1539. if(LoadVoice(buf,1) != NULL)
  1540. {
  1541. if(variant_name[0] != 0)
  1542. {
  1543. LoadVoice(variant_name,2);
  1544. }
  1545. DoVoiceChange(voice);
  1546. voice_selector.languages = voice->language_name;
  1547. SetVoiceStack(&voice_selector, variant_name);
  1548. return(EE_OK);
  1549. }
  1550. if(n_voices_list == 0)
  1551. espeak_ListVoices(NULL); // create the voices list
  1552. if((v = SelectVoiceByName(voices_list,buf)) != NULL)
  1553. {
  1554. if(LoadVoice(v->identifier,0) != NULL)
  1555. {
  1556. if(variant_name[0] != 0)
  1557. {
  1558. LoadVoice(variant_name,2);
  1559. }
  1560. DoVoiceChange(voice);
  1561. voice_selector.languages = voice->language_name;
  1562. SetVoiceStack(&voice_selector, variant_name);
  1563. return(EE_OK);
  1564. }
  1565. }
  1566. return(EE_INTERNAL_ERROR); // voice name not found
  1567. } // end of SetVoiceByName
  1568. espeak_ERROR SetVoiceByProperties(espeak_VOICE *voice_selector)
  1569. {//============================================================
  1570. const char *voice_id;
  1571. int voice_found;
  1572. voice_id = SelectVoice(voice_selector, &voice_found);
  1573. if(voice_found == 0)
  1574. return(EE_NOT_FOUND);
  1575. LoadVoiceVariant(voice_id,0);
  1576. DoVoiceChange(voice);
  1577. SetVoiceStack(voice_selector, "");
  1578. return(EE_OK);
  1579. } // end of SetVoiceByProperties
  1580. void FreeVoiceList()
  1581. {//=================
  1582. int ix;
  1583. for(ix=0; ix<n_voices_list; ix++)
  1584. {
  1585. if(voices_list[ix] != NULL)
  1586. {
  1587. free(voices_list[ix]);
  1588. voices_list[ix] = NULL;
  1589. }
  1590. }
  1591. n_voices_list = 0;
  1592. }
  1593. //=======================================================================
  1594. // Library Interface Functions
  1595. //=======================================================================
  1596. #pragma GCC visibility push(default)
  1597. ESPEAK_API const espeak_VOICE **espeak_ListVoices(espeak_VOICE *voice_spec)
  1598. {//========================================================================
  1599. char path_voices[sizeof(path_home)+12];
  1600. #ifdef PLATFORM_RISCOS
  1601. if(n_voices_list == 0)
  1602. {
  1603. sprintf(path_voices,"%s%cvoices",path_home,PATHSEP);
  1604. len_path_voices = strlen(path_voices)+1;
  1605. GetVoices(path_voices);
  1606. voices_list[n_voices_list] = NULL; // voices list terminator
  1607. }
  1608. return((const espeak_VOICE **)voices_list);
  1609. #else
  1610. int ix;
  1611. int j;
  1612. espeak_VOICE *v;
  1613. static espeak_VOICE **voices = NULL;
  1614. // free previous voice list data
  1615. FreeVoiceList();
  1616. sprintf(path_voices,"%s%cvoices",path_home,PATHSEP);
  1617. len_path_voices = strlen(path_voices)+1;
  1618. GetVoices(path_voices);
  1619. voices_list[n_voices_list] = NULL; // voices list terminator
  1620. LOGI("GetVoices found %d voices.", n_voices_list);
  1621. voices = (espeak_VOICE **)realloc(voices, sizeof(espeak_VOICE *)*(n_voices_list+1));
  1622. // sort the voices list
  1623. qsort(voices_list,n_voices_list,sizeof(espeak_VOICE *),
  1624. (int (__cdecl *)(const void *,const void *))VoiceNameSorter);
  1625. if(voice_spec)
  1626. {
  1627. // select the voices which match the voice_spec, and sort them by preference
  1628. SetVoiceScores(voice_spec,voices,1);
  1629. }
  1630. else
  1631. {
  1632. // list all: omit variant voices and mbrola voices
  1633. j = 0;
  1634. for(ix=0; (v = voices_list[ix]) != NULL; ix++)
  1635. {
  1636. if((v->languages[0] != 0) && (strcmp(&v->languages[1],"variant") != 0)
  1637. && (memcmp(v->identifier,"mb/",3) != 0))
  1638. {
  1639. voices[j++] = v;
  1640. }
  1641. }
  1642. voices[j] = NULL;
  1643. }
  1644. return((const espeak_VOICE **)voices);
  1645. #endif
  1646. } // end of espeak_ListVoices
  1647. ESPEAK_API espeak_VOICE *espeak_GetCurrentVoice(void)
  1648. {//==================================================
  1649. return(&current_voice_selected);
  1650. }
  1651. #pragma GCC visibility pop