/native/external/espeak/src/voices.cpp

http://eyes-free.googlecode.com/ · C++ · 1707 lines · 1297 code · 309 blank · 101 comment · 335 complexity · 8f206aaa6a79bccd37babfabb96657b6 MD5 · raw file

  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2007 by Jonathan Duddington *
  3. * email: jonsd@users.sourceforge.net *
  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, const char *spec_language, 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(strcmp(spec_language,"mbrola")==0)
  864. {
  865. // only list mbrola voices
  866. if(memcmp(voice->identifier,"mb/",3) == 0)
  867. return(100);
  868. return(0);
  869. }
  870. if(spec_n_parts == 0)
  871. {
  872. score = 100;
  873. }
  874. else
  875. {
  876. if((*p == 0) && (strcmp(spec_language,"variants")==0))
  877. {
  878. // match on a voice with no languages if the required language is "variants"
  879. score = 100;
  880. }
  881. // compare the required language with each of the languages of this voice
  882. while(*p != 0)
  883. {
  884. language_priority = *p++;
  885. matching = 1;
  886. matching_parts = 0;
  887. n_parts = 1;
  888. for(ix=0; ; ix++)
  889. {
  890. if((ix >= spec_lang_len) || ((c1 = spec_language[ix]) == '-'))
  891. c1 = 0;
  892. if((c2 = p[ix]) == '-')
  893. c2 = 0;
  894. if(c1 != c2)
  895. {
  896. matching = 0;
  897. }
  898. if(p[ix] == '-')
  899. {
  900. n_parts++;
  901. if(matching)
  902. matching_parts++;
  903. }
  904. if(p[ix] == 0)
  905. break;
  906. }
  907. p += (ix+1);
  908. matching_parts += matching; // number of parts which match
  909. if(matching_parts == 0)
  910. break; // no matching parts for this language
  911. x = 5;
  912. // reduce the score if not all parts of the required language match
  913. if((diff = (spec_n_parts - matching_parts)) > 0)
  914. x -= diff;
  915. // reduce score if the language is more specific than required
  916. if((diff = (n_parts - matching_parts)) > 0)
  917. x -= diff;
  918. x = x*100 - (language_priority * 2);
  919. if(x > score)
  920. score = x;
  921. }
  922. }
  923. if(score == 0)
  924. return(0);
  925. if(voice_spec->name != NULL)
  926. {
  927. if(strcmp(voice_spec->name,voice->name)==0)
  928. {
  929. // match on voice name
  930. score += 500;
  931. }
  932. else
  933. if(strcmp(voice_spec->name,voice->identifier)==0)
  934. {
  935. score += 400;
  936. }
  937. }
  938. if(((voice_spec->gender == 1) || (voice_spec->gender == 2)) &&
  939. ((voice->gender == 1) || (voice->gender == 2)))
  940. {
  941. if(voice_spec->gender == voice->gender)
  942. score += 50;
  943. else
  944. score -= 50;
  945. }
  946. if((voice_spec->age <= 12) && (voice->gender == 2) && (voice->age > 12))
  947. {
  948. score += 5; // give some preference for non-child female voice if a child is requested
  949. }
  950. if(voice->age != 0)
  951. {
  952. if(voice_spec->age == 0)
  953. required_age = 30;
  954. else
  955. required_age = voice_spec->age;
  956. ratio = (required_age*100)/voice->age;
  957. if(ratio < 100)
  958. ratio = 10000/ratio;
  959. ratio = (ratio - 100)/10; // 0=exact match, 10=out by factor of 2
  960. x = 5 - ratio;
  961. if(x > 0) x = 0;
  962. score = score + x;
  963. if(voice_spec->age > 0)
  964. score += 10; // required age specified, favour voices with a specified age (near it)
  965. }
  966. if(score < 1)
  967. score = 1;
  968. return(score);
  969. } // end of ScoreVoice
  970. static int SetVoiceScores(espeak_VOICE *voice_select, espeak_VOICE **voices, int control)
  971. {//======================================================================================
  972. // control: bit0=1 include mbrola voices
  973. int ix;
  974. int score;
  975. int nv; // number of candidates
  976. int n_parts=0;
  977. int lang_len=0;
  978. espeak_VOICE *vp;
  979. char language[80];
  980. // count number of parts in the specified language
  981. if((voice_select->languages != NULL) && (voice_select->languages[0] != 0))
  982. {
  983. n_parts = 1;
  984. lang_len = strlen(voice_select->languages);
  985. for(ix=0; (ix<=lang_len) && ((unsigned)ix < sizeof(language)); ix++)
  986. {
  987. if((language[ix] = tolower(voice_select->languages[ix])) == '-')
  988. n_parts++;
  989. }
  990. }
  991. // select those voices which match the specified language
  992. nv = 0;
  993. for(ix=0; ix<n_voices_list; ix++)
  994. {
  995. vp = voices_list[ix];
  996. if(((control & 1) == 0) && (memcmp(vp->identifier,"mb/",3) == 0))
  997. continue;
  998. if((score = ScoreVoice(voice_select, language, n_parts, lang_len, voices_list[ix])) > 0)
  999. {
  1000. voices[nv++] = vp;
  1001. vp->score = score;
  1002. }
  1003. }
  1004. voices[nv] = NULL; // list terminator
  1005. if(nv==0)
  1006. return(0);
  1007. // sort the selected voices by their score
  1008. qsort(voices,nv,sizeof(espeak_VOICE *),(int (__cdecl *)(const void *,const void *))VoiceScoreSorter);
  1009. return(nv);
  1010. } // end of SetVoiceScores
  1011. espeak_VOICE *SelectVoiceByName(espeak_VOICE **voices, const char *name)
  1012. {//=====================================================================
  1013. int ix;
  1014. int match_fname = -1;
  1015. int match_fname2 = -1;
  1016. int match_name = -1;
  1017. const char *id;
  1018. int last_part_len;
  1019. char last_part[41];
  1020. if(voices == NULL)
  1021. {
  1022. if(n_voices_list == 0)
  1023. espeak_ListVoices(NULL); // create the voices list
  1024. voices = voices_list;
  1025. }
  1026. sprintf(last_part,"%c%s",PATHSEP,name);
  1027. last_part_len = strlen(last_part);
  1028. for(ix=0; voices[ix] != NULL; ix++)
  1029. {
  1030. if(strcmp(name,voices[ix]->name)==0)
  1031. {
  1032. match_name = ix; // found matching voice name
  1033. break;
  1034. }
  1035. else
  1036. if(strcmp(name,id = voices[ix]->identifier)==0)
  1037. {
  1038. match_fname = ix; // matching identifier, use this if no matching name
  1039. }
  1040. else
  1041. if(strcmp(last_part,&id[strlen(id)-last_part_len])==0)
  1042. {
  1043. match_fname2 = ix;
  1044. }
  1045. }
  1046. if(match_name < 0)
  1047. {
  1048. match_name = match_fname; // no matching name, try matching filename
  1049. if(match_name < 0)
  1050. match_name = match_fname2; // try matching just the last part of the filename
  1051. }
  1052. if(match_name < 0)
  1053. return(NULL);
  1054. return(voices[match_name]);
  1055. } // end of SelectVoiceByName
  1056. char const *SelectVoice(espeak_VOICE *voice_select)
  1057. {//================================================
  1058. // Returns a path within espeak-voices, with a possible +variant suffix
  1059. // variant is an output-only parameter
  1060. int nv; // number of candidates
  1061. int ix, ix2;
  1062. int j;
  1063. int n_variants;
  1064. int variant_number;
  1065. int gender;
  1066. int skip;
  1067. int aged=1;
  1068. char *variant_name;
  1069. const char *p, *p_start;
  1070. espeak_VOICE *vp = NULL;
  1071. espeak_VOICE *vp2;
  1072. espeak_VOICE voice_select2;
  1073. espeak_VOICE *voices[N_VOICES_LIST]; // list of candidates
  1074. espeak_VOICE *voices2[N_VOICES_LIST+N_VOICE_VARIANTS];
  1075. static espeak_VOICE voice_variants[N_VOICE_VARIANTS];
  1076. static char voice_id[50];
  1077. memcpy(&voice_select2,voice_select,sizeof(voice_select2));
  1078. if(n_voices_list == 0)
  1079. espeak_ListVoices(NULL); // create the voices list
  1080. if((voice_select2.languages == NULL) || (voice_select2.languages[0] == 0))
  1081. {
  1082. // no language is specified. Get language from the named voice
  1083. static char buf[60];
  1084. if(voice_select2.name == NULL)
  1085. {
  1086. if((voice_select2.name = voice_select2.identifier) == NULL)
  1087. voice_select2.name = "default";
  1088. }
  1089. strncpy0(buf,voice_select2.name,sizeof(buf));
  1090. variant_name = ExtractVoiceVariantName(buf,0);
  1091. vp = SelectVoiceByName(voices_list,buf);
  1092. if(vp != NULL)
  1093. {
  1094. voice_select2.languages = &(vp->languages[1]);
  1095. if((voice_select2.gender==0) && (voice_select2.age==0) && (voice_select2.variant==0))
  1096. {
  1097. if(variant_name[0] != 0)
  1098. {
  1099. sprintf(voice_id,"%s+%s",vp->identifier,&variant_name[3]); // omit the !v/ from variant_name
  1100. return(voice_id);
  1101. }
  1102. return(vp->identifier);
  1103. }
  1104. }
  1105. }
  1106. // select and sort voices for the required language
  1107. nv = SetVoiceScores(&voice_select2,voices,0);
  1108. if(nv == 0)
  1109. {
  1110. // no matching voice, choose the default
  1111. if((voices[0] = SelectVoiceByName(voices_list,"default")) != NULL)
  1112. nv = 1;
  1113. }
  1114. gender = 0;
  1115. if((voice_select2.gender == 2) || ((voice_select2.age > 0) && (voice_select2.age < 13)))
  1116. gender = 2;
  1117. else
  1118. if(voice_select2.gender == 1)
  1119. gender = 1;
  1120. #define AGE_OLD 60
  1121. if(voice_select2.age < AGE_OLD)
  1122. aged = 0;
  1123. p = p_start = variant_lists[gender];
  1124. if(aged == 0)
  1125. p++; // the first voice in the variants list is older
  1126. // add variants for the top voices
  1127. n_variants = 0;
  1128. for(ix=0, ix2=0; ix<nv; ix++)
  1129. {
  1130. vp = voices[ix];
  1131. // is the main voice the required gender?
  1132. skip=0;
  1133. if((gender != 0) && (vp->gender != gender))
  1134. {
  1135. skip=1;
  1136. }
  1137. if((ix2==0) && aged && (vp->age < AGE_OLD))
  1138. {
  1139. skip=1;
  1140. }
  1141. if(skip==0)
  1142. {
  1143. voices2[ix2++] = vp;
  1144. }
  1145. for(j=0; (j < vp->xx1) && (n_variants < N_VOICE_VARIANTS);)
  1146. {
  1147. if((variant_number = *p) == 0)
  1148. {
  1149. p = p_start;
  1150. continue;
  1151. }
  1152. vp2 = &voice_variants[n_variants++]; // allocate space for voice variant
  1153. memcpy(vp2,vp,sizeof(espeak_VOICE)); // copy from the original voice
  1154. vp2->variant = variant_number;
  1155. voices2[ix2++] = vp2;
  1156. p++;
  1157. j++;
  1158. }
  1159. }
  1160. // add any more variants to the end of the list
  1161. while((vp != NULL) && ((variant_number = *p++) != 0) && (n_variants < N_VOICE_VARIANTS))
  1162. {
  1163. vp2 = &voice_variants[n_variants++]; // allocate space for voice variant
  1164. memcpy(vp2,vp,sizeof(espeak_VOICE)); // copy from the original voice
  1165. vp2->variant = variant_number;
  1166. voices2[ix2++] = vp2;
  1167. }
  1168. // index the sorted list by the required variant number
  1169. vp = voices2[voice_select2.variant % ix2];
  1170. if(vp->variant != 0)
  1171. {
  1172. variant_name = ExtractVoiceVariantName(NULL,vp->variant);
  1173. sprintf(voice_id,"%s+%s",vp->identifier,&variant_name[3]);
  1174. return(voice_id);
  1175. }
  1176. return(vp->identifier);
  1177. } // end of SelectVoice
  1178. void GetVoices(const char *path)
  1179. {//=============================
  1180. FILE *f_voice;
  1181. espeak_VOICE *voice_data;
  1182. int ftype;
  1183. char fname[sizeof(path_home)+100];
  1184. #ifdef PLATFORM_RISCOS
  1185. int len;
  1186. int *type;
  1187. char *p;
  1188. _kernel_swi_regs regs;
  1189. _kernel_oserror *error;
  1190. char buf[80];
  1191. char directory2[sizeof(path_home)+100];
  1192. regs.r[0] = 10;
  1193. regs.r[1] = (int)path;
  1194. regs.r[2] = (int)buf;
  1195. regs.r[3] = 1;
  1196. regs.r[4] = 0;
  1197. regs.r[5] = sizeof(buf);
  1198. regs.r[6] = 0;
  1199. while(regs.r[3] > 0)
  1200. {
  1201. error = _kernel_swi(0x0c+0x20000,&regs,&regs); /* OS_GBPB 10, read directory entries */
  1202. if((error != NULL) || (regs.r[3] == 0))
  1203. {
  1204. break;
  1205. }
  1206. type = (int *)(&buf[16]);
  1207. len = strlen(&buf[20]);
  1208. sprintf(fname,"%s.%s",path,&buf[20]);
  1209. if(*type == 2)
  1210. {
  1211. // a sub-directory
  1212. GetVoices(fname);
  1213. }
  1214. else
  1215. {
  1216. // a regular line, add it to the voices list
  1217. if((f_voice = fopen(fname,"r")) == NULL)
  1218. continue;
  1219. // pass voice file name within the voices directory
  1220. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, &buf[20]);
  1221. fclose(f_voice);
  1222. if(voice_data != NULL)
  1223. {
  1224. voices_list[n_voices_list++] = voice_data;
  1225. }
  1226. }
  1227. }
  1228. #else
  1229. #ifdef PLATFORM_WINDOWS
  1230. WIN32_FIND_DATA FindFileData;
  1231. HANDLE hFind = INVALID_HANDLE_VALUE;
  1232. DWORD dwError;
  1233. #undef UNICODE // we need FindFirstFileA() which takes an 8-bit c-string
  1234. sprintf(fname,"%s\\*",path);
  1235. hFind = FindFirstFile(fname, &FindFileData);
  1236. if(hFind == INVALID_HANDLE_VALUE)
  1237. return;
  1238. do {
  1239. sprintf(fname,"%s%c%s",path,PATHSEP,FindFileData.cFileName);
  1240. ftype = GetFileLength(fname);
  1241. if((ftype == -2) && (FindFileData.cFileName[0] != '.'))
  1242. {
  1243. // a sub-sirectory
  1244. GetVoices(fname);
  1245. }
  1246. else
  1247. if(ftype > 0)
  1248. {
  1249. // a regular line, add it to the voices list
  1250. if((f_voice = fopen(fname,"r")) == NULL)
  1251. continue;
  1252. // pass voice file name within the voices directory
  1253. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, FindFileData.cFileName);
  1254. fclose(f_voice);
  1255. if(voice_data != NULL)
  1256. {
  1257. voices_list[n_voices_list++] = voice_data;
  1258. }
  1259. }
  1260. } while(FindNextFile(hFind, &FindFileData) != 0);
  1261. FindClose(hFind);
  1262. #else
  1263. DIR *dir;
  1264. struct dirent *ent;
  1265. if((dir = opendir(path)) == NULL)
  1266. return;
  1267. while((ent = readdir(dir)) != NULL)
  1268. {
  1269. if(n_voices_list >= (N_VOICES_LIST-2))
  1270. break; // voices list is full
  1271. sprintf(fname,"%s%c%s",path,PATHSEP,ent->d_name);
  1272. ftype = GetFileLength(fname);
  1273. if((ftype == -2) && (ent->d_name[0] != '.'))
  1274. {
  1275. // a sub-sirectory
  1276. GetVoices(fname);
  1277. }
  1278. else
  1279. if(ftype > 0)
  1280. {
  1281. // a regular line, add it to the voices list
  1282. if((f_voice = fopen(fname,"r")) == NULL)
  1283. continue;
  1284. // pass voice file name within the voices directory
  1285. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, ent->d_name);
  1286. fclose(f_voice);
  1287. if(voice_data != NULL)
  1288. {
  1289. voices_list[n_voices_list++] = voice_data;
  1290. }
  1291. }
  1292. }
  1293. closedir(dir);
  1294. #endif
  1295. #endif
  1296. } // end of GetVoices
  1297. espeak_ERROR SetVoiceByName(const char *name)
  1298. {//=========================================
  1299. espeak_VOICE *v;
  1300. espeak_VOICE voice_selector;
  1301. char *variant_name;
  1302. static char buf[60];
  1303. strncpy0(buf,name,sizeof(buf));
  1304. variant_name = ExtractVoiceVariantName(buf,0);
  1305. memset(&voice_selector,0,sizeof(voice_selector));
  1306. // voice_selector.name = buf;
  1307. voice_selector.name = (char *)name; // include variant name in voice stack ??
  1308. // first check for a voice with this filename
  1309. // This may avoid the need to call espeak_ListVoices().
  1310. if(LoadVoice(buf,1) != NULL)
  1311. {
  1312. if(variant_name[0] != 0)
  1313. {
  1314. LoadVoice(variant_name,2);
  1315. }
  1316. DoVoiceChange(voice);
  1317. SetVoiceStack(&voice_selector);
  1318. return(EE_OK);
  1319. }
  1320. if(n_voices_list == 0)
  1321. espeak_ListVoices(NULL); // create the voices list
  1322. if((v = SelectVoiceByName(voices_list,buf)) != NULL)
  1323. {
  1324. if(LoadVoice(v->identifier,0) != NULL)
  1325. {
  1326. if(variant_name[0] != 0)
  1327. {
  1328. LoadVoice(variant_name,2);
  1329. }
  1330. DoVoiceChange(voice);
  1331. SetVoiceStack(&voice_selector);
  1332. return(EE_OK);
  1333. }
  1334. }
  1335. return(EE_INTERNAL_ERROR); // voice name not found
  1336. } // end of SetVoiceByName
  1337. espeak_ERROR SetVoiceByProperties(espeak_VOICE *voice_selector)
  1338. {//============================================================
  1339. const char *voice_id;
  1340. voice_id = SelectVoice(voice_selector);
  1341. LoadVoiceVariant(voice_id,0);
  1342. DoVoiceChange(voice);
  1343. SetVoiceStack(voice_selector);
  1344. return(EE_OK);
  1345. } // end of SetVoiceByProperties
  1346. //=======================================================================
  1347. // Library Interface Functions
  1348. //=======================================================================
  1349. #pragma GCC visibility push(default)
  1350. ESPEAK_API const espeak_VOICE **espeak_ListVoices(espeak_VOICE *voice_spec)
  1351. {//========================================================================
  1352. #ifndef PLATFORM_RISCOS
  1353. int ix;
  1354. int j;
  1355. espeak_VOICE *v;
  1356. static espeak_VOICE *voices[N_VOICES_LIST];
  1357. char path_voices[sizeof(path_home)+12];
  1358. // free previous voice list data
  1359. for(ix=0; ix<n_voices_list; ix++)
  1360. {
  1361. if(voices_list[ix] != NULL)
  1362. free(voices_list[ix]);
  1363. }
  1364. n_voices_list = 0;
  1365. sprintf(path_voices,"%s%cvoices",path_home,PATHSEP);
  1366. len_path_voices = strlen(path_voices)+1;
  1367. GetVoices(path_voices);
  1368. voices_list[n_voices_list] = NULL; // voices list terminator
  1369. // sort the voices list
  1370. qsort(voices_list,n_voices_list,sizeof(espeak_VOICE *),
  1371. (int (__cdecl *)(const void *,const void *))VoiceNameSorter);
  1372. if(voice_spec)
  1373. {
  1374. // select the voices which match the voice_spec, and sort them by preference
  1375. SetVoiceScores(voice_spec,voices,1);
  1376. }
  1377. else
  1378. {
  1379. // list all: omit variant voices and mbrola voices
  1380. j = 0;
  1381. for(ix=0; (v = voices_list[ix]) != NULL; ix++)
  1382. {
  1383. if((v->languages[0] != 0) && (strcmp(&v->languages[1],"variant") != 0) && (memcmp(v->identifier,"mb/",3) != 0))
  1384. {
  1385. voices[j++] = v;
  1386. }
  1387. }
  1388. voices[j] = NULL;
  1389. }
  1390. return((const espeak_VOICE **)voices);
  1391. #endif
  1392. return((const espeak_VOICE **)voices_list);
  1393. } // end of espeak_ListVoices
  1394. ESPEAK_API espeak_VOICE *espeak_GetCurrentVoice(void)
  1395. {//==================================================
  1396. return(&voice_selected);
  1397. }
  1398. #pragma GCC visibility pop