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

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