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

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