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

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