eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

synthesize.cpp 44KB

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