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

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