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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2007 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #define N_PHONEME_LIST 1000 // enough for source[N_TR_SOURCE] full of text, else it will truncate
  20. #define MAX_HARMONIC 400 // 400 * 50Hz = 20 kHz, more than enough
  21. #define N_SEQ_FRAMES 25 // max frames in a spectrum sequence (real max is ablut 8)
  22. #define STEPSIZE 64 // 2.9mS at 22 kHz sample rate
  23. #define PITCHfall 0
  24. #define PITCHrise 1
  25. // flags set for frames within a spectrum sequence
  26. #define FRFLAG_KLATT 0x01 // this frame includes extra data for Klatt synthesizer
  27. #define FRFLAG_VOWEL_CENTRE 0x02 // centre point of vowel
  28. #define FRFLAG_LEN_MOD 0x04 // reduce effect of length adjustment
  29. #define FRFLAG_BREAK_LF 0x08 // but keep f3 upwards
  30. #define FRFLAG_BREAK 0x10 // don't merge with next frame
  31. #define FRFLAG_BREAK_2 0x18 // FRFLAG_BREAK_LF or FRFLAG_BREAK
  32. #define FRFLAG_FORMANT_RATE 0x20 // Flag5 allow increased rate of change of formant freq
  33. #define FRFLAG_MODULATE 0x40 // Flag6 modulate amplitude of some cycles to give trill
  34. #define FRFLAG_DEFER_WAV 0x80 // Flag7 defer mixing WAV until the next frame
  35. #define FRFLAG_COPIED 0x8000 // This frame has been copied into temporary rw memory
  36. #define SFLAG_SEQCONTINUE 0x01 // a liquid or nasal after a vowel, but not followed by a vowel
  37. #define SFLAG_EMBEDDED 0x02 // there are embedded commands before this phoneme
  38. #define SFLAG_SYLLABLE 0x04 // vowel or syllabic consonant
  39. #define SFLAG_LENGTHEN 0x08 // lengthen symbol : included after this phoneme
  40. #define SFLAG_DICTIONARY 0x10 // the pronunciation of this word was listed in the xx_list dictionary
  41. #define SFLAG_SWITCHED_LANG 0x20 // this word uses phonemes from a different language
  42. #define SFLAG_PROMOTE_STRESS 0x40 // this unstressed word can be promoted to stressed
  43. #define SFLAG_PREV_PAUSE 0x1000 // consider previous phoneme as pause
  44. #define SFLAG_NEXT_PAUSE 0x2000 // consider next phoneme as pause
  45. // embedded command numbers
  46. #define EMBED_P 1 // pitch
  47. #define EMBED_S 2 // speed (used in setlengths)
  48. #define EMBED_A 3 // amplitude/volume
  49. #define EMBED_R 4 // pitch range/expression
  50. #define EMBED_H 5 // echo/reverberation
  51. #define EMBED_T 6 // different tone for announcing punctuation
  52. #define EMBED_I 7 // sound icon
  53. #define EMBED_S2 8 // speed (used in synthesize)
  54. #define EMBED_Y 9 // say-as commands
  55. #define EMBED_M 10 // mark name
  56. #define EMBED_U 11 // audio uri
  57. #define EMBED_B 12 // break
  58. #define EMBED_F 13 // emphasis
  59. #define N_EMBEDDED_VALUES 14
  60. extern int embedded_value[N_EMBEDDED_VALUES];
  61. extern int embedded_default[N_EMBEDDED_VALUES];
  62. #define N_PEAKS 9
  63. #define N_MARKERS 8
  64. #define N_KLATTP 10 // this affects the phoneme data file format
  65. #define N_KLATTP2 14 // used in vowel files, with extra parameters for future extensions
  66. #define KLATT_AV 0
  67. #define KLATT_FNZ 1 // nasal zero freq
  68. #define KLATT_Tilt 2
  69. #define KLATT_Aspr 3
  70. #define KLATT_Skew 4
  71. #define KLATT_Kopen 5
  72. #define KLATT_AVp 6
  73. #define KLATT_Fric 7
  74. #define KLATT_FricBP 8
  75. #define KLATT_Turb 9
  76. typedef struct { // 44 bytes
  77. short frflags;
  78. short ffreq[7];
  79. unsigned char length;
  80. unsigned char rms;
  81. unsigned char fheight[8];
  82. unsigned char fwidth[6]; // width/4 f0-5
  83. unsigned char fright[3]; // width/4 f0-2
  84. unsigned char bw[4]; // Klatt bandwidth BNZ /2, f1,f2,f3
  85. unsigned char klattp[5]; // AV, FNZ, Tilt, Aspr, Skew
  86. unsigned char klattp2[5]; // continuation of klattp[], Avp, Fric, FricBP, Turb
  87. unsigned char klatt_ap[7]; // Klatt parallel amplitude
  88. unsigned char klatt_bp[7]; // Klatt parallel bandwidth /2
  89. } frame_t; // with extra Klatt parameters for parallel resonators
  90. typedef struct { // 44 bytes
  91. short frflags;
  92. short ffreq[7];
  93. unsigned char length;
  94. unsigned char rms;
  95. unsigned char fheight[8];
  96. unsigned char fwidth[6]; // width/4 f0-5
  97. unsigned char fright[3]; // width/4 f0-2
  98. unsigned char bw[4]; // Klatt bandwidth BNZ /2, f1,f2,f3
  99. unsigned char klattp[5]; // AV, FNZ, Tilt, Aspr, Skew
  100. } frame_t2; // TESTING
  101. #ifdef deleted
  102. typedef struct {
  103. short frflags;
  104. unsigned char length;
  105. unsigned char rms;
  106. short ffreq[9];
  107. unsigned char fheight[9];
  108. unsigned char fwidth[6]; // width/4
  109. unsigned char fright[6]; // width/4
  110. unsigned char fwidth6, fright6;
  111. unsigned char klattp[N_KLATTP];
  112. } frame_t;
  113. typedef struct { // 43 bytes
  114. short frflags;
  115. unsigned char length;
  116. unsigned char rms;
  117. short ffreq[9];
  118. unsigned char fheight[9];
  119. unsigned char fwidth[6]; // width/4
  120. unsigned char fright[6]; // width/4
  121. } frame_t2; // the original, without Klatt additions, used for file "phondata"
  122. #endif
  123. // formant data used by wavegen
  124. typedef struct {
  125. int freq; // Hz<<16
  126. int height; // height<<15
  127. int left; // Hz<<16
  128. int right; // Hz<<16
  129. DOUBLEX freq1; // floating point versions of the above
  130. DOUBLEX height1;
  131. DOUBLEX left1;
  132. DOUBLEX right1;
  133. DOUBLEX freq_inc; // increment by this every 64 samples
  134. DOUBLEX height_inc;
  135. DOUBLEX left_inc;
  136. DOUBLEX right_inc;
  137. } wavegen_peaks_t;
  138. typedef struct {
  139. unsigned char *pitch_env;
  140. int pitch; // pitch Hz*256
  141. int pitch_ix; // index into pitch envelope (*256)
  142. int pitch_inc; // increment to pitch_ix
  143. int pitch_base; // Hz*256 low, before modified by envelope
  144. int pitch_range; // Hz*256 range of envelope
  145. unsigned char *mix_wavefile; // wave file to be added to synthesis
  146. int n_mix_wavefile; // length in bytes
  147. int mix_wave_scale; // 0=2 byte samples
  148. int mix_wave_amp;
  149. int mix_wavefile_ix;
  150. int mix_wavefile_max; // length of available WAV data (in bytes)
  151. int mix_wavefile_offset;
  152. int amplitude;
  153. int amplitude_v;
  154. } WGEN_DATA;
  155. typedef struct {
  156. double a;
  157. double b;
  158. double c;
  159. double x1;
  160. double x2;
  161. } RESONATOR;
  162. typedef struct {
  163. short length_total; // not used
  164. unsigned char n_frames;
  165. unsigned char flags;
  166. frame_t2 frame[N_SEQ_FRAMES]; // max. frames in a spectrum sequence
  167. } SPECT_SEQ; // sequence of espeak formant frames
  168. typedef struct {
  169. short length_total; // not used
  170. unsigned char n_frames;
  171. unsigned char flags;
  172. frame_t frame[N_SEQ_FRAMES]; // max. frames in a spectrum sequence
  173. } SPECT_SEQK; // sequence of klatt formants frames
  174. typedef struct {
  175. short length;
  176. short frflags;
  177. frame_t *frame;
  178. } frameref_t;
  179. // a clause translated into phoneme codes (first stage)
  180. typedef struct {
  181. unsigned char phcode;
  182. unsigned char stresslevel;
  183. unsigned char wordstress;
  184. unsigned char tone_ph; // tone phoneme to use with this vowel
  185. unsigned short synthflags;
  186. unsigned short sourceix; // ix into the original source text string, only set at the start of a word
  187. } PHONEME_LIST2;
  188. typedef struct {
  189. // The first section is a copy of PHONEME_LIST2
  190. unsigned char phcode;
  191. unsigned char stresslevel;
  192. unsigned char wordstress;
  193. unsigned char tone_ph; // tone phoneme to use with this vowel
  194. unsigned short synthflags;
  195. unsigned short sourceix; // ix into the original source text string, only set at the start of a word
  196. PHONEME_TAB *ph;
  197. unsigned char env; // pitch envelope number
  198. unsigned char type;
  199. unsigned char prepause;
  200. unsigned char postpause;
  201. unsigned char amp;
  202. unsigned char newword; // bit 0=start of word, bit 1=end of clause, bit 2=start of sentence
  203. short length; // length_mod
  204. short pitch1; // pitch, 0-4095 within the Voice's pitch range
  205. short pitch2;
  206. } PHONEME_LIST;
  207. #define pd_FMT 0
  208. #define pd_WAV 1
  209. #define pd_VWLSTART 2
  210. #define pd_VWLEND 3
  211. #define pd_ADDWAV 4
  212. #define N_PHONEME_DATA_PARAM 16
  213. #define pd_APPENDPHONEME i_APPEND_PHONEME
  214. #define pd_CHANGEPHONEME i_CHANGE_PHONEME
  215. #define pd_LENGTHMOD i_SET_LENGTH
  216. #define pd_FORNEXTPH 0x2
  217. #define pd_DONTLENGTHEN 0x4
  218. #define pd_REDUCELENGTHCHANGE 0x8
  219. typedef struct {
  220. int pd_control;
  221. int pd_param[N_PHONEME_DATA_PARAM]; // set from group 0 instructions
  222. int sound_addr[5];
  223. int sound_param[5];
  224. int vowel_transition[4];
  225. int pitch_env;
  226. int amp_env;
  227. } PHONEME_DATA;
  228. typedef struct {
  229. int fmt_control;
  230. int use_vowelin;
  231. int fmt_addr;
  232. int fmt_length;
  233. int fmt2_addr;
  234. int fmt2_lenadj;
  235. int wav_addr;
  236. int wav_amp;
  237. int transition0;
  238. int transition1;
  239. int std_length;
  240. } FMT_PARAMS;
  241. // instructions
  242. #define i_RETURN 0x0001
  243. #define i_CONTINUE 0x0002
  244. // Group 0 instrcutions with 8 bit operand. These value go into bits 8-15 if the instruction
  245. #define i_CHANGE_PHONEME 0x01
  246. #define i_REPLACE_NEXT_PHONEME 0x02
  247. #define i_INSERT_PHONEME 0x03
  248. #define i_APPEND_PHONEME 0x04
  249. #define i_APPEND_IFNEXTVOWEL 0x05
  250. #define i_VOICING_SWITCH 0x06
  251. #define i_PAUSE_BEFORE 0x07
  252. #define i_PAUSE_AFTER 0x08
  253. #define i_LENGTH_MOD 0x09
  254. #define i_SET_LENGTH 0x0a
  255. #define i_LONG_LENGTH 0x0b
  256. #define i_CHANGE_PHONEME2 0x0c // not yet used
  257. #define i_CHANGE_IF 0x10 // 0x10 to 0x14
  258. #define i_ADD_LENGTH 0x0c
  259. // conditions and jumps
  260. #define i_CONDITION 0x2000
  261. #define i_OR 0x1000 // added to i_CONDITION
  262. #define i_JUMP 0x6000
  263. #define i_JUMP_FALSE 0x6800
  264. #define i_SWITCH_NEXTVOWEL 0x6a00
  265. #define i_SWITCH_PREVVOWEL 0x6c00
  266. #define MAX_JUMP 255 // max jump distance
  267. // multi-word instructions
  268. #define i_CALLPH 0x9100
  269. #define i_PITCHENV 0x9200
  270. #define i_AMPENV 0x9300
  271. #define i_VOWELIN 0xa100
  272. #define i_VOWELOUT 0xa200
  273. #define i_FMT 0xb000
  274. #define i_WAV 0xc000
  275. #define i_VWLSTART 0xd000
  276. #define i_VWLENDING 0xe000
  277. #define i_WAVADD 0xf000
  278. // conditions
  279. #define i_isDiminished 0x80
  280. #define i_isUnstressed 0x81
  281. #define i_isNotStressed 0x82
  282. #define i_isStressed 0x83
  283. #define i_isMaxStress 0x84
  284. #define i_isBreak 0x85
  285. #define i_isWordStart 0x86
  286. #define i_notWordStart 0x87
  287. #define i_isWordEnd 0x88
  288. #define i_isAfterStress 0x89
  289. #define i_isNotVowel 0x8a
  290. #define i_isFinalVowel 0x8b
  291. #define i_isVoiced 0x8c
  292. #define i_isPalatal 0x49 // bit 9 in phflags
  293. #define i_isRhotic 0x56 // bit 22 in phflags
  294. #define i_StressLevel 0x800
  295. typedef struct {
  296. int name;
  297. int length;
  298. char *data;
  299. char *filename;
  300. } SOUND_ICON;
  301. typedef struct {
  302. int name;
  303. unsigned int next_phoneme;
  304. int mbr_name;
  305. int mbr_name2;
  306. int percent; // percentage length of first component
  307. int control;
  308. } MBROLA_TAB;
  309. typedef struct {
  310. int speed_factor1;
  311. int speed_factor2;
  312. int speed_factor3;
  313. int min_sample_len;
  314. int fast_settings[8];
  315. } SPEED_FACTORS;
  316. // phoneme table
  317. extern PHONEME_TAB *phoneme_tab[N_PHONEME_TAB];
  318. // list of phonemes in a clause
  319. extern int n_phoneme_list;
  320. extern PHONEME_LIST phoneme_list[N_PHONEME_LIST];
  321. extern unsigned int embedded_list[];
  322. extern unsigned char env_fall[128];
  323. extern unsigned char env_rise[128];
  324. extern unsigned char env_frise[128];
  325. #define MAX_PITCH_VALUE 101
  326. extern unsigned char pitch_adjust_tab[MAX_PITCH_VALUE+1];
  327. // queue of commands for wavegen
  328. #define WCMD_KLATT 1
  329. #define WCMD_KLATT2 2
  330. #define WCMD_SPECT 3
  331. #define WCMD_SPECT2 4
  332. #define WCMD_PAUSE 5
  333. #define WCMD_WAVE 6
  334. #define WCMD_WAVE2 7
  335. #define WCMD_AMPLITUDE 8
  336. #define WCMD_PITCH 9
  337. #define WCMD_MARKER 10
  338. #define WCMD_VOICE 11
  339. #define WCMD_EMBEDDED 12
  340. #define N_WCMDQ 160
  341. #define MIN_WCMDQ 22 // need this many free entries before adding new phoneme
  342. extern long wcmdq[N_WCMDQ][4];
  343. extern int wcmdq_head;
  344. extern int wcmdq_tail;
  345. // from Wavegen file
  346. int WcmdqFree();
  347. void WcmdqStop();
  348. int WcmdqUsed();
  349. void WcmdqInc();
  350. int WavegenOpenSound();
  351. int WavegenCloseSound();
  352. int WavegenInitSound();
  353. void WavegenInit(int rate, int wavemult_fact);
  354. float polint(float xa[],float ya[],int n,float x);
  355. int WavegenFill(int fill_zeros);
  356. void MarkerEvent(int type, unsigned int char_position, int value, unsigned char *out_ptr);
  357. extern unsigned char *wavefile_data;
  358. extern int samplerate;
  359. extern int samplerate_native;
  360. extern int wavefile_ix;
  361. extern int wavefile_amp;
  362. extern int wavefile_ix2;
  363. extern int wavefile_amp2;
  364. extern int vowel_transition[4];
  365. extern int vowel_transition0, vowel_transition1;
  366. extern int mbrola_delay;
  367. extern char mbrola_name[20];
  368. // from synthdata file
  369. unsigned int LookupSound(PHONEME_TAB *ph1, PHONEME_TAB *ph2, int which, int *match_level, int control);
  370. frameref_t *LookupSpect(PHONEME_TAB *this_ph, int which, FMT_PARAMS *fmt_params, int *n_frames, PHONEME_LIST *plist);
  371. unsigned char *LookupEnvelope(int ix);
  372. int LoadPhData();
  373. void SynthesizeInit(void);
  374. int Generate(PHONEME_LIST *phoneme_list, int *n_ph, int resume);
  375. void MakeWave2(PHONEME_LIST *p, int n_ph);
  376. int SynthOnTimer(void);
  377. int SpeakNextClause(FILE *f_text, const void *text_in, int control);
  378. int SynthStatus(void);
  379. void SetSpeed(int control);
  380. void SetEmbedded(int control, int value);
  381. void SelectPhonemeTable(int number);
  382. int SelectPhonemeTableName(const char *name);
  383. void Write4Bytes(FILE *f, int value);
  384. int Read4Bytes(FILE *f);
  385. int CompileDictionary(const char *dsource, const char *dict_name, FILE *log, char *err_name,int flags);
  386. extern unsigned char *envelope_data[18];
  387. extern int formant_rate[]; // max rate of change of each formant
  388. extern SPEED_FACTORS speed;
  389. extern long count_samples;
  390. extern int outbuf_size;
  391. extern unsigned char *out_ptr;
  392. extern unsigned char *out_start;
  393. extern unsigned char *out_end;
  394. extern int event_list_ix;
  395. extern espeak_EVENT *event_list;
  396. extern t_espeak_callback* synth_callback;
  397. extern int option_log_frames;
  398. extern const char *version_string;
  399. extern const int version_phdata;
  400. #define N_SOUNDICON_TAB 80 // total entries in soundicon_tab
  401. #define N_SOUNDICON_SLOTS 4 // number of slots reserved for dynamic loading of audio files
  402. extern int n_soundicon_tab;
  403. extern SOUND_ICON soundicon_tab[N_SOUNDICON_TAB];
  404. espeak_ERROR SetVoiceByName(const char *name);
  405. espeak_ERROR SetVoiceByProperties(espeak_VOICE *voice_selector);
  406. espeak_ERROR LoadMbrolaTable(const char *mbrola_voice, const char *phtrans, int srate);
  407. void SetParameter(int parameter, int value, int relative);
  408. void MbrolaTranslate(PHONEME_LIST *plist, int n_phonemes, FILE *f_mbrola);
  409. //int MbrolaSynth(char *p_mbrola);
  410. //int DoSample(PHONEME_TAB *ph1, PHONEME_TAB *ph2, int which, int length_mod, int amp);
  411. int DoSample3(PHONEME_DATA *phdata, int length_mod, int amp);
  412. int DoSpect2(PHONEME_TAB *this_ph, int which, FMT_PARAMS *fmt_params, PHONEME_LIST *plist, int modulation);
  413. int PauseLength(int pause, int control);
  414. int LookupPhonemeTable(const char *name);
  415. void InitBreath(void);
  416. void KlattInit();
  417. void KlattReset(int control);
  418. int Wavegen_Klatt2(int length, int modulation, int resume, frame_t *fr1, frame_t *fr2);