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

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