voices.cpp 41 KB

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