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

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