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