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

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