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

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