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

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