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

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