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

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