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

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