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

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