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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2013 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+1];
  37. int mbrola_delay;
  38. char mbrola_name[20];
  39. SPEED_FACTORS speed;
  40. static int last_pitch_cmd;
  41. static int last_amp_cmd;
  42. static frame_t *last_frame;
  43. static int last_wcmdq;
  44. static int pitch_length;
  45. static int amp_length;
  46. static int modn_flags;
  47. static int fmt_amplitude=0;
  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 // 28
  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<4; 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 EndPitch
  108. static void DoAmplitude(int amp, unsigned char *amp_env)
  109. {//=====================================================
  110. long64 *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] = (long64)amp_env;
  117. q[3] = amp;
  118. WcmdqInc();
  119. } // end of DoAmplitude
  120. static void DoPitch(unsigned char *env, int pitch1, int pitch2)
  121. {//============================================================
  122. long64 *q;
  123. EndPitch(0);
  124. if(pitch1 == 255)
  125. {
  126. // pitch was not set
  127. pitch1 = 55;
  128. pitch2 = 76;
  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] = (long64)env;
  139. q[3] = (pitch1 << 16) + pitch2;
  140. WcmdqInc();
  141. } // end of DoPitch
  142. int PauseLength(int pause, int control)
  143. {//====================================
  144. int len;
  145. if(control == 0)
  146. {
  147. if(pause >= 200)
  148. len = (pause * speed.clause_pause_factor)/256;
  149. else
  150. len = (pause * speed.pause_factor)/256;
  151. }
  152. else
  153. len = (pause * speed.wav_factor)/256;
  154. if(len < speed.min_pause)
  155. {
  156. len = speed.min_pause; // mS, limit the amount to which pauses can be shortened
  157. }
  158. return(len);
  159. }
  160. static void DoPause(int length, int control)
  161. {//=========================================
  162. // control = 1, less shortening at fast speeds
  163. int len;
  164. if(length == 0)
  165. len = 0;
  166. else
  167. {
  168. len = PauseLength(length, control);
  169. len = (len * samplerate) / 1000; // convert from mS to number of samples
  170. }
  171. EndPitch(1);
  172. wcmdq[wcmdq_tail][0] = WCMD_PAUSE;
  173. wcmdq[wcmdq_tail][1] = len;
  174. WcmdqInc();
  175. last_frame = NULL;
  176. if(fmt_amplitude != 0)
  177. {
  178. wcmdq[wcmdq_tail][0] = WCMD_FMT_AMPLITUDE;
  179. wcmdq[wcmdq_tail][1] = fmt_amplitude = 0;
  180. WcmdqInc();
  181. }
  182. } // end of DoPause
  183. extern int seq_len_adjust; // temporary fix to advance the start point for playing the wav sample
  184. static int DoSample2(int index, int which, int std_length, int control, int length_mod, int amp)
  185. {//=============================================================================================
  186. int length;
  187. int wav_length;
  188. int wav_scale;
  189. int min_length;
  190. int x;
  191. int len4;
  192. long64 *q;
  193. unsigned char *p;
  194. index = index & 0x7fffff;
  195. p = &wavefile_data[index];
  196. wav_scale = p[2];
  197. wav_length = (p[1] * 256);
  198. wav_length += p[0]; // length in bytes
  199. if(wav_length == 0)
  200. return(0);
  201. min_length = speed.min_sample_len;
  202. if(wav_scale==0)
  203. min_length *= 2; // 16 bit samples
  204. else
  205. {
  206. // increase consonant amplitude at high speeds, depending on the peak consonant amplitude
  207. // x = ((35 - wav_scale) * speed.loud_consonants);
  208. // if(x < 0) x = 0;
  209. // wav_scale = (wav_scale * (x+256))/256;
  210. }
  211. if(std_length > 0)
  212. {
  213. std_length = (std_length * samplerate)/1000;
  214. if(wav_scale == 0)
  215. std_length *= 2;
  216. x = (min_length * std_length)/wav_length;
  217. if(x > min_length)
  218. min_length = x;
  219. }
  220. else
  221. {
  222. // no length specified, use the length of the stored sound
  223. std_length = wav_length;
  224. }
  225. if(length_mod > 0)
  226. {
  227. std_length = (std_length * length_mod)/256;
  228. }
  229. length = (std_length * speed.wav_factor)/256;
  230. if(control & pd_DONTLENGTHEN)
  231. {
  232. // this option is used for Stops, with short noise bursts.
  233. // Don't change their length much.
  234. if(length > std_length)
  235. {
  236. // don't let length exceed std_length
  237. length = std_length;
  238. }
  239. else
  240. {
  241. // reduce the reduction in length
  242. // length = (length + std_length)/2;
  243. }
  244. }
  245. if(length < min_length)
  246. length = min_length;
  247. if(wav_scale == 0)
  248. {
  249. // 16 bit samples
  250. length /= 2;
  251. wav_length /= 2;
  252. }
  253. if(amp < 0)
  254. return(length);
  255. len4 = wav_length / 4;
  256. index += 4;
  257. if(which & 0x100)
  258. {
  259. // mix this with synthesised wave
  260. last_wcmdq = wcmdq_tail;
  261. q = wcmdq[wcmdq_tail];
  262. q[0] = WCMD_WAVE2;
  263. q[1] = length | (wav_length << 16); // length in samples
  264. q[2] = long64(&wavefile_data[index]);
  265. q[3] = wav_scale + (amp << 8);
  266. WcmdqInc();
  267. return(length);
  268. }
  269. if(length > wav_length)
  270. {
  271. x = len4*3;
  272. length -= x;
  273. }
  274. else
  275. {
  276. x = length;
  277. length = 0;
  278. }
  279. last_wcmdq = wcmdq_tail;
  280. q = wcmdq[wcmdq_tail];
  281. q[0] = WCMD_WAVE;
  282. q[1] = x; // length in samples
  283. q[2] = long64(&wavefile_data[index]);
  284. q[3] = wav_scale + (amp << 8);
  285. WcmdqInc();
  286. while(length > len4*3)
  287. {
  288. x = len4;
  289. if(wav_scale == 0)
  290. x *= 2;
  291. last_wcmdq = wcmdq_tail;
  292. q = wcmdq[wcmdq_tail];
  293. q[0] = WCMD_WAVE;
  294. q[1] = len4*2; // length in samples
  295. q[2] = long64(&wavefile_data[index+x]);
  296. q[3] = wav_scale + (amp << 8);
  297. WcmdqInc();
  298. length -= len4*2;
  299. }
  300. if(length > 0)
  301. {
  302. x = wav_length - length;
  303. if(wav_scale == 0)
  304. x *= 2;
  305. last_wcmdq = wcmdq_tail;
  306. q = wcmdq[wcmdq_tail];
  307. q[0] = WCMD_WAVE;
  308. q[1] = length; // length in samples
  309. q[2] = long64(&wavefile_data[index+x]);
  310. q[3] = wav_scale + (amp << 8);
  311. WcmdqInc();
  312. }
  313. return(length);
  314. } // end of DoSample2
  315. int DoSample3(PHONEME_DATA *phdata, int length_mod, int amp)
  316. {//=========================================================
  317. int amp2;
  318. int len;
  319. EndPitch(1);
  320. if(amp == -1)
  321. {
  322. // just get the length, don't produce sound
  323. amp2 = amp;
  324. }
  325. else
  326. {
  327. amp2 = phdata->sound_param[pd_WAV];
  328. if(amp2 == 0)
  329. amp2 = 100;
  330. amp2 = (amp2 * 32)/100;
  331. }
  332. seq_len_adjust=0;
  333. if(phdata->sound_addr[pd_WAV] == 0)
  334. {
  335. len = 0;
  336. }
  337. else
  338. {
  339. len = DoSample2(phdata->sound_addr[pd_WAV], 2, phdata->pd_param[pd_LENGTHMOD]*2, phdata->pd_control, length_mod, amp2);
  340. }
  341. last_frame = NULL;
  342. return(len);
  343. } // end of DoSample3
  344. static frame_t *AllocFrame()
  345. {//=========================
  346. // Allocate a temporary spectrum frame for the wavegen queue. Use a pool which is big
  347. // enough to use a round-robin without checks.
  348. // Only needed for modifying spectra for blending to consonants
  349. #define N_FRAME_POOL N_WCMDQ
  350. static int ix=0;
  351. static frame_t frame_pool[N_FRAME_POOL];
  352. ix++;
  353. if(ix >= N_FRAME_POOL)
  354. ix = 0;
  355. return(&frame_pool[ix]);
  356. }
  357. static void set_frame_rms(frame_t *fr, int new_rms)
  358. {//=================================================
  359. // Each frame includes its RMS amplitude value, so to set a new
  360. // RMS just adjust the formant amplitudes by the appropriate ratio
  361. int x;
  362. int h;
  363. int ix;
  364. static const short sqrt_tab[200] = {
  365. 0, 64, 90,110,128,143,156,169,181,192,202,212,221,230,239,247,
  366. 256,263,271,278,286,293,300,306,313,320,326,332,338,344,350,356,
  367. 362,367,373,378,384,389,394,399,404,409,414,419,424,429,434,438,
  368. 443,448,452,457,461,465,470,474,478,483,487,491,495,499,503,507,
  369. 512,515,519,523,527,531,535,539,543,546,550,554,557,561,565,568,
  370. 572,576,579,583,586,590,593,596,600,603,607,610,613,617,620,623,
  371. 627,630,633,636,640,643,646,649,652,655,658,662,665,668,671,674,
  372. 677,680,683,686,689,692,695,698,701,704,706,709,712,715,718,721,
  373. 724,726,729,732,735,738,740,743,746,749,751,754,757,759,762,765,
  374. 768,770,773,775,778,781,783,786,789,791,794,796,799,801,804,807,
  375. 809,812,814,817,819,822,824,827,829,832,834,836,839,841,844,846,
  376. 849,851,853,856,858,861,863,865,868,870,872,875,877,879,882,884,
  377. 886,889,891,893,896,898,900,902};
  378. if(voice->klattv[0])
  379. {
  380. if(new_rms == -1)
  381. {
  382. fr->klattp[KLATT_AV] = 50;
  383. }
  384. return;
  385. }
  386. if(fr->rms == 0) return; // check for divide by zero
  387. x = (new_rms * 64)/fr->rms;
  388. if(x >= 200) x = 199;
  389. x = sqrt_tab[x]; // sqrt(new_rms/fr->rms)*0x200;
  390. for(ix=0; ix < 8; ix++)
  391. {
  392. h = fr->fheight[ix] * x;
  393. fr->fheight[ix] = h/0x200;
  394. }
  395. } /* end of set_frame_rms */
  396. static void formants_reduce_hf(frame_t *fr, int level)
  397. {//====================================================
  398. // change height of peaks 2 to 8, percentage
  399. int ix;
  400. int x;
  401. if(voice->klattv[0])
  402. return;
  403. for(ix=2; ix < 8; ix++)
  404. {
  405. x = fr->fheight[ix] * level;
  406. fr->fheight[ix] = x/100;
  407. }
  408. }
  409. static frame_t *CopyFrame(frame_t *frame1, int copy)
  410. {//=================================================
  411. // create a copy of the specified frame in temporary buffer
  412. frame_t *frame2;
  413. if((copy==0) && (frame1->frflags & FRFLAG_COPIED))
  414. {
  415. // this frame has already been copied in temporary rw memory
  416. return(frame1);
  417. }
  418. frame2 = AllocFrame();
  419. if(frame2 != NULL)
  420. {
  421. memcpy(frame2,frame1,sizeof(frame_t));
  422. frame2->length = 0;
  423. frame2->frflags |= FRFLAG_COPIED;
  424. }
  425. return(frame2);
  426. }
  427. static frame_t *DuplicateLastFrame(frameref_t *seq, int n_frames, int length)
  428. {//==========================================================================
  429. frame_t *fr;
  430. seq[n_frames-1].length = length;
  431. fr = CopyFrame(seq[n_frames-1].frame,1);
  432. seq[n_frames].frame = fr;
  433. seq[n_frames].length = 0;
  434. return fr;
  435. }
  436. static void AdjustFormants(frame_t *fr, int target, int min, int max, int f1_adj, int f3_adj, int hf_reduce, int flags)
  437. {//====================================================================================================================
  438. int x;
  439. //hf_reduce = 70; // ?? using fixed amount rather than the parameter??
  440. target = (target * voice->formant_factor)/256;
  441. x = (target - fr->ffreq[2]) / 2;
  442. if(x > max) x = max;
  443. if(x < min) x = min;
  444. fr->ffreq[2] += x;
  445. fr->ffreq[3] += f3_adj;
  446. if(flags & 0x20)
  447. {
  448. f3_adj = -f3_adj; //. reverse direction for f4,f5 change
  449. }
  450. fr->ffreq[4] += f3_adj;
  451. fr->ffreq[5] += f3_adj;
  452. if(f1_adj==1)
  453. {
  454. x = (235 - fr->ffreq[1]);
  455. if(x < -100) x = -100;
  456. if(x > -60) x = -60;
  457. fr->ffreq[1] += x;
  458. }
  459. if(f1_adj==2)
  460. {
  461. x = (235 - fr->ffreq[1]);
  462. if(x < -300) x = -300;
  463. if(x > -150) x = -150;
  464. fr->ffreq[1] += x;
  465. fr->ffreq[0] += x;
  466. }
  467. if(f1_adj==3)
  468. {
  469. x = (100 - fr->ffreq[1]);
  470. if(x < -400) x = -400;
  471. if(x > -300) x = -400;
  472. fr->ffreq[1] += x;
  473. fr->ffreq[0] += x;
  474. }
  475. formants_reduce_hf(fr,hf_reduce);
  476. }
  477. static int VowelCloseness(frame_t *fr)
  478. {//===================================
  479. // return a value 0-3 depending on the vowel's f1
  480. int f1;
  481. if((f1 = fr->ffreq[1]) < 300)
  482. return(3);
  483. if(f1 < 400)
  484. return(2);
  485. if(f1 < 500)
  486. return(1);
  487. return(0);
  488. }
  489. int FormantTransition2(frameref_t *seq, int &n_frames, unsigned int data1, unsigned int data2, PHONEME_TAB *other_ph, int which)
  490. {//==============================================================================================================================
  491. int ix;
  492. int formant;
  493. int next_rms;
  494. int len;
  495. int rms;
  496. int f1;
  497. int f2;
  498. int f2_min;
  499. int f2_max;
  500. int f3_adj;
  501. int f3_amp;
  502. int flags;
  503. int vcolour;
  504. #define N_VCOLOUR 2
  505. // percentage change for each formant in 256ths
  506. static short vcolouring[N_VCOLOUR][5] = {
  507. {243,272,256,256,256}, // palatal consonant follows
  508. {256,256,240,240,240}, // retroflex
  509. };
  510. frame_t *fr = NULL;
  511. if(n_frames < 2)
  512. return(0);
  513. len = (data1 & 0x3f) * 2;
  514. rms = (data1 >> 6) & 0x3f;
  515. flags = (data1 >> 12);
  516. f2 = (data2 & 0x3f) * 50;
  517. f2_min = (((data2 >> 6) & 0x1f) - 15) * 50;
  518. f2_max = (((data2 >> 11) & 0x1f) - 15) * 50;
  519. f3_adj = (((data2 >> 16) & 0x1f) - 15) * 50;
  520. f3_amp = ((data2 >> 21) & 0x1f) * 8;
  521. f1 = ((data2 >> 26) & 0x7);
  522. vcolour = (data2 >> 29);
  523. // fprintf(stderr,"FMT%d %3s %3d-%3d f1=%d f2=%4d %4d %4d f3=%4d %3d\n",
  524. // which,WordToString(other_ph->mnemonic),len,rms,f1,f2,f2_min,f2_max,f3_adj,f3_amp);
  525. if((other_ph != NULL) && (other_ph->mnemonic == '?'))
  526. flags |= 8;
  527. if(which == 1)
  528. {
  529. /* entry to vowel */
  530. fr = CopyFrame(seq[0].frame,0);
  531. seq[0].frame = fr;
  532. seq[0].length = VOWEL_FRONT_LENGTH;
  533. if(len > 0)
  534. seq[0].length = len;
  535. seq[0].frflags |= FRFLAG_LEN_MOD2; // reduce length modification
  536. fr->frflags |= FRFLAG_LEN_MOD2;
  537. next_rms = seq[1].frame->rms;
  538. if(voice->klattv[0])
  539. {
  540. // fr->klattp[KLATT_AV] = 53; // reduce the amplituide of the start of a vowel
  541. fr->klattp[KLATT_AV] = seq[1].frame->klattp[KLATT_AV] - 4;
  542. }
  543. if(f2 != 0)
  544. {
  545. if(rms & 0x20)
  546. {
  547. set_frame_rms(fr,(next_rms * (rms & 0x1f))/30);
  548. }
  549. AdjustFormants(fr, f2, f2_min, f2_max, f1, f3_adj, f3_amp, flags);
  550. if((rms & 0x20) == 0)
  551. {
  552. set_frame_rms(fr,rms*2);
  553. }
  554. }
  555. else
  556. {
  557. if(flags & 8)
  558. set_frame_rms(fr,(next_rms*24)/32);
  559. else
  560. set_frame_rms(fr,RMS_START);
  561. }
  562. if(flags & 8)
  563. {
  564. // set_frame_rms(fr,next_rms - 5);
  565. modn_flags = 0x800 + (VowelCloseness(fr) << 8);
  566. }
  567. }
  568. else
  569. {
  570. // exit from vowel
  571. rms = rms*2;
  572. if((f2 != 0) || (flags != 0))
  573. {
  574. if(flags & 8)
  575. {
  576. fr = CopyFrame(seq[n_frames-1].frame,0);
  577. seq[n_frames-1].frame = fr;
  578. rms = RMS_GLOTTAL1;
  579. // degree of glottal-stop effect depends on closeness of vowel (indicated by f1 freq)
  580. modn_flags = 0x400 + (VowelCloseness(fr) << 8);
  581. }
  582. else
  583. {
  584. fr = DuplicateLastFrame(seq,n_frames++,len);
  585. if(len > 36)
  586. seq_len_adjust += (len - 36);
  587. if(f2 != 0)
  588. {
  589. AdjustFormants(fr, f2, f2_min, f2_max, f1, f3_adj, f3_amp, flags);
  590. }
  591. }
  592. set_frame_rms(fr,rms);
  593. if((vcolour > 0) && (vcolour <= N_VCOLOUR))
  594. {
  595. for(ix=0; ix<n_frames; ix++)
  596. {
  597. fr = CopyFrame(seq[ix].frame,0);
  598. seq[ix].frame = fr;
  599. for(formant=1; formant<=5; formant++)
  600. {
  601. int x;
  602. x = fr->ffreq[formant] * vcolouring[vcolour-1][formant-1];
  603. fr->ffreq[formant] = x / 256;
  604. }
  605. }
  606. }
  607. }
  608. }
  609. if(fr != NULL)
  610. {
  611. if(flags & 4)
  612. fr->frflags |= FRFLAG_FORMANT_RATE;
  613. if(flags & 2)
  614. fr->frflags |= FRFLAG_BREAK; // don't merge with next frame
  615. }
  616. if(flags & 0x40)
  617. DoPause(20,0); // add a short pause after the consonant
  618. if(flags & 16)
  619. return(len);
  620. return(0);
  621. } // end of FormantTransition2
  622. static void SmoothSpect(void)
  623. {//==========================
  624. // Limit the rate of frequence change of formants, to reduce chirping
  625. long64 *q;
  626. frame_t *frame;
  627. frame_t *frame2;
  628. frame_t *frame1;
  629. frame_t *frame_centre;
  630. int ix;
  631. int len;
  632. int pk;
  633. int modified;
  634. int allowed;
  635. int diff;
  636. if(syllable_start == syllable_end)
  637. return;
  638. if((syllable_centre < 0) || (syllable_centre == syllable_start))
  639. {
  640. syllable_start = syllable_end;
  641. return;
  642. }
  643. q = wcmdq[syllable_centre];
  644. frame_centre = (frame_t *)q[2];
  645. // backwards
  646. ix = syllable_centre -1;
  647. frame = frame2 = frame_centre;
  648. for(;;)
  649. {
  650. if(ix < 0) ix = N_WCMDQ-1;
  651. q = wcmdq[ix];
  652. if(q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  653. break;
  654. if(q[0] <= WCMD_SPECT2)
  655. {
  656. len = q[1] & 0xffff;
  657. frame1 = (frame_t *)q[3];
  658. if(frame1 == frame)
  659. {
  660. q[3] = (long64)frame2;
  661. frame1 = frame2;
  662. }
  663. else
  664. break; // doesn't follow on from previous frame
  665. frame = frame2 = (frame_t *)q[2];
  666. modified = 0;
  667. if(frame->frflags & FRFLAG_BREAK)
  668. break;
  669. if(frame->frflags & FRFLAG_FORMANT_RATE)
  670. len = (len * 12)/10; // allow slightly greater rate of change for this frame (was 12/10)
  671. for(pk=0; pk<6; pk++)
  672. {
  673. int f1, f2;
  674. if((frame->frflags & FRFLAG_BREAK_LF) && (pk < 3))
  675. continue;
  676. f1 = frame1->ffreq[pk];
  677. f2 = frame->ffreq[pk];
  678. // backwards
  679. if((diff = f2 - f1) > 0)
  680. {
  681. allowed = f1*2 + f2;
  682. }
  683. else
  684. {
  685. allowed = f1 + f2*2;
  686. }
  687. // the allowed change is specified as percentage (%*10) of the frequency
  688. // take "frequency" as 1/3 from the lower freq
  689. allowed = (allowed * formant_rate[pk])/3000;
  690. allowed = (allowed * len)/256;
  691. if(diff > allowed)
  692. {
  693. if(modified == 0)
  694. {
  695. frame2 = CopyFrame(frame,0);
  696. modified = 1;
  697. }
  698. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  699. q[2] = (long64)frame2;
  700. }
  701. else
  702. if(diff < -allowed)
  703. {
  704. if(modified == 0)
  705. {
  706. frame2 = CopyFrame(frame,0);
  707. modified = 1;
  708. }
  709. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  710. q[2] = (long64)frame2;
  711. }
  712. }
  713. }
  714. if(ix == syllable_start)
  715. break;
  716. ix--;
  717. }
  718. // forwards
  719. ix = syllable_centre;
  720. frame = NULL;
  721. for(;;)
  722. {
  723. q = wcmdq[ix];
  724. if(q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  725. break;
  726. if(q[0] <= WCMD_SPECT2)
  727. {
  728. len = q[1] & 0xffff;
  729. frame1 = (frame_t *)q[2];
  730. if(frame != NULL)
  731. {
  732. if(frame1 == frame)
  733. {
  734. q[2] = (long64)frame2;
  735. frame1 = frame2;
  736. }
  737. else
  738. break; // doesn't follow on from previous frame
  739. }
  740. frame = frame2 = (frame_t *)q[3];
  741. modified = 0;
  742. if(frame1->frflags & FRFLAG_BREAK)
  743. break;
  744. if(frame1->frflags & FRFLAG_FORMANT_RATE)
  745. len = (len *6)/5; // allow slightly greater rate of change for this frame
  746. for(pk=0; pk<6; pk++)
  747. {
  748. int f1, f2;
  749. f1 = frame1->ffreq[pk];
  750. f2 = frame->ffreq[pk];
  751. // forwards
  752. if((diff = f2 - f1) > 0)
  753. {
  754. allowed = f1*2 + f2;
  755. }
  756. else
  757. {
  758. allowed = f1 + f2*2;
  759. }
  760. allowed = (allowed * formant_rate[pk])/3000;
  761. allowed = (allowed * len)/256;
  762. if(diff > allowed)
  763. {
  764. if(modified == 0)
  765. {
  766. frame2 = CopyFrame(frame,0);
  767. modified = 1;
  768. }
  769. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  770. q[3] = (long64)frame2;
  771. }
  772. else
  773. if(diff < -allowed)
  774. {
  775. if(modified == 0)
  776. {
  777. frame2 = CopyFrame(frame,0);
  778. modified = 1;
  779. }
  780. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  781. q[3] = (long64)frame2;
  782. }
  783. }
  784. }
  785. ix++;
  786. if(ix >= N_WCMDQ) ix = 0;
  787. if(ix == syllable_end)
  788. break;
  789. }
  790. syllable_start = syllable_end;
  791. } // end of SmoothSpect
  792. static void StartSyllable(void)
  793. {//============================
  794. // start of syllable, if not already started
  795. if(syllable_end == syllable_start)
  796. syllable_end = wcmdq_tail;
  797. }
  798. int DoSpect2(PHONEME_TAB *this_ph, int which, FMT_PARAMS *fmt_params, PHONEME_LIST *plist, int modulation)
  799. {//========================================================================================================
  800. // which: 0 not a vowel, 1 start of vowel, 2 body and end of vowel
  801. // length_mod: 256 = 100%
  802. // modulation: -1 = don't write to wcmdq
  803. int n_frames;
  804. frameref_t *frames;
  805. int frameix;
  806. frame_t *frame1;
  807. frame_t *frame2;
  808. frame_t *fr;
  809. int ix;
  810. long64 *q;
  811. int len;
  812. int frame_length;
  813. int length_factor;
  814. int length_mod;
  815. int length_sum;
  816. int length_min;
  817. int total_len = 0;
  818. static int wave_flag = 0;
  819. int wcmd_spect = WCMD_SPECT;
  820. int frame_lengths[N_SEQ_FRAMES];
  821. if(fmt_params->fmt_addr == 0)
  822. return(0);
  823. length_mod = plist->length;
  824. if(length_mod==0) length_mod=256;
  825. length_min = (samplerate/70); // greater than one cycle at low pitch (Hz)
  826. if(which==2)
  827. {
  828. if((translator->langopts.param[LOPT_LONG_VOWEL_THRESHOLD] > 0) && ((this_ph->std_length >= translator->langopts.param[LOPT_LONG_VOWEL_THRESHOLD]) || (plist->synthflags & SFLAG_LENGTHEN) || (this_ph->phflags & phLONG)))
  829. length_min *= 2; // ensure long vowels are longer
  830. }
  831. if(which==1)
  832. {
  833. // limit the shortening of sonorants before shortened (eg. unstressed vowels)
  834. if((this_ph->type==phLIQUID) || (plist[-1].type==phLIQUID) || (plist[-1].type==phNASAL))
  835. {
  836. if(length_mod < (len = translator->langopts.param[LOPT_SONORANT_MIN]))
  837. {
  838. length_mod = len;
  839. }
  840. }
  841. }
  842. modn_flags = 0;
  843. frames = LookupSpect(this_ph, which, fmt_params, &n_frames, plist);
  844. if(frames == NULL)
  845. return(0); // not found
  846. if(fmt_params->fmt_amp != fmt_amplitude)
  847. {
  848. // an amplitude adjustment is specified for this sequence
  849. q = wcmdq[wcmdq_tail];
  850. q[0] = WCMD_FMT_AMPLITUDE;
  851. q[1] = fmt_amplitude = fmt_params->fmt_amp;
  852. WcmdqInc();
  853. }
  854. frame1 = frames[0].frame;
  855. if(voice->klattv[0])
  856. wcmd_spect = WCMD_KLATT;
  857. wavefile_ix = fmt_params->wav_addr;
  858. if(fmt_params->wav_amp == 0)
  859. wavefile_amp = 32;
  860. else
  861. wavefile_amp = (fmt_params->wav_amp * 32)/100;
  862. if(wavefile_ix == 0)
  863. {
  864. if(wave_flag)
  865. {
  866. // cancel any wavefile that was playing previously
  867. wcmd_spect = WCMD_SPECT2;
  868. if(voice->klattv[0])
  869. wcmd_spect = WCMD_KLATT2;
  870. wave_flag = 0;
  871. }
  872. else
  873. {
  874. wcmd_spect = WCMD_SPECT;
  875. if(voice->klattv[0])
  876. wcmd_spect = WCMD_KLATT;
  877. }
  878. }
  879. if(last_frame != NULL)
  880. {
  881. if(((last_frame->length < 2) || (last_frame->frflags & FRFLAG_VOWEL_CENTRE))
  882. && !(last_frame->frflags & FRFLAG_BREAK))
  883. {
  884. // last frame of previous sequence was zero-length, replace with first of this sequence
  885. wcmdq[last_wcmdq][3] = (long64)frame1;
  886. if(last_frame->frflags & FRFLAG_BREAK_LF)
  887. {
  888. // but flag indicates keep HF peaks in last segment
  889. fr = CopyFrame(frame1,1);
  890. for(ix=3; ix < 8; ix++)
  891. {
  892. if(ix < 7)
  893. fr->ffreq[ix] = last_frame->ffreq[ix];
  894. fr->fheight[ix] = last_frame->fheight[ix];
  895. }
  896. wcmdq[last_wcmdq][3] = (long64)fr;
  897. }
  898. }
  899. }
  900. if((this_ph->type == phVOWEL) && (which == 2))
  901. {
  902. SmoothSpect(); // process previous syllable
  903. // remember the point in the output queue of the centre of the vowel
  904. syllable_centre = wcmdq_tail;
  905. }
  906. length_sum = 0;
  907. for(frameix=1; frameix < n_frames; frameix++)
  908. {
  909. length_factor = length_mod;
  910. if(frames[frameix-1].frflags & FRFLAG_LEN_MOD) // reduce effect of length mod
  911. {
  912. length_factor = (length_mod*(256-speed.lenmod_factor) + 256*speed.lenmod_factor)/256;
  913. }
  914. else
  915. if(frames[frameix-1].frflags & FRFLAG_LEN_MOD2) // reduce effect of length mod, used for the start of a vowel
  916. {
  917. length_factor = (length_mod*(256-speed.lenmod2_factor) + 256*speed.lenmod2_factor)/256;
  918. }
  919. frame_length = frames[frameix-1].length;
  920. len = (frame_length * samplerate)/1000;
  921. len = (len * length_factor)/256;
  922. length_sum += len;
  923. frame_lengths[frameix] = len;
  924. }
  925. if((length_sum > 0) && (length_sum < length_min))
  926. {
  927. // lengthen, so that the sequence is greater than one cycle at low pitch
  928. for(frameix=1; frameix < n_frames; frameix++)
  929. {
  930. frame_lengths[frameix] = (frame_lengths[frameix] * length_min) / length_sum;
  931. }
  932. }
  933. for(frameix=1; frameix<n_frames; frameix++)
  934. {
  935. frame2 = frames[frameix].frame;
  936. if((fmt_params->wav_addr != 0) && ((frame1->frflags & FRFLAG_DEFER_WAV)==0))
  937. {
  938. // there is a wave file to play along with this synthesis
  939. seq_len_adjust = 0;
  940. DoSample2(fmt_params->wav_addr, which+0x100, 0, fmt_params->fmt_control, 0, wavefile_amp);
  941. wave_flag = 1;
  942. wavefile_ix = 0;
  943. fmt_params->wav_addr = 0;
  944. }
  945. if(modulation >= 0)
  946. {
  947. if(frame1->frflags & FRFLAG_MODULATE)
  948. {
  949. modulation = 6;
  950. }
  951. if((frameix == n_frames-1) && (modn_flags & 0xf00))
  952. modulation |= modn_flags; // before or after a glottal stop
  953. }
  954. len = frame_lengths[frameix];
  955. pitch_length += len;
  956. amp_length += len;
  957. if(len == 0)
  958. {
  959. last_frame = NULL;
  960. frame1 = frame2;
  961. }
  962. else
  963. {
  964. last_wcmdq = wcmdq_tail;
  965. if(modulation >= 0)
  966. {
  967. q = wcmdq[wcmdq_tail];
  968. q[0] = wcmd_spect;
  969. q[1] = len + (modulation << 16);
  970. q[2] = (long64)frame1;
  971. q[3] = (long64)frame2;
  972. WcmdqInc();
  973. }
  974. last_frame = frame1 = frame2;
  975. total_len += len;
  976. }
  977. }
  978. if((which != 1) && (fmt_amplitude != 0))
  979. {
  980. q = wcmdq[wcmdq_tail];
  981. q[0] = WCMD_FMT_AMPLITUDE;
  982. q[1] = fmt_amplitude = 0;
  983. WcmdqInc();
  984. }
  985. return(total_len);
  986. } // end of DoSpect
  987. void DoMarker(int type, int char_posn, int length, int value)
  988. {//==========================================================
  989. // This could be used to return an index to the word currently being spoken
  990. // Type 1=word, 2=sentence, 3=named marker, 4=play audio, 5=end
  991. if(WcmdqFree() > 5)
  992. {
  993. wcmdq[wcmdq_tail][0] = WCMD_MARKER + (type << 8);
  994. wcmdq[wcmdq_tail][1] = (char_posn & 0xffffff) | (length << 24);
  995. wcmdq[wcmdq_tail][2] = value;
  996. WcmdqInc();
  997. }
  998. } // end of DoMarker
  999. void DoPhonemeMarker(int type, int char_posn, int length, char *name)
  1000. {//==================================================================
  1001. // This could be used to return an index to the word currently being spoken
  1002. // Type 7=phoneme
  1003. int *p;
  1004. if(WcmdqFree() > 5)
  1005. {
  1006. wcmdq[wcmdq_tail][0] = WCMD_MARKER + (type << 8);
  1007. wcmdq[wcmdq_tail][1] = (char_posn & 0xffffff) | (length << 24);
  1008. p = (int *)name;
  1009. wcmdq[wcmdq_tail][2] = p[0]; // up to 8 bytes of UTF8 characters
  1010. wcmdq[wcmdq_tail][3] = p[1];
  1011. WcmdqInc();
  1012. }
  1013. } // end of DoMarker
  1014. #ifdef INCLUDE_SONIC
  1015. void DoSonicSpeed(int value)
  1016. {//=========================
  1017. // value, multiplier * 1024
  1018. wcmdq[wcmdq_tail][0] = WCMD_SONIC_SPEED;
  1019. wcmdq[wcmdq_tail][1] = value;
  1020. WcmdqInc();
  1021. } // end of DoSonicSpeed
  1022. #endif
  1023. void DoVoiceChange(voice_t *v)
  1024. {//===========================
  1025. // allocate memory for a copy of the voice data, and free it in wavegenfill()
  1026. voice_t *v2;
  1027. v2 = (voice_t *)malloc(sizeof(voice_t));
  1028. memcpy(v2,v,sizeof(voice_t));
  1029. wcmdq[wcmdq_tail][0] = WCMD_VOICE;
  1030. wcmdq[wcmdq_tail][2] = (long64)v2;
  1031. WcmdqInc();
  1032. }
  1033. void DoEmbedded(int *embix, int sourceix)
  1034. {//======================================
  1035. // There were embedded commands in the text at this point
  1036. unsigned int word; // bit 7=last command for this word, bits 5,6 sign, bits 0-4 command
  1037. unsigned int value;
  1038. int command;
  1039. do {
  1040. word = embedded_list[*embix];
  1041. value = word >> 8;
  1042. command = word & 0x7f;
  1043. if(command == 0)
  1044. return; // error
  1045. (*embix)++;
  1046. switch(command & 0x1f)
  1047. {
  1048. case EMBED_S: // speed
  1049. SetEmbedded((command & 0x60) + EMBED_S2,value); // adjusts embedded_value[EMBED_S2]
  1050. SetSpeed(2);
  1051. break;
  1052. case EMBED_I: // play dynamically loaded wav data (sound icon)
  1053. if((int)value < n_soundicon_tab)
  1054. {
  1055. if(soundicon_tab[value].length != 0)
  1056. {
  1057. DoPause(10,0); // ensure a break in the speech
  1058. wcmdq[wcmdq_tail][0] = WCMD_WAVE;
  1059. wcmdq[wcmdq_tail][1] = soundicon_tab[value].length;
  1060. wcmdq[wcmdq_tail][2] = (long64)soundicon_tab[value].data + 44; // skip WAV header
  1061. wcmdq[wcmdq_tail][3] = 0x1500; // 16 bit data, amp=21
  1062. WcmdqInc();
  1063. }
  1064. }
  1065. break;
  1066. case EMBED_M: // named marker
  1067. DoMarker(espeakEVENT_MARK, (sourceix & 0x7ff) + clause_start_char, 0, value);
  1068. break;
  1069. case EMBED_U: // play sound
  1070. DoMarker(espeakEVENT_PLAY, count_characters+1, 0, value); // always occurs at end of clause
  1071. break;
  1072. default:
  1073. DoPause(10,0); // ensure a break in the speech
  1074. wcmdq[wcmdq_tail][0] = WCMD_EMBEDDED;
  1075. wcmdq[wcmdq_tail][1] = command;
  1076. wcmdq[wcmdq_tail][2] = value;
  1077. WcmdqInc();
  1078. break;
  1079. }
  1080. } while ((word & 0x80) == 0);
  1081. }
  1082. int Generate(PHONEME_LIST *phoneme_list, int *n_ph, int resume)
  1083. {//============================================================
  1084. static int ix;
  1085. static int embedded_ix;
  1086. static int word_count;
  1087. PHONEME_LIST *prev;
  1088. PHONEME_LIST *next;
  1089. PHONEME_LIST *next2;
  1090. PHONEME_LIST *p;
  1091. int released;
  1092. int stress;
  1093. int modulation;
  1094. int pre_voiced;
  1095. int free_min;
  1096. unsigned char *pitch_env=NULL;
  1097. unsigned char *amp_env;
  1098. PHONEME_TAB *ph;
  1099. int use_ipa=0;
  1100. int done_phoneme_marker;
  1101. char phoneme_name[16];
  1102. static int sourceix=0;
  1103. PHONEME_DATA phdata;
  1104. PHONEME_DATA phdata_prev;
  1105. PHONEME_DATA phdata_next;
  1106. PHONEME_DATA phdata_tone;
  1107. FMT_PARAMS fmtp;
  1108. static WORD_PH_DATA worddata;
  1109. if(option_quiet)
  1110. return(0);
  1111. if(option_phoneme_events & espeakINITIALIZE_PHONEME_IPA)
  1112. use_ipa = 1;
  1113. if(mbrola_name[0] != 0)
  1114. return(MbrolaGenerate(phoneme_list,n_ph,resume));
  1115. if(resume == 0)
  1116. {
  1117. ix = 1;
  1118. embedded_ix=0;
  1119. word_count = 0;
  1120. pitch_length = 0;
  1121. amp_length = 0;
  1122. last_frame = NULL;
  1123. last_wcmdq = -1;
  1124. syllable_start = wcmdq_tail;
  1125. syllable_end = wcmdq_tail;
  1126. syllable_centre = -1;
  1127. last_pitch_cmd = -1;
  1128. memset(vowel_transition,0,sizeof(vowel_transition));
  1129. memset(&worddata, 0, sizeof(worddata));
  1130. DoPause(0,0); // isolate from the previous clause
  1131. }
  1132. while((ix < (*n_ph)) && (ix < N_PHONEME_LIST-2))
  1133. {
  1134. p = &phoneme_list[ix];
  1135. if(p->type == phPAUSE)
  1136. free_min = 10;
  1137. else
  1138. if(p->type != phVOWEL)
  1139. free_min = 15; // 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
  1140. else
  1141. free_min = MIN_WCMDQ; // 25
  1142. if(WcmdqFree() <= free_min)
  1143. return(1); // wait
  1144. prev = &phoneme_list[ix-1];
  1145. next = &phoneme_list[ix+1];
  1146. next2 = &phoneme_list[ix+2];
  1147. if(p->synthflags & SFLAG_EMBEDDED)
  1148. {
  1149. DoEmbedded(&embedded_ix, p->sourceix);
  1150. }
  1151. if(p->newword)
  1152. {
  1153. if(((p->type == phVOWEL) && (translator->langopts.param[LOPT_WORD_MERGE] & 1)) ||
  1154. (p->ph->phflags & phNOPAUSE))
  1155. {
  1156. }
  1157. else
  1158. {
  1159. last_frame = NULL;
  1160. }
  1161. sourceix = (p->sourceix & 0x7ff) + clause_start_char;
  1162. if(p->newword & 4)
  1163. DoMarker(espeakEVENT_SENTENCE, sourceix, 0, count_sentences); // start of sentence
  1164. // if(p->newword & 2)
  1165. // DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  1166. if(p->newword & 1)
  1167. DoMarker(espeakEVENT_WORD, sourceix, p->sourceix >> 11, clause_start_word + word_count++); // NOTE, this count doesn't include multiple-word pronunciations in *_list. eg (of a)
  1168. }
  1169. EndAmplitude();
  1170. if(p->prepause > 0)
  1171. DoPause(p->prepause,1);
  1172. done_phoneme_marker = 0;
  1173. if(option_phoneme_events && (p->ph->code != phonEND_WORD))
  1174. {
  1175. if((p->type == phVOWEL) && (prev->type==phLIQUID || prev->type==phNASAL))
  1176. {
  1177. // For vowels following a liquid or nasal, do the phoneme event after the vowel-start
  1178. }
  1179. else
  1180. {
  1181. WritePhMnemonic(phoneme_name, p->ph, p, use_ipa, NULL);
  1182. DoPhonemeMarker(espeakEVENT_PHONEME, sourceix, 0, phoneme_name);
  1183. done_phoneme_marker = 1;
  1184. }
  1185. }
  1186. switch(p->type)
  1187. {
  1188. case phPAUSE:
  1189. DoPause(p->length,0);
  1190. #ifdef _ESPEAKEDIT
  1191. p->std_length = p->ph->std_length;
  1192. #endif
  1193. break;
  1194. case phSTOP:
  1195. released = 0;
  1196. if(next->type==phVOWEL)
  1197. {
  1198. released = 1;
  1199. }
  1200. else
  1201. if(!next->newword)
  1202. {
  1203. if(next->type==phLIQUID) released = 1;
  1204. // if(((p->ph->phflags & phPLACE) == phPLACE_blb) && (next->ph->phflags & phSIBILANT)) released = 1;
  1205. }
  1206. if(released == 0)
  1207. p->synthflags |= SFLAG_NEXT_PAUSE;
  1208. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1209. phdata.pd_control |= pd_DONTLENGTHEN;
  1210. DoSample3(&phdata, 0, 0);
  1211. break;
  1212. case phFRICATIVE:
  1213. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1214. if(p->synthflags & SFLAG_LENGTHEN)
  1215. {
  1216. DoSample3(&phdata, p->length, 0); // play it twice for [s:] etc.
  1217. }
  1218. DoSample3(&phdata, p->length, 0);
  1219. break;
  1220. case phVSTOP:
  1221. ph = p->ph;
  1222. memset(&fmtp, 0, sizeof(fmtp));
  1223. fmtp.fmt_control = pd_DONTLENGTHEN;
  1224. pre_voiced = 0;
  1225. if(next->type==phVOWEL)
  1226. {
  1227. DoAmplitude(p->amp,NULL);
  1228. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1229. pre_voiced = 1;
  1230. }
  1231. else
  1232. if((next->type==phLIQUID) && !next->newword)
  1233. {
  1234. DoAmplitude(next->amp,NULL);
  1235. DoPitch(envelope_data[next->env],next->pitch1,next->pitch2);
  1236. pre_voiced = 1;
  1237. }
  1238. else
  1239. {
  1240. if(last_pitch_cmd < 0)
  1241. {
  1242. DoAmplitude(next->amp,NULL);
  1243. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1244. }
  1245. }
  1246. if((prev->type==phVOWEL) || (prev->ph->phflags & phVOWEL2) || (ph->phflags & phPREVOICE))
  1247. {
  1248. // a period of voicing before the release
  1249. InterpretPhoneme(NULL, 0x01, p, &phdata, &worddata);
  1250. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1251. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1252. DoSpect2(ph, 0, &fmtp, p, 0);
  1253. if(p->synthflags & SFLAG_LENGTHEN)
  1254. {
  1255. DoPause(25,1);
  1256. DoSpect2(ph, 0, &fmtp, p, 0);
  1257. }
  1258. }
  1259. else
  1260. {
  1261. if(p->synthflags & SFLAG_LENGTHEN)
  1262. {
  1263. DoPause(50,0);
  1264. }
  1265. }
  1266. if(pre_voiced)
  1267. {
  1268. // followed by a vowel, or liquid + vowel
  1269. StartSyllable();
  1270. }
  1271. else
  1272. {
  1273. p->synthflags |= SFLAG_NEXT_PAUSE;
  1274. }
  1275. InterpretPhoneme(NULL,0, p, &phdata, &worddata);
  1276. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1277. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1278. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1279. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1280. DoSpect2(ph, 0, &fmtp, p, 0);
  1281. if((p->newword == 0) && (next2->newword == 0))
  1282. {
  1283. if(next->type == phVFRICATIVE)
  1284. DoPause(20,0);
  1285. if(next->type == phFRICATIVE)
  1286. DoPause(12,0);
  1287. }
  1288. break;
  1289. case phVFRICATIVE:
  1290. if(next->type==phVOWEL)
  1291. {
  1292. DoAmplitude(p->amp,NULL);
  1293. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1294. }
  1295. else
  1296. if(next->type==phLIQUID)
  1297. {
  1298. DoAmplitude(next->amp,NULL);
  1299. DoPitch(envelope_data[next->env],next->pitch1,next->pitch2);
  1300. }
  1301. else
  1302. {
  1303. if(last_pitch_cmd < 0)
  1304. {
  1305. DoAmplitude(p->amp,NULL);
  1306. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1307. }
  1308. }
  1309. if((next->type==phVOWEL) || ((next->type==phLIQUID) && (next->newword==0))) // ?? test 14.Aug.2007
  1310. {
  1311. StartSyllable();
  1312. }
  1313. else
  1314. {
  1315. p->synthflags |= SFLAG_NEXT_PAUSE;
  1316. }
  1317. InterpretPhoneme(NULL,0, p, &phdata, &worddata);
  1318. memset(&fmtp, 0, sizeof(fmtp));
  1319. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1320. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1321. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1322. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1323. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1324. if(p->synthflags & SFLAG_LENGTHEN)
  1325. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1326. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1327. break;
  1328. case phNASAL:
  1329. memset(&fmtp, 0, sizeof(fmtp));
  1330. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1331. {
  1332. DoAmplitude(p->amp,NULL);
  1333. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1334. }
  1335. if(prev->type==phNASAL)
  1336. {
  1337. last_frame = NULL;
  1338. }
  1339. InterpretPhoneme(NULL,0, p, &phdata, &worddata);
  1340. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1341. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1342. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1343. if(next->type==phVOWEL)
  1344. {
  1345. StartSyllable();
  1346. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1347. }
  1348. else
  1349. if(prev->type==phVOWEL && (p->synthflags & SFLAG_SEQCONTINUE))
  1350. {
  1351. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1352. }
  1353. else
  1354. {
  1355. last_frame = NULL; // only for nasal ?
  1356. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1357. last_frame = NULL;
  1358. }
  1359. break;
  1360. case phLIQUID:
  1361. memset(&fmtp, 0, sizeof(fmtp));
  1362. modulation = 0;
  1363. if(p->ph->phflags & phTRILL)
  1364. modulation = 5;
  1365. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1366. {
  1367. DoAmplitude(p->amp,NULL);
  1368. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1369. }
  1370. if(prev->type==phNASAL)
  1371. {
  1372. last_frame = NULL;
  1373. }
  1374. if(next->type==phVOWEL)
  1375. {
  1376. StartSyllable();
  1377. }
  1378. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1379. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1380. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1381. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1382. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1383. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1384. DoSpect2(p->ph, 0, &fmtp, p, modulation);
  1385. break;
  1386. case phVOWEL:
  1387. ph = p->ph;
  1388. stress = p->stresslevel & 0xf;
  1389. memset(&fmtp, 0, sizeof(fmtp));
  1390. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1391. fmtp.std_length = phdata.pd_param[i_SET_LENGTH] * 2;
  1392. if(((fmtp.fmt_addr = phdata.sound_addr[pd_VWLSTART]) != 0) && ((phdata.pd_control & pd_FORNEXTPH) == 0))
  1393. {
  1394. // a vowel start has been specified by the Vowel program
  1395. fmtp.fmt_length = phdata.sound_param[pd_VWLSTART];
  1396. }
  1397. else
  1398. if(prev->type != phPAUSE)
  1399. {
  1400. // check the previous phoneme
  1401. InterpretPhoneme(NULL, 0, prev, &phdata_prev, NULL);
  1402. if((fmtp.fmt_addr = phdata_prev.sound_addr[pd_VWLSTART]) != 0)
  1403. {
  1404. // a vowel start has been specified by the Vowel program
  1405. fmtp.fmt2_lenadj = phdata_prev.sound_param[pd_VWLSTART];
  1406. }
  1407. fmtp.transition0 = phdata_prev.vowel_transition[0];
  1408. fmtp.transition1 = phdata_prev.vowel_transition[1];
  1409. }
  1410. if(fmtp.fmt_addr == 0)
  1411. {
  1412. // use the default start for this vowel
  1413. fmtp.use_vowelin = 1;
  1414. fmtp.fmt_control = 1;
  1415. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1416. }
  1417. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1418. pitch_env = envelope_data[p->env];
  1419. amp_env = NULL;
  1420. if(p->tone_ph != 0)
  1421. {
  1422. InterpretPhoneme2(p->tone_ph, &phdata_tone);
  1423. pitch_env = GetEnvelope(phdata_tone.pitch_env);
  1424. if(phdata_tone.amp_env > 0)
  1425. amp_env = GetEnvelope(phdata_tone.amp_env);
  1426. }
  1427. StartSyllable();
  1428. modulation = 2;
  1429. if(stress <= 1)
  1430. modulation = 1; // 16ths
  1431. else
  1432. if(stress >= 7)
  1433. modulation = 3;
  1434. if(prev->type == phVSTOP || prev->type == phVFRICATIVE)
  1435. {
  1436. DoAmplitude(p->amp,amp_env);
  1437. DoPitch(pitch_env,p->pitch1,p->pitch2); // don't use prevocalic rising tone
  1438. DoSpect2(ph, 1, &fmtp, p, modulation);
  1439. }
  1440. else
  1441. if(prev->type==phLIQUID || prev->type==phNASAL)
  1442. {
  1443. DoAmplitude(p->amp,amp_env);
  1444. DoSpect2(ph, 1, &fmtp, p, modulation); // continue with pre-vocalic rising tone
  1445. DoPitch(pitch_env,p->pitch1,p->pitch2);
  1446. }
  1447. else
  1448. {
  1449. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1450. {
  1451. DoAmplitude(p->amp,amp_env);
  1452. DoPitch(pitch_env,p->pitch1,p->pitch2);
  1453. }
  1454. DoSpect2(ph, 1, &fmtp, p, modulation);
  1455. }
  1456. if((option_phoneme_events) && (done_phoneme_marker == 0))
  1457. {
  1458. WritePhMnemonic(phoneme_name, p->ph, p, use_ipa, NULL);
  1459. DoPhonemeMarker(espeakEVENT_PHONEME, sourceix, 0, phoneme_name);
  1460. }
  1461. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1462. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1463. fmtp.transition0 = 0;
  1464. fmtp.transition1 = 0;
  1465. if((fmtp.fmt2_addr = phdata.sound_addr[pd_VWLEND]) != 0)
  1466. {
  1467. fmtp.fmt2_lenadj = phdata.sound_param[pd_VWLEND];
  1468. }
  1469. else
  1470. if(next->type != phPAUSE)
  1471. {
  1472. fmtp.fmt2_lenadj = 0;
  1473. InterpretPhoneme(NULL, 0, next, &phdata_next, NULL);
  1474. fmtp.use_vowelin = 1;
  1475. fmtp.transition0 = phdata_next.vowel_transition[2]; // always do vowel_transition, even if ph_VWLEND ?? consider [N]
  1476. fmtp.transition1 = phdata_next.vowel_transition[3];
  1477. if((fmtp.fmt2_addr = phdata_next.sound_addr[pd_VWLEND]) != 0)
  1478. {
  1479. fmtp.fmt2_lenadj = phdata_next.sound_param[pd_VWLEND];
  1480. }
  1481. }
  1482. DoSpect2(ph, 2, &fmtp, p, modulation);
  1483. break;
  1484. }
  1485. ix++;
  1486. }
  1487. EndPitch(1);
  1488. if(*n_ph > 0)
  1489. {
  1490. DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  1491. *n_ph = 0;
  1492. }
  1493. return(0); // finished the phoneme list
  1494. } // end of Generate
  1495. static int timer_on = 0;
  1496. static int paused = 0;
  1497. int SynthOnTimer()
  1498. {//===============
  1499. if(!timer_on)
  1500. {
  1501. return(WavegenCloseSound());
  1502. }
  1503. do {
  1504. if(WcmdqUsed() > 0)
  1505. WavegenOpenSound();
  1506. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  1507. {
  1508. SpeakNextClause(NULL,NULL,1);
  1509. }
  1510. } while(skipping_text);
  1511. return(0);
  1512. }
  1513. int SynthStatus()
  1514. {//==============
  1515. return(timer_on | paused);
  1516. }
  1517. int SpeakNextClause(FILE *f_in, const void *text_in, int control)
  1518. {//==============================================================
  1519. // Speak text from file (f_in) or memory (text_in)
  1520. // control 0: start
  1521. // either f_in or text_in is set, the other must be NULL
  1522. // The other calls have f_in and text_in = NULL
  1523. // control 1: speak next text
  1524. // 2: stop
  1525. // 3: pause (toggle)
  1526. // 4: is file being read (0=no, 1=yes)
  1527. // 5: interrupt and flush current text.
  1528. int clause_tone;
  1529. char *voice_change;
  1530. static FILE *f_text=NULL;
  1531. static const void *p_text=NULL;
  1532. if(control == 4)
  1533. {
  1534. if((f_text == NULL) && (p_text == NULL))
  1535. return(0);
  1536. else
  1537. return(1);
  1538. }
  1539. if(control == 2)
  1540. {
  1541. // stop speaking
  1542. timer_on = 0;
  1543. p_text = NULL;
  1544. if(f_text != NULL)
  1545. {
  1546. fclose(f_text);
  1547. f_text=NULL;
  1548. }
  1549. n_phoneme_list = 0;
  1550. WcmdqStop();
  1551. return(0);
  1552. }
  1553. if(control == 3)
  1554. {
  1555. // toggle pause
  1556. if(paused == 0)
  1557. {
  1558. timer_on = 0;
  1559. paused = 2;
  1560. }
  1561. else
  1562. {
  1563. WavegenOpenSound();
  1564. timer_on = 1;
  1565. paused = 0;
  1566. Generate(phoneme_list,&n_phoneme_list,0); // re-start from beginning of clause
  1567. }
  1568. return(0);
  1569. }
  1570. if(control == 5)
  1571. {
  1572. // stop speaking, but continue looking for text
  1573. n_phoneme_list = 0;
  1574. WcmdqStop();
  1575. return(0);
  1576. }
  1577. if((f_in != NULL) || (text_in != NULL))
  1578. {
  1579. f_text = f_in;
  1580. p_text = text_in;
  1581. timer_on = 1;
  1582. paused = 0;
  1583. }
  1584. if((f_text==NULL) && (p_text==NULL))
  1585. {
  1586. skipping_text = 0;
  1587. timer_on = 0;
  1588. return(0);
  1589. }
  1590. if((f_text != NULL) && feof(f_text))
  1591. {
  1592. timer_on = 0;
  1593. fclose(f_text);
  1594. f_text=NULL;
  1595. return(0);
  1596. }
  1597. if(current_phoneme_table != voice->phoneme_tab_ix)
  1598. {
  1599. SelectPhonemeTable(voice->phoneme_tab_ix);
  1600. }
  1601. // read the next clause from the input text file, translate it, and generate
  1602. // entries in the wavegen command queue
  1603. p_text = TranslateClause(translator, f_text, p_text, &clause_tone, &voice_change);
  1604. CalcPitches(translator, clause_tone);
  1605. CalcLengths(translator);
  1606. if((option_phonemes > 0) || (phoneme_callback != NULL))
  1607. {
  1608. int use_ipa = 0;
  1609. if(option_phonemes >= 3)
  1610. use_ipa = option_phonemes-2; // 1=ipa, 2=ipa with tie, 3=ipa with ZWJ
  1611. GetTranslatedPhonemeString(translator->phon_out, sizeof(translator->phon_out), use_ipa);
  1612. if(option_phonemes > 0)
  1613. {
  1614. fprintf(f_trans,"%s\n",translator->phon_out);
  1615. if(!iswalpha(0x010d))
  1616. {
  1617. // check that c-caron is recognized as an alphabetic character
  1618. fprintf(stderr,"Warning: Accented letters are not recognized, eg: U+010D\nSet LC_CTYPE to a UTF-8 locale\n");
  1619. }
  1620. }
  1621. if(phoneme_callback != NULL)
  1622. {
  1623. phoneme_callback(translator->phon_out);
  1624. }
  1625. }
  1626. if(skipping_text)
  1627. {
  1628. n_phoneme_list = 0;
  1629. return(1);
  1630. }
  1631. Generate(phoneme_list,&n_phoneme_list,0);
  1632. WavegenOpenSound();
  1633. if(voice_change != NULL)
  1634. {
  1635. // voice change at the end of the clause (i.e. clause was terminated by a voice change)
  1636. new_voice = LoadVoiceVariant(voice_change,0); // add a Voice instruction to wavegen at the end of the clause
  1637. }
  1638. if(new_voice)
  1639. {
  1640. // finished the current clause, now change the voice if there was an embedded
  1641. // change voice command at the end of it (i.e. clause was broken at the change voice command)
  1642. DoVoiceChange(voice);
  1643. new_voice = NULL;
  1644. }
  1645. return(1);
  1646. } // end of SpeakNextClause