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