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.c 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015-2016 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. #include "config.h"
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #include <math.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <wctype.h>
  28. #include <espeak-ng/espeak_ng.h>
  29. #include <espeak/speak_lib.h>
  30. #include "speech.h"
  31. #include "phoneme.h"
  32. #include "synthesize.h"
  33. #include "voice.h"
  34. #include "translate.h"
  35. extern FILE *f_log;
  36. static void SmoothSpect(void);
  37. // list of phonemes in a clause
  38. int n_phoneme_list = 0;
  39. PHONEME_LIST phoneme_list[N_PHONEME_LIST+1];
  40. int mbrola_delay;
  41. char mbrola_name[20];
  42. SPEED_FACTORS speed;
  43. static int last_pitch_cmd;
  44. static int last_amp_cmd;
  45. static frame_t *last_frame;
  46. static int last_wcmdq;
  47. static int pitch_length;
  48. static int amp_length;
  49. static int modn_flags;
  50. static int fmt_amplitude = 0;
  51. static int syllable_start;
  52. static int syllable_end;
  53. static int syllable_centre;
  54. static voice_t *new_voice = NULL;
  55. int n_soundicon_tab = N_SOUNDICON_SLOTS;
  56. SOUND_ICON soundicon_tab[N_SOUNDICON_TAB];
  57. #define RMS_GLOTTAL1 35 // vowel before glottal stop
  58. #define RMS_START 28 // 28
  59. #define VOWEL_FRONT_LENGTH 50
  60. // a dummy phoneme_list entry which looks like a pause
  61. static PHONEME_LIST next_pause;
  62. const char *WordToString(unsigned int word)
  63. {
  64. // Convert a phoneme mnemonic word into a string
  65. int ix;
  66. static char buf[5];
  67. for (ix = 0; ix < 4; ix++)
  68. buf[ix] = word >> (ix*8);
  69. buf[4] = 0;
  70. return buf;
  71. }
  72. void SynthesizeInit()
  73. {
  74. last_pitch_cmd = 0;
  75. last_amp_cmd = 0;
  76. last_frame = NULL;
  77. syllable_centre = -1;
  78. // initialise next_pause, a dummy phoneme_list entry
  79. next_pause.type = phPAUSE;
  80. next_pause.newword = 0;
  81. }
  82. static void EndAmplitude(void)
  83. {
  84. if (amp_length > 0) {
  85. if (wcmdq[last_amp_cmd][1] == 0)
  86. wcmdq[last_amp_cmd][1] = amp_length;
  87. amp_length = 0;
  88. }
  89. }
  90. static void EndPitch(int voice_break)
  91. {
  92. // posssible end of pitch envelope, fill in the length
  93. if ((pitch_length > 0) && (last_pitch_cmd >= 0)) {
  94. if (wcmdq[last_pitch_cmd][1] == 0)
  95. wcmdq[last_pitch_cmd][1] = pitch_length;
  96. pitch_length = 0;
  97. }
  98. if (voice_break) {
  99. last_wcmdq = -1;
  100. last_frame = NULL;
  101. syllable_end = wcmdq_tail;
  102. SmoothSpect();
  103. syllable_centre = -1;
  104. memset(vowel_transition, 0, sizeof(vowel_transition));
  105. }
  106. }
  107. static void DoAmplitude(int amp, unsigned char *amp_env)
  108. {
  109. intptr_t *q;
  110. last_amp_cmd = wcmdq_tail;
  111. amp_length = 0; // total length of vowel with this amplitude envelope
  112. q = wcmdq[wcmdq_tail];
  113. q[0] = WCMD_AMPLITUDE;
  114. q[1] = 0; // fill in later from amp_length
  115. q[2] = (intptr_t)amp_env;
  116. q[3] = amp;
  117. WcmdqInc();
  118. }
  119. static void DoPitch(unsigned char *env, int pitch1, int pitch2)
  120. {
  121. intptr_t *q;
  122. EndPitch(0);
  123. if (pitch1 == 255) {
  124. // pitch was not set
  125. pitch1 = 55;
  126. pitch2 = 76;
  127. env = envelope_data[PITCHfall];
  128. }
  129. last_pitch_cmd = wcmdq_tail;
  130. pitch_length = 0; // total length of spect with this pitch envelope
  131. if (pitch2 < 0)
  132. pitch2 = 0;
  133. q = wcmdq[wcmdq_tail];
  134. q[0] = WCMD_PITCH;
  135. q[1] = 0; // length, fill in later from pitch_length
  136. q[2] = (intptr_t)env;
  137. q[3] = (pitch1 << 16) + pitch2;
  138. WcmdqInc();
  139. }
  140. int PauseLength(int pause, int control)
  141. {
  142. unsigned int len;
  143. if (control == 0) {
  144. if (pause >= 200)
  145. len = (pause * speed.clause_pause_factor)/256;
  146. else
  147. len = (pause * speed.pause_factor)/256;
  148. } else
  149. len = (pause * speed.wav_factor)/256;
  150. if (len < speed.min_pause)
  151. len = speed.min_pause; // mS, limit the amount to which pauses can be shortened
  152. return len;
  153. }
  154. static void DoPause(int length, int control)
  155. {
  156. // length in nominal mS
  157. // control = 1, less shortening at fast speeds
  158. unsigned int len;
  159. int srate2;
  160. if (length == 0)
  161. len = 0;
  162. else {
  163. len = PauseLength(length, control);
  164. if (len < 90000)
  165. len = (len * samplerate) / 1000; // convert from mS to number of samples
  166. else {
  167. srate2 = samplerate / 25; // avoid overflow
  168. len = (len * srate2) / 40;
  169. }
  170. }
  171. EndPitch(1);
  172. wcmdq[wcmdq_tail][0] = WCMD_PAUSE;
  173. wcmdq[wcmdq_tail][1] = len;
  174. WcmdqInc();
  175. last_frame = NULL;
  176. if (fmt_amplitude != 0) {
  177. wcmdq[wcmdq_tail][0] = WCMD_FMT_AMPLITUDE;
  178. wcmdq[wcmdq_tail][1] = fmt_amplitude = 0;
  179. WcmdqInc();
  180. }
  181. }
  182. extern int seq_len_adjust; // temporary fix to advance the start point for playing the wav sample
  183. static int DoSample2(int index, int which, int std_length, int control, int length_mod, int amp)
  184. {
  185. int length;
  186. int wav_length;
  187. int wav_scale;
  188. int min_length;
  189. int x;
  190. int len4;
  191. intptr_t *q;
  192. unsigned char *p;
  193. index = index & 0x7fffff;
  194. p = &wavefile_data[index];
  195. wav_scale = p[2];
  196. wav_length = (p[1] * 256);
  197. wav_length += p[0]; // length in bytes
  198. if (wav_length == 0)
  199. return 0;
  200. min_length = speed.min_sample_len;
  201. if (wav_scale == 0)
  202. min_length *= 2; // 16 bit samples
  203. if (std_length > 0) {
  204. std_length = (std_length * samplerate)/1000;
  205. if (wav_scale == 0)
  206. std_length *= 2;
  207. x = (min_length * std_length)/wav_length;
  208. if (x > min_length)
  209. min_length = x;
  210. } else {
  211. // no length specified, use the length of the stored sound
  212. std_length = wav_length;
  213. }
  214. if (length_mod > 0)
  215. std_length = (std_length * length_mod)/256;
  216. length = (std_length * speed.wav_factor)/256;
  217. if (control & pd_DONTLENGTHEN) {
  218. // this option is used for Stops, with short noise bursts.
  219. // Don't change their length much.
  220. if (length > std_length) {
  221. // don't let length exceed std_length
  222. length = std_length;
  223. }
  224. }
  225. if (length < min_length)
  226. length = min_length;
  227. if (wav_scale == 0) {
  228. // 16 bit samples
  229. length /= 2;
  230. wav_length /= 2;
  231. }
  232. if (amp < 0)
  233. return length;
  234. len4 = wav_length / 4;
  235. index += 4;
  236. if (which & 0x100) {
  237. // mix this with synthesised wave
  238. last_wcmdq = wcmdq_tail;
  239. q = wcmdq[wcmdq_tail];
  240. q[0] = WCMD_WAVE2;
  241. q[1] = length | (wav_length << 16); // length in samples
  242. q[2] = (intptr_t)(&wavefile_data[index]);
  243. q[3] = wav_scale + (amp << 8);
  244. WcmdqInc();
  245. return length;
  246. }
  247. if (length > wav_length) {
  248. x = len4*3;
  249. length -= x;
  250. } else {
  251. x = length;
  252. length = 0;
  253. }
  254. last_wcmdq = wcmdq_tail;
  255. q = wcmdq[wcmdq_tail];
  256. q[0] = WCMD_WAVE;
  257. q[1] = x; // length in samples
  258. q[2] = (intptr_t)(&wavefile_data[index]);
  259. q[3] = wav_scale + (amp << 8);
  260. WcmdqInc();
  261. while (length > len4*3) {
  262. x = len4;
  263. if (wav_scale == 0)
  264. x *= 2;
  265. last_wcmdq = wcmdq_tail;
  266. q = wcmdq[wcmdq_tail];
  267. q[0] = WCMD_WAVE;
  268. q[1] = len4*2; // length in samples
  269. q[2] = (intptr_t)(&wavefile_data[index+x]);
  270. q[3] = wav_scale + (amp << 8);
  271. WcmdqInc();
  272. length -= len4*2;
  273. }
  274. if (length > 0) {
  275. x = wav_length - length;
  276. if (wav_scale == 0)
  277. x *= 2;
  278. last_wcmdq = wcmdq_tail;
  279. q = wcmdq[wcmdq_tail];
  280. q[0] = WCMD_WAVE;
  281. q[1] = length; // length in samples
  282. q[2] = (intptr_t)(&wavefile_data[index+x]);
  283. q[3] = wav_scale + (amp << 8);
  284. WcmdqInc();
  285. }
  286. return length;
  287. }
  288. int DoSample3(PHONEME_DATA *phdata, int length_mod, int amp)
  289. {
  290. int amp2;
  291. int len;
  292. EndPitch(1);
  293. if (amp == -1) {
  294. // just get the length, don't produce sound
  295. amp2 = amp;
  296. } else {
  297. amp2 = phdata->sound_param[pd_WAV];
  298. if (amp2 == 0)
  299. amp2 = 100;
  300. amp2 = (amp2 * 32)/100;
  301. }
  302. seq_len_adjust = 0;
  303. if (phdata->sound_addr[pd_WAV] == 0)
  304. len = 0;
  305. else
  306. len = DoSample2(phdata->sound_addr[pd_WAV], 2, phdata->pd_param[pd_LENGTHMOD]*2, phdata->pd_control, length_mod, amp2);
  307. last_frame = NULL;
  308. return len;
  309. }
  310. static frame_t *AllocFrame()
  311. {
  312. // Allocate a temporary spectrum frame for the wavegen queue. Use a pool which is big
  313. // enough to use a round-robin without checks.
  314. // Only needed for modifying spectra for blending to consonants
  315. #define N_FRAME_POOL N_WCMDQ
  316. static int ix = 0;
  317. static frame_t frame_pool[N_FRAME_POOL];
  318. ix++;
  319. if (ix >= N_FRAME_POOL)
  320. ix = 0;
  321. return &frame_pool[ix];
  322. }
  323. static void set_frame_rms(frame_t *fr, int new_rms)
  324. {
  325. // Each frame includes its RMS amplitude value, so to set a new
  326. // RMS just adjust the formant amplitudes by the appropriate ratio
  327. int x;
  328. int h;
  329. int ix;
  330. static const short sqrt_tab[200] = {
  331. 0, 64, 90, 110, 128, 143, 156, 169, 181, 192, 202, 212, 221, 230, 239, 247,
  332. 256, 263, 271, 278, 286, 293, 300, 306, 313, 320, 326, 332, 338, 344, 350, 356,
  333. 362, 367, 373, 378, 384, 389, 394, 399, 404, 409, 414, 419, 424, 429, 434, 438,
  334. 443, 448, 452, 457, 461, 465, 470, 474, 478, 483, 487, 491, 495, 499, 503, 507,
  335. 512, 515, 519, 523, 527, 531, 535, 539, 543, 546, 550, 554, 557, 561, 565, 568,
  336. 572, 576, 579, 583, 586, 590, 593, 596, 600, 603, 607, 610, 613, 617, 620, 623,
  337. 627, 630, 633, 636, 640, 643, 646, 649, 652, 655, 658, 662, 665, 668, 671, 674,
  338. 677, 680, 683, 686, 689, 692, 695, 698, 701, 704, 706, 709, 712, 715, 718, 721,
  339. 724, 726, 729, 732, 735, 738, 740, 743, 746, 749, 751, 754, 757, 759, 762, 765,
  340. 768, 770, 773, 775, 778, 781, 783, 786, 789, 791, 794, 796, 799, 801, 804, 807,
  341. 809, 812, 814, 817, 819, 822, 824, 827, 829, 832, 834, 836, 839, 841, 844, 846,
  342. 849, 851, 853, 856, 858, 861, 863, 865, 868, 870, 872, 875, 877, 879, 882, 884,
  343. 886, 889, 891, 893, 896, 898, 900, 902
  344. };
  345. if (voice->klattv[0]) {
  346. if (new_rms == -1)
  347. fr->klattp[KLATT_AV] = 50;
  348. return;
  349. }
  350. if (fr->rms == 0) return; // check for divide by zero
  351. x = (new_rms * 64)/fr->rms;
  352. if (x >= 200) x = 199;
  353. x = sqrt_tab[x]; // sqrt(new_rms/fr->rms)*0x200;
  354. for (ix = 0; ix < 8; ix++) {
  355. h = fr->fheight[ix] * x;
  356. fr->fheight[ix] = h/0x200;
  357. }
  358. }
  359. static void formants_reduce_hf(frame_t *fr, int level)
  360. {
  361. // change height of peaks 2 to 8, percentage
  362. int ix;
  363. int x;
  364. if (voice->klattv[0])
  365. return;
  366. for (ix = 2; ix < 8; ix++) {
  367. x = fr->fheight[ix] * level;
  368. fr->fheight[ix] = x/100;
  369. }
  370. }
  371. static frame_t *CopyFrame(frame_t *frame1, int copy)
  372. {
  373. // create a copy of the specified frame in temporary buffer
  374. frame_t *frame2;
  375. if ((copy == 0) && (frame1->frflags & FRFLAG_COPIED)) {
  376. // this frame has already been copied in temporary rw memory
  377. return frame1;
  378. }
  379. frame2 = AllocFrame();
  380. if (frame2 != NULL) {
  381. memcpy(frame2, frame1, sizeof(frame_t));
  382. frame2->length = 0;
  383. frame2->frflags |= FRFLAG_COPIED;
  384. }
  385. return frame2;
  386. }
  387. static frame_t *DuplicateLastFrame(frameref_t *seq, int n_frames, int length)
  388. {
  389. frame_t *fr;
  390. seq[n_frames-1].length = length;
  391. fr = CopyFrame(seq[n_frames-1].frame, 1);
  392. seq[n_frames].frame = fr;
  393. seq[n_frames].length = 0;
  394. return fr;
  395. }
  396. static void AdjustFormants(frame_t *fr, int target, int min, int max, int f1_adj, int f3_adj, int hf_reduce, int flags)
  397. {
  398. int x;
  399. target = (target * voice->formant_factor)/256;
  400. x = (target - fr->ffreq[2]) / 2;
  401. if (x > max) x = max;
  402. if (x < min) x = min;
  403. fr->ffreq[2] += x;
  404. fr->ffreq[3] += f3_adj;
  405. if (flags & 0x20)
  406. f3_adj = -f3_adj; // reverse direction for f4,f5 change
  407. fr->ffreq[4] += f3_adj;
  408. fr->ffreq[5] += f3_adj;
  409. if (f1_adj == 1) {
  410. x = (235 - fr->ffreq[1]);
  411. if (x < -100) x = -100;
  412. if (x > -60) x = -60;
  413. fr->ffreq[1] += x;
  414. }
  415. if (f1_adj == 2) {
  416. x = (235 - fr->ffreq[1]);
  417. if (x < -300) x = -300;
  418. if (x > -150) x = -150;
  419. fr->ffreq[1] += x;
  420. fr->ffreq[0] += x;
  421. }
  422. if (f1_adj == 3) {
  423. x = (100 - fr->ffreq[1]);
  424. if (x < -400) x = -400;
  425. if (x > -300) x = -400;
  426. fr->ffreq[1] += x;
  427. fr->ffreq[0] += x;
  428. }
  429. formants_reduce_hf(fr, hf_reduce);
  430. }
  431. static int VowelCloseness(frame_t *fr)
  432. {
  433. // return a value 0-3 depending on the vowel's f1
  434. int f1;
  435. if ((f1 = fr->ffreq[1]) < 300)
  436. return 3;
  437. if (f1 < 400)
  438. return 2;
  439. if (f1 < 500)
  440. return 1;
  441. return 0;
  442. }
  443. int FormantTransition2(frameref_t *seq, int *n_frames, unsigned int data1, unsigned int data2, PHONEME_TAB *other_ph, int which)
  444. {
  445. int ix;
  446. int formant;
  447. int next_rms;
  448. int len;
  449. int rms;
  450. int f1;
  451. int f2;
  452. int f2_min;
  453. int f2_max;
  454. int f3_adj;
  455. int f3_amp;
  456. int flags;
  457. int vcolour;
  458. #define N_VCOLOUR 2
  459. // percentage change for each formant in 256ths
  460. static short vcolouring[N_VCOLOUR][5] = {
  461. { 243, 272, 256, 256, 256 }, // palatal consonant follows
  462. { 256, 256, 240, 240, 240 }, // retroflex
  463. };
  464. frame_t *fr = NULL;
  465. if (*n_frames < 2)
  466. return 0;
  467. len = (data1 & 0x3f) * 2;
  468. rms = (data1 >> 6) & 0x3f;
  469. flags = (data1 >> 12);
  470. f2 = (data2 & 0x3f) * 50;
  471. f2_min = (((data2 >> 6) & 0x1f) - 15) * 50;
  472. f2_max = (((data2 >> 11) & 0x1f) - 15) * 50;
  473. f3_adj = (((data2 >> 16) & 0x1f) - 15) * 50;
  474. f3_amp = ((data2 >> 21) & 0x1f) * 8;
  475. f1 = ((data2 >> 26) & 0x7);
  476. vcolour = (data2 >> 29);
  477. if ((other_ph != NULL) && (other_ph->mnemonic == '?'))
  478. flags |= 8;
  479. if (which == 1) {
  480. // entry to vowel
  481. fr = CopyFrame(seq[0].frame, 0);
  482. seq[0].frame = fr;
  483. seq[0].length = VOWEL_FRONT_LENGTH;
  484. if (len > 0)
  485. seq[0].length = len;
  486. seq[0].frflags |= FRFLAG_LEN_MOD2; // reduce length modification
  487. fr->frflags |= FRFLAG_LEN_MOD2;
  488. next_rms = seq[1].frame->rms;
  489. if (voice->klattv[0])
  490. fr->klattp[KLATT_AV] = seq[1].frame->klattp[KLATT_AV] - 4;
  491. if (f2 != 0) {
  492. if (rms & 0x20)
  493. set_frame_rms(fr, (next_rms * (rms & 0x1f))/30);
  494. AdjustFormants(fr, f2, f2_min, f2_max, f1, f3_adj, f3_amp, flags);
  495. if ((rms & 0x20) == 0)
  496. set_frame_rms(fr, rms*2);
  497. } else {
  498. if (flags & 8)
  499. set_frame_rms(fr, (next_rms*24)/32);
  500. else
  501. set_frame_rms(fr, RMS_START);
  502. }
  503. if (flags & 8)
  504. modn_flags = 0x800 + (VowelCloseness(fr) << 8);
  505. } else {
  506. // exit from vowel
  507. rms = rms*2;
  508. if ((f2 != 0) || (flags != 0)) {
  509. if (flags & 8) {
  510. fr = CopyFrame(seq[*n_frames-1].frame, 0);
  511. seq[*n_frames-1].frame = fr;
  512. rms = RMS_GLOTTAL1;
  513. // degree of glottal-stop effect depends on closeness of vowel (indicated by f1 freq)
  514. modn_flags = 0x400 + (VowelCloseness(fr) << 8);
  515. } else {
  516. fr = DuplicateLastFrame(seq, (*n_frames)++, len);
  517. if (len > 36)
  518. seq_len_adjust += (len - 36);
  519. if (f2 != 0)
  520. AdjustFormants(fr, f2, f2_min, f2_max, f1, f3_adj, f3_amp, flags);
  521. }
  522. set_frame_rms(fr, rms);
  523. if ((vcolour > 0) && (vcolour <= N_VCOLOUR)) {
  524. for (ix = 0; ix < *n_frames; ix++) {
  525. fr = CopyFrame(seq[ix].frame, 0);
  526. seq[ix].frame = fr;
  527. for (formant = 1; formant <= 5; formant++) {
  528. int x;
  529. x = fr->ffreq[formant] * vcolouring[vcolour-1][formant-1];
  530. fr->ffreq[formant] = x / 256;
  531. }
  532. }
  533. }
  534. }
  535. }
  536. if (fr != NULL) {
  537. if (flags & 4)
  538. fr->frflags |= FRFLAG_FORMANT_RATE;
  539. if (flags & 2)
  540. fr->frflags |= FRFLAG_BREAK; // don't merge with next frame
  541. }
  542. if (flags & 0x40)
  543. DoPause(20, 0); // add a short pause after the consonant
  544. if (flags & 16)
  545. return len;
  546. return 0;
  547. }
  548. static void SmoothSpect(void)
  549. {
  550. // Limit the rate of frequence change of formants, to reduce chirping
  551. intptr_t *q;
  552. frame_t *frame;
  553. frame_t *frame2;
  554. frame_t *frame1;
  555. frame_t *frame_centre;
  556. int ix;
  557. int len;
  558. int pk;
  559. int modified;
  560. int allowed;
  561. int diff;
  562. if (syllable_start == syllable_end)
  563. return;
  564. if ((syllable_centre < 0) || (syllable_centre == syllable_start)) {
  565. syllable_start = syllable_end;
  566. return;
  567. }
  568. q = wcmdq[syllable_centre];
  569. frame_centre = (frame_t *)q[2];
  570. // backwards
  571. ix = syllable_centre -1;
  572. frame = frame2 = frame_centre;
  573. for (;;) {
  574. if (ix < 0) ix = N_WCMDQ-1;
  575. q = wcmdq[ix];
  576. if (q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  577. break;
  578. if (q[0] <= WCMD_SPECT2) {
  579. len = q[1] & 0xffff;
  580. frame1 = (frame_t *)q[3];
  581. if (frame1 == frame) {
  582. q[3] = (intptr_t)frame2;
  583. frame1 = frame2;
  584. } else
  585. break; // doesn't follow on from previous frame
  586. frame = frame2 = (frame_t *)q[2];
  587. modified = 0;
  588. if (frame->frflags & FRFLAG_BREAK)
  589. break;
  590. if (frame->frflags & FRFLAG_FORMANT_RATE)
  591. len = (len * 12)/10; // allow slightly greater rate of change for this frame (was 12/10)
  592. for (pk = 0; pk < 6; pk++) {
  593. int f1, f2;
  594. if ((frame->frflags & FRFLAG_BREAK_LF) && (pk < 3))
  595. continue;
  596. f1 = frame1->ffreq[pk];
  597. f2 = frame->ffreq[pk];
  598. // backwards
  599. if ((diff = f2 - f1) > 0)
  600. allowed = f1*2 + f2;
  601. else
  602. allowed = f1 + f2*2;
  603. // the allowed change is specified as percentage (%*10) of the frequency
  604. // take "frequency" as 1/3 from the lower freq
  605. allowed = (allowed * formant_rate[pk])/3000;
  606. allowed = (allowed * len)/256;
  607. if (diff > allowed) {
  608. if (modified == 0) {
  609. frame2 = CopyFrame(frame, 0);
  610. modified = 1;
  611. }
  612. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  613. q[2] = (intptr_t)frame2;
  614. } else if (diff < -allowed) {
  615. if (modified == 0) {
  616. frame2 = CopyFrame(frame, 0);
  617. modified = 1;
  618. }
  619. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  620. q[2] = (intptr_t)frame2;
  621. }
  622. }
  623. }
  624. if (ix == syllable_start)
  625. break;
  626. ix--;
  627. }
  628. // forwards
  629. ix = syllable_centre;
  630. frame = NULL;
  631. for (;;) {
  632. q = wcmdq[ix];
  633. if (q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  634. break;
  635. if (q[0] <= WCMD_SPECT2) {
  636. len = q[1] & 0xffff;
  637. frame1 = (frame_t *)q[2];
  638. if (frame != NULL) {
  639. if (frame1 == frame) {
  640. q[2] = (intptr_t)frame2;
  641. frame1 = frame2;
  642. } else
  643. break; // doesn't follow on from previous frame
  644. }
  645. frame = frame2 = (frame_t *)q[3];
  646. modified = 0;
  647. if (frame1->frflags & FRFLAG_BREAK)
  648. break;
  649. if (frame1->frflags & FRFLAG_FORMANT_RATE)
  650. len = (len *6)/5; // allow slightly greater rate of change for this frame
  651. for (pk = 0; pk < 6; pk++) {
  652. int f1, f2;
  653. f1 = frame1->ffreq[pk];
  654. f2 = frame->ffreq[pk];
  655. // forwards
  656. if ((diff = f2 - f1) > 0)
  657. allowed = f1*2 + f2;
  658. else
  659. allowed = f1 + f2*2;
  660. allowed = (allowed * formant_rate[pk])/3000;
  661. allowed = (allowed * len)/256;
  662. if (diff > allowed) {
  663. if (modified == 0) {
  664. frame2 = CopyFrame(frame, 0);
  665. modified = 1;
  666. }
  667. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  668. q[3] = (intptr_t)frame2;
  669. } else if (diff < -allowed) {
  670. if (modified == 0) {
  671. frame2 = CopyFrame(frame, 0);
  672. modified = 1;
  673. }
  674. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  675. q[3] = (intptr_t)frame2;
  676. }
  677. }
  678. }
  679. ix++;
  680. if (ix >= N_WCMDQ) ix = 0;
  681. if (ix == syllable_end)
  682. break;
  683. }
  684. syllable_start = syllable_end;
  685. }
  686. static void StartSyllable(void)
  687. {
  688. // start of syllable, if not already started
  689. if (syllable_end == syllable_start)
  690. syllable_end = wcmdq_tail;
  691. }
  692. int DoSpect2(PHONEME_TAB *this_ph, int which, FMT_PARAMS *fmt_params, PHONEME_LIST *plist, int modulation)
  693. {
  694. // which: 0 not a vowel, 1 start of vowel, 2 body and end of vowel
  695. // length_mod: 256 = 100%
  696. // modulation: -1 = don't write to wcmdq
  697. int n_frames;
  698. frameref_t *frames;
  699. int frameix;
  700. frame_t *frame1;
  701. frame_t *frame2;
  702. frame_t *fr;
  703. int ix;
  704. intptr_t *q;
  705. int len;
  706. int frame_length;
  707. int length_factor;
  708. int length_mod;
  709. int length_sum;
  710. int length_min;
  711. int total_len = 0;
  712. static int wave_flag = 0;
  713. int wcmd_spect = WCMD_SPECT;
  714. int frame_lengths[N_SEQ_FRAMES];
  715. if (fmt_params->fmt_addr == 0)
  716. return 0;
  717. length_mod = plist->length;
  718. if (length_mod == 0) length_mod = 256;
  719. length_min = (samplerate/70); // greater than one cycle at low pitch (Hz)
  720. if (which == 2) {
  721. if ((translator->langopts.param[LOPT_LONG_VOWEL_THRESHOLD] > 0) && ((this_ph->std_length >= translator->langopts.param[LOPT_LONG_VOWEL_THRESHOLD]) || (plist->synthflags & SFLAG_LENGTHEN) || (this_ph->phflags & phLONG)))
  722. length_min *= 2; // ensure long vowels are longer
  723. }
  724. if (which == 1) {
  725. // limit the shortening of sonorants before shortened (eg. unstressed vowels)
  726. if ((this_ph->type == phLIQUID) || (plist[-1].type == phLIQUID) || (plist[-1].type == phNASAL)) {
  727. if (length_mod < (len = translator->langopts.param[LOPT_SONORANT_MIN]))
  728. length_mod = len;
  729. }
  730. }
  731. modn_flags = 0;
  732. frames = LookupSpect(this_ph, which, fmt_params, &n_frames, plist);
  733. if (frames == NULL)
  734. return 0; // not found
  735. if (fmt_params->fmt_amp != fmt_amplitude) {
  736. // an amplitude adjustment is specified for this sequence
  737. q = wcmdq[wcmdq_tail];
  738. q[0] = WCMD_FMT_AMPLITUDE;
  739. q[1] = fmt_amplitude = fmt_params->fmt_amp;
  740. WcmdqInc();
  741. }
  742. frame1 = frames[0].frame;
  743. if (voice->klattv[0])
  744. wcmd_spect = WCMD_KLATT;
  745. wavefile_ix = fmt_params->wav_addr;
  746. if (fmt_params->wav_amp == 0)
  747. wavefile_amp = 32;
  748. else
  749. wavefile_amp = (fmt_params->wav_amp * 32)/100;
  750. if (wavefile_ix == 0) {
  751. if (wave_flag) {
  752. // cancel any wavefile that was playing previously
  753. wcmd_spect = WCMD_SPECT2;
  754. if (voice->klattv[0])
  755. wcmd_spect = WCMD_KLATT2;
  756. wave_flag = 0;
  757. } else {
  758. wcmd_spect = WCMD_SPECT;
  759. if (voice->klattv[0])
  760. wcmd_spect = WCMD_KLATT;
  761. }
  762. }
  763. if (last_frame != NULL) {
  764. if (((last_frame->length < 2) || (last_frame->frflags & FRFLAG_VOWEL_CENTRE))
  765. && !(last_frame->frflags & FRFLAG_BREAK)) {
  766. // last frame of previous sequence was zero-length, replace with first of this sequence
  767. wcmdq[last_wcmdq][3] = (intptr_t)frame1;
  768. if (last_frame->frflags & FRFLAG_BREAK_LF) {
  769. // but flag indicates keep HF peaks in last segment
  770. fr = CopyFrame(frame1, 1);
  771. for (ix = 3; ix < 8; ix++) {
  772. if (ix < 7)
  773. fr->ffreq[ix] = last_frame->ffreq[ix];
  774. fr->fheight[ix] = last_frame->fheight[ix];
  775. }
  776. wcmdq[last_wcmdq][3] = (intptr_t)fr;
  777. }
  778. }
  779. }
  780. if ((this_ph->type == phVOWEL) && (which == 2)) {
  781. SmoothSpect(); // process previous syllable
  782. // remember the point in the output queue of the centre of the vowel
  783. syllable_centre = wcmdq_tail;
  784. }
  785. length_sum = 0;
  786. for (frameix = 1; frameix < n_frames; frameix++) {
  787. length_factor = length_mod;
  788. if (frames[frameix-1].frflags & FRFLAG_LEN_MOD) // reduce effect of length mod
  789. length_factor = (length_mod*(256-speed.lenmod_factor) + 256*speed.lenmod_factor)/256;
  790. else if (frames[frameix-1].frflags & FRFLAG_LEN_MOD2) // reduce effect of length mod, used for the start of a vowel
  791. length_factor = (length_mod*(256-speed.lenmod2_factor) + 256*speed.lenmod2_factor)/256;
  792. frame_length = frames[frameix-1].length;
  793. len = (frame_length * samplerate)/1000;
  794. len = (len * length_factor)/256;
  795. length_sum += len;
  796. frame_lengths[frameix] = len;
  797. }
  798. if ((length_sum > 0) && (length_sum < length_min)) {
  799. // lengthen, so that the sequence is greater than one cycle at low pitch
  800. for (frameix = 1; frameix < n_frames; frameix++)
  801. frame_lengths[frameix] = (frame_lengths[frameix] * length_min) / length_sum;
  802. }
  803. for (frameix = 1; frameix < n_frames; frameix++) {
  804. frame2 = frames[frameix].frame;
  805. if ((fmt_params->wav_addr != 0) && ((frame1->frflags & FRFLAG_DEFER_WAV) == 0)) {
  806. // there is a wave file to play along with this synthesis
  807. seq_len_adjust = 0;
  808. DoSample2(fmt_params->wav_addr, which+0x100, 0, fmt_params->fmt_control, 0, wavefile_amp);
  809. wave_flag = 1;
  810. wavefile_ix = 0;
  811. fmt_params->wav_addr = 0;
  812. }
  813. if (modulation >= 0) {
  814. if (frame1->frflags & FRFLAG_MODULATE)
  815. modulation = 6;
  816. if ((frameix == n_frames-1) && (modn_flags & 0xf00))
  817. modulation |= modn_flags; // before or after a glottal stop
  818. }
  819. len = frame_lengths[frameix];
  820. pitch_length += len;
  821. amp_length += len;
  822. if (len == 0) {
  823. last_frame = NULL;
  824. frame1 = frame2;
  825. } else {
  826. last_wcmdq = wcmdq_tail;
  827. if (modulation >= 0) {
  828. q = wcmdq[wcmdq_tail];
  829. q[0] = wcmd_spect;
  830. q[1] = len + (modulation << 16);
  831. q[2] = (intptr_t)frame1;
  832. q[3] = (intptr_t)frame2;
  833. WcmdqInc();
  834. }
  835. last_frame = frame1 = frame2;
  836. total_len += len;
  837. }
  838. }
  839. if ((which != 1) && (fmt_amplitude != 0)) {
  840. q = wcmdq[wcmdq_tail];
  841. q[0] = WCMD_FMT_AMPLITUDE;
  842. q[1] = fmt_amplitude = 0;
  843. WcmdqInc();
  844. }
  845. return total_len;
  846. }
  847. void DoMarker(int type, int char_posn, int length, int value)
  848. {
  849. // This could be used to return an index to the word currently being spoken
  850. // Type 1=word, 2=sentence, 3=named marker, 4=play audio, 5=end
  851. if (WcmdqFree() > 5) {
  852. wcmdq[wcmdq_tail][0] = WCMD_MARKER + (type << 8);
  853. wcmdq[wcmdq_tail][1] = (char_posn & 0xffffff) | (length << 24);
  854. wcmdq[wcmdq_tail][2] = value;
  855. WcmdqInc();
  856. }
  857. }
  858. void DoPhonemeMarker(int type, int char_posn, int length, char *name)
  859. {
  860. // This could be used to return an index to the word currently being spoken
  861. // Type 7=phoneme
  862. int *p;
  863. if (WcmdqFree() > 5) {
  864. wcmdq[wcmdq_tail][0] = WCMD_MARKER + (type << 8);
  865. wcmdq[wcmdq_tail][1] = (char_posn & 0xffffff) | (length << 24);
  866. p = (int *)name;
  867. wcmdq[wcmdq_tail][2] = p[0]; // up to 8 bytes of UTF8 characters
  868. wcmdq[wcmdq_tail][3] = p[1];
  869. WcmdqInc();
  870. }
  871. }
  872. #if HAVE_SONIC_H
  873. void DoSonicSpeed(int value)
  874. {
  875. // value, multiplier * 1024
  876. wcmdq[wcmdq_tail][0] = WCMD_SONIC_SPEED;
  877. wcmdq[wcmdq_tail][1] = value;
  878. WcmdqInc();
  879. }
  880. #endif
  881. espeak_ng_STATUS DoVoiceChange(voice_t *v)
  882. {
  883. // allocate memory for a copy of the voice data, and free it in wavegenfill()
  884. voice_t *v2;
  885. if ((v2 = (voice_t *)malloc(sizeof(voice_t))) == NULL)
  886. return ENOMEM;
  887. memcpy(v2, v, sizeof(voice_t));
  888. wcmdq[wcmdq_tail][0] = WCMD_VOICE;
  889. wcmdq[wcmdq_tail][2] = (intptr_t)v2;
  890. WcmdqInc();
  891. return ENS_OK;
  892. }
  893. void DoEmbedded(int *embix, int sourceix)
  894. {
  895. // There were embedded commands in the text at this point
  896. unsigned int word; // bit 7=last command for this word, bits 5,6 sign, bits 0-4 command
  897. unsigned int value;
  898. int command;
  899. do {
  900. word = embedded_list[*embix];
  901. value = word >> 8;
  902. command = word & 0x7f;
  903. if (command == 0)
  904. return; // error
  905. (*embix)++;
  906. switch (command & 0x1f)
  907. {
  908. case EMBED_S: // speed
  909. SetEmbedded((command & 0x60) + EMBED_S2, value); // adjusts embedded_value[EMBED_S2]
  910. SetSpeed(2);
  911. break;
  912. case EMBED_I: // play dynamically loaded wav data (sound icon)
  913. if ((int)value < n_soundicon_tab) {
  914. if (soundicon_tab[value].length != 0) {
  915. DoPause(10, 0); // ensure a break in the speech
  916. wcmdq[wcmdq_tail][0] = WCMD_WAVE;
  917. wcmdq[wcmdq_tail][1] = soundicon_tab[value].length;
  918. wcmdq[wcmdq_tail][2] = (intptr_t)soundicon_tab[value].data + 44; // skip WAV header
  919. wcmdq[wcmdq_tail][3] = 0x1500; // 16 bit data, amp=21
  920. WcmdqInc();
  921. }
  922. }
  923. break;
  924. case EMBED_M: // named marker
  925. DoMarker(espeakEVENT_MARK, (sourceix & 0x7ff) + clause_start_char, 0, value);
  926. break;
  927. case EMBED_U: // play sound
  928. DoMarker(espeakEVENT_PLAY, count_characters+1, 0, value); // always occurs at end of clause
  929. break;
  930. default:
  931. DoPause(10, 0); // ensure a break in the speech
  932. wcmdq[wcmdq_tail][0] = WCMD_EMBEDDED;
  933. wcmdq[wcmdq_tail][1] = command;
  934. wcmdq[wcmdq_tail][2] = value;
  935. WcmdqInc();
  936. break;
  937. }
  938. } while ((word & 0x80) == 0);
  939. }
  940. int Generate(PHONEME_LIST *phoneme_list, int *n_ph, int resume)
  941. {
  942. static int ix;
  943. static int embedded_ix;
  944. static int word_count;
  945. PHONEME_LIST *prev;
  946. PHONEME_LIST *next;
  947. PHONEME_LIST *next2;
  948. PHONEME_LIST *p;
  949. int released;
  950. int stress;
  951. int modulation;
  952. int pre_voiced;
  953. int free_min;
  954. int value;
  955. unsigned char *pitch_env = NULL;
  956. unsigned char *amp_env;
  957. PHONEME_TAB *ph;
  958. int use_ipa = 0;
  959. int done_phoneme_marker;
  960. int vowelstart_prev;
  961. char phoneme_name[16];
  962. static int sourceix = 0;
  963. PHONEME_DATA phdata;
  964. PHONEME_DATA phdata_prev;
  965. PHONEME_DATA phdata_next;
  966. PHONEME_DATA phdata_tone;
  967. FMT_PARAMS fmtp;
  968. static WORD_PH_DATA worddata;
  969. if (option_quiet)
  970. return 0;
  971. if (option_phoneme_events & espeakINITIALIZE_PHONEME_IPA)
  972. use_ipa = 1;
  973. if (mbrola_name[0] != 0)
  974. return MbrolaGenerate(phoneme_list, n_ph, resume);
  975. if (resume == 0) {
  976. ix = 1;
  977. embedded_ix = 0;
  978. word_count = 0;
  979. pitch_length = 0;
  980. amp_length = 0;
  981. last_frame = NULL;
  982. last_wcmdq = -1;
  983. syllable_start = wcmdq_tail;
  984. syllable_end = wcmdq_tail;
  985. syllable_centre = -1;
  986. last_pitch_cmd = -1;
  987. memset(vowel_transition, 0, sizeof(vowel_transition));
  988. memset(&worddata, 0, sizeof(worddata));
  989. DoPause(0, 0); // isolate from the previous clause
  990. }
  991. while ((ix < (*n_ph)) && (ix < N_PHONEME_LIST-2)) {
  992. p = &phoneme_list[ix];
  993. if (p->type == phPAUSE)
  994. free_min = 10;
  995. else if (p->type != phVOWEL)
  996. free_min = 15; // we need less Q space for non-vowels, and we need to generate phonemes after a vowel so that the pitch_length is filled in
  997. else
  998. free_min = MIN_WCMDQ;
  999. if (WcmdqFree() <= free_min)
  1000. return 1; // wait
  1001. prev = &phoneme_list[ix-1];
  1002. next = &phoneme_list[ix+1];
  1003. next2 = &phoneme_list[ix+2];
  1004. if (p->synthflags & SFLAG_EMBEDDED)
  1005. DoEmbedded(&embedded_ix, p->sourceix);
  1006. if (p->newword) {
  1007. if (((p->type == phVOWEL) && (translator->langopts.param[LOPT_WORD_MERGE] & 1)) ||
  1008. (p->ph->phflags & phNOPAUSE)) {
  1009. } else
  1010. last_frame = NULL;
  1011. sourceix = (p->sourceix & 0x7ff) + clause_start_char;
  1012. if (p->newword & 4)
  1013. DoMarker(espeakEVENT_SENTENCE, sourceix, 0, count_sentences); // start of sentence
  1014. if (p->newword & 1)
  1015. DoMarker(espeakEVENT_WORD, sourceix, p->sourceix >> 11, clause_start_word + word_count++); // NOTE, this count doesn't include multiple-word pronunciations in *_list. eg (of a)
  1016. }
  1017. EndAmplitude();
  1018. if ((p->prepause > 0) && !(p->ph->phflags & phPREVOICE))
  1019. DoPause(p->prepause, 1);
  1020. done_phoneme_marker = 0;
  1021. if (option_phoneme_events && (p->ph->code != phonEND_WORD)) {
  1022. if ((p->type == phVOWEL) && (prev->type == phLIQUID || prev->type == phNASAL)) {
  1023. // For vowels following a liquid or nasal, do the phoneme event after the vowel-start
  1024. } else {
  1025. WritePhMnemonic(phoneme_name, p->ph, p, use_ipa, NULL);
  1026. DoPhonemeMarker(espeakEVENT_PHONEME, sourceix, 0, phoneme_name);
  1027. done_phoneme_marker = 1;
  1028. }
  1029. }
  1030. switch (p->type)
  1031. {
  1032. case phPAUSE:
  1033. DoPause(p->length, 0);
  1034. p->std_length = p->ph->std_length;
  1035. break;
  1036. case phSTOP:
  1037. released = 0;
  1038. ph = p->ph;
  1039. if (next->type == phVOWEL)
  1040. released = 1;
  1041. else if (!next->newword) {
  1042. if (next->type == phLIQUID) released = 1;
  1043. }
  1044. if (released == 0)
  1045. p->synthflags |= SFLAG_NEXT_PAUSE;
  1046. if (ph->phflags & phPREVOICE) {
  1047. // a period of voicing before the release
  1048. memset(&fmtp, 0, sizeof(fmtp));
  1049. InterpretPhoneme(NULL, 0x01, p, &phdata, &worddata);
  1050. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1051. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1052. if (last_pitch_cmd < 0) {
  1053. DoAmplitude(next->amp, NULL);
  1054. DoPitch(envelope_data[p->env], next->pitch1, next->pitch2);
  1055. }
  1056. DoSpect2(ph, 0, &fmtp, p, 0);
  1057. }
  1058. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1059. phdata.pd_control |= pd_DONTLENGTHEN;
  1060. DoSample3(&phdata, 0, 0);
  1061. break;
  1062. case phFRICATIVE:
  1063. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1064. if (p->synthflags & SFLAG_LENGTHEN)
  1065. DoSample3(&phdata, p->length, 0); // play it twice for [s:] etc.
  1066. DoSample3(&phdata, p->length, 0);
  1067. break;
  1068. case phVSTOP:
  1069. ph = p->ph;
  1070. memset(&fmtp, 0, sizeof(fmtp));
  1071. fmtp.fmt_control = pd_DONTLENGTHEN;
  1072. pre_voiced = 0;
  1073. if (next->type == phVOWEL) {
  1074. DoAmplitude(p->amp, NULL);
  1075. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1076. pre_voiced = 1;
  1077. } else if ((next->type == phLIQUID) && !next->newword) {
  1078. DoAmplitude(next->amp, NULL);
  1079. DoPitch(envelope_data[next->env], next->pitch1, next->pitch2);
  1080. pre_voiced = 1;
  1081. } else {
  1082. if (last_pitch_cmd < 0) {
  1083. DoAmplitude(next->amp, NULL);
  1084. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1085. }
  1086. }
  1087. if ((prev->type == phVOWEL) || (prev->ph->phflags & phVOWEL2) || (ph->phflags & phPREVOICE)) {
  1088. // a period of voicing before the release
  1089. InterpretPhoneme(NULL, 0x01, p, &phdata, &worddata);
  1090. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1091. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1092. DoSpect2(ph, 0, &fmtp, p, 0);
  1093. if (p->synthflags & SFLAG_LENGTHEN) {
  1094. DoPause(25, 1);
  1095. DoSpect2(ph, 0, &fmtp, p, 0);
  1096. }
  1097. } else {
  1098. if (p->synthflags & SFLAG_LENGTHEN)
  1099. DoPause(50, 0);
  1100. }
  1101. if (pre_voiced) {
  1102. // followed by a vowel, or liquid + vowel
  1103. StartSyllable();
  1104. } else
  1105. p->synthflags |= SFLAG_NEXT_PAUSE;
  1106. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1107. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1108. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1109. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1110. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1111. DoSpect2(ph, 0, &fmtp, p, 0);
  1112. if ((p->newword == 0) && (next2->newword == 0)) {
  1113. if (next->type == phVFRICATIVE)
  1114. DoPause(20, 0);
  1115. if (next->type == phFRICATIVE)
  1116. DoPause(12, 0);
  1117. }
  1118. break;
  1119. case phVFRICATIVE:
  1120. if (next->type == phVOWEL) {
  1121. DoAmplitude(p->amp, NULL);
  1122. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1123. } else if (next->type == phLIQUID) {
  1124. DoAmplitude(next->amp, NULL);
  1125. DoPitch(envelope_data[next->env], next->pitch1, next->pitch2);
  1126. } else {
  1127. if (last_pitch_cmd < 0) {
  1128. DoAmplitude(p->amp, NULL);
  1129. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1130. }
  1131. }
  1132. if ((next->type == phVOWEL) || ((next->type == phLIQUID) && (next->newword == 0))) // ?? test 14.Aug.2007
  1133. StartSyllable();
  1134. else
  1135. p->synthflags |= SFLAG_NEXT_PAUSE;
  1136. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1137. memset(&fmtp, 0, sizeof(fmtp));
  1138. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1139. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1140. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1141. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1142. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1143. if (p->synthflags & SFLAG_LENGTHEN)
  1144. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1145. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1146. break;
  1147. case phNASAL:
  1148. memset(&fmtp, 0, sizeof(fmtp));
  1149. if (!(p->synthflags & SFLAG_SEQCONTINUE)) {
  1150. DoAmplitude(p->amp, NULL);
  1151. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1152. }
  1153. if (prev->type == phNASAL)
  1154. last_frame = NULL;
  1155. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1156. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1157. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1158. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1159. if (next->type == phVOWEL) {
  1160. StartSyllable();
  1161. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1162. } else if (prev->type == phVOWEL && (p->synthflags & SFLAG_SEQCONTINUE))
  1163. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1164. else {
  1165. last_frame = NULL; // only for nasal ?
  1166. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1167. last_frame = NULL;
  1168. }
  1169. break;
  1170. case phLIQUID:
  1171. memset(&fmtp, 0, sizeof(fmtp));
  1172. modulation = 0;
  1173. if (p->ph->phflags & phTRILL)
  1174. modulation = 5;
  1175. if (!(p->synthflags & SFLAG_SEQCONTINUE)) {
  1176. DoAmplitude(p->amp, NULL);
  1177. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1178. }
  1179. if (prev->type == phNASAL)
  1180. last_frame = NULL;
  1181. if (next->type == phVOWEL)
  1182. StartSyllable();
  1183. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1184. if ((value = (phdata.pd_param[i_PAUSE_BEFORE] - p->prepause)) > 0)
  1185. DoPause(value, 1);
  1186. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1187. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1188. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1189. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1190. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1191. DoSpect2(p->ph, 0, &fmtp, p, modulation);
  1192. break;
  1193. case phVOWEL:
  1194. ph = p->ph;
  1195. stress = p->stresslevel & 0xf;
  1196. memset(&fmtp, 0, sizeof(fmtp));
  1197. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1198. fmtp.std_length = phdata.pd_param[i_SET_LENGTH] * 2;
  1199. vowelstart_prev = 0;
  1200. if (((fmtp.fmt_addr = phdata.sound_addr[pd_VWLSTART]) != 0) && ((phdata.pd_control & pd_FORNEXTPH) == 0)) {
  1201. // a vowel start has been specified by the Vowel program
  1202. fmtp.fmt_length = phdata.sound_param[pd_VWLSTART];
  1203. } else if (prev->type != phPAUSE) {
  1204. // check the previous phoneme
  1205. InterpretPhoneme(NULL, 0, prev, &phdata_prev, NULL);
  1206. if (((fmtp.fmt_addr = phdata_prev.sound_addr[pd_VWLSTART]) != 0) && (phdata_prev.pd_control & pd_FORNEXTPH)) {
  1207. // a vowel start has been specified by the previous phoneme
  1208. vowelstart_prev = 1;
  1209. fmtp.fmt2_lenadj = phdata_prev.sound_param[pd_VWLSTART];
  1210. }
  1211. fmtp.transition0 = phdata_prev.vowel_transition[0];
  1212. fmtp.transition1 = phdata_prev.vowel_transition[1];
  1213. }
  1214. if (fmtp.fmt_addr == 0) {
  1215. // use the default start for this vowel
  1216. fmtp.use_vowelin = 1;
  1217. fmtp.fmt_control = 1;
  1218. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1219. }
  1220. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1221. pitch_env = envelope_data[p->env];
  1222. amp_env = NULL;
  1223. if (p->tone_ph != 0) {
  1224. InterpretPhoneme2(p->tone_ph, &phdata_tone);
  1225. pitch_env = GetEnvelope(phdata_tone.pitch_env);
  1226. if (phdata_tone.amp_env > 0)
  1227. amp_env = GetEnvelope(phdata_tone.amp_env);
  1228. }
  1229. StartSyllable();
  1230. modulation = 2;
  1231. if (stress <= 1)
  1232. modulation = 1; // 16ths
  1233. else if (stress >= 7)
  1234. modulation = 3;
  1235. if (prev->type == phVSTOP || prev->type == phVFRICATIVE) {
  1236. DoAmplitude(p->amp, amp_env);
  1237. DoPitch(pitch_env, p->pitch1, p->pitch2); // don't use prevocalic rising tone
  1238. DoSpect2(ph, 1, &fmtp, p, modulation);
  1239. } else if (prev->type == phLIQUID || prev->type == phNASAL) {
  1240. DoAmplitude(p->amp, amp_env);
  1241. DoSpect2(ph, 1, &fmtp, p, modulation); // continue with pre-vocalic rising tone
  1242. DoPitch(pitch_env, p->pitch1, p->pitch2);
  1243. } else if (vowelstart_prev) {
  1244. // VowelStart from the previous phoneme, but not phLIQUID or phNASAL
  1245. DoPitch(envelope_data[PITCHrise], p->pitch2 - 15, p->pitch2);
  1246. DoAmplitude(p->amp-1, amp_env);
  1247. DoSpect2(ph, 1, &fmtp, p, modulation); // continue with pre-vocalic rising tone
  1248. DoPitch(pitch_env, p->pitch1, p->pitch2);
  1249. } else {
  1250. if (!(p->synthflags & SFLAG_SEQCONTINUE)) {
  1251. DoAmplitude(p->amp, amp_env);
  1252. DoPitch(pitch_env, p->pitch1, p->pitch2);
  1253. }
  1254. DoSpect2(ph, 1, &fmtp, p, modulation);
  1255. }
  1256. if ((option_phoneme_events) && (done_phoneme_marker == 0)) {
  1257. WritePhMnemonic(phoneme_name, p->ph, p, use_ipa, NULL);
  1258. DoPhonemeMarker(espeakEVENT_PHONEME, sourceix, 0, phoneme_name);
  1259. }
  1260. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1261. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1262. fmtp.transition0 = 0;
  1263. fmtp.transition1 = 0;
  1264. if ((fmtp.fmt2_addr = phdata.sound_addr[pd_VWLEND]) != 0)
  1265. fmtp.fmt2_lenadj = phdata.sound_param[pd_VWLEND];
  1266. else if (next->type != phPAUSE) {
  1267. fmtp.fmt2_lenadj = 0;
  1268. InterpretPhoneme(NULL, 0, next, &phdata_next, NULL);
  1269. fmtp.use_vowelin = 1;
  1270. fmtp.transition0 = phdata_next.vowel_transition[2]; // always do vowel_transition, even if ph_VWLEND ?? consider [N]
  1271. fmtp.transition1 = phdata_next.vowel_transition[3];
  1272. if ((fmtp.fmt2_addr = phdata_next.sound_addr[pd_VWLEND]) != 0)
  1273. fmtp.fmt2_lenadj = phdata_next.sound_param[pd_VWLEND];
  1274. }
  1275. DoSpect2(ph, 2, &fmtp, p, modulation);
  1276. break;
  1277. }
  1278. ix++;
  1279. }
  1280. EndPitch(1);
  1281. if (*n_ph > 0) {
  1282. DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  1283. *n_ph = 0;
  1284. }
  1285. return 0; // finished the phoneme list
  1286. }
  1287. static int timer_on = 0;
  1288. static int paused = 0;
  1289. int SynthOnTimer()
  1290. {
  1291. if (!timer_on)
  1292. return WavegenCloseSound();
  1293. do {
  1294. if (WcmdqUsed() > 0)
  1295. WavegenOpenSound();
  1296. if (Generate(phoneme_list, &n_phoneme_list, 1) == 0)
  1297. SpeakNextClause(NULL, NULL, 1);
  1298. } while (skipping_text);
  1299. return 0;
  1300. }
  1301. int SynthStatus()
  1302. {
  1303. return timer_on | paused;
  1304. }
  1305. int SpeakNextClause(FILE *f_in, const void *text_in, int control)
  1306. {
  1307. // Speak text from file (f_in) or memory (text_in)
  1308. // control 0: start
  1309. // either f_in or text_in is set, the other must be NULL
  1310. // The other calls have f_in and text_in = NULL
  1311. // control 1: speak next text
  1312. // 2: stop
  1313. // 3: pause (toggle)
  1314. // 4: is file being read (0=no, 1=yes)
  1315. // 5: interrupt and flush current text.
  1316. int clause_tone;
  1317. char *voice_change;
  1318. static FILE *f_text = NULL;
  1319. static const void *p_text = NULL;
  1320. const char *phon_out;
  1321. if (control == 4) {
  1322. if ((f_text == NULL) && (p_text == NULL))
  1323. return 0;
  1324. else
  1325. return 1;
  1326. }
  1327. if (control == 2) {
  1328. // stop speaking
  1329. timer_on = 0;
  1330. p_text = NULL;
  1331. if (f_text != NULL) {
  1332. fclose(f_text);
  1333. f_text = NULL;
  1334. }
  1335. n_phoneme_list = 0;
  1336. WcmdqStop();
  1337. return 0;
  1338. }
  1339. if (control == 3) {
  1340. // toggle pause
  1341. if (paused == 0) {
  1342. timer_on = 0;
  1343. paused = 2;
  1344. } else {
  1345. WavegenOpenSound();
  1346. timer_on = 1;
  1347. paused = 0;
  1348. Generate(phoneme_list, &n_phoneme_list, 0); // re-start from beginning of clause
  1349. }
  1350. return 0;
  1351. }
  1352. if (control == 5) {
  1353. // stop speaking, but continue looking for text
  1354. n_phoneme_list = 0;
  1355. WcmdqStop();
  1356. return 0;
  1357. }
  1358. if ((f_in != NULL) || (text_in != NULL)) {
  1359. f_text = f_in;
  1360. p_text = text_in;
  1361. timer_on = 1;
  1362. paused = 0;
  1363. }
  1364. if ((f_text == NULL) && (p_text == NULL)) {
  1365. skipping_text = 0;
  1366. timer_on = 0;
  1367. return 0;
  1368. }
  1369. if ((f_text != NULL) && feof(f_text)) {
  1370. timer_on = 0;
  1371. fclose(f_text);
  1372. f_text = NULL;
  1373. return 0;
  1374. }
  1375. if (current_phoneme_table != voice->phoneme_tab_ix)
  1376. SelectPhonemeTable(voice->phoneme_tab_ix);
  1377. // read the next clause from the input text file, translate it, and generate
  1378. // entries in the wavegen command queue
  1379. p_text = TranslateClause(translator, f_text, p_text, &clause_tone, &voice_change);
  1380. CalcPitches(translator, clause_tone);
  1381. CalcLengths(translator);
  1382. if ((option_phonemes & 0xf) || (phoneme_callback != NULL)) {
  1383. phon_out = GetTranslatedPhonemeString(option_phonemes);
  1384. if (option_phonemes & 0xf)
  1385. fprintf(f_trans, "%s\n", phon_out);
  1386. if (phoneme_callback != NULL)
  1387. phoneme_callback(phon_out);
  1388. }
  1389. if (skipping_text) {
  1390. n_phoneme_list = 0;
  1391. return 1;
  1392. }
  1393. Generate(phoneme_list, &n_phoneme_list, 0);
  1394. WavegenOpenSound();
  1395. if (voice_change != NULL) {
  1396. // voice change at the end of the clause (i.e. clause was terminated by a voice change)
  1397. new_voice = LoadVoiceVariant(voice_change, 0); // add a Voice instruction to wavegen at the end of the clause
  1398. }
  1399. if (new_voice) {
  1400. // finished the current clause, now change the voice if there was an embedded
  1401. // change voice command at the end of it (i.e. clause was broken at the change voice command)
  1402. DoVoiceChange(voice);
  1403. new_voice = NULL;
  1404. }
  1405. return 1;
  1406. }