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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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=N_SOUNDICON_SLOTS;
  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(len > 36)
  459. seq_len_adjust += (len - 36);
  460. if(f2 != 0)
  461. {
  462. AdjustFormants(fr, f2, f2_min, f2_max, f1, f3_adj, f3_amp, flags);
  463. }
  464. }
  465. set_frame_rms(fr,rms);
  466. if((vcolour > 0) && (vcolour <= N_VCOLOUR))
  467. {
  468. for(ix=0; ix<n_frames; ix++)
  469. {
  470. fr = CopyFrame(seq[ix].frame,0);
  471. seq[ix].frame = fr;
  472. for(formant=1; formant<=5; formant++)
  473. {
  474. int x;
  475. x = fr->ffreq[formant] * vcolouring[vcolour-1][formant-1];
  476. fr->ffreq[formant] = x / 256;
  477. }
  478. }
  479. }
  480. }
  481. }
  482. if(fr != NULL)
  483. {
  484. if(flags & 4)
  485. fr->frflags |= FRFLAG_FORMANT_RATE;
  486. if(flags & 2)
  487. fr->frflags |= FRFLAG_BREAK; // don't merge with next frame
  488. }
  489. if(flags & 0x40)
  490. DoPause(12); // add a short pause after the consonant
  491. if(flags & 16)
  492. return(len);
  493. return(0);
  494. } // end of FormantTransition2
  495. static void SmoothSpect(void)
  496. {//==========================
  497. // Limit the rate of frequence change of formants, to reduce chirping
  498. long *q;
  499. frame_t *frame;
  500. frame_t *frame2;
  501. frame_t *frame1;
  502. frame_t *frame_centre;
  503. int ix;
  504. int len;
  505. int pk;
  506. int modified;
  507. int allowed;
  508. int diff;
  509. if(syllable_start == syllable_end)
  510. return;
  511. if((syllable_centre < 0) || (syllable_centre == syllable_start))
  512. {
  513. syllable_start = syllable_end;
  514. return;
  515. }
  516. q = wcmdq[syllable_centre];
  517. frame_centre = (frame_t *)q[2];
  518. // backwards
  519. ix = syllable_centre -1;
  520. frame = frame2 = frame_centre;
  521. for(;;)
  522. {
  523. if(ix < 0) ix = N_WCMDQ-1;
  524. q = wcmdq[ix];
  525. if(q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  526. break;
  527. if(q[0] == WCMD_SPECT || q[0] == WCMD_SPECT2)
  528. {
  529. len = q[1] & 0xffff;
  530. frame1 = (frame_t *)q[3];
  531. if(frame1 == frame)
  532. {
  533. q[3] = (long)frame2;
  534. frame1 = frame2;
  535. }
  536. else
  537. break; // doesn't follow on from previous frame
  538. frame = frame2 = (frame_t *)q[2];
  539. modified = 0;
  540. if(frame->frflags & FRFLAG_BREAK)
  541. break;
  542. if(frame->frflags & FRFLAG_FORMANT_RATE)
  543. len = (len * 12)/10; // allow slightly greater rate of change for this frame (was 12/10)
  544. for(pk=0; pk<6; pk++)
  545. {
  546. int f1, f2;
  547. if((frame->frflags & FRFLAG_BREAK_LF) && (pk < 3))
  548. continue;
  549. f1 = frame1->ffreq[pk];
  550. f2 = frame->ffreq[pk];
  551. // backwards
  552. if((diff = f2 - f1) > 0)
  553. {
  554. allowed = f1*2 + f2;
  555. }
  556. else
  557. {
  558. allowed = f1 + f2*2;
  559. }
  560. // the allowed change is specified as percentage (%*10) of the frequency
  561. // take "frequency" as 1/3 from the lower freq
  562. allowed = (allowed * formant_rate[pk])/3000;
  563. allowed = (allowed * len)/256;
  564. if(diff > allowed)
  565. {
  566. if(modified == 0)
  567. {
  568. frame2 = CopyFrame(frame,0);
  569. modified = 1;
  570. }
  571. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  572. q[2] = (long)frame2;
  573. }
  574. else
  575. if(diff < -allowed)
  576. {
  577. if(modified == 0)
  578. {
  579. frame2 = CopyFrame(frame,0);
  580. modified = 1;
  581. }
  582. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  583. q[2] = (long)frame2;
  584. }
  585. }
  586. }
  587. if(ix == syllable_start)
  588. break;
  589. ix--;
  590. }
  591. // forwards
  592. ix = syllable_centre;
  593. frame = NULL;
  594. for(;;)
  595. {
  596. q = wcmdq[ix];
  597. if(q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  598. break;
  599. if(q[0] == WCMD_SPECT || q[0] == WCMD_SPECT2)
  600. {
  601. len = q[1] & 0xffff;
  602. frame1 = (frame_t *)q[2];
  603. if(frame != NULL)
  604. {
  605. if(frame1 == frame)
  606. {
  607. q[2] = (long)frame2;
  608. frame1 = frame2;
  609. }
  610. else
  611. break; // doesn't follow on from previous frame
  612. }
  613. frame = frame2 = (frame_t *)q[3];
  614. modified = 0;
  615. if(frame1->frflags & FRFLAG_BREAK)
  616. break;
  617. if(frame1->frflags & FRFLAG_FORMANT_RATE)
  618. len = (len *6)/5; // allow slightly greater rate of change for this frame
  619. for(pk=0; pk<6; pk++)
  620. {
  621. int f1, f2;
  622. f1 = frame1->ffreq[pk];
  623. f2 = frame->ffreq[pk];
  624. // forwards
  625. if((diff = f2 - f1) > 0)
  626. {
  627. allowed = f1*2 + f2;
  628. }
  629. else
  630. {
  631. allowed = f1 + f2*2;
  632. }
  633. allowed = (allowed * formant_rate[pk])/3000;
  634. allowed = (allowed * len)/256;
  635. if(diff > allowed)
  636. {
  637. if(modified == 0)
  638. {
  639. frame2 = CopyFrame(frame,0);
  640. modified = 1;
  641. }
  642. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  643. q[3] = (long)frame2;
  644. }
  645. else
  646. if(diff < -allowed)
  647. {
  648. if(modified == 0)
  649. {
  650. frame2 = CopyFrame(frame,0);
  651. modified = 1;
  652. }
  653. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  654. q[3] = (long)frame2;
  655. }
  656. }
  657. }
  658. ix++;
  659. if(ix >= N_WCMDQ) ix = 0;
  660. if(ix == syllable_end)
  661. break;
  662. }
  663. syllable_start = syllable_end;
  664. }
  665. static void StartSyllable(void)
  666. {//============================
  667. // start of syllable, if not already started
  668. if(syllable_end == syllable_start)
  669. syllable_end = wcmdq_tail;
  670. }
  671. int DoSpect(PHONEME_TAB *this_ph, PHONEME_TAB *prev_ph, PHONEME_TAB *next_ph,
  672. int which, PHONEME_LIST *plist, int modulation)
  673. {//===================================================================================
  674. // which 1 start of phoneme, 2 body and end
  675. // length_mod: 256 = 100%
  676. // modulation: -1 = don't write to wcmdq
  677. int n_frames;
  678. frameref_t *frames;
  679. int frameix;
  680. frame_t *frame1;
  681. frame_t *frame2;
  682. frame_t *fr;
  683. int ix;
  684. long *q;
  685. int len;
  686. int match_level;
  687. int frame_length;
  688. int frame1_length;
  689. int frame2_length;
  690. int length_factor;
  691. int length_mod;
  692. int total_len = 0;
  693. static int wave_flag = 0;
  694. int wcmd_spect = WCMD_SPECT;
  695. length_mod = plist->length;
  696. if(length_mod==0) length_mod=256;
  697. if(which==1)
  698. {
  699. // limit the shortening of sonorants before shortened (eg. unstressed vowels)
  700. if((this_ph->type==phLIQUID) || (prev_ph->type==phLIQUID) || (prev_ph->type==phNASAL))
  701. {
  702. if(length_mod < (len = translator->langopts.param[LOPT_SONORANT_MIN]))
  703. {
  704. length_mod = len;
  705. }
  706. }
  707. }
  708. modn_flags = 0;
  709. frames = LookupSpect(this_ph,prev_ph,next_ph,which,&match_level,&n_frames, plist);
  710. if(frames == NULL)
  711. return(0); // not found
  712. if(wavefile_ix == 0)
  713. {
  714. if(wave_flag)
  715. {
  716. // cancel any wavefile that was playing previously
  717. wcmd_spect = WCMD_SPECT2;
  718. wave_flag = 0;
  719. }
  720. else
  721. {
  722. wcmd_spect = WCMD_SPECT;
  723. }
  724. }
  725. frame1 = frames[0].frame;
  726. frame1_length = frames[0].length;
  727. if(last_frame != NULL)
  728. {
  729. if(((last_frame->length < 2) || (last_frame->frflags & FRFLAG_VOWEL_CENTRE))
  730. && !(last_frame->frflags & FRFLAG_BREAK))
  731. {
  732. // last frame of previous sequence was zero-length, replace with first of this sequence
  733. wcmdq[last_wcmdq][3] = (long)frame1;
  734. if(last_frame->frflags & FRFLAG_BREAK_LF)
  735. {
  736. // but flag indicates keep HF peaks in last segment
  737. fr = CopyFrame(frame1,1);
  738. for(ix=3; ix<N_PEAKS; ix++)
  739. {
  740. fr->ffreq[ix] = last_frame->ffreq[ix];
  741. fr->fheight[ix] = last_frame->fheight[ix];
  742. }
  743. wcmdq[last_wcmdq][3] = (long)fr;
  744. }
  745. }
  746. }
  747. if((this_ph->type == phVOWEL) && (which == 2))
  748. {
  749. SmoothSpect(); // process previous syllable
  750. // remember the point in the output queue of the centre of the vowel
  751. syllable_centre = wcmdq_tail;
  752. }
  753. frame_length = frame1_length;
  754. for(frameix=1; frameix<n_frames; frameix++)
  755. {
  756. frame2 = frames[frameix].frame;
  757. frame2_length = frames[frameix].length;
  758. if((wavefile_ix != 0) && ((frame1->frflags & FRFLAG_DEFER_WAV)==0))
  759. {
  760. // there is a wave file to play along with this synthesis
  761. seq_len_adjust = 0;
  762. DoSample2(wavefile_ix,which+0x100,0,wavefile_amp);
  763. wave_flag = 1;
  764. wavefile_ix = 0;
  765. }
  766. length_factor = length_mod;
  767. if(frame1->frflags & FRFLAG_LEN_MOD) // reduce effect of length mod
  768. {
  769. length_factor = (length_mod*4 + 256*3)/7;
  770. }
  771. len = (frame_length * samplerate)/1000;
  772. len = (len * length_factor)/256;
  773. if(modulation >= 0)
  774. {
  775. if(frame1->frflags & FRFLAG_MODULATE)
  776. {
  777. modulation = 6;
  778. }
  779. if((frameix == n_frames-1) && (modn_flags & 0xf00))
  780. modulation |= modn_flags; // before or after a glottal stop
  781. }
  782. pitch_length += len;
  783. amp_length += len;
  784. if(frame_length < 2)
  785. {
  786. last_frame = NULL;
  787. frame_length = frame2_length;
  788. frame1 = frame2;
  789. }
  790. else
  791. {
  792. last_wcmdq = wcmdq_tail;
  793. if(modulation >= 0)
  794. {
  795. q = wcmdq[wcmdq_tail];
  796. q[0] = wcmd_spect;
  797. q[1] = len + (modulation << 16);
  798. q[2] = long(frame1);
  799. q[3] = long(frame2);
  800. WcmdqInc();
  801. }
  802. last_frame = frame1 = frame2;
  803. frame_length = frame2_length;
  804. total_len += len;
  805. }
  806. }
  807. return(total_len);
  808. } // end of Synthesize::DoSpect
  809. static void DoMarker(int type, int char_posn, int length, int value)
  810. {//=================================================================
  811. // This could be used to return an index to the word currently being spoken
  812. // Type 1=word, 2=sentence, 3=named marker, 4=play audio, 5=end
  813. wcmdq[wcmdq_tail][0] = WCMD_MARKER;
  814. wcmdq[wcmdq_tail][1] = type;
  815. wcmdq[wcmdq_tail][2] = (char_posn & 0xffffff) | (length << 24);
  816. wcmdq[wcmdq_tail][3] = value;
  817. WcmdqInc();
  818. } // end of Synthesize::DoMarker
  819. void DoVoiceChange(voice_t *v)
  820. {//===========================
  821. // allocate memory for a copy of the voice data, and free it in wavegenfill()
  822. voice_t *v2;
  823. v2 = (voice_t *)malloc(sizeof(voice_t));
  824. memcpy(v2,v,sizeof(voice_t));
  825. wcmdq[wcmdq_tail][0] = WCMD_VOICE;
  826. wcmdq[wcmdq_tail][1] = (long)(v2);
  827. WcmdqInc();
  828. }
  829. static void DoEmbedded(int &embix, int sourceix)
  830. {//=============================================
  831. // There were embedded commands in the text at this point
  832. unsigned int word; // bit 7=last command for this word, bits 5,6 sign, bits 0-4 command
  833. unsigned int value;
  834. int command;
  835. do {
  836. word = embedded_list[embix++];
  837. value = word >> 8;
  838. command = word & 0x7f;
  839. switch(command & 0x1f)
  840. {
  841. case EMBED_S: // speed
  842. SetEmbedded((command & 0x60) + EMBED_S2,value); // adjusts embedded_value[EMBED_S2]
  843. SetSpeed(2);
  844. break;
  845. case EMBED_I: // play dynamically loaded wav data (sound icon)
  846. if((int)value < n_soundicon_tab)
  847. {
  848. if(soundicon_tab[value].length != 0)
  849. {
  850. DoPause(10); // ensure a break in the speech
  851. wcmdq[wcmdq_tail][0] = WCMD_WAVE;
  852. wcmdq[wcmdq_tail][1] = soundicon_tab[value].length;
  853. wcmdq[wcmdq_tail][2] = (long)soundicon_tab[value].data + 44; // skip WAV header
  854. wcmdq[wcmdq_tail][3] = 0x1500; // 16 bit data, amp=21
  855. WcmdqInc();
  856. }
  857. }
  858. break;
  859. case EMBED_M: // named marker
  860. DoMarker(espeakEVENT_MARK, (sourceix & 0x7ff) + clause_start_char, 0, value);
  861. break;
  862. case EMBED_U: // play sound
  863. DoMarker(espeakEVENT_PLAY, count_characters+1, 0, value); // always occurs at end of clause
  864. break;
  865. default:
  866. DoPause(10); // ensure a break in the speech
  867. wcmdq[wcmdq_tail][0] = WCMD_EMBEDDED;
  868. wcmdq[wcmdq_tail][1] = command;
  869. wcmdq[wcmdq_tail][2] = value;
  870. WcmdqInc();
  871. break;
  872. }
  873. } while ((word & 0x80) == 0);
  874. }
  875. void SwitchDictionary()
  876. {//====================
  877. }
  878. int Generate(PHONEME_LIST *phoneme_list, int *n_ph, int resume)
  879. {//============================================================
  880. static int ix;
  881. static int embedded_ix;
  882. static int word_count;
  883. PHONEME_LIST *prev;
  884. PHONEME_LIST *next;
  885. PHONEME_LIST *next2;
  886. PHONEME_LIST *p;
  887. int released;
  888. int stress;
  889. int modulation;
  890. int pre_voiced;
  891. int free_min;
  892. unsigned char *pitch_env=NULL;
  893. unsigned char *amp_env;
  894. PHONEME_TAB *ph;
  895. PHONEME_TAB *prev_ph;
  896. static int sourceix=0;
  897. #ifdef TEST_MBROLA
  898. if(mbrola_name[0] != 0)
  899. return(MbrolaGenerate(phoneme_list,n_ph,resume));
  900. #endif
  901. if(option_quiet)
  902. return(0);
  903. if(resume == 0)
  904. {
  905. ix = 1;
  906. embedded_ix=0;
  907. word_count = 0;
  908. pitch_length = 0;
  909. amp_length = 0;
  910. last_frame = NULL;
  911. last_wcmdq = -1;
  912. syllable_start = wcmdq_tail;
  913. syllable_end = wcmdq_tail;
  914. syllable_centre = -1;
  915. last_pitch_cmd = -1;
  916. memset(vowel_transition,0,sizeof(vowel_transition));
  917. }
  918. while(ix < (*n_ph))
  919. {
  920. p = &phoneme_list[ix];
  921. if(p->type == phPAUSE)
  922. free_min = 5;
  923. else
  924. if(p->type != phVOWEL)
  925. free_min = 10; // we need less Q space for non-vowels, and we need to generate phonemes after a vowel so that the pitch_length is filled in
  926. else
  927. free_min = MIN_WCMDQ; // 22
  928. if(WcmdqFree() <= free_min)
  929. return(1); // wait
  930. prev = &phoneme_list[ix-1];
  931. next = &phoneme_list[ix+1];
  932. next2 = &phoneme_list[ix+2];
  933. if(p->synthflags & SFLAG_EMBEDDED)
  934. {
  935. DoEmbedded(embedded_ix, p->sourceix);
  936. }
  937. if(p->newword)
  938. {
  939. if(translator->langopts.param[LOPT_WORD_MERGE] == 0)
  940. last_frame = NULL;
  941. sourceix = (p->sourceix & 0x7ff) + clause_start_char;
  942. if(p->newword & 4)
  943. DoMarker(espeakEVENT_SENTENCE, sourceix, 0, count_sentences); // start of sentence
  944. // if(p->newword & 2)
  945. // DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  946. if(p->newword & 1)
  947. DoMarker(espeakEVENT_WORD, sourceix, p->sourceix >> 11, clause_start_word + word_count++);
  948. }
  949. EndAmplitude();
  950. if(p->prepause > 0)
  951. DoPause(p->prepause);
  952. if(option_phoneme_events && (p->type != phVOWEL))
  953. {
  954. // Note, for vowels, do the phoneme event after the vowel-start
  955. DoMarker(espeakEVENT_PHONEME, sourceix, 0, p->ph->mnemonic);
  956. }
  957. switch(p->type)
  958. {
  959. case phPAUSE:
  960. DoPause(p->length);
  961. break;
  962. case phSTOP:
  963. released = 0;
  964. if(next->type==phVOWEL) released = 1;
  965. if(next->type==phLIQUID && !next->newword) released = 1;
  966. if(released)
  967. DoSample(p->ph,next->ph,2,0,0);
  968. else
  969. DoSample(p->ph,phoneme_tab[phonPAUSE],2,0,0);
  970. break;
  971. case phFRICATIVE:
  972. if(p->synthflags & SFLAG_LENGTHEN)
  973. DoSample(p->ph,next->ph,2,p->length,0); // play it twice for [s:] etc.
  974. DoSample(p->ph,next->ph,2,p->length,0);
  975. break;
  976. case phVSTOP:
  977. pre_voiced = 0;
  978. if(next->type==phVOWEL)
  979. {
  980. DoAmplitude(p->amp,NULL);
  981. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  982. pre_voiced = 1;
  983. }
  984. else
  985. if((next->type==phLIQUID) && !next->newword)
  986. {
  987. DoAmplitude(next->amp,NULL);
  988. DoPitch(envelope_data[next->env],next->pitch1,next->pitch2);
  989. pre_voiced = 1;
  990. }
  991. else
  992. {
  993. if(last_pitch_cmd < 0)
  994. {
  995. DoAmplitude(next->amp,NULL);
  996. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  997. }
  998. }
  999. if((prev->type==phVOWEL) || (prev->ph->phflags & phVOWEL2))
  1000. {
  1001. // a period of voicing before the release
  1002. DoSpect(p->ph,phoneme_tab[phonSCHWA],next->ph,1,p,0);
  1003. if(p->synthflags & SFLAG_LENGTHEN)
  1004. {
  1005. DoPause(20);
  1006. DoSpect(p->ph,phoneme_tab[phonSCHWA],next->ph,1,p,0);
  1007. }
  1008. }
  1009. else
  1010. {
  1011. if(p->synthflags & SFLAG_LENGTHEN)
  1012. {
  1013. DoPause(50);
  1014. }
  1015. }
  1016. if(pre_voiced)
  1017. {
  1018. // followed by a vowel, or liquid + vowel
  1019. StartSyllable();
  1020. DoSpect(p->ph,prev->ph,next->ph,2,p,0);
  1021. }
  1022. else
  1023. {
  1024. // if((prev->type != phVOWEL) && ((prev->ph->phflags & phVOICED)==0) && ((next->ph->phflags & phVOICED)==0))
  1025. // DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE_SHORT],2,p,0);
  1026. // else
  1027. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1028. // DoSpect(p->ph,prev->ph,next->ph,2,p,0);
  1029. }
  1030. break;
  1031. case phVFRICATIVE:
  1032. if(next->type==phVOWEL)
  1033. {
  1034. DoAmplitude(p->amp,NULL);
  1035. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1036. }
  1037. else
  1038. if(next->type==phLIQUID)
  1039. {
  1040. DoAmplitude(next->amp,NULL);
  1041. DoPitch(envelope_data[next->env],next->pitch1,next->pitch2);
  1042. }
  1043. else
  1044. {
  1045. if(last_pitch_cmd < 0)
  1046. {
  1047. DoAmplitude(p->amp,NULL);
  1048. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1049. }
  1050. }
  1051. if((next->type==phVOWEL) || ((next->type==phLIQUID)) && (next->newword==0)) // ?? test 14.Aug.2007
  1052. {
  1053. StartSyllable();
  1054. if(p->synthflags & SFLAG_LENGTHEN)
  1055. DoSpect(p->ph,prev->ph,next->ph,2,p,0);
  1056. DoSpect(p->ph,prev->ph,next->ph,2,p,0);
  1057. }
  1058. else
  1059. {
  1060. if(p->synthflags & SFLAG_LENGTHEN)
  1061. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1062. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1063. }
  1064. break;
  1065. case phNASAL:
  1066. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1067. {
  1068. DoAmplitude(p->amp,NULL);
  1069. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1070. }
  1071. if(prev->type==phNASAL)
  1072. {
  1073. last_frame = NULL;
  1074. }
  1075. if(next->type==phVOWEL)
  1076. {
  1077. StartSyllable();
  1078. DoSpect(p->ph,prev->ph,next->ph,1,p,0);
  1079. }
  1080. else
  1081. if(prev->type==phVOWEL && (p->synthflags & SFLAG_SEQCONTINUE))
  1082. {
  1083. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1084. }
  1085. else
  1086. {
  1087. last_frame = NULL; // only for nasal ?
  1088. if(next->type == phLIQUID)
  1089. DoSpect(p->ph,prev->ph,phoneme_tab[phonSONORANT],2,p,0);
  1090. else
  1091. DoSpect(p->ph,prev->ph,phoneme_tab[phonPAUSE],2,p,0);
  1092. last_frame = NULL;
  1093. }
  1094. break;
  1095. case phLIQUID:
  1096. modulation = 0;
  1097. if(p->ph->phflags & phTRILL)
  1098. modulation = 5;
  1099. prev_ph = prev->ph;
  1100. // if(p->newword)
  1101. // prev_ph = phoneme_tab[phonPAUSE]; // pronounce fully at the start of a word
  1102. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1103. {
  1104. DoAmplitude(p->amp,NULL);
  1105. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1106. }
  1107. if(prev->type==phNASAL)
  1108. {
  1109. last_frame = NULL;
  1110. }
  1111. if(next->type==phVOWEL)
  1112. {
  1113. StartSyllable();
  1114. DoSpect(p->ph,prev_ph,next->ph,1,p,modulation); // (,)r
  1115. }
  1116. else
  1117. if(prev->type==phVOWEL && (p->synthflags & SFLAG_SEQCONTINUE))
  1118. {
  1119. DoSpect(p->ph,prev_ph,next->ph,1,p,modulation);
  1120. }
  1121. else
  1122. {
  1123. DoSpect(p->ph,prev_ph,next->ph,1,p,modulation);
  1124. }
  1125. break;
  1126. case phVOWEL:
  1127. ph = p->ph;
  1128. stress = p->tone & 0xf;
  1129. // vowel transition from the preceding phoneme
  1130. vowel_transition0 = vowel_transition[0];
  1131. vowel_transition1 = vowel_transition[1];
  1132. pitch_env = envelope_data[p->env];
  1133. amp_env = NULL;
  1134. if(p->tone_ph != 0)
  1135. {
  1136. pitch_env = LookupEnvelope(phoneme_tab[p->tone_ph]->spect);
  1137. amp_env = LookupEnvelope(phoneme_tab[p->tone_ph]->after);
  1138. }
  1139. StartSyllable();
  1140. modulation = 2;
  1141. if(stress <= 1)
  1142. modulation = 1; // 16ths
  1143. else
  1144. if(stress >= 7)
  1145. modulation = 3;
  1146. if(prev->type == phVSTOP || prev->type == phVFRICATIVE)
  1147. {
  1148. DoAmplitude(p->amp,amp_env);
  1149. DoPitch(pitch_env,p->pitch1,p->pitch2); // don't use prevocalic rising tone
  1150. DoSpect(ph,prev->ph,next->ph,1,p,modulation);
  1151. }
  1152. else
  1153. if(prev->type==phLIQUID || prev->type==phNASAL)
  1154. {
  1155. DoAmplitude(p->amp,amp_env);
  1156. DoSpect(ph,prev->ph,next->ph,1,p,modulation); // continue with pre-vocalic rising tone
  1157. DoPitch(pitch_env,p->pitch1,p->pitch2);
  1158. }
  1159. else
  1160. {
  1161. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1162. {
  1163. DoAmplitude(p->amp,amp_env);
  1164. DoPitch(pitch_env,p->pitch1,p->pitch2);
  1165. }
  1166. DoSpect(ph,prev->ph,next->ph,1,p,modulation);
  1167. }
  1168. if(option_phoneme_events)
  1169. {
  1170. DoMarker(espeakEVENT_PHONEME, sourceix, 0, p->ph->mnemonic);
  1171. }
  1172. DoSpect(p->ph,prev->ph,next->ph,2,p,modulation);
  1173. memset(vowel_transition,0,sizeof(vowel_transition));
  1174. break;
  1175. }
  1176. ix++;
  1177. }
  1178. EndPitch(1);
  1179. if(*n_ph > 0)
  1180. {
  1181. DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  1182. *n_ph = 0;
  1183. }
  1184. return(0); // finished the phoneme list
  1185. } // end of Generate
  1186. static int timer_on = 0;
  1187. static int paused = 0;
  1188. int SynthOnTimer()
  1189. {//===============
  1190. if(!timer_on)
  1191. {
  1192. return(WavegenCloseSound());
  1193. }
  1194. do {
  1195. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  1196. {
  1197. SpeakNextClause(NULL,NULL,1);
  1198. }
  1199. } while(skipping_text);
  1200. return(0);
  1201. }
  1202. int SynthStatus()
  1203. {//==============
  1204. return(timer_on | paused);
  1205. }
  1206. int SpeakNextClause(FILE *f_in, const void *text_in, int control)
  1207. {//==============================================================
  1208. // Speak text from file (f_in) or memory (text_in)
  1209. // control 0: start
  1210. // either f_in or text_in is set, the other must be NULL
  1211. // The other calls have f_in and text_in = NULL
  1212. // control 1: speak next text
  1213. // 2: stop
  1214. // 3: pause (toggle)
  1215. // 4: is file being read (0=no, 1=yes)
  1216. // 5: interrupt and flush current text.
  1217. int clause_tone;
  1218. char *voice_change;
  1219. static FILE *f_text=NULL;
  1220. static const void *p_text=NULL;
  1221. if(control == 4)
  1222. {
  1223. if((f_text == NULL) && (p_text == NULL))
  1224. return(0);
  1225. else
  1226. return(1);
  1227. }
  1228. if(control == 2)
  1229. {
  1230. // stop speaking
  1231. timer_on = 0;
  1232. p_text = NULL;
  1233. if(f_text != NULL)
  1234. {
  1235. fclose(f_text);
  1236. f_text=NULL;
  1237. }
  1238. n_phoneme_list = 0;
  1239. WcmdqStop();
  1240. return(0);
  1241. }
  1242. if(control == 3)
  1243. {
  1244. // toggle pause
  1245. if(paused == 0)
  1246. {
  1247. timer_on = 0;
  1248. paused = 2;
  1249. }
  1250. else
  1251. {
  1252. WavegenOpenSound();
  1253. timer_on = 1;
  1254. paused = 0;
  1255. Generate(phoneme_list,&n_phoneme_list,0); // re-start from beginning of clause
  1256. }
  1257. return(0);
  1258. }
  1259. if(control == 5)
  1260. {
  1261. // stop speaking, but continue looking for text
  1262. n_phoneme_list = 0;
  1263. WcmdqStop();
  1264. return(0);
  1265. }
  1266. if((f_in != NULL) || (text_in != NULL))
  1267. {
  1268. f_text = f_in;
  1269. p_text = text_in;
  1270. timer_on = 1;
  1271. paused = 0;
  1272. }
  1273. if((f_text==NULL) && (p_text==NULL))
  1274. {
  1275. skipping_text = 0;
  1276. timer_on = 0;
  1277. return(0);
  1278. }
  1279. if((f_text != NULL) && feof(f_text))
  1280. {
  1281. timer_on = 0;
  1282. fclose(f_text);
  1283. f_text=NULL;
  1284. return(0);
  1285. }
  1286. if(current_phoneme_table != voice->phoneme_tab_ix)
  1287. {
  1288. SelectPhonemeTable(voice->phoneme_tab_ix);
  1289. }
  1290. // read the next clause from the input text file, translate it, and generate
  1291. // entries in the wavegen command queue
  1292. p_text = translator->TranslateClause(f_text,p_text,&clause_tone,&voice_change);
  1293. translator->CalcPitches(clause_tone);
  1294. translator->CalcLengths();
  1295. translator->GetTranslatedPhonemeString(translator->phon_out,sizeof(translator->phon_out));
  1296. if(option_phonemes > 0)
  1297. {
  1298. fprintf(f_trans,"%s\n",translator->phon_out);
  1299. if(!iswalpha(0x010d))
  1300. {
  1301. // check that c-caron is recognized as an alphabetic character
  1302. fprintf(stderr,"Warning: Accented letters are not recognized, eg: U+010D\nSet LC_CTYPE to a UTF-8 locale\n");
  1303. }
  1304. }
  1305. if(phoneme_callback != NULL)
  1306. {
  1307. phoneme_callback(translator->phon_out);
  1308. }
  1309. if(skipping_text)
  1310. {
  1311. n_phoneme_list = 0;
  1312. return(1);
  1313. }
  1314. if(mbrola_name[0] != 0)
  1315. {
  1316. #ifdef USE_MBROLA_LIB
  1317. MbrolaTranslate(phoneme_list,n_phoneme_list,NULL);
  1318. #else
  1319. MbrolaTranslate(phoneme_list,n_phoneme_list,stdout);
  1320. #endif
  1321. }
  1322. Generate(phoneme_list,&n_phoneme_list,0);
  1323. WavegenOpenSound();
  1324. if(voice_change != NULL)
  1325. {
  1326. // voice change at the end of the clause (i.e. clause was terminated by a voice change)
  1327. new_voice = LoadVoiceVariant(voice_change,0); // add a Voice instruction to wavegen at the end of the clause
  1328. }
  1329. if(new_voice)
  1330. {
  1331. // finished the current clause, now change the voice if there was an embedded
  1332. // change voice command at the end of it (i.e. clause was broken at the change voice command)
  1333. DoVoiceChange(voice);
  1334. new_voice = NULL;
  1335. }
  1336. return(1);
  1337. } // end of SpeakNextClause