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

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