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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2010 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include "StdAfx.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <wctype.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <math.h>
  26. #include "speak_lib.h"
  27. #include "speech.h"
  28. #include "phoneme.h"
  29. #include "synthesize.h"
  30. #include "voice.h"
  31. #include "translate.h"
  32. extern FILE *f_log;
  33. static void SmoothSpect(void);
  34. // list of phonemes in a clause
  35. int n_phoneme_list=0;
  36. PHONEME_LIST phoneme_list[N_PHONEME_LIST];
  37. int mbrola_delay;
  38. char mbrola_name[20];
  39. 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<3; ix++)
  65. buf[ix] = word >> (ix*8);
  66. buf[4] = 0;
  67. return(buf);
  68. }
  69. void SynthesizeInit()
  70. {//==================
  71. last_pitch_cmd = 0;
  72. last_amp_cmd = 0;
  73. last_frame = NULL;
  74. syllable_centre = -1;
  75. // initialise next_pause, a dummy phoneme_list entry
  76. // next_pause.ph = phoneme_tab[phonPAUSE]; // this must be done after voice selection
  77. next_pause.type = phPAUSE;
  78. next_pause.newword = 0;
  79. }
  80. static void EndAmplitude(void)
  81. {//===========================
  82. if(amp_length > 0)
  83. {
  84. if(wcmdq[last_amp_cmd][1] == 0)
  85. wcmdq[last_amp_cmd][1] = amp_length;
  86. amp_length = 0;
  87. }
  88. }
  89. static void EndPitch(int voice_break)
  90. {//==================================
  91. // posssible end of pitch envelope, fill in the length
  92. if((pitch_length > 0) && (last_pitch_cmd >= 0))
  93. {
  94. if(wcmdq[last_pitch_cmd][1] == 0)
  95. wcmdq[last_pitch_cmd][1] = pitch_length;
  96. pitch_length = 0;
  97. }
  98. if(voice_break)
  99. {
  100. last_wcmdq = -1;
  101. last_frame = NULL;
  102. syllable_end = wcmdq_tail;
  103. SmoothSpect();
  104. syllable_centre = -1;
  105. memset(vowel_transition,0,sizeof(vowel_transition));
  106. }
  107. } // end of EndPitch
  108. static void DoAmplitude(int amp, unsigned char *amp_env)
  109. {//=====================================================
  110. long *q;
  111. last_amp_cmd = wcmdq_tail;
  112. amp_length = 0; // total length of vowel with this amplitude envelope
  113. q = wcmdq[wcmdq_tail];
  114. q[0] = WCMD_AMPLITUDE;
  115. q[1] = 0; // fill in later from amp_length
  116. q[2] = (long)amp_env;
  117. q[3] = amp;
  118. WcmdqInc();
  119. } // end of DoAmplitude
  120. static void DoPitch(unsigned char *env, int pitch1, int pitch2)
  121. {//============================================================
  122. long *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] = (long)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. long *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] = long(&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] = long(&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] = long(&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] = long(&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. long *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] = (long)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] = (long)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] = (long)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] = (long)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] = (long)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] = (long)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. long *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] = (long)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] = (long)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] = long(frame1);
  971. q[3] = long(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;
  994. wcmdq[wcmdq_tail][1] = type;
  995. wcmdq[wcmdq_tail][2] = (char_posn & 0xffffff) | (length << 24);
  996. wcmdq[wcmdq_tail][3] = value;
  997. WcmdqInc();
  998. }
  999. } // end of DoMarker
  1000. void DoSonicSpeed(int value)
  1001. {//=========================
  1002. // value, multiplier * 1024
  1003. wcmdq[wcmdq_tail][0] = WCMD_SONIC_SPEED;
  1004. wcmdq[wcmdq_tail][1] = value;
  1005. WcmdqInc();
  1006. } // end of DoSonicSpeed
  1007. void DoVoiceChange(voice_t *v)
  1008. {//===========================
  1009. // allocate memory for a copy of the voice data, and free it in wavegenfill()
  1010. voice_t *v2;
  1011. v2 = (voice_t *)malloc(sizeof(voice_t));
  1012. memcpy(v2,v,sizeof(voice_t));
  1013. wcmdq[wcmdq_tail][0] = WCMD_VOICE;
  1014. wcmdq[wcmdq_tail][1] = (long)(v2);
  1015. WcmdqInc();
  1016. }
  1017. void DoEmbedded(int *embix, int sourceix)
  1018. {//======================================
  1019. // There were embedded commands in the text at this point
  1020. unsigned int word; // bit 7=last command for this word, bits 5,6 sign, bits 0-4 command
  1021. unsigned int value;
  1022. int command;
  1023. do {
  1024. word = embedded_list[*embix];
  1025. value = word >> 8;
  1026. command = word & 0x7f;
  1027. if(command == 0)
  1028. return; // error
  1029. (*embix)++;
  1030. switch(command & 0x1f)
  1031. {
  1032. case EMBED_S: // speed
  1033. SetEmbedded((command & 0x60) + EMBED_S2,value); // adjusts embedded_value[EMBED_S2]
  1034. SetSpeed(2);
  1035. break;
  1036. case EMBED_I: // play dynamically loaded wav data (sound icon)
  1037. if((int)value < n_soundicon_tab)
  1038. {
  1039. if(soundicon_tab[value].length != 0)
  1040. {
  1041. DoPause(10,0); // ensure a break in the speech
  1042. wcmdq[wcmdq_tail][0] = WCMD_WAVE;
  1043. wcmdq[wcmdq_tail][1] = soundicon_tab[value].length;
  1044. wcmdq[wcmdq_tail][2] = (long)soundicon_tab[value].data + 44; // skip WAV header
  1045. wcmdq[wcmdq_tail][3] = 0x1500; // 16 bit data, amp=21
  1046. WcmdqInc();
  1047. }
  1048. }
  1049. break;
  1050. case EMBED_M: // named marker
  1051. DoMarker(espeakEVENT_MARK, (sourceix & 0x7ff) + clause_start_char, 0, value);
  1052. break;
  1053. case EMBED_U: // play sound
  1054. DoMarker(espeakEVENT_PLAY, count_characters+1, 0, value); // always occurs at end of clause
  1055. break;
  1056. default:
  1057. DoPause(10,0); // ensure a break in the speech
  1058. wcmdq[wcmdq_tail][0] = WCMD_EMBEDDED;
  1059. wcmdq[wcmdq_tail][1] = command;
  1060. wcmdq[wcmdq_tail][2] = value;
  1061. WcmdqInc();
  1062. break;
  1063. }
  1064. } while ((word & 0x80) == 0);
  1065. }
  1066. int Generate(PHONEME_LIST *phoneme_list, int *n_ph, int resume)
  1067. {//============================================================
  1068. static int ix;
  1069. static int embedded_ix;
  1070. static int word_count;
  1071. PHONEME_LIST *prev;
  1072. PHONEME_LIST *next;
  1073. PHONEME_LIST *next2;
  1074. PHONEME_LIST *p;
  1075. int released;
  1076. int stress;
  1077. int modulation;
  1078. int pre_voiced;
  1079. int free_min;
  1080. unsigned char *pitch_env=NULL;
  1081. unsigned char *amp_env;
  1082. PHONEME_TAB *ph;
  1083. static int sourceix=0;
  1084. PHONEME_DATA phdata;
  1085. PHONEME_DATA phdata_prev;
  1086. PHONEME_DATA phdata_next;
  1087. PHONEME_DATA phdata_tone;
  1088. FMT_PARAMS fmtp;
  1089. if(option_quiet)
  1090. return(0);
  1091. if(mbrola_name[0] != 0)
  1092. return(MbrolaGenerate(phoneme_list,n_ph,resume));
  1093. if(resume == 0)
  1094. {
  1095. ix = 1;
  1096. embedded_ix=0;
  1097. word_count = 0;
  1098. pitch_length = 0;
  1099. amp_length = 0;
  1100. last_frame = NULL;
  1101. last_wcmdq = -1;
  1102. syllable_start = wcmdq_tail;
  1103. syllable_end = wcmdq_tail;
  1104. syllable_centre = -1;
  1105. last_pitch_cmd = -1;
  1106. memset(vowel_transition,0,sizeof(vowel_transition));
  1107. DoPause(0,0); // isolate from the previous clause
  1108. }
  1109. while(ix < (*n_ph))
  1110. {
  1111. p = &phoneme_list[ix];
  1112. if(p->type == phPAUSE)
  1113. free_min = 10;
  1114. else
  1115. if(p->type != phVOWEL)
  1116. 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
  1117. else
  1118. free_min = MIN_WCMDQ; // 25
  1119. if(WcmdqFree() <= free_min)
  1120. return(1); // wait
  1121. prev = &phoneme_list[ix-1];
  1122. next = &phoneme_list[ix+1];
  1123. next2 = &phoneme_list[ix+2];
  1124. if(p->synthflags & SFLAG_EMBEDDED)
  1125. {
  1126. DoEmbedded(&embedded_ix, p->sourceix);
  1127. }
  1128. if(p->newword)
  1129. {
  1130. if(((p->type == phVOWEL) && (translator->langopts.param[LOPT_WORD_MERGE] & 1)) ||
  1131. (p->ph->phflags & phNOPAUSE))
  1132. {
  1133. }
  1134. else
  1135. {
  1136. last_frame = NULL;
  1137. }
  1138. sourceix = (p->sourceix & 0x7ff) + clause_start_char;
  1139. if(p->newword & 4)
  1140. DoMarker(espeakEVENT_SENTENCE, sourceix, 0, count_sentences); // start of sentence
  1141. // if(p->newword & 2)
  1142. // DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  1143. if(p->newword & 1)
  1144. 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)
  1145. }
  1146. EndAmplitude();
  1147. if(p->prepause > 0)
  1148. DoPause(p->prepause,1);
  1149. if(option_phoneme_events && (p->type != phVOWEL) && (p->ph->code != phonEND_WORD))
  1150. {
  1151. // Note, for vowels, do the phoneme event after the vowel-start
  1152. DoMarker(espeakEVENT_PHONEME, sourceix, 0, p->ph->mnemonic);
  1153. }
  1154. switch(p->type)
  1155. {
  1156. case phPAUSE:
  1157. DoPause(p->length,0);
  1158. break;
  1159. case phSTOP:
  1160. released = 0;
  1161. if(next->type==phVOWEL)
  1162. {
  1163. released = 1;
  1164. }
  1165. else
  1166. if(!next->newword)
  1167. {
  1168. if(next->type==phLIQUID) released = 1;
  1169. // if(((p->ph->phflags & phPLACE) == phPLACE_blb) && (next->ph->phflags & phSIBILANT)) released = 1;
  1170. }
  1171. if(released == 0)
  1172. p->synthflags |= SFLAG_NEXT_PAUSE;
  1173. InterpretPhoneme(NULL, 0, p, &phdata);
  1174. phdata.pd_control |= pd_DONTLENGTHEN;
  1175. DoSample3(&phdata, 0, 0);
  1176. break;
  1177. case phFRICATIVE:
  1178. InterpretPhoneme(NULL, 0, p, &phdata);
  1179. if(p->synthflags & SFLAG_LENGTHEN)
  1180. {
  1181. DoSample3(&phdata, p->length, 0); // play it twice for [s:] etc.
  1182. }
  1183. DoSample3(&phdata, p->length, 0);
  1184. break;
  1185. case phVSTOP:
  1186. ph = p->ph;
  1187. memset(&fmtp, 0, sizeof(fmtp));
  1188. fmtp.fmt_control = pd_DONTLENGTHEN;
  1189. pre_voiced = 0;
  1190. if(next->type==phVOWEL)
  1191. {
  1192. DoAmplitude(p->amp,NULL);
  1193. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1194. pre_voiced = 1;
  1195. }
  1196. else
  1197. if((next->type==phLIQUID) && !next->newword)
  1198. {
  1199. DoAmplitude(next->amp,NULL);
  1200. DoPitch(envelope_data[next->env],next->pitch1,next->pitch2);
  1201. pre_voiced = 1;
  1202. }
  1203. else
  1204. {
  1205. if(last_pitch_cmd < 0)
  1206. {
  1207. DoAmplitude(next->amp,NULL);
  1208. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1209. }
  1210. }
  1211. if((prev->type==phVOWEL) || (prev->ph->phflags & phVOWEL2) || (ph->phflags & phPREVOICE))
  1212. {
  1213. // a period of voicing before the release
  1214. InterpretPhoneme(NULL, 0x01, p, &phdata);
  1215. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1216. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1217. DoSpect2(ph, 0, &fmtp, p, 0);
  1218. if(p->synthflags & SFLAG_LENGTHEN)
  1219. {
  1220. DoPause(25,1);
  1221. DoSpect2(ph, 0, &fmtp, p, 0);
  1222. }
  1223. }
  1224. else
  1225. {
  1226. if(p->synthflags & SFLAG_LENGTHEN)
  1227. {
  1228. DoPause(50,0);
  1229. }
  1230. }
  1231. if(pre_voiced)
  1232. {
  1233. // followed by a vowel, or liquid + vowel
  1234. StartSyllable();
  1235. }
  1236. else
  1237. {
  1238. p->synthflags |= SFLAG_NEXT_PAUSE;
  1239. }
  1240. InterpretPhoneme(NULL,0, p, &phdata);
  1241. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1242. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1243. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1244. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1245. DoSpect2(ph, 0, &fmtp, p, 0);
  1246. if((p->newword == 0) && (next2->newword == 0))
  1247. {
  1248. if(next->type == phVFRICATIVE)
  1249. DoPause(20,0);
  1250. if(next->type == phFRICATIVE)
  1251. DoPause(12,0);
  1252. }
  1253. break;
  1254. case phVFRICATIVE:
  1255. if(next->type==phVOWEL)
  1256. {
  1257. DoAmplitude(p->amp,NULL);
  1258. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1259. }
  1260. else
  1261. if(next->type==phLIQUID)
  1262. {
  1263. DoAmplitude(next->amp,NULL);
  1264. DoPitch(envelope_data[next->env],next->pitch1,next->pitch2);
  1265. }
  1266. else
  1267. {
  1268. if(last_pitch_cmd < 0)
  1269. {
  1270. DoAmplitude(p->amp,NULL);
  1271. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1272. }
  1273. }
  1274. if((next->type==phVOWEL) || ((next->type==phLIQUID) && (next->newword==0))) // ?? test 14.Aug.2007
  1275. {
  1276. StartSyllable();
  1277. }
  1278. else
  1279. {
  1280. p->synthflags |= SFLAG_NEXT_PAUSE;
  1281. }
  1282. InterpretPhoneme(NULL,0, p, &phdata);
  1283. memset(&fmtp, 0, sizeof(fmtp));
  1284. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1285. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1286. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1287. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1288. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1289. if(p->synthflags & SFLAG_LENGTHEN)
  1290. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1291. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1292. break;
  1293. case phNASAL:
  1294. memset(&fmtp, 0, sizeof(fmtp));
  1295. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1296. {
  1297. DoAmplitude(p->amp,NULL);
  1298. DoPitch(envelope_data[p->env],p->pitch1,p->pitch2);
  1299. }
  1300. if(prev->type==phNASAL)
  1301. {
  1302. last_frame = NULL;
  1303. }
  1304. InterpretPhoneme(NULL,0, p, &phdata);
  1305. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1306. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1307. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1308. if(next->type==phVOWEL)
  1309. {
  1310. StartSyllable();
  1311. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1312. }
  1313. else
  1314. if(prev->type==phVOWEL && (p->synthflags & SFLAG_SEQCONTINUE))
  1315. {
  1316. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1317. }
  1318. else
  1319. {
  1320. last_frame = NULL; // only for nasal ?
  1321. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1322. last_frame = NULL;
  1323. }
  1324. break;
  1325. case phLIQUID:
  1326. memset(&fmtp, 0, sizeof(fmtp));
  1327. modulation = 0;
  1328. if(p->ph->phflags & phTRILL)
  1329. modulation = 5;
  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. if(next->type==phVOWEL)
  1340. {
  1341. StartSyllable();
  1342. }
  1343. InterpretPhoneme(NULL, 0, p, &phdata);
  1344. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1345. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1346. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1347. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1348. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1349. DoSpect2(p->ph, 0, &fmtp, p, modulation);
  1350. break;
  1351. case phVOWEL:
  1352. ph = p->ph;
  1353. stress = p->stresslevel & 0xf;
  1354. memset(&fmtp, 0, sizeof(fmtp));
  1355. InterpretPhoneme(NULL, 0, p, &phdata);
  1356. fmtp.std_length = phdata.pd_param[i_SET_LENGTH] * 2;
  1357. if(((fmtp.fmt_addr = phdata.sound_addr[pd_VWLSTART]) != 0) && ((phdata.pd_control & pd_FORNEXTPH) == 0))
  1358. {
  1359. // a vowel start has been specified by the Vowel program
  1360. fmtp.fmt_length = phdata.sound_param[pd_VWLSTART];
  1361. }
  1362. else
  1363. if(prev->type != phPAUSE)
  1364. {
  1365. // check the previous phoneme
  1366. InterpretPhoneme(NULL, 0, prev, &phdata_prev);
  1367. if((fmtp.fmt_addr = phdata_prev.sound_addr[pd_VWLSTART]) != 0)
  1368. {
  1369. // a vowel start has been specified by the Vowel program
  1370. fmtp.fmt2_lenadj = phdata_prev.sound_param[pd_VWLSTART];
  1371. }
  1372. fmtp.transition0 = phdata_prev.vowel_transition[0];
  1373. fmtp.transition1 = phdata_prev.vowel_transition[1];
  1374. }
  1375. if(fmtp.fmt_addr == 0)
  1376. {
  1377. // use the default start for this vowel
  1378. fmtp.use_vowelin = 1;
  1379. fmtp.fmt_control = 1;
  1380. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1381. }
  1382. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1383. pitch_env = envelope_data[p->env];
  1384. amp_env = NULL;
  1385. if(p->tone_ph != 0)
  1386. {
  1387. InterpretPhoneme2(p->tone_ph, &phdata_tone);
  1388. pitch_env = GetEnvelope(phdata_tone.pitch_env);
  1389. if(phdata_tone.amp_env > 0)
  1390. amp_env = GetEnvelope(phdata_tone.amp_env);
  1391. }
  1392. StartSyllable();
  1393. modulation = 2;
  1394. if(stress <= 1)
  1395. modulation = 1; // 16ths
  1396. else
  1397. if(stress >= 7)
  1398. modulation = 3;
  1399. if(prev->type == phVSTOP || prev->type == phVFRICATIVE)
  1400. {
  1401. DoAmplitude(p->amp,amp_env);
  1402. DoPitch(pitch_env,p->pitch1,p->pitch2); // don't use prevocalic rising tone
  1403. DoSpect2(ph, 1, &fmtp, p, modulation);
  1404. }
  1405. else
  1406. if(prev->type==phLIQUID || prev->type==phNASAL)
  1407. {
  1408. DoAmplitude(p->amp,amp_env);
  1409. DoSpect2(ph, 1, &fmtp, p, modulation); // continue with pre-vocalic rising tone
  1410. DoPitch(pitch_env,p->pitch1,p->pitch2);
  1411. }
  1412. else
  1413. {
  1414. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  1415. {
  1416. DoAmplitude(p->amp,amp_env);
  1417. DoPitch(pitch_env,p->pitch1,p->pitch2);
  1418. }
  1419. DoSpect2(ph, 1, &fmtp, p, modulation);
  1420. }
  1421. if(option_phoneme_events)
  1422. {
  1423. DoMarker(espeakEVENT_PHONEME, sourceix, 0, p->ph->mnemonic);
  1424. }
  1425. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1426. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1427. fmtp.transition0 = 0;
  1428. fmtp.transition1 = 0;
  1429. if((fmtp.fmt2_addr = phdata.sound_addr[pd_VWLEND]) != 0)
  1430. {
  1431. fmtp.fmt2_lenadj = phdata.sound_param[pd_VWLEND];
  1432. }
  1433. else
  1434. if(next->type != phPAUSE)
  1435. {
  1436. fmtp.fmt2_lenadj = 0;
  1437. InterpretPhoneme(NULL, 0, next, &phdata_next);
  1438. fmtp.use_vowelin = 1;
  1439. fmtp.transition0 = phdata_next.vowel_transition[2]; // always do vowel_transition, even if ph_VWLEND ?? consider [N]
  1440. fmtp.transition1 = phdata_next.vowel_transition[3];
  1441. if((fmtp.fmt2_addr = phdata_next.sound_addr[pd_VWLEND]) != 0)
  1442. {
  1443. fmtp.fmt2_lenadj = phdata_next.sound_param[pd_VWLEND];
  1444. }
  1445. }
  1446. DoSpect2(ph, 2, &fmtp, p, modulation);
  1447. break;
  1448. }
  1449. ix++;
  1450. }
  1451. EndPitch(1);
  1452. if(*n_ph > 0)
  1453. {
  1454. DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  1455. *n_ph = 0;
  1456. }
  1457. return(0); // finished the phoneme list
  1458. } // end of Generate
  1459. static int timer_on = 0;
  1460. static int paused = 0;
  1461. int SynthOnTimer()
  1462. {//===============
  1463. if(!timer_on)
  1464. {
  1465. return(WavegenCloseSound());
  1466. }
  1467. do {
  1468. if(WcmdqUsed() > 0)
  1469. WavegenOpenSound();
  1470. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  1471. {
  1472. SpeakNextClause(NULL,NULL,1);
  1473. }
  1474. } while(skipping_text);
  1475. return(0);
  1476. }
  1477. int SynthStatus()
  1478. {//==============
  1479. return(timer_on | paused);
  1480. }
  1481. int SpeakNextClause(FILE *f_in, const void *text_in, int control)
  1482. {//==============================================================
  1483. // Speak text from file (f_in) or memory (text_in)
  1484. // control 0: start
  1485. // either f_in or text_in is set, the other must be NULL
  1486. // The other calls have f_in and text_in = NULL
  1487. // control 1: speak next text
  1488. // 2: stop
  1489. // 3: pause (toggle)
  1490. // 4: is file being read (0=no, 1=yes)
  1491. // 5: interrupt and flush current text.
  1492. int clause_tone;
  1493. char *voice_change;
  1494. static FILE *f_text=NULL;
  1495. static const void *p_text=NULL;
  1496. if(control == 4)
  1497. {
  1498. if((f_text == NULL) && (p_text == NULL))
  1499. return(0);
  1500. else
  1501. return(1);
  1502. }
  1503. if(control == 2)
  1504. {
  1505. // stop speaking
  1506. timer_on = 0;
  1507. p_text = NULL;
  1508. if(f_text != NULL)
  1509. {
  1510. fclose(f_text);
  1511. f_text=NULL;
  1512. }
  1513. n_phoneme_list = 0;
  1514. WcmdqStop();
  1515. embedded_value[EMBED_T] = 0;
  1516. return(0);
  1517. }
  1518. if(control == 3)
  1519. {
  1520. // toggle pause
  1521. if(paused == 0)
  1522. {
  1523. timer_on = 0;
  1524. paused = 2;
  1525. }
  1526. else
  1527. {
  1528. WavegenOpenSound();
  1529. timer_on = 1;
  1530. paused = 0;
  1531. Generate(phoneme_list,&n_phoneme_list,0); // re-start from beginning of clause
  1532. }
  1533. return(0);
  1534. }
  1535. if(control == 5)
  1536. {
  1537. // stop speaking, but continue looking for text
  1538. n_phoneme_list = 0;
  1539. WcmdqStop();
  1540. return(0);
  1541. }
  1542. if((f_in != NULL) || (text_in != NULL))
  1543. {
  1544. f_text = f_in;
  1545. p_text = text_in;
  1546. timer_on = 1;
  1547. paused = 0;
  1548. }
  1549. if((f_text==NULL) && (p_text==NULL))
  1550. {
  1551. skipping_text = 0;
  1552. timer_on = 0;
  1553. return(0);
  1554. }
  1555. if((f_text != NULL) && feof(f_text))
  1556. {
  1557. timer_on = 0;
  1558. fclose(f_text);
  1559. f_text=NULL;
  1560. return(0);
  1561. }
  1562. if(current_phoneme_table != voice->phoneme_tab_ix)
  1563. {
  1564. SelectPhonemeTable(voice->phoneme_tab_ix);
  1565. }
  1566. // read the next clause from the input text file, translate it, and generate
  1567. // entries in the wavegen command queue
  1568. p_text = TranslateClause(translator, f_text, p_text, &clause_tone, &voice_change);
  1569. CalcPitches(translator, clause_tone);
  1570. CalcLengths(translator);
  1571. if((option_phonemes > 0) || (phoneme_callback != NULL))
  1572. {
  1573. GetTranslatedPhonemeString(translator->phon_out,sizeof(translator->phon_out));
  1574. if(option_phonemes > 0)
  1575. {
  1576. fprintf(f_trans,"%s\n",translator->phon_out);
  1577. if(!iswalpha(0x010d))
  1578. {
  1579. // check that c-caron is recognized as an alphabetic character
  1580. fprintf(stderr,"Warning: Accented letters are not recognized, eg: U+010D\nSet LC_CTYPE to a UTF-8 locale\n");
  1581. }
  1582. }
  1583. if(phoneme_callback != NULL)
  1584. {
  1585. phoneme_callback(translator->phon_out);
  1586. }
  1587. }
  1588. if(skipping_text)
  1589. {
  1590. n_phoneme_list = 0;
  1591. return(1);
  1592. }
  1593. Generate(phoneme_list,&n_phoneme_list,0);
  1594. WavegenOpenSound();
  1595. if(voice_change != NULL)
  1596. {
  1597. // voice change at the end of the clause (i.e. clause was terminated by a voice change)
  1598. new_voice = LoadVoiceVariant(voice_change,0); // add a Voice instruction to wavegen at the end of the clause
  1599. }
  1600. if(new_voice)
  1601. {
  1602. // finished the current clause, now change the voice if there was an embedded
  1603. // change voice command at the end of it (i.e. clause was broken at the change voice command)
  1604. DoVoiceChange(voice);
  1605. new_voice = NULL;
  1606. }
  1607. return(1);
  1608. } // end of SpeakNextClause