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

voices.c 41KB

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