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

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