eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

voices.c 42KB

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