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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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 "speech.h"
  37. #include "phoneme.h"
  38. #include "voice.h"
  39. #include "synthesize.h"
  40. #include "translate.h"
  41. MNEM_TAB genders[] = {
  42. { "male", ENGENDER_MALE },
  43. { "female", ENGENDER_FEMALE },
  44. { NULL, ENGENDER_MALE }
  45. };
  46. int tone_points[12] = { 600, 170, 1200, 135, 2000, 110, 3000, 110, -1, 0 };
  47. // limit the rate of change for each formant number
  48. static int formant_rate_22050[9] = { 240, 170, 170, 170, 170, 170, 170, 170, 170 }; // values for 22kHz sample rate
  49. int formant_rate[9]; // values adjusted for actual sample rate
  50. #define DEFAULT_LANGUAGE_PRIORITY 5
  51. #define N_VOICES_LIST 250
  52. static int n_voices_list = 0;
  53. static espeak_VOICE *voices_list[N_VOICES_LIST];
  54. 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. V_VARIANTS,
  63. V_MAINTAINER,
  64. V_STATUS,
  65. // these affect voice quality, are independent of language
  66. V_FORMANT,
  67. V_PITCH,
  68. V_ECHO,
  69. V_FLUTTER,
  70. V_ROUGHNESS,
  71. V_CLARITY,
  72. V_TONE,
  73. V_VOICING,
  74. V_BREATH,
  75. V_BREATHW,
  76. // these override defaults set by the translator
  77. V_WORDGAP,
  78. V_INTONATION,
  79. V_TUNES,
  80. V_STRESSLENGTH,
  81. V_STRESSAMP,
  82. V_STRESSADD,
  83. V_DICTRULES,
  84. V_STRESSRULE,
  85. V_STRESSOPT,
  86. V_NUMBERS,
  87. V_OPTION,
  88. V_MBROLA,
  89. V_KLATT,
  90. V_FAST,
  91. V_SPEED,
  92. V_DICTMIN,
  93. V_ALPHABET2,
  94. // these need a phoneme table to have been specified
  95. V_REPLACE,
  96. V_CONSONANTS,
  97. // these are alpha features that need to be tested and categorized
  98. V_LETTER_VOWEL
  99. };
  100. static MNEM_TAB options_tab[] = {
  101. { "reduce_t", LOPT_REDUCE_T },
  102. { "bracket", LOPT_BRACKET_PAUSE },
  103. { NULL, -1 }
  104. };
  105. static MNEM_TAB keyword_tab[] = {
  106. { "name", V_NAME },
  107. { "language", V_LANGUAGE },
  108. { "gender", V_GENDER },
  109. { "maintainer", V_MAINTAINER },
  110. { "status", V_STATUS },
  111. { "variants", V_VARIANTS },
  112. { "formant", V_FORMANT },
  113. { "pitch", V_PITCH },
  114. { "phonemes", V_PHONEMES },
  115. { "translator", V_TRANSLATOR },
  116. { "dictionary", V_DICTIONARY },
  117. { "stressLength", V_STRESSLENGTH },
  118. { "stressAmp", V_STRESSAMP },
  119. { "stressAdd", V_STRESSADD },
  120. { "intonation", V_INTONATION },
  121. { "tunes", V_TUNES },
  122. { "dictrules", V_DICTRULES },
  123. { "stressRule", V_STRESSRULE },
  124. { "stressopt", V_STRESSOPT },
  125. { "replace", V_REPLACE },
  126. { "words", V_WORDGAP },
  127. { "echo", V_ECHO },
  128. { "flutter", V_FLUTTER },
  129. { "roughness", V_ROUGHNESS },
  130. { "clarity", V_CLARITY },
  131. { "tone", V_TONE },
  132. { "voicing", V_VOICING },
  133. { "breath", V_BREATH },
  134. { "breathw", V_BREATHW },
  135. { "numbers", V_NUMBERS },
  136. { "option", V_OPTION },
  137. { "mbrola", V_MBROLA },
  138. { "consonants", V_CONSONANTS },
  139. { "klatt", V_KLATT },
  140. { "fast_test2", V_FAST },
  141. { "speed", V_SPEED },
  142. { "dict_min", V_DICTMIN },
  143. // these just set a value in langopts.param[]
  144. { "l_dieresis", 0x100+LOPT_DIERESES },
  145. { "l_prefix", 0x100+LOPT_PREFIXES },
  146. { "l_regressive_v", 0x100+LOPT_REGRESSIVE_VOICING },
  147. { "l_unpronouncable", 0x100+LOPT_UNPRONOUNCABLE },
  148. { "l_sonorant_min", 0x100+LOPT_SONORANT_MIN },
  149. { "l_length_mods", 0x100+LOPT_LENGTH_MODS },
  150. { "apostrophe", 0x100+LOPT_APOSTROPHE },
  151. // these are alpha features that need to be tested and categorized
  152. { "letterVowel", V_LETTER_VOWEL },
  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] = 450;
  331. speed.fast_settings[1] = 800;
  332. speed.fast_settings[2] = 175;
  333. voice->roughness = 2;
  334. InitBreath();
  335. for (pk = 0; pk < N_PEAKS; pk++) {
  336. voice->freq[pk] = 256;
  337. voice->height[pk] = default_heights[pk]*2;
  338. voice->width[pk] = default_widths[pk]*2;
  339. voice->breath[pk] = 0;
  340. voice->breathw[pk] = breath_widths[pk]; // default breath formant 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 %d", &option_tone_flags, &option_tone2);
  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. case V_LETTER_VOWEL: {
  755. char str[5] = "";
  756. char c = '0';
  757. char *endptr = NULL;
  758. sscanf(p, "%s", str);
  759. // assume a hex value if string starts with "0x"
  760. if (str[0] == '0' && str[1] == 'x') {
  761. c = strtoul(str, &endptr, 16);
  762. if (errno == ERANGE)
  763. fprintf(stderr, "letterVowel out of range.\n");
  764. }
  765. else {// otherwise, assume a single letter
  766. c = str[0];
  767. if (c < 97 || c > 122) // valid values are a-z, ascii 97-122
  768. fprintf(stderr, "letterVowel out of range.\n");
  769. }
  770. new_translator->letter_bits[c] = (new_translator->letter_bits[c] & 0x40) | 0x81; // keep value for group 6 (front vowels e,i,y)
  771. break;
  772. }
  773. default:
  774. if ((key & 0xff00) == 0x100) {
  775. if (langopts)
  776. sscanf(p, "%d", &langopts->param[key &0xff]);
  777. else
  778. fprintf(stderr, "Cannot set voice attribute: language not set, or is invalid.\n");
  779. } else
  780. fprintf(stderr, "Bad voice attribute: %s\n", buf);
  781. break;
  782. }
  783. }
  784. if (f_voice != NULL)
  785. fclose(f_voice);
  786. if ((new_translator == NULL) && (!tone_only)) {
  787. // not set by language attribute
  788. new_translator = SelectTranslator(translator_name);
  789. }
  790. SetSpeed(3); // for speed_percent
  791. for (ix = 0; ix < N_PEAKS; ix++) {
  792. voice->freq2[ix] = voice->freq[ix];
  793. voice->height2[ix] = voice->height[ix];
  794. voice->width2[ix] = voice->width[ix];
  795. }
  796. if (tone_only)
  797. new_translator = translator;
  798. else {
  799. if ((ix = SelectPhonemeTableName(phonemes_name)) < 0) {
  800. fprintf(stderr, "Unknown phoneme table: '%s'\n", phonemes_name);
  801. ix = 0;
  802. }
  803. voice->phoneme_tab_ix = ix;
  804. new_translator->phoneme_tab_ix = ix;
  805. new_translator->dict_min_size = dict_min;
  806. LoadDictionary(new_translator, new_dictionary, control & 4);
  807. if (dictionary_name[0] == 0) {
  808. DeleteTranslator(new_translator);
  809. return NULL; // no dictionary loaded
  810. }
  811. new_translator->dict_condition = conditional_rules;
  812. voice_languages[langix] = 0;
  813. }
  814. langopts = &new_translator->langopts;
  815. if ((value = langopts->param[LOPT_LENGTH_MODS]) != 0)
  816. SetLengthMods(new_translator, value);
  817. voice->width[0] = (voice->width[0] * 105)/100;
  818. if (!tone_only)
  819. translator = new_translator;
  820. // relative lengths of different stress syllables
  821. for (ix = 0; ix < stress_lengths_set; ix++)
  822. translator->stress_lengths[ix] = stress_lengths[ix];
  823. for (ix = 0; ix < stress_add_set; ix++)
  824. translator->stress_lengths[ix] += stress_add[ix];
  825. for (ix = 0; ix < stress_amps_set; ix++) {
  826. translator->stress_amps[ix] = stress_amps[ix];
  827. translator->stress_amps_r[ix] = stress_amps[ix] -1;
  828. }
  829. return voice;
  830. }
  831. static char *ExtractVoiceVariantName(char *vname, int variant_num, int add_dir)
  832. {
  833. // Remove any voice variant suffix (name or number) from a voice name
  834. // Returns the voice variant name
  835. char *p;
  836. static char variant_name[40];
  837. char variant_prefix[5];
  838. variant_name[0] = 0;
  839. sprintf(variant_prefix, "!v%c", PATHSEP);
  840. if (add_dir == 0)
  841. variant_prefix[0] = 0;
  842. if (vname != NULL) {
  843. if ((p = strchr(vname, '+')) != NULL) {
  844. // The voice name has a +variant suffix
  845. variant_num = 0;
  846. *p++ = 0; // delete the suffix from the voice name
  847. if (IsDigit09(*p))
  848. variant_num = atoi(p); // variant number
  849. else {
  850. // voice variant name, not number
  851. sprintf(variant_name, "%s%s", variant_prefix, p);
  852. }
  853. }
  854. }
  855. if (variant_num > 0) {
  856. if (variant_num < 10)
  857. sprintf(variant_name, "%sm%d", variant_prefix, variant_num); // male
  858. else
  859. sprintf(variant_name, "%sf%d", variant_prefix, variant_num-10); // female
  860. }
  861. return variant_name;
  862. }
  863. voice_t *LoadVoiceVariant(const char *vname, int variant_num)
  864. {
  865. // Load a voice file.
  866. // Also apply a voice variant if specified by "variant", or by "+number" or "+name" in the "vname"
  867. voice_t *v;
  868. char *variant_name;
  869. char buf[60];
  870. strncpy0(buf, vname, sizeof(buf));
  871. variant_name = ExtractVoiceVariantName(buf, variant_num, 1);
  872. if ((v = LoadVoice(buf, 0)) == NULL)
  873. return NULL;
  874. if (variant_name[0] != 0)
  875. v = LoadVoice(variant_name, 2);
  876. return v;
  877. }
  878. static int __cdecl VoiceNameSorter(const void *p1, const void *p2)
  879. {
  880. int ix;
  881. espeak_VOICE *v1 = *(espeak_VOICE **)p1;
  882. espeak_VOICE *v2 = *(espeak_VOICE **)p2;
  883. if ((ix = strcmp(&v1->languages[1], &v2->languages[1])) != 0) // primary language name
  884. return ix;
  885. if ((ix = v1->languages[0] - v2->languages[0]) != 0) // priority number
  886. return ix;
  887. return strcmp(v1->name, v2->name);
  888. }
  889. static int __cdecl VoiceScoreSorter(const void *p1, const void *p2)
  890. {
  891. int ix;
  892. espeak_VOICE *v1 = *(espeak_VOICE **)p1;
  893. espeak_VOICE *v2 = *(espeak_VOICE **)p2;
  894. if ((ix = v2->score - v1->score) != 0)
  895. return ix;
  896. return strcmp(v1->name, v2->name);
  897. }
  898. static int ScoreVoice(espeak_VOICE *voice_spec, const char *spec_language, int spec_n_parts, int spec_lang_len, espeak_VOICE *voice)
  899. {
  900. int ix;
  901. const char *p;
  902. int c1, c2;
  903. int language_priority;
  904. int n_parts;
  905. int matching;
  906. int matching_parts;
  907. int score = 0;
  908. int x;
  909. int ratio;
  910. int required_age;
  911. int diff;
  912. p = voice->languages; // list of languages+dialects for which this voice is suitable
  913. if (spec_n_parts < 0) {
  914. // match on the subdirectory
  915. if (memcmp(voice->identifier, spec_language, spec_lang_len) == 0)
  916. return 100;
  917. return 0;
  918. }
  919. if (spec_n_parts == 0)
  920. score = 100;
  921. else {
  922. if ((*p == 0) && (strcmp(spec_language, "variants") == 0)) {
  923. // match on a voice with no languages if the required language is "variants"
  924. score = 100;
  925. }
  926. // compare the required language with each of the languages of this voice
  927. while (*p != 0) {
  928. language_priority = *p++;
  929. matching = 1;
  930. matching_parts = 0;
  931. n_parts = 1;
  932. for (ix = 0;; ix++) {
  933. if ((ix >= spec_lang_len) || ((c1 = spec_language[ix]) == '-'))
  934. c1 = 0;
  935. if ((c2 = p[ix]) == '-')
  936. c2 = 0;
  937. if (c1 != c2)
  938. matching = 0;
  939. if (p[ix] == '-') {
  940. n_parts++;
  941. if (matching)
  942. matching_parts++;
  943. }
  944. if (p[ix] == 0)
  945. break;
  946. }
  947. p += (ix+1);
  948. matching_parts += matching; // number of parts which match
  949. if (matching_parts == 0)
  950. continue; // no matching parts for this language
  951. x = 5;
  952. // reduce the score if not all parts of the required language match
  953. if ((diff = (spec_n_parts - matching_parts)) > 0)
  954. x -= diff;
  955. // reduce score if the language is more specific than required
  956. if ((diff = (n_parts - matching_parts)) > 0)
  957. x -= diff;
  958. x = x*100 - (language_priority * 2);
  959. if (x > score)
  960. score = x;
  961. }
  962. }
  963. if (score == 0)
  964. return 0;
  965. if (voice_spec->name != NULL) {
  966. if (strcmp(voice_spec->name, voice->name) == 0) {
  967. // match on voice name
  968. score += 500;
  969. } else if (strcmp(voice_spec->name, voice->identifier) == 0)
  970. score += 400;
  971. }
  972. if (((voice_spec->gender == ENGENDER_MALE) || (voice_spec->gender == ENGENDER_FEMALE)) &&
  973. ((voice->gender == ENGENDER_MALE) || (voice->gender == ENGENDER_FEMALE))) {
  974. if (voice_spec->gender == voice->gender)
  975. score += 50;
  976. else
  977. score -= 50;
  978. }
  979. if ((voice_spec->age <= 12) && (voice->gender == ENGENDER_FEMALE) && (voice->age > 12))
  980. score += 5; // give some preference for non-child female voice if a child is requested
  981. if (voice->age != 0) {
  982. if (voice_spec->age == 0)
  983. required_age = 30;
  984. else
  985. required_age = voice_spec->age;
  986. ratio = (required_age*100)/voice->age;
  987. if (ratio < 100)
  988. ratio = 10000/ratio;
  989. ratio = (ratio - 100)/10; // 0=exact match, 10=out by factor of 2
  990. x = 5 - ratio;
  991. if (x > 0) x = 0;
  992. score = score + x;
  993. if (voice_spec->age > 0)
  994. score += 10; // required age specified, favour voices with a specified age (near it)
  995. }
  996. if (score < 1)
  997. score = 1;
  998. return score;
  999. }
  1000. static int SetVoiceScores(espeak_VOICE *voice_select, espeak_VOICE **voices, int control)
  1001. {
  1002. // control: bit0=1 include mbrola voices
  1003. int ix;
  1004. int score;
  1005. int nv; // number of candidates
  1006. int n_parts = 0;
  1007. int lang_len = 0;
  1008. espeak_VOICE *vp;
  1009. char language[80];
  1010. char buf[sizeof(path_home)+80];
  1011. // count number of parts in the specified language
  1012. if ((voice_select->languages != NULL) && (voice_select->languages[0] != 0)) {
  1013. n_parts = 1;
  1014. lang_len = strlen(voice_select->languages);
  1015. for (ix = 0; (ix <= lang_len) && ((unsigned)ix < sizeof(language)); ix++) {
  1016. if ((language[ix] = tolower(voice_select->languages[ix])) == '-')
  1017. n_parts++;
  1018. }
  1019. }
  1020. if ((n_parts == 1) && (control & 1)) {
  1021. if (strcmp(language, "mbrola") == 0) {
  1022. language[2] = 0; // truncate to "mb"
  1023. lang_len = 2;
  1024. }
  1025. sprintf(buf, "%s/voices/%s", path_home, language);
  1026. if (GetFileLength(buf) == -EISDIR) {
  1027. // A subdirectory name has been specified. List all the voices in that subdirectory
  1028. language[lang_len++] = PATHSEP;
  1029. language[lang_len] = 0;
  1030. n_parts = -1;
  1031. }
  1032. }
  1033. // select those voices which match the specified language
  1034. nv = 0;
  1035. for (ix = 0; ix < n_voices_list; ix++) {
  1036. vp = voices_list[ix];
  1037. if (((control & 1) == 0) && (memcmp(vp->identifier, "mb/", 3) == 0))
  1038. continue;
  1039. if ((score = ScoreVoice(voice_select, language, n_parts, lang_len, voices_list[ix])) > 0) {
  1040. voices[nv++] = vp;
  1041. vp->score = score;
  1042. }
  1043. }
  1044. voices[nv] = NULL; // list terminator
  1045. if (nv == 0)
  1046. return 0;
  1047. // sort the selected voices by their score
  1048. qsort(voices, nv, sizeof(espeak_VOICE *), (int(__cdecl *)(const void *, const void *))VoiceScoreSorter);
  1049. return nv;
  1050. }
  1051. espeak_VOICE *SelectVoiceByName(espeak_VOICE **voices, const char *name2)
  1052. {
  1053. int ix;
  1054. int match_fname = -1;
  1055. int match_fname2 = -1;
  1056. int match_name = -1;
  1057. const char *id; // this is the filename within espeak-ng-data/voices
  1058. char *variant_name;
  1059. int last_part_len;
  1060. char last_part[41];
  1061. char name[40];
  1062. if (voices == NULL) {
  1063. if (n_voices_list == 0)
  1064. espeak_ListVoices(NULL); // create the voices list
  1065. voices = voices_list;
  1066. }
  1067. strncpy0(name, name2, sizeof(name));
  1068. if ((variant_name = strchr(name, '+')) != NULL) {
  1069. *variant_name = 0;
  1070. variant_name++;
  1071. }
  1072. sprintf(last_part, "%c%s", PATHSEP, name);
  1073. last_part_len = strlen(last_part);
  1074. for (ix = 0; voices[ix] != NULL; ix++) {
  1075. if (strcasecmp(name, voices[ix]->name) == 0) {
  1076. match_name = ix; // found matching voice name
  1077. break;
  1078. } else {
  1079. id = voices[ix]->identifier;
  1080. if (strcasecmp(name, id) == 0)
  1081. match_fname = ix; // matching identifier, use this if no matching name
  1082. else if (strcasecmp(last_part, &id[strlen(id)-last_part_len]) == 0)
  1083. match_fname2 = ix;
  1084. }
  1085. }
  1086. if (match_name < 0) {
  1087. match_name = match_fname; // no matching name, try matching filename
  1088. if (match_name < 0)
  1089. match_name = match_fname2; // try matching just the last part of the filename
  1090. }
  1091. if (match_name < 0)
  1092. return NULL;
  1093. return voices[match_name];
  1094. }
  1095. char const *SelectVoice(espeak_VOICE *voice_select, int *found)
  1096. {
  1097. // Returns a path within espeak-voices, with a possible +variant suffix
  1098. // variant is an output-only parameter
  1099. int nv; // number of candidates
  1100. int ix, ix2;
  1101. int j;
  1102. int n_variants;
  1103. int variant_number;
  1104. int gender;
  1105. int skip;
  1106. int aged = 1;
  1107. char *variant_name;
  1108. const char *p, *p_start;
  1109. espeak_VOICE *vp = NULL;
  1110. espeak_VOICE *vp2;
  1111. espeak_VOICE voice_select2;
  1112. espeak_VOICE *voices[N_VOICES_LIST]; // list of candidates
  1113. espeak_VOICE *voices2[N_VOICES_LIST+N_VOICE_VARIANTS];
  1114. static espeak_VOICE voice_variants[N_VOICE_VARIANTS];
  1115. static char voice_id[50];
  1116. *found = 1;
  1117. memcpy(&voice_select2, voice_select, sizeof(voice_select2));
  1118. if (n_voices_list == 0)
  1119. espeak_ListVoices(NULL); // create the voices list
  1120. if ((voice_select2.languages == NULL) || (voice_select2.languages[0] == 0)) {
  1121. // no language is specified. Get language from the named voice
  1122. static char buf[60];
  1123. if (voice_select2.name == NULL) {
  1124. if ((voice_select2.name = voice_select2.identifier) == NULL)
  1125. voice_select2.name = ESPEAKNG_DEFAULT_VOICE;
  1126. }
  1127. strncpy0(buf, voice_select2.name, sizeof(buf));
  1128. variant_name = ExtractVoiceVariantName(buf, 0, 0);
  1129. vp = SelectVoiceByName(voices_list, buf);
  1130. if (vp != NULL) {
  1131. voice_select2.languages = &(vp->languages[1]);
  1132. if ((voice_select2.gender == ENGENDER_UNKNOWN) && (voice_select2.age == 0) && (voice_select2.variant == 0)) {
  1133. if (variant_name[0] != 0) {
  1134. sprintf(voice_id, "%s+%s", vp->identifier, variant_name);
  1135. return voice_id;
  1136. }
  1137. return vp->identifier;
  1138. }
  1139. }
  1140. }
  1141. // select and sort voices for the required language
  1142. nv = SetVoiceScores(&voice_select2, voices, 0);
  1143. if (nv == 0) {
  1144. // no matching voice, choose the default
  1145. *found = 0;
  1146. if ((voices[0] = SelectVoiceByName(voices_list, ESPEAKNG_DEFAULT_VOICE)) != NULL)
  1147. nv = 1;
  1148. }
  1149. gender = 0;
  1150. if ((voice_select2.gender == ENGENDER_FEMALE) || ((voice_select2.age > 0) && (voice_select2.age < 13)))
  1151. gender = ENGENDER_FEMALE;
  1152. else if (voice_select2.gender == ENGENDER_MALE)
  1153. gender = ENGENDER_MALE;
  1154. #define AGE_OLD 60
  1155. if (voice_select2.age < AGE_OLD)
  1156. aged = 0;
  1157. p = p_start = variant_lists[gender];
  1158. if (aged == 0)
  1159. p++; // the first voice in the variants list is older
  1160. // add variants for the top voices
  1161. n_variants = 0;
  1162. for (ix = 0, ix2 = 0; ix < nv; ix++) {
  1163. vp = voices[ix];
  1164. // is the main voice the required gender?
  1165. skip = 0;
  1166. if ((gender != ENGENDER_UNKNOWN) && (vp->gender != gender))
  1167. skip = 1;
  1168. if ((ix2 == 0) && aged && (vp->age < AGE_OLD))
  1169. skip = 1;
  1170. if (skip == 0)
  1171. voices2[ix2++] = vp;
  1172. for (j = 0; (j < vp->xx1) && (n_variants < N_VOICE_VARIANTS);) {
  1173. if ((variant_number = *p) == 0) {
  1174. p = p_start;
  1175. continue;
  1176. }
  1177. vp2 = &voice_variants[n_variants++]; // allocate space for voice variant
  1178. memcpy(vp2, vp, sizeof(espeak_VOICE)); // copy from the original voice
  1179. vp2->variant = variant_number;
  1180. voices2[ix2++] = vp2;
  1181. p++;
  1182. j++;
  1183. }
  1184. }
  1185. // add any more variants to the end of the list
  1186. while ((vp != NULL) && ((variant_number = *p++) != 0) && (n_variants < N_VOICE_VARIANTS)) {
  1187. vp2 = &voice_variants[n_variants++]; // allocate space for voice variant
  1188. memcpy(vp2, vp, sizeof(espeak_VOICE)); // copy from the original voice
  1189. vp2->variant = variant_number;
  1190. voices2[ix2++] = vp2;
  1191. }
  1192. // index the sorted list by the required variant number
  1193. if (ix2 == 0)
  1194. return NULL;
  1195. vp = voices2[voice_select2.variant % ix2];
  1196. if (vp->variant != 0) {
  1197. variant_name = ExtractVoiceVariantName(NULL, vp->variant, 0);
  1198. sprintf(voice_id, "%s+%s", vp->identifier, variant_name);
  1199. return voice_id;
  1200. }
  1201. return vp->identifier;
  1202. }
  1203. static void GetVoices(const char *path, int len_path_voices, int is_language_file)
  1204. {
  1205. FILE *f_voice;
  1206. espeak_VOICE *voice_data;
  1207. int ftype;
  1208. char fname[sizeof(path_home)+100];
  1209. #ifdef PLATFORM_WINDOWS
  1210. WIN32_FIND_DATAA FindFileData;
  1211. HANDLE hFind = INVALID_HANDLE_VALUE;
  1212. #undef UNICODE // we need FindFirstFileA() which takes an 8-bit c-string
  1213. sprintf(fname, "%s\\*", path);
  1214. hFind = FindFirstFileA(fname, &FindFileData);
  1215. if (hFind == INVALID_HANDLE_VALUE)
  1216. return;
  1217. do {
  1218. if (n_voices_list >= (N_VOICES_LIST-2))
  1219. break; // voices list is full
  1220. if (FindFileData.cFileName[0] != '.') {
  1221. sprintf(fname, "%s%c%s", path, PATHSEP, FindFileData.cFileName);
  1222. ftype = GetFileLength(fname);
  1223. if (ftype == -EISDIR) {
  1224. // a sub-directory
  1225. GetVoices(fname, len_path_voices, is_language_file);
  1226. } else if (ftype > 0) {
  1227. // a regular file, add it to the voices list
  1228. if ((f_voice = fopen(fname, "r")) == NULL)
  1229. continue;
  1230. // pass voice file name within the voices directory
  1231. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, is_language_file);
  1232. fclose(f_voice);
  1233. if (voice_data != NULL)
  1234. voices_list[n_voices_list++] = voice_data;
  1235. }
  1236. }
  1237. } while (FindNextFileA(hFind, &FindFileData) != 0);
  1238. FindClose(hFind);
  1239. #else
  1240. DIR *dir;
  1241. struct dirent *ent;
  1242. if ((dir = opendir((char *)path)) == NULL) // note: (char *) is needed for WINCE
  1243. return;
  1244. while ((ent = readdir(dir)) != NULL) {
  1245. if (n_voices_list >= (N_VOICES_LIST-2))
  1246. break; // voices list is full
  1247. if (ent->d_name[0] == '.')
  1248. continue;
  1249. sprintf(fname, "%s%c%s", path, PATHSEP, ent->d_name);
  1250. ftype = GetFileLength(fname);
  1251. if (ftype == -EISDIR) {
  1252. // a sub-directory
  1253. GetVoices(fname, len_path_voices, is_language_file);
  1254. } else if (ftype > 0) {
  1255. // a regular file, add it to the voices list
  1256. if ((f_voice = fopen(fname, "r")) == NULL)
  1257. continue;
  1258. // pass voice file name within the voices directory
  1259. voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, is_language_file);
  1260. fclose(f_voice);
  1261. if (voice_data != NULL)
  1262. voices_list[n_voices_list++] = voice_data;
  1263. }
  1264. }
  1265. closedir(dir);
  1266. #endif
  1267. }
  1268. #pragma GCC visibility push(default)
  1269. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SetVoiceByName(const char *name)
  1270. {
  1271. espeak_VOICE *v;
  1272. int ix;
  1273. espeak_VOICE voice_selector;
  1274. char *variant_name;
  1275. static char buf[60];
  1276. strncpy0(buf, name, sizeof(buf));
  1277. variant_name = ExtractVoiceVariantName(buf, 0, 1);
  1278. for (ix = 0;; ix++) {
  1279. // convert voice name to lower case (ascii)
  1280. if ((buf[ix] = tolower(buf[ix])) == 0)
  1281. break;
  1282. }
  1283. memset(&voice_selector, 0, sizeof(voice_selector));
  1284. voice_selector.name = (char *)name; // include variant name in voice stack ??
  1285. // first check for a voice with this filename
  1286. // This may avoid the need to call espeak_ListVoices().
  1287. if (LoadVoice(buf, 1) != NULL) {
  1288. if (variant_name[0] != 0)
  1289. LoadVoice(variant_name, 2);
  1290. DoVoiceChange(voice);
  1291. voice_selector.languages = voice->language_name;
  1292. SetVoiceStack(&voice_selector, variant_name);
  1293. return ENS_OK;
  1294. }
  1295. if (n_voices_list == 0)
  1296. espeak_ListVoices(NULL); // create the voices list
  1297. if ((v = SelectVoiceByName(voices_list, buf)) != NULL) {
  1298. if (LoadVoice(v->identifier, 0) != NULL) {
  1299. if (variant_name[0] != 0)
  1300. LoadVoice(variant_name, 2);
  1301. DoVoiceChange(voice);
  1302. voice_selector.languages = voice->language_name;
  1303. SetVoiceStack(&voice_selector, variant_name);
  1304. return ENS_OK;
  1305. }
  1306. }
  1307. return ENS_VOICE_NOT_FOUND;
  1308. }
  1309. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SetVoiceByProperties(espeak_VOICE *voice_selector)
  1310. {
  1311. const char *voice_id;
  1312. int voice_found;
  1313. voice_id = SelectVoice(voice_selector, &voice_found);
  1314. if (voice_found == 0)
  1315. return ENS_VOICE_NOT_FOUND;
  1316. LoadVoiceVariant(voice_id, 0);
  1317. DoVoiceChange(voice);
  1318. SetVoiceStack(voice_selector, "");
  1319. return ENS_OK;
  1320. }
  1321. #pragma GCC visibility pop
  1322. void FreeVoiceList()
  1323. {
  1324. int ix;
  1325. for (ix = 0; ix < n_voices_list; ix++) {
  1326. if (voices_list[ix] != NULL) {
  1327. free(voices_list[ix]);
  1328. voices_list[ix] = NULL;
  1329. }
  1330. }
  1331. n_voices_list = 0;
  1332. }
  1333. #pragma GCC visibility push(default)
  1334. ESPEAK_API const espeak_VOICE **espeak_ListVoices(espeak_VOICE *voice_spec)
  1335. {
  1336. char path_voices[sizeof(path_home)+12];
  1337. int ix;
  1338. int j;
  1339. espeak_VOICE *v;
  1340. static espeak_VOICE **voices = NULL;
  1341. // free previous voice list data
  1342. FreeVoiceList();
  1343. sprintf(path_voices, "%s%cvoices", path_home, PATHSEP);
  1344. GetVoices(path_voices, strlen(path_voices)+1, 0);
  1345. sprintf(path_voices, "%s%clang", path_home, PATHSEP);
  1346. GetVoices(path_voices, strlen(path_voices)+1, 1);
  1347. voices_list[n_voices_list] = NULL; // voices list terminator
  1348. espeak_VOICE **new_voices = (espeak_VOICE **)realloc(voices, sizeof(espeak_VOICE *)*(n_voices_list+1));
  1349. if (new_voices == NULL)
  1350. return (const espeak_VOICE **)voices;
  1351. voices = new_voices;
  1352. // sort the voices list
  1353. qsort(voices_list, n_voices_list, sizeof(espeak_VOICE *),
  1354. (int(__cdecl *)(const void *, const void *))VoiceNameSorter);
  1355. if (voice_spec) {
  1356. // select the voices which match the voice_spec, and sort them by preference
  1357. SetVoiceScores(voice_spec, voices, 1);
  1358. } else {
  1359. // list all: omit variant and mbrola voices
  1360. j = 0;
  1361. for (ix = 0; (v = voices_list[ix]) != NULL; ix++) {
  1362. if ((v->languages[0] != 0) && (strcmp(&v->languages[1], "variant") != 0)
  1363. && (memcmp(v->identifier, "mb/", 3) != 0))
  1364. voices[j++] = v;
  1365. }
  1366. voices[j] = NULL;
  1367. }
  1368. return (const espeak_VOICE **)voices;
  1369. }
  1370. ESPEAK_API espeak_VOICE *espeak_GetCurrentVoice(void)
  1371. {
  1372. return &current_voice_selected;
  1373. }
  1374. #pragma GCC visibility pop