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.c 43KB

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