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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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 "speech.h"
  37. #include "phoneme.h"
  38. #include "synthesize.h"
  39. #include "voice.h"
  40. #include "translate.h"
  41. MNEM_TAB genders[] = {
  42. { "male", ENGENDER_MALE },
  43. { "female", ENGENDER_FEMALE },
  44. { NULL, ENGENDER_MALE }
  45. };
  46. int tone_points[12] = { 600, 170, 1200, 135, 2000, 110, 3000, 110, -1, 0 };
  47. // limit the rate of change for each formant number
  48. static int formant_rate_22050[9] = { 240, 170, 170, 170, 170, 170, 170, 170, 170 }; // values for 22kHz sample rate
  49. int formant_rate[9]; // values adjusted for actual sample rate
  50. #define DEFAULT_LANGUAGE_PRIORITY 5
  51. #define N_VOICES_LIST 250
  52. static int n_voices_list = 0;
  53. static espeak_VOICE *voices_list[N_VOICES_LIST];
  54. espeak_VOICE current_voice_selected;
  55. enum {
  56. V_NAME = 1,
  57. V_LANGUAGE,
  58. V_GENDER,
  59. V_TRANSLATOR,
  60. V_PHONEMES,
  61. V_DICTIONARY,
  62. V_VARIANTS,
  63. V_MAINTAINER,
  64. V_STATUS,
  65. // these affect voice quality, are independent of language
  66. V_FORMANT,
  67. V_PITCH,
  68. V_ECHO,
  69. V_FLUTTER,
  70. V_ROUGHNESS,
  71. V_CLARITY,
  72. V_TONE,
  73. V_VOICING,
  74. V_BREATH,
  75. V_BREATHW,
  76. // these override defaults set by the translator
  77. V_WORDGAP,
  78. V_INTONATION,
  79. V_TUNES,
  80. V_STRESSLENGTH,
  81. V_STRESSAMP,
  82. V_STRESSADD,
  83. V_DICTRULES,
  84. V_STRESSRULE,
  85. V_STRESSOPT,
  86. V_NUMBERS,
  87. V_OPTION,
  88. V_MBROLA,
  89. V_KLATT,
  90. V_FAST,
  91. V_SPEED,
  92. V_DICTMIN,
  93. V_ALPHABET2,
  94. // these need a phoneme table to have been specified
  95. V_REPLACE,
  96. V_CONSONANTS
  97. };
  98. static MNEM_TAB options_tab[] = {
  99. { "reduce_t", LOPT_REDUCE_T },
  100. { "bracket", LOPT_BRACKET_PAUSE },
  101. { NULL, -1 }
  102. };
  103. static MNEM_TAB keyword_tab[] = {
  104. { "name", V_NAME },
  105. { "language", V_LANGUAGE },
  106. { "gender", V_GENDER },
  107. { "maintainer", V_MAINTAINER },
  108. { "status", V_STATUS },
  109. { "variants", V_VARIANTS },
  110. { "formant", V_FORMANT },
  111. { "pitch", V_PITCH },
  112. { "phonemes", V_PHONEMES },
  113. { "translator", V_TRANSLATOR },
  114. { "dictionary", V_DICTIONARY },
  115. { "stressLength", V_STRESSLENGTH },
  116. { "stressAmp", V_STRESSAMP },
  117. { "stressAdd", V_STRESSADD },
  118. { "intonation", V_INTONATION },
  119. { "tunes", V_TUNES },
  120. { "dictrules", V_DICTRULES },
  121. { "stressrule", V_STRESSRULE },
  122. { "stressopt", V_STRESSOPT },
  123. { "replace", V_REPLACE },
  124. { "words", V_WORDGAP },
  125. { "echo", V_ECHO },
  126. { "flutter", V_FLUTTER },
  127. { "roughness", V_ROUGHNESS },
  128. { "clarity", V_CLARITY },
  129. { "tone", V_TONE },
  130. { "voicing", V_VOICING },
  131. { "breath", V_BREATH },
  132. { "breathw", V_BREATHW },
  133. { "numbers", V_NUMBERS },
  134. { "option", V_OPTION },
  135. { "mbrola", V_MBROLA },
  136. { "consonants", V_CONSONANTS },
  137. { "klatt", V_KLATT },
  138. { "fast_test2", V_FAST },
  139. { "speed", V_SPEED },
  140. { "dict_min", V_DICTMIN },
  141. // these just set a value in langopts.param[]
  142. { "l_dieresis", 0x100+LOPT_DIERESES },
  143. { "l_prefix", 0x100+LOPT_PREFIXES },
  144. { "l_regressive_v", 0x100+LOPT_REGRESSIVE_VOICING },
  145. { "l_unpronouncable", 0x100+LOPT_UNPRONOUNCABLE },
  146. { "l_sonorant_min", 0x100+LOPT_SONORANT_MIN },
  147. { "l_length_mods", 0x100+LOPT_LENGTH_MODS },
  148. { "apostrophe", 0x100+LOPT_APOSTROPHE },
  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 *voice, 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. voice->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. static 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. static unsigned int StringToWord2(const char *string)
  401. {
  402. // Convert a language name string to a word such as L('e','n')
  403. int ix;
  404. int c;
  405. unsigned int value = 0;
  406. for (ix = 0; (ix < 4) && ((c = string[ix]) != 0); ix++)
  407. value = (value << 8) | (c & 0xff);
  408. return value;
  409. }
  410. voice_t *LoadVoice(const char *vname, int control)
  411. {
  412. // control, bit 0 1= no_default
  413. // bit 1 1 = change tone only, not language
  414. // bit 2 1 = don't report error on LoadDictionary
  415. // bit 4 1 = vname = full path
  416. FILE *f_voice = NULL;
  417. char *p;
  418. int key;
  419. int ix;
  420. int n;
  421. int value;
  422. int value2;
  423. int langix = 0;
  424. int tone_only = control & 2;
  425. int language_set = 0;
  426. int phonemes_set = 0;
  427. int stress_amps_set = 0;
  428. int stress_lengths_set = 0;
  429. int stress_add_set = 0;
  430. int conditional_rules = 0;
  431. LANGUAGE_OPTIONS *langopts = NULL;
  432. Translator *new_translator = NULL;
  433. char voicename[40];
  434. char language_name[40];
  435. char translator_name[40];
  436. char new_dictionary[40];
  437. char phonemes_name[40];
  438. char option_name[40];
  439. const char *language_type;
  440. char buf[sizeof(path_home)+30];
  441. char path_voices[sizeof(path_home)+12];
  442. int dict_min = 0;
  443. int stress_amps[8];
  444. int stress_lengths[8];
  445. int stress_add[8];
  446. char names[8][40];
  447. char name1[40];
  448. char name2[80];
  449. int pitch1;
  450. int pitch2;
  451. static char voice_identifier[40]; // file name for current_voice_selected
  452. static char voice_name[40]; // voice name for current_voice_selected
  453. static char voice_languages[100]; // list of languages and priorities for current_voice_selected
  454. strncpy0(voicename, vname, sizeof(voicename));
  455. if (control & 0x10) {
  456. strcpy(buf, vname);
  457. if (GetFileLength(buf) <= 0)
  458. return NULL;
  459. } else {
  460. if (voicename[0] == 0)
  461. strcpy(voicename, ESPEAKNG_DEFAULT_VOICE);
  462. sprintf(path_voices, "%s%cvoices%c", path_home, PATHSEP, PATHSEP);
  463. sprintf(buf, "%s%s", path_voices, voicename); // look in the main voices directory
  464. if (GetFileLength(buf) <= 0) {
  465. sprintf(path_voices, "%s%clang%c", path_home, PATHSEP, PATHSEP);
  466. sprintf(buf, "%s%s", path_voices, voicename); // look in the main languages directory
  467. }
  468. }
  469. f_voice = fopen(buf, "r");
  470. language_type = "en"; // default
  471. if (f_voice == NULL) {
  472. if (control & 3)
  473. return NULL; // can't open file
  474. if (SelectPhonemeTableName(voicename) >= 0)
  475. language_type = voicename;
  476. }
  477. if (!tone_only && (translator != NULL)) {
  478. DeleteTranslator(translator);
  479. translator = NULL;
  480. }
  481. strcpy(translator_name, language_type);
  482. strcpy(new_dictionary, language_type);
  483. strcpy(phonemes_name, language_type);
  484. if (!tone_only) {
  485. voice = &voicedata;
  486. strncpy0(voice_identifier, vname, sizeof(voice_identifier));
  487. voice_name[0] = 0;
  488. voice_languages[0] = 0;
  489. current_voice_selected.identifier = voice_identifier;
  490. current_voice_selected.name = voice_name;
  491. current_voice_selected.languages = voice_languages;
  492. } else {
  493. // append the variant file name to the voice identifier
  494. if ((p = strchr(voice_identifier, '+')) != NULL)
  495. *p = 0; // remove previous variant name
  496. sprintf(buf, "+%s", &vname[3]); // omit !v/ from the variant filename
  497. strcat(voice_identifier, buf);
  498. langopts = &translator->langopts;
  499. }
  500. VoiceReset(tone_only);
  501. if (!tone_only)
  502. SelectPhonemeTableName(phonemes_name); // set up phoneme_tab
  503. while ((f_voice != NULL) && (fgets_strip(buf, sizeof(buf), f_voice) != NULL)) {
  504. // isolate the attribute name
  505. for (p = buf; (*p != 0) && !isspace(*p); p++) ;
  506. *p++ = 0;
  507. if (buf[0] == 0) continue;
  508. key = LookupMnem(keyword_tab, buf);
  509. switch (key)
  510. {
  511. case V_LANGUAGE:
  512. {
  513. unsigned int len;
  514. int priority;
  515. if (tone_only)
  516. break;
  517. priority = DEFAULT_LANGUAGE_PRIORITY;
  518. language_name[0] = 0;
  519. sscanf(p, "%s %d", language_name, &priority);
  520. if (strcmp(language_name, "variant") == 0)
  521. break;
  522. len = strlen(language_name) + 2;
  523. // check for space in languages[]
  524. if (len < (sizeof(voice_languages)-langix-1)) {
  525. voice_languages[langix] = priority;
  526. strcpy(&voice_languages[langix+1], language_name);
  527. langix += len;
  528. }
  529. // only act on the first language line
  530. if (language_set == 0) {
  531. language_type = strtok(language_name, "-");
  532. language_set = 1;
  533. strcpy(translator_name, language_type);
  534. strcpy(new_dictionary, language_type);
  535. strcpy(phonemes_name, language_type);
  536. SelectPhonemeTableName(phonemes_name);
  537. if (new_translator != NULL)
  538. DeleteTranslator(new_translator);
  539. new_translator = SelectTranslator(translator_name);
  540. langopts = &new_translator->langopts;
  541. strncpy0(voice->language_name, language_name, sizeof(voice->language_name));
  542. }
  543. }
  544. break;
  545. case V_NAME:
  546. if (tone_only == 0) {
  547. while (isspace(*p)) p++;
  548. strncpy0(voice_name, p, sizeof(voice_name));
  549. }
  550. break;
  551. case V_GENDER:
  552. {
  553. int age = 0;
  554. char vgender[80];
  555. sscanf(p, "%s %d", vgender, &age);
  556. current_voice_selected.gender = LookupMnem(genders, vgender);
  557. current_voice_selected.age = age;
  558. }
  559. break;
  560. case V_TRANSLATOR:
  561. if (tone_only) break;
  562. sscanf(p, "%s", translator_name);
  563. if (new_translator != NULL)
  564. DeleteTranslator(new_translator);
  565. new_translator = SelectTranslator(translator_name);
  566. langopts = &new_translator->langopts;
  567. break;
  568. case V_DICTIONARY: // dictionary
  569. sscanf(p, "%s", new_dictionary);
  570. break;
  571. case V_PHONEMES: // phoneme table
  572. sscanf(p, "%s", phonemes_name);
  573. break;
  574. case V_FORMANT:
  575. VoiceFormant(p);
  576. break;
  577. case V_PITCH:
  578. // default is pitch 82 118
  579. if (sscanf(p, "%d %d", &pitch1, &pitch2) == 2) {
  580. voice->pitch_base = (pitch1 - 9) << 12;
  581. voice->pitch_range = (pitch2 - pitch1) * 108;
  582. double factor = (double)(pitch1 - 82)/82;
  583. voice->formant_factor = (int)((1+factor/4) * 256); // nominal formant shift for a different voice pitch
  584. }
  585. break;
  586. case V_STRESSLENGTH: // stressLength
  587. stress_lengths_set = Read8Numbers(p, stress_lengths);
  588. break;
  589. case V_STRESSAMP: // stressAmp
  590. stress_amps_set = Read8Numbers(p, stress_amps);
  591. break;
  592. case V_STRESSADD: // stressAdd
  593. stress_add_set = Read8Numbers(p, stress_add);
  594. break;
  595. case V_INTONATION: // intonation
  596. sscanf(p, "%d %d", &option_tone_flags, &option_tone2);
  597. if ((option_tone_flags & 0xff) != 0) {
  598. if (langopts)
  599. langopts->intonation_group = option_tone_flags & 0xff;
  600. else
  601. fprintf(stderr, "Cannot set intonation: language not set, or is invalid.\n");
  602. }
  603. break;
  604. case V_TUNES:
  605. n = sscanf(p, "%s %s %s %s %s %s", names[0], names[1], names[2], names[3], names[4], names[5]);
  606. if (langopts) {
  607. langopts->intonation_group = 0;
  608. for (ix = 0; ix < n; ix++) {
  609. if (strcmp(names[ix], "NULL") == 0)
  610. continue;
  611. if ((value = LookupTune(names[ix])) < 0)
  612. fprintf(stderr, "Unknown tune '%s'\n", names[ix]);
  613. else
  614. langopts->tunes[ix] = value;
  615. }
  616. } else
  617. fprintf(stderr, "Cannot set tunes: language not set, or is invalid.\n");
  618. break;
  619. case V_DICTRULES: // conditional dictionary rules and list entries
  620. case V_NUMBERS:
  621. case V_STRESSOPT:
  622. if (langopts) {
  623. // expect a list of numbers
  624. while (*p != 0) {
  625. while (isspace(*p)) p++;
  626. if ((n = atoi(p)) > 0) {
  627. p++;
  628. if (n < 32) {
  629. if (key == V_DICTRULES)
  630. conditional_rules |= (1 << n);
  631. else if (key == V_NUMBERS)
  632. langopts->numbers |= (1 << n);
  633. else if (key == V_STRESSOPT)
  634. langopts->stress_flags |= (1 << n);
  635. } else {
  636. if ((key == V_NUMBERS) && (n < 64))
  637. langopts->numbers2 |= (1 << (n-32));
  638. else
  639. fprintf(stderr, "Bad option number %d\n", n);
  640. }
  641. }
  642. while (isalnum(*p)) p++;
  643. }
  644. ProcessLanguageOptions(langopts);
  645. } else
  646. fprintf(stderr, "Cannot set stressopt: language not set, or is invalid.\n");
  647. break;
  648. case V_REPLACE:
  649. if (phonemes_set == 0) {
  650. // must set up a phoneme table before we can lookup phoneme mnemonics
  651. SelectPhonemeTableName(phonemes_name);
  652. phonemes_set = 1;
  653. }
  654. PhonemeReplacement(p);
  655. break;
  656. case V_WORDGAP: // words
  657. if (langopts)
  658. sscanf(p, "%d %d", &langopts->word_gap, &langopts->vowel_pause);
  659. else
  660. fprintf(stderr, "Cannot set wordgap: language not set, or is invalid.\n");
  661. break;
  662. case V_STRESSRULE:
  663. if (langopts)
  664. sscanf(p, "%d %d %d %d", &langopts->stress_rule,
  665. &langopts->stress_flags,
  666. &langopts->unstressed_wd1,
  667. &langopts->unstressed_wd2);
  668. else
  669. fprintf(stderr, "Cannot set stressrule: language not set, or is invalid.\n");
  670. break;
  671. case V_OPTION:
  672. if (langopts) {
  673. value2 = 0;
  674. if (((sscanf(p, "%s %d %d", option_name, &value, &value2) >= 2) && ((ix = LookupMnem(options_tab, option_name)) >= 0)) ||
  675. ((sscanf(p, "%d %d %d", &ix, &value, &value2) >= 2) && (ix < N_LOPTS))) {
  676. langopts->param[ix] = value;
  677. langopts->param2[ix] = value2;
  678. } else
  679. fprintf(stderr, "Bad voice option: %s %s\n", buf, p);
  680. } else
  681. fprintf(stderr, "Cannot set option: language not set, or is invalid.\n");
  682. break;
  683. case V_ECHO:
  684. // echo. suggest: 135mS 11%
  685. value = 0;
  686. voice->echo_amp = 0;
  687. sscanf(p, "%d %d", &voice->echo_delay, &voice->echo_amp);
  688. break;
  689. case V_FLUTTER: // flutter
  690. if (sscanf(p, "%d", &value) == 1)
  691. voice->flutter = value * 32;
  692. break;
  693. case V_ROUGHNESS: // roughness
  694. if (sscanf(p, "%d", &value) == 1)
  695. voice->roughness = value;
  696. break;
  697. case V_CLARITY: // formantshape
  698. if (sscanf(p, "%d", &value) == 1) {
  699. if (value > 4) {
  700. voice->peak_shape = 1; // squarer formant peaks
  701. value = 4;
  702. }
  703. voice->n_harmonic_peaks = 1+value;
  704. }
  705. break;
  706. case V_TONE:
  707. {
  708. int tone_data[12];
  709. ReadTonePoints(p, tone_data);
  710. SetToneAdjust(voice, tone_data);
  711. }
  712. break;
  713. case V_VOICING:
  714. if (sscanf(p, "%d", &value) == 1)
  715. voice->voicing = (value * 64)/100;
  716. break;
  717. case V_BREATH:
  718. voice->breath[0] = Read8Numbers(p, &voice->breath[1]);
  719. for (ix = 1; ix < 8; ix++) {
  720. if (ix % 2)
  721. voice->breath[ix] = -voice->breath[ix];
  722. }
  723. break;
  724. case V_BREATHW:
  725. voice->breathw[0] = Read8Numbers(p, &voice->breathw[1]);
  726. break;
  727. case V_CONSONANTS:
  728. value = sscanf(p, "%d %d", &voice->consonant_amp, &voice->consonant_ampv);
  729. break;
  730. case V_SPEED:
  731. sscanf(p, "%d", &voice->speed_percent);
  732. break;
  733. case V_MBROLA:
  734. {
  735. int srate = 16000;
  736. name2[0] = 0;
  737. sscanf(p, "%s %s %d", name1, name2, &srate);
  738. espeak_ng_STATUS status = LoadMbrolaTable(name1, name2, &srate);
  739. if (status != ENS_OK)
  740. espeak_ng_PrintStatusCodeMessage(status, stderr, NULL);
  741. else
  742. voice->samplerate = srate;
  743. }
  744. break;
  745. case V_KLATT:
  746. voice->klattv[0] = 1; // default source: IMPULSIVE
  747. Read8Numbers(p, voice->klattv);
  748. voice->klattv[KLATT_Kopen] -= 40;
  749. break;
  750. case V_FAST:
  751. Read8Numbers(p, speed.fast_settings);
  752. SetSpeed(3);
  753. break;
  754. case V_DICTMIN:
  755. sscanf(p, "%d", &dict_min);
  756. break;
  757. case V_MAINTAINER:
  758. case V_STATUS:
  759. break;
  760. default:
  761. if ((key & 0xff00) == 0x100) {
  762. if (langopts)
  763. sscanf(p, "%d", &langopts->param[key &0xff]);
  764. else
  765. fprintf(stderr, "Cannot set voice attribute: language not set, or is invalid.\n");
  766. } else
  767. fprintf(stderr, "Bad voice attribute: %s\n", buf);
  768. break;
  769. }
  770. }
  771. if (f_voice != NULL)
  772. fclose(f_voice);
  773. if ((new_translator == NULL) && (!tone_only)) {
  774. // not set by language attribute
  775. new_translator = SelectTranslator(translator_name);
  776. }
  777. SetSpeed(3); // for speed_percent
  778. for (ix = 0; ix < N_PEAKS; ix++) {
  779. voice->freq2[ix] = voice->freq[ix];
  780. voice->height2[ix] = voice->height[ix];
  781. voice->width2[ix] = voice->width[ix];
  782. }
  783. if (tone_only)
  784. new_translator = translator;
  785. else {
  786. if ((ix = SelectPhonemeTableName(phonemes_name)) < 0) {
  787. fprintf(stderr, "Unknown phoneme table: '%s'\n", phonemes_name);
  788. ix = 0;
  789. }
  790. voice->phoneme_tab_ix = ix;
  791. new_translator->phoneme_tab_ix = ix;
  792. new_translator->dict_min_size = dict_min;
  793. LoadDictionary(new_translator, new_dictionary, control & 4);
  794. if (dictionary_name[0] == 0) {
  795. DeleteTranslator(new_translator);
  796. return NULL; // no dictionary loaded
  797. }
  798. new_translator->dict_condition = conditional_rules;
  799. voice_languages[langix] = 0;
  800. }
  801. langopts = &new_translator->langopts;
  802. if ((value = langopts->param[LOPT_LENGTH_MODS]) != 0)
  803. SetLengthMods(new_translator, value);
  804. voice->width[0] = (voice->width[0] * 105)/100;
  805. if (!tone_only)
  806. translator = new_translator;
  807. // relative lengths of different stress syllables
  808. for (ix = 0; ix < stress_lengths_set; ix++)
  809. translator->stress_lengths[ix] = stress_lengths[ix];
  810. for (ix = 0; ix < stress_add_set; ix++)
  811. translator->stress_lengths[ix] += stress_add[ix];
  812. for (ix = 0; ix < stress_amps_set; ix++) {
  813. translator->stress_amps[ix] = stress_amps[ix];
  814. translator->stress_amps_r[ix] = stress_amps[ix] -1;
  815. }
  816. return voice;
  817. }
  818. static char *ExtractVoiceVariantName(char *vname, int variant_num, int add_dir)
  819. {
  820. // Remove any voice variant suffix (name or number) from a voice name
  821. // Returns the voice variant name
  822. char *p;
  823. static char variant_name[40];
  824. char variant_prefix[5];
  825. variant_name[0] = 0;
  826. sprintf(variant_prefix, "!v%c", PATHSEP);
  827. if (add_dir == 0)
  828. variant_prefix[0] = 0;
  829. if (vname != NULL) {
  830. if ((p = strchr(vname, '+')) != NULL) {
  831. // The voice name has a +variant suffix
  832. variant_num = 0;
  833. *p++ = 0; // delete the suffix from the voice name
  834. if (IsDigit09(*p))
  835. variant_num = atoi(p); // variant number
  836. else {
  837. // voice variant name, not number
  838. sprintf(variant_name, "%s%s", variant_prefix, p);
  839. }
  840. }
  841. }
  842. if (variant_num > 0) {
  843. if (variant_num < 10)
  844. sprintf(variant_name, "%sm%d", variant_prefix, variant_num); // male
  845. else
  846. sprintf(variant_name, "%sf%d", variant_prefix, variant_num-10); // female
  847. }
  848. return variant_name;
  849. }
  850. voice_t *LoadVoiceVariant(const char *vname, int variant_num)
  851. {
  852. // Load a voice file.
  853. // Also apply a voice variant if specified by "variant", or by "+number" or "+name" in the "vname"
  854. voice_t *v;
  855. char *variant_name;
  856. char buf[60];
  857. strncpy0(buf, vname, sizeof(buf));
  858. variant_name = ExtractVoiceVariantName(buf, variant_num, 1);
  859. if ((v = LoadVoice(buf, 0)) == NULL)
  860. return NULL;
  861. if (variant_name[0] != 0)
  862. v = LoadVoice(variant_name, 2);
  863. return v;
  864. }
  865. static int __cdecl VoiceNameSorter(const void *p1, const void *p2)
  866. {
  867. int ix;
  868. espeak_VOICE *v1 = *(espeak_VOICE **)p1;
  869. espeak_VOICE *v2 = *(espeak_VOICE **)p2;
  870. if ((ix = strcmp(&v1->languages[1], &v2->languages[1])) != 0) // primary language name
  871. return ix;
  872. if ((ix = v1->languages[0] - v2->languages[0]) != 0) // priority number
  873. return ix;
  874. return strcmp(v1->name, v2->name);
  875. }
  876. static int __cdecl VoiceScoreSorter(const void *p1, const void *p2)
  877. {
  878. int ix;
  879. espeak_VOICE *v1 = *(espeak_VOICE **)p1;
  880. espeak_VOICE *v2 = *(espeak_VOICE **)p2;
  881. if ((ix = v2->score - v1->score) != 0)
  882. return ix;
  883. return strcmp(v1->name, v2->name);
  884. }
  885. static int ScoreVoice(espeak_VOICE *voice_spec, const char *spec_language, int spec_n_parts, int spec_lang_len, espeak_VOICE *voice)
  886. {
  887. int ix;
  888. const char *p;
  889. int c1, c2;
  890. int language_priority;
  891. int n_parts;
  892. int matching;
  893. int matching_parts;
  894. int score = 0;
  895. int x;
  896. int ratio;
  897. int required_age;
  898. int diff;
  899. p = voice->languages; // list of languages+dialects for which this voice is suitable
  900. if (spec_n_parts < 0) {
  901. // match on the subdirectory
  902. if (memcmp(voice->identifier, spec_language, spec_lang_len) == 0)
  903. return 100;
  904. return 0;
  905. }
  906. if (spec_n_parts == 0)
  907. score = 100;
  908. else {
  909. if ((*p == 0) && (strcmp(spec_language, "variants") == 0)) {
  910. // match on a voice with no languages if the required language is "variants"
  911. score = 100;
  912. }
  913. // compare the required language with each of the languages of this voice
  914. while (*p != 0) {
  915. language_priority = *p++;
  916. matching = 1;
  917. matching_parts = 0;
  918. n_parts = 1;
  919. for (ix = 0;; ix++) {
  920. if ((ix >= spec_lang_len) || ((c1 = spec_language[ix]) == '-'))
  921. c1 = 0;
  922. if ((c2 = p[ix]) == '-')
  923. c2 = 0;
  924. if (c1 != c2)
  925. matching = 0;
  926. if (p[ix] == '-') {
  927. n_parts++;
  928. if (matching)
  929. matching_parts++;
  930. }
  931. if (p[ix] == 0)
  932. break;
  933. }
  934. p += (ix+1);
  935. matching_parts += matching; // number of parts which match
  936. if (matching_parts == 0)
  937. continue; // no matching parts for this language
  938. x = 5;
  939. // reduce the score if not all parts of the required language match
  940. if ((diff = (spec_n_parts - matching_parts)) > 0)
  941. x -= diff;
  942. // reduce score if the language is more specific than required
  943. if ((diff = (n_parts - matching_parts)) > 0)
  944. x -= diff;
  945. x = x*100 - (language_priority * 2);
  946. if (x > score)
  947. score = x;
  948. }
  949. }
  950. if (score == 0)
  951. return 0;
  952. if (voice_spec->name != NULL) {
  953. if (strcmp(voice_spec->name, voice->name) == 0) {
  954. // match on voice name
  955. score += 500;
  956. } else if (strcmp(voice_spec->name, voice->identifier) == 0)
  957. score += 400;
  958. }
  959. if (((voice_spec->gender == ENGENDER_MALE) || (voice_spec->gender == ENGENDER_FEMALE)) &&
  960. ((voice->gender == ENGENDER_MALE) || (voice->gender == ENGENDER_FEMALE))) {
  961. if (voice_spec->gender == voice->gender)
  962. score += 50;
  963. else
  964. score -= 50;
  965. }
  966. if ((voice_spec->age <= 12) && (voice->gender == ENGENDER_FEMALE) && (voice->age > 12))
  967. score += 5; // give some preference for non-child female voice if a child is requested
  968. if (voice->age != 0) {
  969. if (voice_spec->age == 0)
  970. required_age = 30;
  971. else
  972. required_age = voice_spec->age;
  973. ratio = (required_age*100)/voice->age;
  974. if (ratio < 100)
  975. ratio = 10000/ratio;
  976. ratio = (ratio - 100)/10; // 0=exact match, 10=out by factor of 2
  977. x = 5 - ratio;
  978. if (x > 0) x = 0;
  979. score = score + x;
  980. if (voice_spec->age > 0)
  981. score += 10; // required age specified, favour voices with a specified age (near it)
  982. }
  983. if (score < 1)
  984. score = 1;
  985. return score;
  986. }
  987. static int SetVoiceScores(espeak_VOICE *voice_select, espeak_VOICE **voices, int control)
  988. {
  989. // control: bit0=1 include mbrola voices
  990. int ix;
  991. int score;
  992. int nv; // number of candidates
  993. int n_parts = 0;
  994. int lang_len = 0;
  995. espeak_VOICE *vp;
  996. char language[80];
  997. char buf[sizeof(path_home)+80];
  998. // count number of parts in the specified language
  999. if ((voice_select->languages != NULL) && (voice_select->languages[0] != 0)) {
  1000. n_parts = 1;
  1001. lang_len = strlen(voice_select->languages);
  1002. for (ix = 0; (ix <= lang_len) && ((unsigned)ix < sizeof(language)); ix++) {
  1003. if ((language[ix] = tolower(voice_select->languages[ix])) == '-')
  1004. n_parts++;
  1005. }
  1006. }
  1007. if ((n_parts == 1) && (control & 1)) {
  1008. if (strcmp(language, "mbrola") == 0) {
  1009. language[2] = 0; // truncate to "mb"
  1010. lang_len = 2;
  1011. }
  1012. sprintf(buf, "%s/voices/%s", path_home, language);
  1013. if (GetFileLength(buf) == -EISDIR) {
  1014. // A subdirectory name has been specified. List all the voices in that subdirectory
  1015. language[lang_len++] = PATHSEP;
  1016. language[lang_len] = 0;
  1017. n_parts = -1;
  1018. }
  1019. }
  1020. // select those voices which match the specified language
  1021. nv = 0;
  1022. for (ix = 0; ix < n_voices_list; ix++) {
  1023. vp = voices_list[ix];
  1024. if (((control & 1) == 0) && (memcmp(vp->identifier, "mb/", 3) == 0))
  1025. continue;
  1026. if ((score = ScoreVoice(voice_select, language, n_parts, lang_len, voices_list[ix])) > 0) {
  1027. voices[nv++] = vp;
  1028. vp->score = score;
  1029. }
  1030. }
  1031. voices[nv] = NULL; // list terminator
  1032. if (nv == 0)
  1033. return 0;
  1034. // sort the selected voices by their score
  1035. qsort(voices, nv, sizeof(espeak_VOICE *), (int(__cdecl *)(const void *, const void *))VoiceScoreSorter);
  1036. return nv;
  1037. }
  1038. espeak_VOICE *SelectVoiceByName(espeak_VOICE **voices, const char *name2)
  1039. {
  1040. int ix;
  1041. int match_fname = -1;
  1042. int match_fname2 = -1;
  1043. int match_name = -1;
  1044. const char *id; // this is the filename within espeak-ng-data/voices
  1045. char *variant_name;
  1046. int last_part_len;
  1047. char last_part[41];
  1048. char name[40];
  1049. if (voices == NULL) {
  1050. if (n_voices_list == 0)
  1051. espeak_ListVoices(NULL); // create the voices list
  1052. voices = voices_list;
  1053. }
  1054. strncpy0(name, name2, sizeof(name));
  1055. if ((variant_name = strchr(name, '+')) != NULL) {
  1056. *variant_name = 0;
  1057. variant_name++;
  1058. }
  1059. sprintf(last_part, "%c%s", PATHSEP, name);
  1060. last_part_len = strlen(last_part);
  1061. for (ix = 0; voices[ix] != NULL; ix++) {
  1062. if (strcasecmp(name, voices[ix]->name) == 0) {
  1063. match_name = ix; // found matching voice name
  1064. break;
  1065. } else {
  1066. id = voices[ix]->identifier;
  1067. if (strcasecmp(name, id) == 0)
  1068. match_fname = ix; // matching identifier, use this if no matching name
  1069. else if (strcasecmp(last_part, &id[strlen(id)-last_part_len]) == 0)
  1070. match_fname2 = ix;
  1071. }
  1072. }
  1073. if (match_name < 0) {
  1074. match_name = match_fname; // no matching name, try matching filename
  1075. if (match_name < 0)
  1076. match_name = match_fname2; // try matching just the last part of the filename
  1077. }
  1078. if (match_name < 0)
  1079. return NULL;
  1080. return voices[match_name];
  1081. }
  1082. char const *SelectVoice(espeak_VOICE *voice_select, int *found)
  1083. {
  1084. // Returns a path within espeak-voices, with a possible +variant suffix
  1085. // variant is an output-only parameter
  1086. int nv; // number of candidates
  1087. int ix, ix2;
  1088. int j;
  1089. int n_variants;
  1090. int variant_number;
  1091. int gender;
  1092. int skip;
  1093. int aged = 1;
  1094. char *variant_name;
  1095. const char *p, *p_start;
  1096. espeak_VOICE *vp = NULL;
  1097. espeak_VOICE *vp2;
  1098. espeak_VOICE voice_select2;
  1099. espeak_VOICE *voices[N_VOICES_LIST]; // list of candidates
  1100. espeak_VOICE *voices2[N_VOICES_LIST+N_VOICE_VARIANTS];
  1101. static espeak_VOICE voice_variants[N_VOICE_VARIANTS];
  1102. static char voice_id[50];
  1103. *found = 1;
  1104. memcpy(&voice_select2, voice_select, sizeof(voice_select2));
  1105. if (n_voices_list == 0)
  1106. espeak_ListVoices(NULL); // create the voices list
  1107. if ((voice_select2.languages == NULL) || (voice_select2.languages[0] == 0)) {
  1108. // no language is specified. Get language from the named voice
  1109. static char buf[60];
  1110. if (voice_select2.name == NULL) {
  1111. if ((voice_select2.name = voice_select2.identifier) == NULL)
  1112. voice_select2.name = ESPEAKNG_DEFAULT_VOICE;
  1113. }
  1114. strncpy0(buf, voice_select2.name, sizeof(buf));
  1115. variant_name = ExtractVoiceVariantName(buf, 0, 0);
  1116. vp = SelectVoiceByName(voices_list, buf);
  1117. if (vp != NULL) {
  1118. voice_select2.languages = &(vp->languages[1]);
  1119. if ((voice_select2.gender == ENGENDER_UNKNOWN) && (voice_select2.age == 0) && (voice_select2.variant == 0)) {
  1120. if (variant_name[0] != 0) {
  1121. sprintf(voice_id, "%s+%s", vp->identifier, variant_name);
  1122. return voice_id;
  1123. }
  1124. return vp->identifier;
  1125. }
  1126. }
  1127. }
  1128. // select and sort voices for the required language
  1129. nv = SetVoiceScores(&voice_select2, voices, 0);
  1130. if (nv == 0) {
  1131. // no matching voice, choose the default
  1132. *found = 0;
  1133. if ((voices[0] = SelectVoiceByName(voices_list, ESPEAKNG_DEFAULT_VOICE)) != NULL)
  1134. nv = 1;
  1135. }
  1136. gender = 0;
  1137. if ((voice_select2.gender == ENGENDER_FEMALE) || ((voice_select2.age > 0) && (voice_select2.age < 13)))
  1138. gender = ENGENDER_FEMALE;
  1139. else if (voice_select2.gender == ENGENDER_MALE)
  1140. gender = ENGENDER_MALE;
  1141. #define AGE_OLD 60
  1142. if (voice_select2.age < AGE_OLD)
  1143. aged = 0;
  1144. p = p_start = variant_lists[gender];
  1145. if (aged == 0)
  1146. p++; // the first voice in the variants list is older
  1147. // add variants for the top voices
  1148. n_variants = 0;
  1149. for (ix = 0, ix2 = 0; ix < nv; ix++) {
  1150. vp = voices[ix];
  1151. // is the main voice the required gender?
  1152. skip = 0;
  1153. if ((gender != ENGENDER_UNKNOWN) && (vp->gender != gender))
  1154. skip = 1;
  1155. if ((ix2 == 0) && aged && (vp->age < AGE_OLD))
  1156. skip = 1;
  1157. if (skip == 0)
  1158. voices2[ix2++] = vp;
  1159. for (j = 0; (j < vp->xx1) && (n_variants < N_VOICE_VARIANTS);) {
  1160. if ((variant_number = *p) == 0) {
  1161. p = p_start;
  1162. continue;
  1163. }
  1164. vp2 = &voice_variants[n_variants++]; // allocate space for voice variant
  1165. memcpy(vp2, vp, sizeof(espeak_VOICE)); // copy from the original voice
  1166. vp2->variant = variant_number;
  1167. voices2[ix2++] = vp2;
  1168. p++;
  1169. j++;
  1170. }
  1171. }
  1172. // add any more variants to the end of the list
  1173. while ((vp != NULL) && ((variant_number = *p++) != 0) && (n_variants < N_VOICE_VARIANTS)) {
  1174. vp2 = &voice_variants[n_variants++]; // allocate space for voice variant
  1175. memcpy(vp2, vp, sizeof(espeak_VOICE)); // copy from the original voice
  1176. vp2->variant = variant_number;
  1177. voices2[ix2++] = vp2;
  1178. }
  1179. // index the sorted list by the required variant number
  1180. if (ix2 == 0)
  1181. return NULL;
  1182. vp = voices2[voice_select2.variant % ix2];
  1183. if (vp->variant != 0) {
  1184. variant_name = ExtractVoiceVariantName(NULL, vp->variant, 0);
  1185. sprintf(voice_id, "%s+%s", vp->identifier, variant_name);
  1186. return voice_id;
  1187. }
  1188. return vp->identifier;
  1189. }
  1190. static void GetVoices(const char *path, int len_path_voices, int is_language_file)
  1191. {
  1192. FILE *f_voice;
  1193. espeak_VOICE *voice_data;
  1194. int ftype;
  1195. char fname[sizeof(path_home)+100];
  1196. #ifdef PLATFORM_WINDOWS
  1197. WIN32_FIND_DATAA FindFileData;
  1198. HANDLE hFind = INVALID_HANDLE_VALUE;
  1199. #undef UNICODE // we need FindFirstFileA() which takes an 8-bit c-string
  1200. sprintf(fname, "%s\\*", path);
  1201. hFind = FindFirstFileA(fname, &FindFileData);
  1202. if (hFind == INVALID_HANDLE_VALUE)
  1203. return;
  1204. do {
  1205. if (n_voices_list >= (N_VOICES_LIST-2))
  1206. break; // voices list is full
  1207. if (FindFileData.cFileName[0] != '.') {
  1208. sprintf(fname, "%s%c%s", path, PATHSEP, FindFileData.cFileName);
  1209. ftype = GetFileLength(fname);
  1210. if (ftype == -EISDIR) {
  1211. // a sub-directory
  1212. GetVoices(fname, len_path_voices, is_language_file);
  1213. } else if (ftype > 0) {
  1214. // a regular file, add it to the voices list
  1215. if ((f_voice = fopen(fname, "r")) == NULL)
  1216. continue;
  1217. // pass voice file name within the voices directory
  1218. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, is_language_file);
  1219. fclose(f_voice);
  1220. if (voice_data != NULL)
  1221. voices_list[n_voices_list++] = voice_data;
  1222. }
  1223. }
  1224. } while (FindNextFileA(hFind, &FindFileData) != 0);
  1225. FindClose(hFind);
  1226. #else
  1227. DIR *dir;
  1228. struct dirent *ent;
  1229. if ((dir = opendir((char *)path)) == NULL) // note: (char *) is needed for WINCE
  1230. return;
  1231. while ((ent = readdir(dir)) != NULL) {
  1232. if (n_voices_list >= (N_VOICES_LIST-2))
  1233. break; // voices list is full
  1234. if (ent->d_name[0] == '.')
  1235. continue;
  1236. sprintf(fname, "%s%c%s", path, PATHSEP, ent->d_name);
  1237. ftype = GetFileLength(fname);
  1238. if (ftype == -EISDIR) {
  1239. // a sub-directory
  1240. GetVoices(fname, len_path_voices, is_language_file);
  1241. } else if (ftype > 0) {
  1242. // a regular file, add it to the voices list
  1243. if ((f_voice = fopen(fname, "r")) == NULL)
  1244. continue;
  1245. // pass voice file name within the voices directory
  1246. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, is_language_file);
  1247. fclose(f_voice);
  1248. if (voice_data != NULL)
  1249. voices_list[n_voices_list++] = voice_data;
  1250. }
  1251. }
  1252. closedir(dir);
  1253. #endif
  1254. }
  1255. #pragma GCC visibility push(default)
  1256. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SetVoiceByName(const char *name)
  1257. {
  1258. espeak_VOICE *v;
  1259. int ix;
  1260. espeak_VOICE voice_selector;
  1261. char *variant_name;
  1262. static char buf[60];
  1263. strncpy0(buf, name, sizeof(buf));
  1264. variant_name = ExtractVoiceVariantName(buf, 0, 1);
  1265. for (ix = 0;; ix++) {
  1266. // convert voice name to lower case (ascii)
  1267. if ((buf[ix] = tolower(buf[ix])) == 0)
  1268. break;
  1269. }
  1270. memset(&voice_selector, 0, sizeof(voice_selector));
  1271. voice_selector.name = (char *)name; // include variant name in voice stack ??
  1272. // first check for a voice with this filename
  1273. // This may avoid the need to call espeak_ListVoices().
  1274. if (LoadVoice(buf, 1) != NULL) {
  1275. if (variant_name[0] != 0)
  1276. LoadVoice(variant_name, 2);
  1277. DoVoiceChange(voice);
  1278. voice_selector.languages = voice->language_name;
  1279. SetVoiceStack(&voice_selector, variant_name);
  1280. return ENS_OK;
  1281. }
  1282. if (n_voices_list == 0)
  1283. espeak_ListVoices(NULL); // create the voices list
  1284. if ((v = SelectVoiceByName(voices_list, buf)) != NULL) {
  1285. if (LoadVoice(v->identifier, 0) != 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. }
  1294. return ENS_VOICE_NOT_FOUND;
  1295. }
  1296. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SetVoiceByProperties(espeak_VOICE *voice_selector)
  1297. {
  1298. const char *voice_id;
  1299. int voice_found;
  1300. voice_id = SelectVoice(voice_selector, &voice_found);
  1301. if (voice_found == 0)
  1302. return ENS_VOICE_NOT_FOUND;
  1303. LoadVoiceVariant(voice_id, 0);
  1304. DoVoiceChange(voice);
  1305. SetVoiceStack(voice_selector, "");
  1306. return ENS_OK;
  1307. }
  1308. #pragma GCC visibility pop
  1309. void FreeVoiceList()
  1310. {
  1311. int ix;
  1312. for (ix = 0; ix < n_voices_list; ix++) {
  1313. if (voices_list[ix] != NULL) {
  1314. free(voices_list[ix]);
  1315. voices_list[ix] = NULL;
  1316. }
  1317. }
  1318. n_voices_list = 0;
  1319. }
  1320. #pragma GCC visibility push(default)
  1321. ESPEAK_API const espeak_VOICE **espeak_ListVoices(espeak_VOICE *voice_spec)
  1322. {
  1323. char path_voices[sizeof(path_home)+12];
  1324. int ix;
  1325. int j;
  1326. espeak_VOICE *v;
  1327. static espeak_VOICE **voices = NULL;
  1328. // free previous voice list data
  1329. FreeVoiceList();
  1330. sprintf(path_voices, "%s%cvoices", path_home, PATHSEP);
  1331. GetVoices(path_voices, strlen(path_voices)+1, 0);
  1332. sprintf(path_voices, "%s%clang", path_home, PATHSEP);
  1333. GetVoices(path_voices, strlen(path_voices)+1, 1);
  1334. voices_list[n_voices_list] = NULL; // voices list terminator
  1335. espeak_VOICE **new_voices = (espeak_VOICE **)realloc(voices, sizeof(espeak_VOICE *)*(n_voices_list+1));
  1336. if (new_voices == NULL)
  1337. return (const espeak_VOICE **)voices;
  1338. voices = new_voices;
  1339. // sort the voices list
  1340. qsort(voices_list, n_voices_list, sizeof(espeak_VOICE *),
  1341. (int(__cdecl *)(const void *, const void *))VoiceNameSorter);
  1342. if (voice_spec) {
  1343. // select the voices which match the voice_spec, and sort them by preference
  1344. SetVoiceScores(voice_spec, voices, 1);
  1345. } else {
  1346. // list all: omit variant and mbrola voices
  1347. j = 0;
  1348. for (ix = 0; (v = voices_list[ix]) != NULL; ix++) {
  1349. if ((v->languages[0] != 0) && (strcmp(&v->languages[1], "variant") != 0)
  1350. && (memcmp(v->identifier, "mb/", 3) != 0))
  1351. voices[j++] = v;
  1352. }
  1353. voices[j] = NULL;
  1354. }
  1355. return (const espeak_VOICE **)voices;
  1356. }
  1357. ESPEAK_API espeak_VOICE *espeak_GetCurrentVoice(void)
  1358. {
  1359. return &current_voice_selected;
  1360. }
  1361. #pragma GCC visibility pop