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

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