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

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