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

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