| 1 |
/* |
| 2 |
* Copyright (C) 2005 to 2014 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 |
|
| 20 |
#include "espeak__config.h" |
| 21 |
|
| 22 |
#include <ctype.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <stdbool.h> |
| 25 |
#include <stdint.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include "espeak_ng.h" |
| 31 |
#include "speak_lib.h" |
| 32 |
#include "encoding.h" |
| 33 |
|
| 34 |
#include "synthdata.h" |
| 35 |
#include "common.h" // for GetFileLength |
| 36 |
#include "error.h" // for create_file_error_context, crea... |
| 37 |
#include "phoneme.h" // for PHONEME_TAB, PHONEME_TAB_LIST |
| 38 |
#include "speech.h" // for path_home, PATHSEP |
| 39 |
#include "mbrola.h" // for mbrola_name |
| 40 |
#include "soundicon.h" // for soundicon_tab |
| 41 |
#include "synthesize.h" // for PHONEME_LIST, frameref_t, PHONE... |
| 42 |
#include "translate.h" // for Translator, LANGUAGE_OPTIONS |
| 43 |
#include "voice.h" // for ReadTonePoints, tone_points, voice |
| 44 |
|
| 45 |
#include "espeak_praat.h" // for theEspeakPraatFileInMemorySet |
| 46 |
|
| 47 |
int n_tunes = 0; |
| 48 |
TUNE *tunes = NULL; |
| 49 |
|
| 50 |
const int version_phdata = 0x014801; |
| 51 |
|
| 52 |
// copy the current phoneme table into here |
| 53 |
int n_phoneme_tab; |
| 54 |
static int current_phoneme_table; |
| 55 |
PHONEME_TAB *phoneme_tab[N_PHONEME_TAB]; |
| 56 |
|
| 57 |
static unsigned short *phoneme_index = NULL; |
| 58 |
static char *phondata_ptr = NULL; |
| 59 |
unsigned char *wavefile_data = NULL; |
| 60 |
static unsigned char *phoneme_tab_data = NULL; |
| 61 |
|
| 62 |
static int n_phoneme_tables; |
| 63 |
PHONEME_TAB_LIST phoneme_tab_list[N_PHONEME_TABS]; |
| 64 |
int phoneme_tab_number = 0; |
| 65 |
|
| 66 |
int seq_len_adjust; |
| 67 |
|
| 68 |
static espeak_ng_STATUS ReadPhFile(void **ptr, const char *fname, int *size, espeak_ng_ERROR_CONTEXT *context) |
| 69 |
{ |
| 70 |
if (!ptr) return static_cast<espeak_ng_STATUS> (EINVAL); |
| 71 |
|
| 72 |
FileInMemory f_in; |
| 73 |
int length; |
| 74 |
char buf[sizeof(path_home)+40]; |
| 75 |
|
| 76 |
sprintf(buf, "%s%c%s", path_home, PATHSEP, fname); |
| 77 |
length = GetFileLength(buf); |
| 78 |
if (length < 0) // length == -errno |
| 79 |
return create_file_error_context(context, static_cast<espeak_ng_STATUS> (-length), buf); |
| 80 |
|
| 81 |
if ((f_in = FileInMemorySet_fopen(theEspeakPraatFileInMemorySet(), buf, "rb")) == NULL) |
| 82 |
return create_file_error_context(context, static_cast<espeak_ng_STATUS> (errno), buf); |
| 83 |
|
| 84 |
if (*ptr != NULL) { |
| 85 |
free(*ptr); |
| 86 |
*ptr = NULL; |
| 87 |
} |
| 88 |
|
| 89 |
if (length == 0) { |
| 90 |
*ptr = NULL; |
| 91 |
return static_cast<espeak_ng_STATUS> (0); |
| 92 |
} |
| 93 |
|
| 94 |
if ((*ptr = malloc(length)) == NULL) { |
| 95 |
FileInMemory_fclose(f_in); |
| 96 |
return static_cast<espeak_ng_STATUS> (ENOMEM); |
| 97 |
} |
| 98 |
if (FileInMemory_fread(*ptr, 1, length, f_in) != length) { |
| 99 |
int error = errno; |
| 100 |
FileInMemory_fclose(f_in); |
| 101 |
free(*ptr); |
| 102 |
*ptr = NULL; |
| 103 |
return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), buf); |
| 104 |
} |
| 105 |
|
| 106 |
FileInMemory_fclose(f_in); |
| 107 |
if (size != NULL) |
| 108 |
*size = length; |
| 109 |
return ENS_OK; |
| 110 |
} |
| 111 |
|
| 112 |
espeak_ng_STATUS LoadPhData(int *srate, espeak_ng_ERROR_CONTEXT *context) |
| 113 |
{ |
| 114 |
int ix; |
| 115 |
int version; |
| 116 |
int length = 0; |
| 117 |
int rate; |
| 118 |
unsigned char *p; |
| 119 |
|
| 120 |
espeak_ng_STATUS status; |
| 121 |
if ((status = ReadPhFile((void **)&phoneme_tab_data, "phontab", NULL, context)) != ENS_OK) |
| 122 |
return status; |
| 123 |
if ((status = ReadPhFile((void **)&phoneme_index, "phonindex", NULL, context)) != ENS_OK) |
| 124 |
return status; |
| 125 |
if ((status = ReadPhFile((void **)&phondata_ptr, "phondata", NULL, context)) != ENS_OK) |
| 126 |
return status; |
| 127 |
if ((status = ReadPhFile((void **)&tunes, "intonations", &length, context)) != ENS_OK) |
| 128 |
return status; |
| 129 |
wavefile_data = (unsigned char *)phondata_ptr; |
| 130 |
n_tunes = length / sizeof(TUNE); |
| 131 |
|
| 132 |
// read the version number and sample rate from the first 8 bytes of phondata |
| 133 |
version = 0; // bytes 0-3, version number |
| 134 |
rate = 0; // bytes 4-7, sample rate |
| 135 |
if (wavefile_data) { |
| 136 |
for (ix = 0; ix < 4; ix++) { |
| 137 |
version += (wavefile_data[ix] << (ix*8)); |
| 138 |
rate += (wavefile_data[ix+4] << (ix*8)); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (version != version_phdata) |
| 143 |
return create_version_mismatch_error_context(context, path_home, version, version_phdata); |
| 144 |
|
| 145 |
// set up phoneme tables |
| 146 |
p = phoneme_tab_data; |
| 147 |
n_phoneme_tables = p[0]; |
| 148 |
p += 4; |
| 149 |
|
| 150 |
for (ix = 0; ix < n_phoneme_tables; ix++) { |
| 151 |
int n_phonemes = p[0]; |
| 152 |
phoneme_tab_list[ix].n_phonemes = p[0]; |
| 153 |
phoneme_tab_list[ix].includes = p[1]; |
| 154 |
p += 4; |
| 155 |
memcpy(phoneme_tab_list[ix].name, p, N_PHONEME_TAB_NAME); |
| 156 |
p += N_PHONEME_TAB_NAME; |
| 157 |
phoneme_tab_list[ix].phoneme_tab_ptr = (PHONEME_TAB *)p; |
| 158 |
p += (n_phonemes * sizeof(PHONEME_TAB)); |
| 159 |
} |
| 160 |
|
| 161 |
if (phoneme_tab_number >= n_phoneme_tables) |
| 162 |
phoneme_tab_number = 0; |
| 163 |
|
| 164 |
if (srate != NULL) |
| 165 |
*srate = rate; |
| 166 |
return ENS_OK; |
| 167 |
} |
| 168 |
|
| 169 |
void FreePhData(void) |
| 170 |
{ |
| 171 |
free(phoneme_tab_data); |
| 172 |
free(phoneme_index); |
| 173 |
free(phondata_ptr); |
| 174 |
free(tunes); |
| 175 |
phoneme_tab_data = NULL; |
| 176 |
phoneme_index = NULL; |
| 177 |
phondata_ptr = NULL; |
| 178 |
tunes = NULL; |
| 179 |
current_phoneme_table = -1; |
| 180 |
} |
| 181 |
|
| 182 |
int PhonemeCode(unsigned int mnem) |
| 183 |
{ |
| 184 |
int ix; |
| 185 |
|
| 186 |
for (ix = 0; ix < n_phoneme_tab; ix++) { |
| 187 |
if (phoneme_tab[ix] == NULL) |
| 188 |
continue; |
| 189 |
if (phoneme_tab[ix]->mnemonic == mnem) |
| 190 |
return phoneme_tab[ix]->code; |
| 191 |
} |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
int LookupPhonemeString(const char *string) |
| 196 |
{ |
| 197 |
int ix; |
| 198 |
unsigned int mnem; |
| 199 |
|
| 200 |
// Pack up to 4 characters into a word |
| 201 |
mnem = 0; |
| 202 |
for (ix = 0; ix < 4; ix++) { |
| 203 |
if (string[ix] == 0) break; |
| 204 |
unsigned char c = string[ix]; |
| 205 |
mnem |= (c << (ix*8)); |
| 206 |
} |
| 207 |
|
| 208 |
return PhonemeCode(mnem); |
| 209 |
} |
| 210 |
|
| 211 |
frameref_t *LookupSpect(PHONEME_TAB *this_ph, int which, FMT_PARAMS *fmt_params, int *n_frames, PHONEME_LIST *plist) |
| 212 |
{ |
| 213 |
int ix; |
| 214 |
int nf; |
| 215 |
int nf1; |
| 216 |
int seq_break; |
| 217 |
frameref_t *frames; |
| 218 |
int length1; |
| 219 |
SPECT_SEQ *seq, *seq2; |
| 220 |
SPECT_SEQK *seqk, *seqk2; |
| 221 |
frame_t *frame; |
| 222 |
static frameref_t frames_buf[N_SEQ_FRAMES]; |
| 223 |
|
| 224 |
MAKE_MEM_UNDEFINED(&frames_buf, sizeof(frames_buf)); |
| 225 |
|
| 226 |
seq = (SPECT_SEQ *)(&phondata_ptr[fmt_params->fmt_addr]); |
| 227 |
seqk = (SPECT_SEQK *)seq; |
| 228 |
nf = seq->n_frames; |
| 229 |
|
| 230 |
if (nf >= N_SEQ_FRAMES) |
| 231 |
nf = N_SEQ_FRAMES - 1; |
| 232 |
|
| 233 |
seq_len_adjust = fmt_params->fmt2_lenadj + fmt_params->fmt_length; |
| 234 |
seq_break = 0; |
| 235 |
|
| 236 |
for (ix = 0; ix < nf; ix++) { |
| 237 |
if (seq->frame[0].frflags & FRFLAG_KLATT) |
| 238 |
frame = &seqk->frame[ix]; |
| 239 |
else |
| 240 |
frame = (frame_t *)&seq->frame[ix]; |
| 241 |
frames_buf[ix].frame = frame; |
| 242 |
frames_buf[ix].frflags = frame->frflags; |
| 243 |
frames_buf[ix].length = frame->length; |
| 244 |
if (frame->frflags & FRFLAG_VOWEL_CENTRE) |
| 245 |
seq_break = ix; |
| 246 |
} |
| 247 |
|
| 248 |
frames = &frames_buf[0]; |
| 249 |
if (seq_break > 0) { |
| 250 |
if (which == 1) |
| 251 |
nf = seq_break + 1; |
| 252 |
else { |
| 253 |
frames = &frames_buf[seq_break]; // body of vowel, skip past initial frames |
| 254 |
nf -= seq_break; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// do we need to modify a frame for blending with a consonant? |
| 259 |
if ((this_ph->type == phVOWEL) && (fmt_params->fmt2_addr == 0) && (fmt_params->use_vowelin)) |
| 260 |
seq_len_adjust += FormantTransition2(frames, &nf, fmt_params->transition0, fmt_params->transition1, NULL, which); |
| 261 |
|
| 262 |
length1 = 0; |
| 263 |
nf1 = nf - 1; |
| 264 |
for (ix = 0; ix < nf1; ix++) |
| 265 |
length1 += frames[ix].length; |
| 266 |
|
| 267 |
if (fmt_params->fmt2_addr != 0) { |
| 268 |
// a secondary reference has been returned, which is not a wavefile |
| 269 |
// add these spectra to the main sequence |
| 270 |
seq2 = (SPECT_SEQ *)(&phondata_ptr[fmt_params->fmt2_addr]); |
| 271 |
seqk2 = (SPECT_SEQK *)seq2; |
| 272 |
|
| 273 |
// first frame of the addition just sets the length of the last frame of the main seq |
| 274 |
nf--; |
| 275 |
for (ix = 0; ix < seq2->n_frames; ix++) { |
| 276 |
if (seq2->frame[0].frflags & FRFLAG_KLATT) |
| 277 |
frame = &seqk2->frame[ix]; |
| 278 |
else |
| 279 |
frame = (frame_t *)&seq2->frame[ix]; |
| 280 |
|
| 281 |
frames[nf].length = frame->length; |
| 282 |
if (ix > 0) { |
| 283 |
frames[nf].frame = frame; |
| 284 |
frames[nf].frflags = frame->frflags; |
| 285 |
} |
| 286 |
nf++; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (length1 > 0) { |
| 291 |
int length_factor; |
| 292 |
if (which == 2) { |
| 293 |
// adjust the length of the main part to match the standard length specified for the vowel |
| 294 |
// less the front part of the vowel and any added suffix |
| 295 |
|
| 296 |
int length_std = fmt_params->std_length + seq_len_adjust - 45; |
| 297 |
if (length_std < 10) |
| 298 |
length_std = 10; |
| 299 |
if (plist->synthflags & SFLAG_LENGTHEN) |
| 300 |
length_std += (phoneme_tab[phonLENGTHEN]->std_length * 2); // phoneme was followed by an extra : symbol |
| 301 |
|
| 302 |
// can adjust vowel length for stressed syllables here |
| 303 |
|
| 304 |
length_factor = (length_std * 256)/ length1; |
| 305 |
|
| 306 |
for (ix = 0; ix < nf1; ix++) |
| 307 |
frames[ix].length = (frames[ix].length * length_factor)/256; |
| 308 |
} else { |
| 309 |
if (which == 1) { |
| 310 |
// front of a vowel |
| 311 |
if (fmt_params->fmt_control == 1) { |
| 312 |
// This is the default start of a vowel. |
| 313 |
// Allow very short vowels to have shorter front parts |
| 314 |
if (fmt_params->std_length < 130) |
| 315 |
frames[0].length = (frames[0].length * fmt_params->std_length)/130; |
| 316 |
} |
| 317 |
} else { |
| 318 |
// not a vowel |
| 319 |
if (fmt_params->std_length > 0) |
| 320 |
seq_len_adjust += (fmt_params->std_length - length1); |
| 321 |
} |
| 322 |
|
| 323 |
if (seq_len_adjust != 0) { |
| 324 |
length_factor = ((length1 + seq_len_adjust) * 256)/length1; |
| 325 |
for (ix = 0; ix < nf1; ix++) |
| 326 |
frames[ix].length = (frames[ix].length * length_factor)/256; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
*n_frames = nf; |
| 332 |
return frames; |
| 333 |
} |
| 334 |
|
| 335 |
const unsigned char *GetEnvelope(int index) |
| 336 |
{ |
| 337 |
if (index == 0) { |
| 338 |
fprintf(stderr, "espeak: No envelope\n"); |
| 339 |
return envelope_data[0]; // not found, use a default envelope |
| 340 |
} |
| 341 |
return (unsigned char *)&phondata_ptr[index]; |
| 342 |
} |
| 343 |
|
| 344 |
static void SetUpPhonemeTable(int number) |
| 345 |
{ |
| 346 |
int ix; |
| 347 |
int includes; |
| 348 |
PHONEME_TAB *phtab; |
| 349 |
|
| 350 |
if ((includes = phoneme_tab_list[number].includes) > 0) { |
| 351 |
// recursively include base phoneme tables |
| 352 |
SetUpPhonemeTable(includes - 1); |
| 353 |
} |
| 354 |
|
| 355 |
// now add the phonemes from this table |
| 356 |
phtab = phoneme_tab_list[number].phoneme_tab_ptr; |
| 357 |
for (ix = 0; ix < phoneme_tab_list[number].n_phonemes; ix++) { |
| 358 |
int ph_code = phtab[ix].code; |
| 359 |
phoneme_tab[ph_code] = &phtab[ix]; |
| 360 |
if (ph_code > n_phoneme_tab) { |
| 361 |
memset(&phoneme_tab[n_phoneme_tab+1], 0, (ph_code - (n_phoneme_tab+1)) * sizeof(*phoneme_tab)); |
| 362 |
n_phoneme_tab = ph_code; |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
void SelectPhonemeTable(int number) |
| 368 |
{ |
| 369 |
if (current_phoneme_table == number) return; |
| 370 |
n_phoneme_tab = 0; |
| 371 |
MAKE_MEM_UNDEFINED(&phoneme_tab, sizeof(phoneme_tab)); |
| 372 |
SetUpPhonemeTable(number); // recursively for included phoneme tables |
| 373 |
n_phoneme_tab++; |
| 374 |
current_phoneme_table = number; |
| 375 |
} |
| 376 |
|
| 377 |
int LookupPhonemeTable(const char *name) |
| 378 |
{ |
| 379 |
int ix; |
| 380 |
|
| 381 |
for (ix = 0; ix < n_phoneme_tables; ix++) { |
| 382 |
if (strcmp(name, phoneme_tab_list[ix].name) == 0) { |
| 383 |
phoneme_tab_number = ix; |
| 384 |
break; |
| 385 |
} |
| 386 |
} |
| 387 |
if (ix == n_phoneme_tables) |
| 388 |
return -1; |
| 389 |
|
| 390 |
return ix; |
| 391 |
} |
| 392 |
|
| 393 |
int SelectPhonemeTableName(const char *name) |
| 394 |
{ |
| 395 |
// Look up a phoneme set by name, and select it if it exists |
| 396 |
// Returns the phoneme table number |
| 397 |
int ix; |
| 398 |
|
| 399 |
if ((ix = LookupPhonemeTable(name)) == -1) |
| 400 |
return -1; |
| 401 |
|
| 402 |
SelectPhonemeTable(ix); |
| 403 |
return ix; |
| 404 |
} |
| 405 |
|
| 406 |
static void InvalidInstn(PHONEME_TAB *ph, int instn) |
| 407 |
{ |
| 408 |
char buf[5]; |
| 409 |
fprintf(stderr, "Invalid instruction %.4x for phoneme '%s'\n", instn, WordToString(buf, ph->mnemonic)); |
| 410 |
} |
| 411 |
|
| 412 |
static bool StressCondition(Translator *tr, PHONEME_LIST *plist, int condition, int control) |
| 413 |
{ |
| 414 |
int stress_level; |
| 415 |
PHONEME_LIST *pl; |
| 416 |
static const int condition_level[4] = { 1, 2, 4, 15 }; |
| 417 |
|
| 418 |
if (phoneme_tab[plist[0].phcode]->type == phVOWEL) |
| 419 |
pl = plist; |
| 420 |
else { |
| 421 |
// consonant, get stress from the following vowel |
| 422 |
if (phoneme_tab[plist[1].phcode]->type == phVOWEL) |
| 423 |
pl = &plist[1]; |
| 424 |
else |
| 425 |
return false; // no stress elevel for this consonant |
| 426 |
} |
| 427 |
|
| 428 |
stress_level = pl->stresslevel & 0xf; |
| 429 |
|
| 430 |
if (tr != NULL) { |
| 431 |
if ((control & 1) && (plist->synthflags & SFLAG_DICTIONARY) && ((tr->langopts.param[LOPT_REDUCE] & 1) == 0)) { |
| 432 |
// change phoneme. Don't change phonemes which are given for the word in the dictionary. |
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
if ((tr->langopts.param[LOPT_REDUCE] & 0x2) && (stress_level >= pl->wordstress)) { |
| 437 |
// treat the most stressed syllable in an unstressed word as stressed |
| 438 |
stress_level = STRESS_IS_PRIMARY; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
if (condition == STRESS_IS_PRIMARY) |
| 443 |
return stress_level >= pl->wordstress; |
| 444 |
|
| 445 |
if (condition == STRESS_IS_SECONDARY) { |
| 446 |
if (stress_level > STRESS_IS_SECONDARY) |
| 447 |
return true; |
| 448 |
} else { |
| 449 |
if (stress_level < condition_level[condition]) |
| 450 |
return true; |
| 451 |
} |
| 452 |
return false; |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
static int CountVowelPosition(PHONEME_LIST *plist) |
| 457 |
{ |
| 458 |
int count = 0; |
| 459 |
|
| 460 |
for (;;) { |
| 461 |
if (plist->ph->type == phVOWEL) |
| 462 |
count++; |
| 463 |
if (plist->sourceix != 0) |
| 464 |
break; |
| 465 |
plist--; |
| 466 |
} |
| 467 |
return count; |
| 468 |
} |
| 469 |
|
| 470 |
static bool InterpretCondition(Translator *tr, int control, PHONEME_LIST *plist, unsigned short *p_prog, WORD_PH_DATA *worddata) |
| 471 |
{ |
| 472 |
unsigned int data; |
| 473 |
int instn; |
| 474 |
int instn2; |
| 475 |
|
| 476 |
// instruction: 2xxx, 3xxx |
| 477 |
|
| 478 |
// bits 8-10 = 0 to 5, which phoneme, =6 the 'which' information is in the next instruction. |
| 479 |
// bit 11 = 0, bits 0-7 are a phoneme code |
| 480 |
// bit 11 = 1, bits 5-7 type of data, bits 0-4 data value |
| 481 |
|
| 482 |
// bits 8-10 = 7, other conditions |
| 483 |
|
| 484 |
instn = (*p_prog) & 0xfff; |
| 485 |
data = instn & 0xff; |
| 486 |
instn2 = instn >> 8; |
| 487 |
|
| 488 |
if (instn2 < 14) { |
| 489 |
PHONEME_LIST *plist_this; |
| 490 |
plist_this = plist; |
| 491 |
int which = (instn2) % 7; |
| 492 |
|
| 493 |
if (which == 6) { |
| 494 |
// the 'which' code is in the next instruction |
| 495 |
p_prog++; |
| 496 |
which = (*p_prog); |
| 497 |
} |
| 498 |
|
| 499 |
if (which == 4) { |
| 500 |
// nextPhW not word boundary |
| 501 |
if (plist[1].sourceix) |
| 502 |
return false; |
| 503 |
} |
| 504 |
if (which == 5) { |
| 505 |
// prevPhW, not word boundary |
| 506 |
if (plist[0].sourceix) |
| 507 |
return false; |
| 508 |
} |
| 509 |
if (which == 6) { |
| 510 |
// next2PhW, not word boundary |
| 511 |
if (plist[1].sourceix || plist[2].sourceix) |
| 512 |
return false; |
| 513 |
} |
| 514 |
|
| 515 |
bool check_endtype = false; |
| 516 |
switch (which) |
| 517 |
{ |
| 518 |
case 0: // prevPh |
| 519 |
case 5: // prevPhW |
| 520 |
plist--; |
| 521 |
check_endtype = true; |
| 522 |
break; |
| 523 |
case 1: // thisPh |
| 524 |
break; |
| 525 |
case 2: // nextPh |
| 526 |
case 4: // nextPhW |
| 527 |
plist++; |
| 528 |
break; |
| 529 |
case 3: // next2Ph |
| 530 |
case 6: // next2PhW |
| 531 |
plist += 2; |
| 532 |
break; |
| 533 |
case 7: |
| 534 |
// nextVowel, not word boundary |
| 535 |
for (which = 1;; which++) { |
| 536 |
if (plist[which].sourceix) |
| 537 |
return false; |
| 538 |
if (phoneme_tab[plist[which].phcode]->type == phVOWEL) { |
| 539 |
plist = &plist[which]; |
| 540 |
break; |
| 541 |
} |
| 542 |
} |
| 543 |
break; |
| 544 |
case 8: // prevVowel in this word |
| 545 |
if ((worddata == NULL) || (worddata->prev_vowel.ph == NULL)) |
| 546 |
return false; // no previous vowel |
| 547 |
plist = &(worddata->prev_vowel); |
| 548 |
check_endtype = true; |
| 549 |
break; |
| 550 |
case 9: // next3PhW |
| 551 |
for (int ix = 1; ix <= 3; ix++) { |
| 552 |
if (plist[ix].sourceix) |
| 553 |
return false; |
| 554 |
} |
| 555 |
plist = &plist[3]; |
| 556 |
break; |
| 557 |
case 10: // prev2PhW |
| 558 |
if ((plist[0].sourceix) || (plist[-1].sourceix)) |
| 559 |
return false; |
| 560 |
plist -= 2; |
| 561 |
check_endtype = true; |
| 562 |
break; |
| 563 |
} |
| 564 |
|
| 565 |
if ((which == 0) || (which == 5)) { |
| 566 |
if (plist->phcode == 1) { |
| 567 |
// This is a NULL phoneme, a phoneme has been deleted so look at the previous phoneme |
| 568 |
plist--; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
if (control & 0x100) { |
| 573 |
// "change phonemes" pass |
| 574 |
plist->ph = phoneme_tab[plist->phcode]; |
| 575 |
} |
| 576 |
PHONEME_TAB *ph; |
| 577 |
ph = plist->ph; |
| 578 |
|
| 579 |
if (instn2 < 7) { |
| 580 |
// 'data' is a phoneme number |
| 581 |
if ((phoneme_tab[data]->mnemonic == ph->mnemonic) == true) |
| 582 |
return true; |
| 583 |
|
| 584 |
// not an exact match, check for a vowel type (eg. #i ) |
| 585 |
if ((check_endtype) && (ph->type == phVOWEL)) |
| 586 |
return data == ph->end_type; // prevPh() match on end_type |
| 587 |
return data == ph->start_type; // thisPh() or nextPh(), match on start_type |
| 588 |
} |
| 589 |
|
| 590 |
data = instn & 0x1f; |
| 591 |
|
| 592 |
switch (instn & 0xe0) |
| 593 |
{ |
| 594 |
case CONDITION_IS_PHONEME_TYPE: |
| 595 |
return ph->type == data; |
| 596 |
case CONDITION_IS_PLACE_OF_ARTICULATION: |
| 597 |
return ((ph->phflags >> 16) & 0xf) == data; |
| 598 |
case CONDITION_IS_PHFLAG_SET: |
| 599 |
return (ph->phflags & (1 << data)) != 0; |
| 600 |
case CONDITION_IS_OTHER: |
| 601 |
switch (data) |
| 602 |
{ |
| 603 |
case STRESS_IS_DIMINISHED: |
| 604 |
case STRESS_IS_UNSTRESSED: |
| 605 |
case STRESS_IS_NOT_STRESSED: |
| 606 |
case STRESS_IS_SECONDARY: |
| 607 |
case STRESS_IS_PRIMARY: |
| 608 |
return StressCondition(tr, plist, data, 0); |
| 609 |
case isBreak: |
| 610 |
return (ph->type == phPAUSE) || (plist_this->synthflags & SFLAG_NEXT_PAUSE); |
| 611 |
case isWordStart: |
| 612 |
return plist->sourceix != 0; |
| 613 |
case isWordEnd: |
| 614 |
return plist[1].sourceix || (plist[1].ph->type == phPAUSE); |
| 615 |
case isAfterStress: |
| 616 |
if (plist->sourceix != 0) |
| 617 |
return false; |
| 618 |
do { |
| 619 |
plist--; |
| 620 |
if ((plist->stresslevel & 0xf) >= 4) |
| 621 |
return true; |
| 622 |
|
| 623 |
} while (plist->sourceix == 0); |
| 624 |
break; |
| 625 |
case isNotVowel: |
| 626 |
return ph->type != phVOWEL; |
| 627 |
case isFinalVowel: |
| 628 |
for (;;) { |
| 629 |
plist++; |
| 630 |
if (plist->sourceix != 0) |
| 631 |
return true; // start of next word, without finding another vowel |
| 632 |
if (plist->ph->type == phVOWEL) |
| 633 |
return false; |
| 634 |
} |
| 635 |
case isVoiced: |
| 636 |
return (ph->type == phVOWEL) || (ph->type == phLIQUID) || (ph->phflags & phVOICED); |
| 637 |
case isFirstVowel: |
| 638 |
return CountVowelPosition(plist) == 1; |
| 639 |
case isSecondVowel: |
| 640 |
return CountVowelPosition(plist) == 2; |
| 641 |
case isTranslationGiven: |
| 642 |
return (plist->synthflags & SFLAG_DICTIONARY) != 0; |
| 643 |
} |
| 644 |
break; |
| 645 |
|
| 646 |
} |
| 647 |
return false; |
| 648 |
} else if (instn2 == 0xf) { |
| 649 |
// Other conditions |
| 650 |
switch (data) |
| 651 |
{ |
| 652 |
case 1: // PreVoicing |
| 653 |
return control & 1; |
| 654 |
#if USE_KLATT |
| 655 |
case 2: // KlattSynth |
| 656 |
return voice->klattv[0] != 0; |
| 657 |
#endif |
| 658 |
#if USE_MBROLA |
| 659 |
case 3: // MbrolaSynth |
| 660 |
return mbrola_name[0] != 0; |
| 661 |
#endif |
| 662 |
} |
| 663 |
} |
| 664 |
return false; |
| 665 |
} |
| 666 |
|
| 667 |
static void SwitchOnVowelType(PHONEME_LIST *plist, PHONEME_DATA *phdata, unsigned short **p_prog, int instn_type) |
| 668 |
{ |
| 669 |
int voweltype; |
| 670 |
|
| 671 |
|
| 672 |
if (instn_type == 2) { |
| 673 |
phdata->pd_control |= pd_FORNEXTPH; |
| 674 |
voweltype = plist[1].ph->start_type; // SwitchNextVowelType |
| 675 |
} else |
| 676 |
voweltype = plist[-1].ph->end_type; // SwitchPrevVowelType |
| 677 |
|
| 678 |
voweltype -= phonVOWELTYPES; |
| 679 |
if ((voweltype >= 0) && (voweltype < 6)) { |
| 680 |
unsigned short *prog; |
| 681 |
signed char x; |
| 682 |
|
| 683 |
prog = *p_prog + voweltype*2; |
| 684 |
phdata->sound_addr[instn_type] = (((prog[1] & 0xf) << 16) + prog[2]) * 4; |
| 685 |
x = (prog[1] >> 4) & 0xff; |
| 686 |
phdata->sound_param[instn_type] = x; // sign extend |
| 687 |
} |
| 688 |
|
| 689 |
*p_prog += 12; |
| 690 |
} |
| 691 |
|
| 692 |
static int NumInstnWords(unsigned short *prog) |
| 693 |
{ |
| 694 |
int instn; |
| 695 |
int instn2; |
| 696 |
int instn_type; |
| 697 |
int n; |
| 698 |
int type2; |
| 699 |
static const char n_words[16] = { 0, 1, 0, 0, 1, 1, 0, 1, 1, 2, 4, 0, 0, 0, 0, 0 }; |
| 700 |
|
| 701 |
instn = *prog; |
| 702 |
instn_type = instn >> 12; |
| 703 |
if ((n = n_words[instn_type]) > 0) |
| 704 |
return n; |
| 705 |
|
| 706 |
switch (instn_type) |
| 707 |
{ |
| 708 |
case 0: |
| 709 |
if (((instn & 0xf00) >> 8) == i_IPA_NAME) { |
| 710 |
n = ((instn & 0xff) + 1) / 2; |
| 711 |
return n+1; |
| 712 |
} |
| 713 |
return 1; |
| 714 |
case 6: |
| 715 |
type2 = (instn & 0xf00) >> 9; |
| 716 |
if ((type2 == 5) || (type2 == 6)) |
| 717 |
return 12; // switch on vowel type |
| 718 |
return 1; |
| 719 |
case 2: |
| 720 |
case 3: |
| 721 |
// a condition, check for a 2-word instruction |
| 722 |
if (((n = instn & 0x0f00) == 0x600) || (n == 0x0d00)) |
| 723 |
return 2; |
| 724 |
return 1; |
| 725 |
default: |
| 726 |
// instn_type 11 to 15, 2 words |
| 727 |
instn2 = prog[2]; |
| 728 |
if ((instn2 >> 12) == 0xf) { |
| 729 |
// This instruction is followed by addWav(), 2 more words |
| 730 |
return 4; |
| 731 |
} |
| 732 |
if (instn2 == INSTN_CONTINUE) |
| 733 |
return 3; |
| 734 |
return 2; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
void InterpretPhoneme(Translator *tr, int control, PHONEME_LIST *plist, PHONEME_DATA *phdata, WORD_PH_DATA *worddata) |
| 739 |
{ |
| 740 |
// control: |
| 741 |
// bit 0: PreVoicing |
| 742 |
// bit 8: change phonemes |
| 743 |
|
| 744 |
PHONEME_TAB *ph; |
| 745 |
unsigned short *prog; |
| 746 |
int or_flag; |
| 747 |
bool truth; |
| 748 |
bool truth2; |
| 749 |
int data; |
| 750 |
int end_flag; |
| 751 |
int ix; |
| 752 |
signed char param_sc; |
| 753 |
|
| 754 |
#define N_RETURN 10 |
| 755 |
int n_return = 0; |
| 756 |
unsigned short *return_addr[N_RETURN]; // return address stack |
| 757 |
|
| 758 |
ph = plist->ph; |
| 759 |
|
| 760 |
if ((worddata != NULL) && (plist->sourceix)) { |
| 761 |
// start of a word, reset word data |
| 762 |
worddata->prev_vowel.ph = NULL; |
| 763 |
} |
| 764 |
|
| 765 |
memset(phdata, 0, sizeof(PHONEME_DATA)); |
| 766 |
phdata->pd_param[i_SET_LENGTH] = ph->std_length; |
| 767 |
phdata->pd_param[i_LENGTH_MOD] = ph->length_mod; |
| 768 |
|
| 769 |
if (ph->program == 0) |
| 770 |
return; |
| 771 |
|
| 772 |
end_flag = 0; |
| 773 |
|
| 774 |
for (prog = &phoneme_index[ph->program]; end_flag != 1; prog++) { |
| 775 |
unsigned short instn; |
| 776 |
int instn2; |
| 777 |
|
| 778 |
instn = *prog; |
| 779 |
instn2 = (instn >> 8) & 0xf; |
| 780 |
|
| 781 |
switch (instn >> 12) |
| 782 |
{ |
| 783 |
case 0: // 0xxx |
| 784 |
data = instn & 0xff; |
| 785 |
|
| 786 |
if (instn2 == 0) { |
| 787 |
// instructions with no operand |
| 788 |
switch (data) |
| 789 |
{ |
| 790 |
case INSTN_RETURN: |
| 791 |
end_flag = 1; |
| 792 |
break; |
| 793 |
case INSTN_CONTINUE: |
| 794 |
break; |
| 795 |
default: |
| 796 |
InvalidInstn(ph, instn); |
| 797 |
break; |
| 798 |
} |
| 799 |
} else if (instn2 == i_APPEND_IFNEXTVOWEL) { |
| 800 |
if (phoneme_tab[plist[1].phcode]->type == phVOWEL) |
| 801 |
phdata->pd_param[i_APPEND_PHONEME] = data; |
| 802 |
} else if (instn2 == i_ADD_LENGTH) { |
| 803 |
if (data & 0x80) { |
| 804 |
// a negative value, do sign extension |
| 805 |
data = -(0x100 - data); |
| 806 |
} |
| 807 |
phdata->pd_param[i_SET_LENGTH] += data; |
| 808 |
} else if (instn2 == i_IPA_NAME) { |
| 809 |
// followed by utf-8 characters, 2 per instn word |
| 810 |
for (ix = 0; (ix < data) && (ix < 16); ix += 2) { |
| 811 |
prog++; |
| 812 |
phdata->ipa_string[ix] = prog[0] >> 8; |
| 813 |
phdata->ipa_string[ix+1] = prog[0] & 0xff; |
| 814 |
} |
| 815 |
phdata->ipa_string[ix] = 0; |
| 816 |
} else if (instn2 < N_PHONEME_DATA_PARAM) { |
| 817 |
phdata->pd_param[instn2] = data; |
| 818 |
if ((instn2 == i_CHANGE_PHONEME) && (control & 0x100)) { |
| 819 |
// found ChangePhoneme() in PhonemeList mode, exit |
| 820 |
end_flag = 1; |
| 821 |
} |
| 822 |
} else |
| 823 |
InvalidInstn(ph, instn); |
| 824 |
break; |
| 825 |
case 1: |
| 826 |
if (tr == NULL) |
| 827 |
break; // ignore if in synthesis stage |
| 828 |
|
| 829 |
if (instn2 < 8) { |
| 830 |
// ChangeIf |
| 831 |
if (StressCondition(tr, plist, instn2 & 7, 1) == true) { |
| 832 |
phdata->pd_param[i_CHANGE_PHONEME] = instn & 0xff; |
| 833 |
end_flag = 1; // change phoneme, exit |
| 834 |
} |
| 835 |
} |
| 836 |
break; |
| 837 |
case 2: |
| 838 |
case 3: |
| 839 |
// conditions |
| 840 |
or_flag = 0; |
| 841 |
truth = true; |
| 842 |
while ((instn & 0xe000) == 0x2000) { |
| 843 |
// process a sequence of conditions, using boolean accumulator |
| 844 |
truth2 = InterpretCondition(tr, control, plist, prog, worddata); |
| 845 |
prog += NumInstnWords(prog); |
| 846 |
if (*prog == i_NOT) { |
| 847 |
truth2 = truth2 ^ 1; |
| 848 |
prog++; |
| 849 |
} |
| 850 |
|
| 851 |
if (or_flag) |
| 852 |
truth = truth || truth2; |
| 853 |
else |
| 854 |
truth = truth && truth2; |
| 855 |
or_flag = instn & 0x1000; |
| 856 |
instn = *prog; |
| 857 |
} |
| 858 |
|
| 859 |
if (truth == false) { |
| 860 |
if ((instn & 0xf800) == i_JUMP_FALSE) |
| 861 |
prog += instn & 0xff; |
| 862 |
else { |
| 863 |
// instruction after a condition is not JUMP_FALSE, so skip the instruction. |
| 864 |
prog += NumInstnWords(prog); |
| 865 |
if ((prog[0] & 0xfe00) == 0x6000) |
| 866 |
prog++; // and skip ELSE jump |
| 867 |
} |
| 868 |
} |
| 869 |
prog--; |
| 870 |
break; |
| 871 |
case 6: |
| 872 |
// JUMP |
| 873 |
switch (instn2 >> 1) |
| 874 |
{ |
| 875 |
case 0: |
| 876 |
prog += (instn & 0xff) - 1; |
| 877 |
break; |
| 878 |
case 4: |
| 879 |
// conditional jumps should have been processed in the Condition section |
| 880 |
break; |
| 881 |
case 5: // NexttVowelStarts |
| 882 |
SwitchOnVowelType(plist, phdata, &prog, 2); |
| 883 |
break; |
| 884 |
case 6: // PrevVowelTypeEndings |
| 885 |
SwitchOnVowelType(plist, phdata, &prog, 3); |
| 886 |
break; |
| 887 |
} |
| 888 |
break; |
| 889 |
case 9: |
| 890 |
data = ((instn & 0xf) << 16) + prog[1]; |
| 891 |
prog++; |
| 892 |
switch (instn2) |
| 893 |
{ |
| 894 |
case 1: |
| 895 |
// call a procedure or another phoneme |
| 896 |
if (n_return < N_RETURN) { |
| 897 |
return_addr[n_return++] = prog; |
| 898 |
prog = &phoneme_index[data] - 1; |
| 899 |
} |
| 900 |
break; |
| 901 |
case 2: |
| 902 |
// pitch envelope |
| 903 |
phdata->pitch_env = data; |
| 904 |
break; |
| 905 |
case 3: |
| 906 |
// amplitude envelope |
| 907 |
phdata->amp_env = data; |
| 908 |
break; |
| 909 |
} |
| 910 |
break; |
| 911 |
case 10: // Vowelin, Vowelout |
| 912 |
if (instn2 == 1) |
| 913 |
ix = 0; |
| 914 |
else |
| 915 |
ix = 2; |
| 916 |
|
| 917 |
phdata->vowel_transition[ix] = ((prog[0] & 0xff) << 16) + prog[1]; |
| 918 |
phdata->vowel_transition[ix+1] = (prog[2] << 16) + prog[3]; |
| 919 |
prog += 3; |
| 920 |
break; |
| 921 |
case 11: // FMT |
| 922 |
case 12: // WAV |
| 923 |
case 13: // VowelStart |
| 924 |
case 14: // VowelEnd |
| 925 |
case 15: // addWav |
| 926 |
instn2 = (instn >> 12) - 11; |
| 927 |
phdata->sound_addr[instn2] = ((instn & 0xf) << 18) + (prog[1] << 2); |
| 928 |
param_sc = phdata->sound_param[instn2] = (instn >> 4) & 0xff; |
| 929 |
prog++; |
| 930 |
|
| 931 |
if (prog[1] != INSTN_CONTINUE) { |
| 932 |
if (instn2 < 2) { |
| 933 |
// FMT() and WAV() imply Return |
| 934 |
end_flag = 1; |
| 935 |
if ((prog[1] >> 12) == 0xf) { |
| 936 |
// Return after the following addWav() |
| 937 |
end_flag = 2; |
| 938 |
} |
| 939 |
} else if (instn2 == pd_ADDWAV) { |
| 940 |
// addWav(), return if previous instruction was FMT() or WAV() |
| 941 |
end_flag--; |
| 942 |
} |
| 943 |
|
| 944 |
if ((instn2 == pd_VWLSTART) || (instn2 == pd_VWLEND)) { |
| 945 |
// VowelStart or VowelEnding. |
| 946 |
phdata->sound_param[instn2] = param_sc; // sign extend |
| 947 |
} |
| 948 |
} |
| 949 |
break; |
| 950 |
default: |
| 951 |
InvalidInstn(ph, instn); |
| 952 |
break; |
| 953 |
} |
| 954 |
|
| 955 |
if ((end_flag == 1) && (n_return > 0)) { |
| 956 |
// return from called procedure or phoneme |
| 957 |
end_flag = 0; |
| 958 |
prog = return_addr[--n_return]; |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
if ((worddata != NULL) && (plist->type == phVOWEL)) |
| 963 |
memcpy(&worddata->prev_vowel, &plist[0], sizeof(PHONEME_LIST)); |
| 964 |
|
| 965 |
plist->std_length = phdata->pd_param[i_SET_LENGTH]; |
| 966 |
if (phdata->sound_addr[0] != 0) { |
| 967 |
plist->phontab_addr = phdata->sound_addr[0]; // FMT address |
| 968 |
plist->sound_param = phdata->sound_param[0]; |
| 969 |
} else { |
| 970 |
plist->phontab_addr = phdata->sound_addr[1]; // WAV address |
| 971 |
plist->sound_param = phdata->sound_param[1]; |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
void InterpretPhoneme2(int phcode, PHONEME_DATA *phdata) |
| 976 |
{ |
| 977 |
// Examine the program of a single isolated phoneme |
| 978 |
int ix; |
| 979 |
PHONEME_LIST plist[4]; |
| 980 |
memset(plist, 0, sizeof(plist)); |
| 981 |
|
| 982 |
for (ix = 0; ix < 4; ix++) { |
| 983 |
plist[ix].phcode = phonPAUSE; |
| 984 |
plist[ix].ph = phoneme_tab[phonPAUSE]; |
| 985 |
} |
| 986 |
|
| 987 |
plist[1].phcode = phcode; |
| 988 |
plist[1].ph = phoneme_tab[phcode]; |
| 989 |
plist[2].sourceix = 1; |
| 990 |
|
| 991 |
InterpretPhoneme(NULL, 0, &plist[1], phdata, NULL); |
| 992 |
} |