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.cpp 35KB

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