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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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 "voice.h"
  29. #include "phoneme.h"
  30. #include "synthesize.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. static void DoVoice(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] = 0; // 16 bit data
  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. #ifdef TEST_MBROLA
  888. if(mbrola_name[0] != 0)
  889. return(MbrolaGenerate(phoneme_list,n_ph,resume));
  890. #endif
  891. if(option_quiet)
  892. return(0);
  893. if(resume == 0)
  894. {
  895. ix = 1;
  896. embedded_ix=0;
  897. word_count = 0;
  898. pitch_length = 0;
  899. amp_length = 0;
  900. last_frame = NULL;
  901. last_wcmdq = -1;
  902. syllable_start = wcmdq_tail;
  903. syllable_end = wcmdq_tail;
  904. syllable_centre = -1;
  905. last_pitch_cmd = -1;
  906. memset(vowel_transition,0,sizeof(vowel_transition));
  907. }
  908. while(ix < (*n_ph))
  909. {
  910. if(WcmdqFree() <= MIN_WCMDQ)
  911. return(1); // wait
  912. prev = &phoneme_list[ix-1];
  913. p = &phoneme_list[ix];
  914. next = &phoneme_list[ix+1];
  915. next2 = &phoneme_list[ix+2];
  916. if(p->synthflags & SFLAG_EMBEDDED)
  917. {
  918. DoEmbedded(embedded_ix, p->sourceix);
  919. }
  920. if(p->newword)
  921. {
  922. last_frame = NULL;
  923. if(p->newword & 4)
  924. DoMarker(espeakEVENT_SENTENCE, (p->sourceix & 0x7ff) + clause_start_char, 0, count_sentences); // start of sentence
  925. // if(p->newword & 2)
  926. // DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  927. if(p->newword & 1)
  928. DoMarker(espeakEVENT_WORD, (p->sourceix & 0x7ff) + clause_start_char, p->sourceix >> 11, clause_start_word + word_count++);
  929. }
  930. if((translator->langopts.word_gap & 1) || (translator->langopts.vowel_pause && (next->type == phVOWEL)))
  931. {
  932. // prevent word merging into next, make it look as though next is a pause
  933. if((next->newword) && (next->type != phPAUSE))
  934. {
  935. // next_pause.ph = phoneme_tab[phonPAUSE];
  936. // next = &next_pause;
  937. }
  938. }
  939. EndAmplitude();
  940. if(p->prepause > 0)
  941. DoPause(p->prepause);
  942. if(option_phoneme_events)
  943. {
  944. DoMarker(espeakEVENT_PHONEME, (p->sourceix & 0x7ff) + clause_start_char, 0, p->ph->mnemonic);
  945. }
  946. switch(p->type)
  947. {
  948. case phPAUSE:
  949. DoPause(p->length);
  950. break;
  951. case phSTOP:
  952. released = 0;
  953. if(next->type==phVOWEL) released = 1;
  954. if(next->type==phLIQUID && !next->newword) released = 1;
  955. if(released)
  956. DoSample(p->ph,next->ph,2,0,0);
  957. else
  958. DoSample(p->ph,phoneme_tab[phonPAUSE],2,0,0);
  959. break;
  960. case phFRICATIVE:
  961. if(p->synthflags & SFLAG_LENGTHEN)
  962. DoSample(p->ph,next->ph,2,p->length,0); // play it twice for [s:] etc.
  963. DoSample(p->ph,next->ph,2,p->length,0);
  964. break;
  965. case phVSTOP:
  966. pre_voiced = 0;
  967. if(next->type==phVOWEL)
  968. {
  969. DoAmplitude(p->amp,NULL);
  970. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  971. pre_voiced = 1;
  972. }
  973. else
  974. if((next->type==phLIQUID) && !next->newword)
  975. {
  976. DoAmplitude(next->amp,NULL);
  977. DoPitch(envelope_data[next->env],next->pitch1,next->pitch2);
  978. pre_voiced = 1;
  979. }
  980. else
  981. {
  982. if(last_pitch_cmd < 0)
  983. {
  984. DoAmplitude(next->amp,NULL);
  985. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  986. }
  987. }
  988. if((prev->type==phVOWEL) || (prev->ph->phflags & phVOWEL2))
  989. {
  990. // a period of voicing before the release
  991. DoSpect(p->ph,phoneme_tab[phonSCHWA],next->ph,1,p,0);
  992. if(p->synthflags & SFLAG_LENGTHEN)
  993. {
  994. DoPause(20);
  995. DoSpect(p->ph,phoneme_tab[phonSCHWA],next->ph,1,p,0);
  996. }
  997. }
  998. else
  999. {
  1000. if(p->synthflags & SFLAG_LENGTHEN)
  1001. {
  1002. DoPause(50);
  1003. }
  1004. }
  1005. if(pre_voiced)
  1006. {
  1007. // followed by a vowel, or liquid + vowel
  1008. StartSyllable();
  1009. DoSpect(p->ph,prev->ph,next->ph,2,p,0);
  1010. }
  1011. else
  1012. {
  1013. // if((prev->type != phVOWEL) && ((prev->ph->phflags & phVOICED)==0) && ((next->ph->phflags & phVOICED)==0))
  1014. // DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE_SHORT],2,p,0);
  1015. // else
  1016. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1017. // DoSpect(p->ph,prev->ph,next->ph,2,p,0);
  1018. }
  1019. break;
  1020. case phVFRICATIVE:
  1021. if(next->type==phVOWEL)
  1022. {
  1023. DoAmplitude(p->amp,NULL);
  1024. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1025. }
  1026. else
  1027. if(next->type==phLIQUID)
  1028. {
  1029. DoAmplitude(next->amp,NULL);
  1030. DoPitch(envelope_data[next->env],next->pitch1,next->pitch2);
  1031. }
  1032. else
  1033. {
  1034. if(last_pitch_cmd < 0)
  1035. {
  1036. DoAmplitude(p->amp,NULL);
  1037. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1038. }
  1039. }
  1040. if((next->type==phVOWEL) || (next->type==phLIQUID))
  1041. {
  1042. StartSyllable();
  1043. if(p->synthflags & SFLAG_LENGTHEN)
  1044. DoSpect(p->ph,prev->ph,next->ph,2,p,0);
  1045. DoSpect(p->ph,prev->ph,next->ph,2,p,0);
  1046. }
  1047. else
  1048. {
  1049. if(p->synthflags & SFLAG_LENGTHEN)
  1050. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1051. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1052. }
  1053. break;
  1054. case phNASAL:
  1055. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1056. {
  1057. DoAmplitude(p->amp,NULL);
  1058. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1059. }
  1060. if(prev->type==phNASAL)
  1061. {
  1062. last_frame = NULL;
  1063. }
  1064. if(next->type==phVOWEL)
  1065. {
  1066. StartSyllable();
  1067. DoSpect(p->ph,prev->ph,next->ph,1,p,0);
  1068. }
  1069. else
  1070. if(prev->type==phVOWEL && (p->synthflags & SFLAG_SEQCONTINUE))
  1071. {
  1072. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1073. }
  1074. else
  1075. {
  1076. last_frame = NULL; // only for nasal ?
  1077. if(next->type == phLIQUID)
  1078. DoSpect(p->ph,prev->ph,phoneme_tab[phonSONORANT],2,p,0);
  1079. else
  1080. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1081. last_frame = NULL;
  1082. }
  1083. break;
  1084. case phLIQUID:
  1085. modulation = 0;
  1086. if(p->ph->phflags & phTRILL)
  1087. modulation = 5;
  1088. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1089. {
  1090. DoAmplitude(p->amp,NULL);
  1091. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1092. }
  1093. if(prev->type==phNASAL)
  1094. {
  1095. last_frame = NULL;
  1096. }
  1097. if(next->type==phVOWEL)
  1098. {
  1099. StartSyllable();
  1100. DoSpect(p->ph,prev->ph,next->ph,1,p,modulation); // (,)r
  1101. }
  1102. else
  1103. if(prev->type==phVOWEL && (p->synthflags & SFLAG_SEQCONTINUE))
  1104. {
  1105. DoSpect(p->ph,prev->ph,next->ph,1,p,modulation);
  1106. }
  1107. else
  1108. {
  1109. DoSpect(p->ph,prev->ph,next->ph,1,p,modulation);
  1110. }
  1111. break;
  1112. case phVOWEL:
  1113. ph = p->ph;
  1114. stress = p->tone & 0xf;
  1115. // vowel transition from the preceding phoneme
  1116. vowel_transition0 = vowel_transition[0];
  1117. vowel_transition1 = vowel_transition[1];
  1118. pitch_env = envelope_data[p->env];
  1119. amp_env = NULL;
  1120. if(p->tone_ph != 0)
  1121. {
  1122. pitch_env = LookupEnvelope(phoneme_tab[p->tone_ph]->spect);
  1123. amp_env = LookupEnvelope(phoneme_tab[p->tone_ph]->after);
  1124. }
  1125. StartSyllable();
  1126. modulation = 2;
  1127. if(stress <= 1)
  1128. modulation = 1; // 16ths
  1129. else
  1130. if(stress >= 7)
  1131. modulation = 3;
  1132. if(prev->type == phVSTOP || prev->type == phVFRICATIVE)
  1133. {
  1134. DoAmplitude(p->amp,amp_env);
  1135. DoPitch(pitch_env,p->pitch1,p->pitch2); // don't use prevocalic rising tone
  1136. DoSpect(ph,prev->ph,next->ph,1,p,modulation);
  1137. }
  1138. else
  1139. if(prev->type==phLIQUID || prev->type==phNASAL)
  1140. {
  1141. DoAmplitude(p->amp,amp_env);
  1142. DoSpect(ph,prev->ph,next->ph,1,p,modulation); // continue with pre-vocalic rising tone
  1143. DoPitch(pitch_env,p->pitch1,p->pitch2);
  1144. }
  1145. else
  1146. {
  1147. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1148. {
  1149. DoAmplitude(p->amp,amp_env);
  1150. DoPitch(pitch_env,p->pitch1,p->pitch2);
  1151. }
  1152. DoSpect(ph,prev->ph,next->ph,1,p,modulation);
  1153. }
  1154. DoSpect(p->ph,prev->ph,next->ph,2,p,modulation);
  1155. memset(vowel_transition,0,sizeof(vowel_transition));
  1156. break;
  1157. }
  1158. ix++;
  1159. }
  1160. EndPitch(1);
  1161. if(*n_ph > 0)
  1162. {
  1163. DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  1164. *n_ph = 0;
  1165. }
  1166. if(new_voice)
  1167. {
  1168. // finished the current clause, now change the voice if there was an embedded
  1169. // change voice command at the end of it (i.e. clause was broken at the change voice command)
  1170. DoVoice(new_voice);
  1171. new_voice = NULL;
  1172. }
  1173. return(0); // finished the phoneme list
  1174. } // end of Generate
  1175. static int timer_on = 0;
  1176. static int paused = 0;
  1177. int SynthOnTimer()
  1178. {//===============
  1179. if(!timer_on)
  1180. {
  1181. return(WavegenCloseSound());
  1182. }
  1183. do {
  1184. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  1185. {
  1186. SpeakNextClause(NULL,NULL,1);
  1187. }
  1188. } while(skipping_text);
  1189. return(0);
  1190. }
  1191. int SynthStatus()
  1192. {//==============
  1193. return(timer_on | paused);
  1194. }
  1195. int SpeakNextClause(FILE *f_in, const void *text_in, int control)
  1196. {//==============================================================
  1197. // Speak text from file (f_in) or memory (text_in)
  1198. // control 0: start
  1199. // either f_in or text_in is set, the other must be NULL
  1200. // The other calls have f_in and text_in = NULL
  1201. // control 1: speak next text
  1202. // 2: stop
  1203. // 3: pause (toggle)
  1204. // 4: is file being read (0=no, 1=yes)
  1205. // 5: interrupt and flush current text.
  1206. int clause_tone;
  1207. char *voice_change;
  1208. static FILE *f_text=NULL;
  1209. static const void *p_text=NULL;
  1210. if(control == 4)
  1211. {
  1212. if((f_text == NULL) && (p_text == NULL))
  1213. return(0);
  1214. else
  1215. return(1);
  1216. }
  1217. if(control == 2)
  1218. {
  1219. // stop speaking
  1220. timer_on = 0;
  1221. p_text = NULL;
  1222. if(f_text != NULL)
  1223. {
  1224. fclose(f_text);
  1225. f_text=NULL;
  1226. }
  1227. n_phoneme_list = 0;
  1228. WcmdqStop();
  1229. return(0);
  1230. }
  1231. if(control == 3)
  1232. {
  1233. // toggle pause
  1234. if(paused == 0)
  1235. {
  1236. timer_on = 0;
  1237. paused = 2;
  1238. }
  1239. else
  1240. {
  1241. WavegenOpenSound();
  1242. timer_on = 1;
  1243. paused = 0;
  1244. Generate(phoneme_list,&n_phoneme_list,0); // re-start from beginning of clause
  1245. }
  1246. return(0);
  1247. }
  1248. if(control == 5)
  1249. {
  1250. // stop speaking, but continue looking for text
  1251. n_phoneme_list = 0;
  1252. WcmdqStop();
  1253. return(0);
  1254. }
  1255. if((f_in != NULL) || (text_in != NULL))
  1256. {
  1257. f_text = f_in;
  1258. p_text = text_in;
  1259. timer_on = 1;
  1260. paused = 0;
  1261. }
  1262. if((f_text==NULL) && (p_text==NULL))
  1263. {
  1264. skipping_text = 0;
  1265. timer_on = 0;
  1266. return(0);
  1267. }
  1268. if((f_text != NULL) && feof(f_text))
  1269. {
  1270. timer_on = 0;
  1271. fclose(f_text);
  1272. f_text=NULL;
  1273. return(0);
  1274. }
  1275. // read the next clause from the input text file, translate it, and generate
  1276. // entries in the wavegen command queue
  1277. p_text = translator->TranslateClause(f_text,p_text,&clause_tone,&voice_change);
  1278. if(option_phonemes > 0)
  1279. {
  1280. fprintf(f_trans,"%s\n",translator->phon_out);
  1281. }
  1282. translator->CalcPitches(clause_tone);
  1283. translator->CalcLengths();
  1284. if(voice_change != NULL)
  1285. {
  1286. // voice change at the end of the clause (i.e. clause was terminated by a voice change)
  1287. new_voice = LoadVoiceVariant(voice_change+1,voice_change[0]); // add a Voice instruction to wavegen at the end of the clause
  1288. if(new_voice != NULL)
  1289. voice = new_voice;
  1290. }
  1291. if(skipping_text)
  1292. {
  1293. n_phoneme_list = 0;
  1294. return(1);
  1295. }
  1296. if(mbrola_name[0] != 0)
  1297. {
  1298. #ifdef USE_MBROLA_LIB
  1299. MbrolaTranslate(phoneme_list,n_phoneme_list,NULL);
  1300. #else
  1301. MbrolaTranslate(phoneme_list,n_phoneme_list,stdout);
  1302. #endif
  1303. }
  1304. Generate(phoneme_list,&n_phoneme_list,0);
  1305. WavegenOpenSound();
  1306. return(1);
  1307. } // end of SpeakNextClause