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

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