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

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