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

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