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 43KB

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