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

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