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

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