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

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