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

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