eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

synthesize.h 15KB

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