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

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