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

voices.c 42KB

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