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