eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

voices.cpp 45KB

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