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