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

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