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

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