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

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