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

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