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

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