eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

voices.c 41KB

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