| 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 | #ifndef ESPEAK_NG_SYNTHESIZE_H |
| 21 | #define ESPEAK_NG_SYNTHESIZE_H |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" |
| 25 | { |
| 26 | #endif |
| 27 | |
| 28 | #include <stdint.h> |
| 29 | #include <stdbool.h> |
| 30 | #include "espeak_ng.h" |
| 31 | #include "phoneme.h" // for PHONEME_TAB, N_PHONEME_TAB |
| 32 | |
| 33 | #define espeakINITIALIZE_PHONEME_IPA 0x0002 // move this to speak_lib.h, after eSpeak version 1.46.02 |
| 34 | |
| 35 | #define N_PHONEME_LIST 1000 // enough for source[N_TR_SOURCE] full of text, else it will truncate |
| 36 | |
| 37 | #define N_SEQ_FRAMES 25 // max frames in a spectrum sequence (real max is ablut 8) |
| 38 | #define STEPSIZE 64 // 2.9mS at 22 kHz sample rate |
| 39 | |
| 40 | // flags set for frames within a spectrum sequence |
| 41 | #define FRFLAG_KLATT 0x01 // this frame includes extra data for Klatt synthesizer |
| 42 | #define FRFLAG_VOWEL_CENTRE 0x02 // centre point of vowel |
| 43 | #define FRFLAG_LEN_MOD 0x04 // reduce effect of length adjustment |
| 44 | #define FRFLAG_BREAK_LF 0x08 // but keep f3 upwards |
| 45 | #define FRFLAG_BREAK 0x10 // don't merge with next frame |
| 46 | #define FRFLAG_FORMANT_RATE 0x20 // Flag5 allow increased rate of change of formant freq |
| 47 | #define FRFLAG_MODULATE 0x40 // Flag6 modulate amplitude of some cycles to give trill |
| 48 | #define FRFLAG_DEFER_WAV 0x80 // Flag7 defer mixing WAV until the next frame |
| 49 | #define FRFLAG_LEN_MOD2 0x4000 // reduce effect of length adjustment, used for the start of a vowel |
| 50 | #define FRFLAG_COPIED 0x8000 // This frame has been copied into temporary rw memory |
| 51 | |
| 52 | #define SFLAG_SEQCONTINUE 0x01 // a liquid or nasal after a vowel, but not followed by a vowel |
| 53 | #define SFLAG_EMBEDDED 0x02 // there are embedded commands before this phoneme |
| 54 | #define SFLAG_SYLLABLE 0x04 // vowel or syllabic consonant |
| 55 | #define SFLAG_LENGTHEN 0x08 // lengthen symbol : included after this phoneme |
| 56 | #define SFLAG_DICTIONARY 0x10 // the pronunciation of this word was listed in the xx_list dictionary |
| 57 | #define SFLAG_SWITCHED_LANG 0x20 // this word uses phonemes from a different language |
| 58 | #define SFLAG_PROMOTE_STRESS 0x40 // this unstressed word can be promoted to stressed |
| 59 | |
| 60 | #define SFLAG_NEXT_PAUSE 0x2000 // consider next phoneme as pause |
| 61 | |
| 62 | // embedded command numbers |
| 63 | #define EMBED_P 1 // pitch |
| 64 | #define EMBED_S 2 // speed (used in setlengths) |
| 65 | #define EMBED_A 3 // amplitude/volume |
| 66 | #define EMBED_R 4 // pitch range/expression |
| 67 | #define EMBED_H 5 // echo/reverberation |
| 68 | #define EMBED_T 6 // different tone for announcing punctuation (not used) |
| 69 | #define EMBED_I 7 // sound icon |
| 70 | #define EMBED_S2 8 // speed (used in synthesize) |
| 71 | #define EMBED_Y 9 // say-as commands |
| 72 | #define EMBED_M 10 // mark name |
| 73 | #define EMBED_U 11 // audio uri |
| 74 | #define EMBED_B 12 // break |
| 75 | #define EMBED_F 13 // emphasis |
| 76 | |
| 77 | #define N_EMBEDDED_VALUES 15 |
| 78 | extern int embedded_value[N_EMBEDDED_VALUES]; |
| 79 | extern const int embedded_default[N_EMBEDDED_VALUES]; |
| 80 | |
| 81 | #define N_KLATTP 10 // this affects the phoneme data file format |
| 82 | #define N_KLATTP2 14 // used in vowel files, with extra parameters for future extensions |
| 83 | |
| 84 | #define KLATT_AV 0 |
| 85 | #define KLATT_FNZ 1 // nasal zero freq |
| 86 | #define KLATT_Tilt 2 |
| 87 | #define KLATT_Aspr 3 |
| 88 | #define KLATT_Skew 4 |
| 89 | |
| 90 | #define KLATT_Kopen 5 |
| 91 | #define KLATT_AVp 6 |
| 92 | #define KLATT_Fric 7 |
| 93 | #define KLATT_FricBP 8 |
| 94 | #define KLATT_Turb 9 |
| 95 | |
| 96 | typedef struct { // 64 bytes |
| 97 | short frflags; |
| 98 | short ffreq[7]; |
| 99 | unsigned char length; |
| 100 | unsigned char rms; |
| 101 | unsigned char fheight[8]; |
| 102 | unsigned char fwidth[6]; // width/4 f0-5 |
| 103 | unsigned char fright[3]; // width/4 f0-2 |
| 104 | unsigned char bw[4]; // Klatt bandwidth BNZ /2, f1,f2,f3 |
| 105 | unsigned char klattp[5]; // AV, FNZ, Tilt, Aspr, Skew |
| 106 | unsigned char klattp2[5]; // continuation of klattp[], Avp, Fric, FricBP, Turb |
| 107 | unsigned char klatt_ap[7]; // Klatt parallel amplitude |
| 108 | unsigned char klatt_bp[7]; // Klatt parallel bandwidth /2 |
| 109 | unsigned char spare; // pad to multiple of 4 bytes |
| 110 | } frame_t; // with extra Klatt parameters for parallel resonators |
| 111 | |
| 112 | typedef struct { // 44 bytes |
| 113 | short frflags; |
| 114 | short ffreq[7]; |
| 115 | unsigned char length; |
| 116 | unsigned char rms; |
| 117 | unsigned char fheight[8]; |
| 118 | unsigned char fwidth[6]; // width/4 f0-5 |
| 119 | unsigned char fright[3]; // width/4 f0-2 |
| 120 | unsigned char bw[4]; // Klatt bandwidth BNZ /2, f1,f2,f3 |
| 121 | unsigned char klattp[5]; // AV, FNZ, Tilt, Aspr, Skew |
| 122 | } frame_t2; // without the extra Klatt parameters |
| 123 | |
| 124 | typedef struct { |
| 125 | const unsigned char *pitch_env; |
| 126 | int pitch; // pitch Hz*256 |
| 127 | int pitch_ix; // index into pitch envelope (*256) |
| 128 | int pitch_inc; // increment to pitch_ix |
| 129 | int pitch_base; // Hz*256 low, before modified by envelope |
| 130 | int pitch_range; // Hz*256 range of envelope |
| 131 | |
| 132 | unsigned char *mix_wavefile; // wave file to be added to synthesis |
| 133 | int n_mix_wavefile; // length in bytes |
| 134 | int mix_wave_scale; // 0=2 byte samples |
| 135 | int mix_wave_amp; |
| 136 | int mix_wavefile_ix; |
| 137 | int mix_wavefile_max; // length of available WAV data (in bytes) |
| 138 | int mix_wavefile_offset; |
| 139 | |
| 140 | int amplitude; |
| 141 | int amplitude_v; |
| 142 | int amplitude_fmt; // percentage amplitude adjustment for formant synthesis |
| 143 | } WGEN_DATA; |
| 144 | |
| 145 | typedef struct { |
| 146 | double a; |
| 147 | double b; |
| 148 | double c; |
| 149 | double x1; |
| 150 | double x2; |
| 151 | } RESONATOR; |
| 152 | |
| 153 | typedef struct { |
| 154 | short length_total; // not used |
| 155 | unsigned char n_frames; |
| 156 | unsigned char sqflags; |
| 157 | frame_t2 frame[N_SEQ_FRAMES]; // max. frames in a spectrum sequence |
| 158 | } SPECT_SEQ; // sequence of espeak formant frames |
| 159 | |
| 160 | typedef struct { |
| 161 | short length_total; // not used |
| 162 | unsigned char n_frames; |
| 163 | unsigned char sqflags; |
| 164 | frame_t frame[N_SEQ_FRAMES]; // max. frames in a spectrum sequence |
| 165 | } SPECT_SEQK; // sequence of klatt formants frames |
| 166 | |
| 167 | typedef struct { |
| 168 | short length; |
| 169 | short frflags; |
| 170 | frame_t *frame; |
| 171 | } frameref_t; |
| 172 | |
| 173 | // a clause translated into phoneme codes (first stage) |
| 174 | typedef struct { |
| 175 | unsigned short synthflags; // NOTE Put shorts on 32bit boundaries, because of RISC OS compiler bug? |
| 176 | unsigned char phcode; |
| 177 | unsigned char stresslevel; |
| 178 | unsigned short sourceix; // ix into the original source text string, only set at the start of a word |
| 179 | unsigned char wordstress; // the highest level stress in this word |
| 180 | unsigned char tone_ph; // tone phoneme to use with this vowel |
| 181 | } PHONEME_LIST2; |
| 182 | |
| 183 | #define PHLIST_START_OF_WORD 1 |
| 184 | #define PHLIST_END_OF_CLAUSE 2 |
| 185 | #define PHLIST_START_OF_SENTENCE 4 |
| 186 | #define PHLIST_START_OF_CLAUSE 8 |
| 187 | |
| 188 | typedef struct { |
| 189 | // The first section is a copy of PHONEME_LIST2 |
| 190 | unsigned short synthflags; |
| 191 | unsigned char phcode; |
| 192 | unsigned char stresslevel; |
| 193 | unsigned short sourceix; // ix into the original source text string, only set at the start of a word |
| 194 | unsigned char wordstress; // the highest level stress in this word |
| 195 | unsigned char tone_ph; // tone phoneme to use with this vowel |
| 196 | |
| 197 | PHONEME_TAB *ph; |
| 198 | unsigned int length; // length_mod |
| 199 | unsigned char env; // pitch envelope number |
| 200 | unsigned char type; |
| 201 | unsigned char prepause; |
| 202 | unsigned char amp; |
| 203 | unsigned char newword; // bit flags, see PHLIST_(START|END)_OF_* |
| 204 | unsigned char pitch1; |
| 205 | unsigned char pitch2; |
| 206 | unsigned char std_length; |
| 207 | unsigned int phontab_addr; |
| 208 | int sound_param; |
| 209 | } PHONEME_LIST; |
| 210 | |
| 211 | #define pd_FMT 0 |
| 212 | #define pd_WAV 1 |
| 213 | #define pd_VWLSTART 2 |
| 214 | #define pd_VWLEND 3 |
| 215 | #define pd_ADDWAV 4 |
| 216 | |
| 217 | #define N_PHONEME_DATA_PARAM 16 |
| 218 | #define pd_INSERTPHONEME i_INSERT_PHONEME |
| 219 | #define pd_APPENDPHONEME i_APPEND_PHONEME |
| 220 | #define pd_CHANGEPHONEME i_CHANGE_PHONEME |
| 221 | #define pd_CHANGE_NEXTPHONEME i_REPLACE_NEXT_PHONEME |
| 222 | #define pd_LENGTHMOD i_SET_LENGTH |
| 223 | |
| 224 | #define pd_FORNEXTPH 0x2 |
| 225 | #define pd_DONTLENGTHEN 0x4 |
| 226 | |
| 227 | typedef struct { |
| 228 | int pd_control; |
| 229 | int pd_param[N_PHONEME_DATA_PARAM]; // set from group 0 instructions |
| 230 | int sound_addr[5]; |
| 231 | int sound_param[5]; |
| 232 | int vowel_transition[4]; |
| 233 | int pitch_env; |
| 234 | int amp_env; |
| 235 | char ipa_string[18]; |
| 236 | } PHONEME_DATA; |
| 237 | |
| 238 | typedef struct { |
| 239 | int fmt_control; |
| 240 | int use_vowelin; |
| 241 | int fmt_addr; |
| 242 | int fmt_length; |
| 243 | int fmt_amp; |
| 244 | int fmt2_addr; |
| 245 | int fmt2_lenadj; |
| 246 | int wav_addr; |
| 247 | int wav_amp; |
| 248 | int transition0; |
| 249 | int transition1; |
| 250 | int std_length; |
| 251 | } FMT_PARAMS; |
| 252 | |
| 253 | typedef struct { |
| 254 | PHONEME_LIST prev_vowel; |
| 255 | } WORD_PH_DATA; |
| 256 | |
| 257 | // instructions |
| 258 | |
| 259 | #define INSTN_RETURN 0x0001 |
| 260 | #define INSTN_CONTINUE 0x0002 |
| 261 | |
| 262 | // Group 0 instructions with 8 bit operand. These values go into bits 8-15 of the instruction |
| 263 | #define i_CHANGE_PHONEME 0x01 |
| 264 | #define i_REPLACE_NEXT_PHONEME 0x02 |
| 265 | #define i_INSERT_PHONEME 0x03 |
| 266 | #define i_APPEND_PHONEME 0x04 |
| 267 | #define i_APPEND_IFNEXTVOWEL 0x05 |
| 268 | #define i_VOICING_SWITCH 0x06 |
| 269 | #define i_PAUSE_BEFORE 0x07 |
| 270 | #define i_PAUSE_AFTER 0x08 |
| 271 | #define i_LENGTH_MOD 0x09 |
| 272 | #define i_SET_LENGTH 0x0a |
| 273 | #define i_LONG_LENGTH 0x0b |
| 274 | #define i_ADD_LENGTH 0x0c |
| 275 | #define i_IPA_NAME 0x0d |
| 276 | |
| 277 | #define i_CHANGE_IF 0x10 // 0x10 to 0x14 |
| 278 | |
| 279 | // conditions and jumps |
| 280 | #define i_CONDITION 0x2000 |
| 281 | #define i_OR 0x1000 // added to i_CONDITION |
| 282 | #define i_NOT 0x0003 |
| 283 | |
| 284 | #define i_JUMP 0x6000 |
| 285 | #define i_JUMP_FALSE 0x6800 |
| 286 | #define i_SWITCH_NEXTVOWEL 0x6a00 |
| 287 | #define i_SWITCH_PREVVOWEL 0x6c00 |
| 288 | #define MAX_JUMP 255 // max jump distance |
| 289 | |
| 290 | // multi-word instructions |
| 291 | #define i_CALLPH 0x9100 |
| 292 | #define i_PITCHENV 0x9200 |
| 293 | #define i_AMPENV 0x9300 |
| 294 | #define i_VOWELIN 0xa100 |
| 295 | #define i_VOWELOUT 0xa200 |
| 296 | #define i_FMT 0xb000 |
| 297 | #define i_WAV 0xc000 |
| 298 | #define i_VWLSTART 0xd000 |
| 299 | #define i_VWLENDING 0xe000 |
| 300 | #define i_WAVADD 0xf000 |
| 301 | |
| 302 | // conditions |
| 303 | #define CONDITION_IS_PHONEME_TYPE 0x00 |
| 304 | #define CONDITION_IS_PLACE_OF_ARTICULATION 0x20 |
| 305 | #define CONDITION_IS_PHFLAG_SET 0x40 |
| 306 | #define CONDITION_IS_OTHER 0x80 |
| 307 | |
| 308 | // other conditions (stress) |
| 309 | #define STRESS_IS_DIMINISHED 0 // diminished, unstressed within a word |
| 310 | #define STRESS_IS_UNSTRESSED 1 // unstressed, weak |
| 311 | #define STRESS_IS_NOT_STRESSED 2 // default, not stressed |
| 312 | #define STRESS_IS_SECONDARY 3 // secondary stress |
| 313 | #define STRESS_IS_PRIMARY 4 // primary (main) stress |
| 314 | #define STRESS_IS_PRIORITY 5 // replaces primary markers |
| 315 | |
| 316 | // other conditions |
| 317 | #define isAfterStress 9 |
| 318 | #define isNotVowel 10 |
| 319 | #define isFinalVowel 11 |
| 320 | #define isVoiced 12 // voiced consonant, or vowel |
| 321 | #define isFirstVowel 13 |
| 322 | #define isSecondVowel 14 |
| 323 | #define isTranslationGiven 16 // phoneme translation given in **_list or as [[...]] |
| 324 | #define isBreak 17 // pause phoneme or (stop/vstop/vfric not followed by vowel or (liquid in same word)) |
| 325 | #define isWordStart 18 |
| 326 | #define isWordEnd 19 |
| 327 | |
| 328 | #define i_StressLevel 0x800 |
| 329 | |
| 330 | typedef struct { |
| 331 | int pause_factor; |
| 332 | int clause_pause_factor; |
| 333 | unsigned int min_pause; |
| 334 | int wav_factor; |
| 335 | int lenmod_factor; |
| 336 | int lenmod2_factor; |
| 337 | int min_sample_len; |
| 338 | int fast_settings; // TODO: rename this variable to better explain the purpose, or delete if there is none |
| 339 | } SPEED_FACTORS; |
| 340 | |
| 341 | typedef struct { |
| 342 | char name[12]; |
| 343 | unsigned char flags[4]; |
| 344 | signed char head_extend[8]; |
| 345 | |
| 346 | unsigned char prehead_start; |
| 347 | unsigned char prehead_end; |
| 348 | unsigned char stressed_env; |
| 349 | unsigned char stressed_drop; |
| 350 | unsigned char secondary_drop; |
| 351 | unsigned char unstressed_shape; |
| 352 | |
| 353 | unsigned char onset; |
| 354 | unsigned char head_start; |
| 355 | unsigned char head_end; |
| 356 | unsigned char head_last; |
| 357 | |
| 358 | unsigned char head_max_steps; |
| 359 | unsigned char n_head_extend; |
| 360 | |
| 361 | signed char unstr_start[3]; // for: onset, head, last |
| 362 | signed char unstr_end[3]; |
| 363 | |
| 364 | unsigned char nucleus0_env; // pitch envelope, tonic syllable is at end, no tail |
| 365 | unsigned char nucleus0_max; |
| 366 | unsigned char nucleus0_min; |
| 367 | |
| 368 | unsigned char nucleus1_env; // when followed by a tail |
| 369 | unsigned char nucleus1_max; |
| 370 | unsigned char nucleus1_min; |
| 371 | unsigned char tail_start; |
| 372 | unsigned char tail_end; |
| 373 | |
| 374 | unsigned char split_nucleus_env; |
| 375 | unsigned char split_nucleus_max; |
| 376 | unsigned char split_nucleus_min; |
| 377 | unsigned char split_tail_start; |
| 378 | unsigned char split_tail_end; |
| 379 | unsigned char split_tune; |
| 380 | |
| 381 | unsigned char spare[8]; |
| 382 | int spare2; // the struct length should be a multiple of 4 bytes |
| 383 | } TUNE; |
| 384 | |
| 385 | |
| 386 | |
| 387 | // phoneme table |
| 388 | extern PHONEME_TAB *phoneme_tab[N_PHONEME_TAB]; |
| 389 | |
| 390 | // list of phonemes in a clause |
| 391 | extern int n_phoneme_list; |
| 392 | extern PHONEME_LIST phoneme_list[N_PHONEME_LIST+1]; |
| 393 | extern unsigned int embedded_list[]; |
| 394 | |
| 395 | extern const unsigned char env_fall[128]; |
| 396 | |
| 397 | // queue of commands for wavegen |
| 398 | #define WCMD_KLATT 1 |
| 399 | #define WCMD_KLATT2 2 |
| 400 | #define WCMD_SPECT 3 |
| 401 | #define WCMD_SPECT2 4 |
| 402 | #define WCMD_PAUSE 5 |
| 403 | #define WCMD_WAVE 6 |
| 404 | #define WCMD_WAVE2 7 |
| 405 | #define WCMD_AMPLITUDE 8 |
| 406 | #define WCMD_PITCH 9 |
| 407 | #define WCMD_MARKER 10 |
| 408 | #define WCMD_VOICE 11 |
| 409 | #define WCMD_EMBEDDED 12 |
| 410 | #define WCMD_MBROLA_DATA 13 |
| 411 | #define WCMD_FMT_AMPLITUDE 14 |
| 412 | #define WCMD_SONIC_SPEED 15 |
| 413 | #define WCMD_PHONEME_ALIGNMENT 16 |
| 414 | |
| 415 | #define N_WCMDQ 170 // normally 170 |
| 416 | #define MIN_WCMDQ 25 // need this many free entries before adding new phoneme |
| 417 | |
| 418 | extern intptr_t wcmdq[N_WCMDQ][4]; |
| 419 | extern int wcmdq_head; |
| 420 | extern int wcmdq_tail; |
| 421 | |
| 422 | void MarkerEvent(int type, unsigned int char_position, int value, int value2, unsigned char *out_ptr); |
| 423 | |
| 424 | extern unsigned char *wavefile_data; |
| 425 | extern int samplerate; |
| 426 | |
| 427 | #define N_ECHO_BUF 5500 // max of 250mS at 22050 Hz |
| 428 | extern int echo_head; |
| 429 | extern int echo_tail; |
| 430 | extern int echo_amp; |
| 431 | extern short echo_buf[N_ECHO_BUF]; |
| 432 | |
| 433 | void SynthesizeInit(void); |
| 434 | int Generate(PHONEME_LIST *phoneme_list, int *n_ph, bool resume); |
| 435 | int SpeakNextClause(int control); |
| 436 | void SetSpeed(int control); |
| 437 | void SetEmbedded(int control, int value); |
| 438 | int FormantTransition2(frameref_t *seq, int *n_frames, unsigned int data1, unsigned int data2, PHONEME_TAB *other_ph, int which); |
| 439 | |
| 440 | void Write4Bytes(FILE *f, int value); |
| 441 | |
| 442 | #if USE_LIBSONIC |
| 443 | void DoSonicSpeed(int value); |
| 444 | #endif |
| 445 | |
| 446 | #define ENV_LEN 128 // length of pitch envelopes |
| 447 | #define PITCHfall 0 // standard pitch envelopes |
| 448 | #define PITCHrise 2 |
| 449 | #define N_ENVELOPE_DATA 20 |
| 450 | extern const unsigned char *const envelope_data[N_ENVELOPE_DATA]; |
| 451 | |
| 452 | extern int formant_rate[]; // max rate of change of each formant |
| 453 | extern SPEED_FACTORS speed; |
| 454 | |
| 455 | extern unsigned char *out_ptr; |
| 456 | extern unsigned char *out_end; |
| 457 | extern espeak_EVENT *event_list; |
| 458 | extern const int version_phdata; |
| 459 | |
| 460 | void DoEmbedded(int *embix, int sourceix); |
| 461 | void DoMarker(int type, int char_posn, int length, int value); |
| 462 | void DoPhonemeMarker(int type, int char_posn, int length, char *name); |
| 463 | int DoSample3(PHONEME_DATA *phdata, int length_mod, int amp); |
| 464 | int DoSpect2(PHONEME_TAB *this_ph, int which, FMT_PARAMS *fmt_params, PHONEME_LIST *plist, int modulation); |
| 465 | int PauseLength(int pause, int control); |
| 466 | const char *WordToString(char buf[5], unsigned int word); |
| 467 | |
| 468 | #ifdef __cplusplus |
| 469 | } |
| 470 | #endif |
| 471 | |
| 472 | #endif |