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

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