/Libraries/voices.cpp

http://espeak-engine.googlecode.com/ · C++ · 1797 lines · 1365 code · 326 blank · 106 comment · 342 complexity · fda6053de91206ba71ef85410a136489 MD5 · raw file

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