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

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