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.

klatt.cpp 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /***************************************************************************
  2. * Copyright (C) 2008 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * *
  5. * Based on a re-implementation by: *
  6. * (c) 1993,94 Jon Iles and Nick Ing-Simmons *
  7. * of the Klatt cascade-parallel formant synthesizer *
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 3 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, *
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  17. * GNU General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU General Public License *
  20. * along with this program; if not, see: *
  21. * <http://www.gnu.org/licenses/>. *
  22. ***************************************************************************/
  23. // See URL: ftp://svr-ftp.eng.cam.ac.uk/pub/comp.speech/synthesis/klatt.3.04.tar.gz
  24. #include "StdAfx.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <string.h>
  29. #include "speak_lib.h"
  30. #include "speech.h"
  31. #include "klatt.h"
  32. #include "phoneme.h"
  33. #include "synthesize.h"
  34. #include "voice.h"
  35. #ifdef INCLUDE_KLATT // conditional compilation for the whole file
  36. extern unsigned char *out_ptr; // **JSD
  37. extern unsigned char *out_start;
  38. extern unsigned char *out_end;
  39. extern WGEN_DATA wdata;
  40. static int nsamples;
  41. static int sample_count;
  42. #ifdef _MSC_VER
  43. #define getrandom(min,max) ((rand()%(int)(((max)+1)-(min)))+(min))
  44. #else
  45. #define getrandom(min,max) ((rand()%(long)(((max)+1)-(min)))+(min))
  46. #endif
  47. /* function prototypes for functions private to this file */
  48. static void flutter(klatt_frame_ptr);
  49. static double sampled_source (void);
  50. static double impulsive_source (void);
  51. static double natural_source (void);
  52. static void pitch_synch_par_reset (klatt_frame_ptr);
  53. static double gen_noise (double);
  54. static double DBtoLIN (long);
  55. static void frame_init (klatt_frame_ptr);
  56. static void setabc (long,long,resonator_ptr);
  57. static void setzeroabc (long,long,resonator_ptr);
  58. static klatt_frame_t kt_frame;
  59. static klatt_global_t kt_globals;
  60. /*
  61. function RESONATOR
  62. This is a generic resonator function. Internal memory for the resonator
  63. is stored in the globals structure.
  64. */
  65. static double resonator(resonator_ptr r, double input)
  66. {
  67. double x;
  68. x = (double) ((double)r->a * (double)input + (double)r->b * (double)r->p1 + (double)r->c * (double)r->p2);
  69. r->p2 = (double)r->p1;
  70. r->p1 = (double)x;
  71. return (double)x;
  72. }
  73. static double resonator2(resonator_ptr r, double input)
  74. {
  75. double x;
  76. x = (double) ((double)r->a * (double)input + (double)r->b * (double)r->p1 + (double)r->c * (double)r->p2);
  77. r->p2 = (double)r->p1;
  78. r->p1 = (double)x;
  79. r->a += r->a_inc;
  80. r->b += r->b_inc;
  81. r->c += r->c_inc;
  82. return (double)x;
  83. }
  84. /*
  85. function ANTIRESONATOR
  86. This is a generic anti-resonator function. The code is the same as resonator
  87. except that a,b,c need to be set with setzeroabc() and we save inputs in
  88. p1/p2 rather than outputs. There is currently only one of these - "rnz"
  89. Output = (rnz.a * input) + (rnz.b * oldin1) + (rnz.c * oldin2)
  90. */
  91. #ifdef deleted
  92. static double antiresonator(resonator_ptr r, double input)
  93. {
  94. register double x = (double)r->a * (double)input + (double)r->b * (double)r->p1 + (double)r->c * (double)r->p2;
  95. r->p2 = (double)r->p1;
  96. r->p1 = (double)input;
  97. return (double)x;
  98. }
  99. #endif
  100. static double antiresonator2(resonator_ptr r, double input)
  101. {
  102. register double x = (double)r->a * (double)input + (double)r->b * (double)r->p1 + (double)r->c * (double)r->p2;
  103. r->p2 = (double)r->p1;
  104. r->p1 = (double)input;
  105. r->a += r->a_inc;
  106. r->b += r->b_inc;
  107. r->c += r->c_inc;
  108. return (double)x;
  109. }
  110. /*
  111. function FLUTTER
  112. This function adds F0 flutter, as specified in:
  113. "Analysis, synthesis and perception of voice quality variations among
  114. female and male talkers" D.H. Klatt and L.C. Klatt JASA 87(2) February 1990.
  115. Flutter is added by applying a quasi-random element constructed from three
  116. slowly varying sine waves.
  117. */
  118. static void flutter(klatt_frame_ptr frame)
  119. {
  120. static int time_count;
  121. double delta_f0;
  122. double fla,flb,flc,fld,fle;
  123. fla = (double) kt_globals.f0_flutter / 50;
  124. flb = (double) kt_globals.original_f0 / 100;
  125. // flc = sin(2*PI*12.7*time_count);
  126. // fld = sin(2*PI*7.1*time_count);
  127. // fle = sin(2*PI*4.7*time_count);
  128. flc = sin(PI*12.7*time_count); // because we are calling flutter() more frequently, every 2.9mS
  129. fld = sin(PI*7.1*time_count);
  130. fle = sin(PI*4.7*time_count);
  131. delta_f0 = fla * flb * (flc + fld + fle) * 10;
  132. frame->F0hz10 = frame->F0hz10 + (long) delta_f0;
  133. time_count++;
  134. }
  135. /*
  136. function SAMPLED_SOURCE
  137. Allows the use of a glottal excitation waveform sampled from a real
  138. voice.
  139. */
  140. static double sampled_source()
  141. {
  142. int itemp;
  143. double ftemp;
  144. double result;
  145. double diff_value;
  146. int current_value;
  147. int next_value;
  148. double temp_diff;
  149. if(kt_globals.T0!=0)
  150. {
  151. ftemp = (double) kt_globals.nper;
  152. ftemp = ftemp / kt_globals.T0;
  153. ftemp = ftemp * kt_globals.num_samples;
  154. itemp = (int) ftemp;
  155. temp_diff = ftemp - (double) itemp;
  156. current_value = kt_globals.natural_samples[itemp];
  157. next_value = kt_globals.natural_samples[itemp+1];
  158. diff_value = (double) next_value - (double) current_value;
  159. diff_value = diff_value * temp_diff;
  160. result = kt_globals.natural_samples[itemp] + diff_value;
  161. result = result * kt_globals.sample_factor;
  162. }
  163. else
  164. {
  165. result = 0;
  166. }
  167. return(result);
  168. }
  169. /*
  170. function PARWAVE
  171. Converts synthesis parameters to a waveform.
  172. */
  173. static int parwave(klatt_frame_ptr frame)
  174. {
  175. double temp;
  176. int value;
  177. double outbypas;
  178. double out;
  179. long n4;
  180. double frics;
  181. double glotout;
  182. double aspiration;
  183. double casc_next_in;
  184. double par_glotout;
  185. static double noise;
  186. static double voice;
  187. static double vlast;
  188. static double glotlast;
  189. static double sourc;
  190. int ix;
  191. flutter(frame); /* add f0 flutter */
  192. #ifdef LOG_FRAMES
  193. if(option_log_frames)
  194. {
  195. FILE *f;
  196. f=fopen("log-klatt","a");
  197. fprintf(f,"%4dhz %2dAV %4d %3d, %4d %3d, %4d %3d, %4d %3d, %4d, %3d, FNZ=%3d TLT=%2d\n",frame->F0hz10,frame->AVdb,
  198. frame->Fhz[1],frame->Bhz[1],frame->Fhz[2],frame->Bhz[2],frame->Fhz[3],frame->Bhz[3],frame->Fhz[4],frame->Bhz[4],frame->Fhz[5],frame->Bhz[5],frame->Fhz[0],frame->TLTdb);
  199. fclose(f);
  200. }
  201. #endif
  202. /* MAIN LOOP, for each output sample of current frame: */
  203. for (kt_globals.ns=0; kt_globals.ns<kt_globals.nspfr; kt_globals.ns++)
  204. {
  205. /* Get low-passed random number for aspiration and frication noise */
  206. noise = gen_noise(noise);
  207. /*
  208. Amplitude modulate noise (reduce noise amplitude during
  209. second half of glottal period) if voicing simultaneously present.
  210. */
  211. if (kt_globals.nper > kt_globals.nmod)
  212. {
  213. noise *= (double) 0.5;
  214. }
  215. /* Compute frication noise */
  216. frics = kt_globals.amp_frica * noise;
  217. /*
  218. Compute voicing waveform. Run glottal source simulation at 4
  219. times normal sample rate to minimize quantization noise in
  220. period of female voice.
  221. */
  222. for (n4=0; n4<4; n4++)
  223. {
  224. switch(kt_globals.glsource)
  225. {
  226. case IMPULSIVE:
  227. voice = impulsive_source();
  228. break;
  229. case NATURAL:
  230. voice = natural_source();
  231. break;
  232. case SAMPLED:
  233. voice = sampled_source();
  234. break;
  235. }
  236. /* Reset period when counter 'nper' reaches T0 */
  237. if (kt_globals.nper >= kt_globals.T0)
  238. {
  239. kt_globals.nper = 0;
  240. pitch_synch_par_reset(frame);
  241. }
  242. /*
  243. Low-pass filter voicing waveform before downsampling from 4*samrate
  244. to samrate samples/sec. Resonator f=.09*samrate, bw=.06*samrate
  245. */
  246. voice = resonator(&(kt_globals.rsn[RLP]),voice);
  247. /* Increment counter that keeps track of 4*samrate samples per sec */
  248. kt_globals.nper++;
  249. }
  250. /*
  251. Tilt spectrum of voicing source down by soft low-pass filtering, amount
  252. of tilt determined by TLTdb
  253. */
  254. voice = (voice * kt_globals.onemd) + (vlast * kt_globals.decay);
  255. vlast = voice;
  256. /*
  257. Add breathiness during glottal open phase. Amount of breathiness
  258. determined by parameter Aturb Use nrand rather than noise because
  259. noise is low-passed.
  260. */
  261. if (kt_globals.nper < kt_globals.nopen)
  262. {
  263. voice += kt_globals.amp_breth * kt_globals.nrand;
  264. }
  265. /* Set amplitude of voicing */
  266. glotout = kt_globals.amp_voice * voice;
  267. par_glotout = kt_globals.par_amp_voice * voice;
  268. /* Compute aspiration amplitude and add to voicing source */
  269. aspiration = kt_globals.amp_aspir * noise;
  270. glotout += aspiration;
  271. par_glotout += aspiration;
  272. /*
  273. Cascade vocal tract, excited by laryngeal sources.
  274. Nasal antiresonator, then formants FNP, F5, F4, F3, F2, F1
  275. */
  276. out=0;
  277. if(kt_globals.synthesis_model != ALL_PARALLEL)
  278. {
  279. casc_next_in = antiresonator2(&(kt_globals.rsn[Rnz]),glotout);
  280. casc_next_in = resonator(&(kt_globals.rsn[Rnpc]),casc_next_in);
  281. casc_next_in = resonator(&(kt_globals.rsn[R8c]),casc_next_in);
  282. casc_next_in = resonator(&(kt_globals.rsn[R7c]),casc_next_in);
  283. casc_next_in = resonator(&(kt_globals.rsn[R6c]),casc_next_in);
  284. casc_next_in = resonator2(&(kt_globals.rsn[R5c]),casc_next_in);
  285. casc_next_in = resonator2(&(kt_globals.rsn[R4c]),casc_next_in);
  286. casc_next_in = resonator2(&(kt_globals.rsn[R3c]),casc_next_in);
  287. casc_next_in = resonator2(&(kt_globals.rsn[R2c]),casc_next_in);
  288. out = resonator2(&(kt_globals.rsn[R1c]),casc_next_in);
  289. }
  290. /* Excite parallel F1 and FNP by voicing waveform */
  291. sourc = par_glotout; /* Source is voicing plus aspiration */
  292. /*
  293. Standard parallel vocal tract Formants F6,F5,F4,F3,F2,
  294. outputs added with alternating sign. Sound source for other
  295. parallel resonators is frication plus first difference of
  296. voicing waveform.
  297. */
  298. out += resonator(&(kt_globals.rsn[R1p]),sourc);
  299. out += resonator(&(kt_globals.rsn[Rnpp]),sourc);
  300. sourc = frics + par_glotout - glotlast;
  301. glotlast = par_glotout;
  302. for(ix=R2p; ix<=R6p; ix++)
  303. {
  304. out = resonator(&(kt_globals.rsn[ix]),sourc) - out;
  305. }
  306. outbypas = kt_globals.amp_bypas * sourc;
  307. out = outbypas - out;
  308. #ifdef deleted
  309. // for testing
  310. if (kt_globals.outsl != 0)
  311. {
  312. switch(kt_globals.outsl)
  313. {
  314. case 1:
  315. out = voice;
  316. break;
  317. case 2:
  318. out = aspiration;
  319. break;
  320. case 3:
  321. out = frics;
  322. break;
  323. case 4:
  324. out = glotout;
  325. break;
  326. case 5:
  327. out = par_glotout;
  328. break;
  329. case 6:
  330. out = outbypas;
  331. break;
  332. case 7:
  333. out = sourc;
  334. break;
  335. }
  336. }
  337. #endif
  338. out = resonator(&(kt_globals.rsn[Rout]),out);
  339. temp = (int)(out * wdata.amplitude * kt_globals.amp_gain0) ; /* Convert back to integer */
  340. // mix with a recorded WAV if required for this phoneme
  341. {
  342. int z2;
  343. signed char c;
  344. int sample;
  345. z2 = 0;
  346. if(wdata.mix_wavefile_ix < wdata.n_mix_wavefile)
  347. {
  348. if(wdata.mix_wave_scale == 0)
  349. {
  350. // a 16 bit sample
  351. c = wdata.mix_wavefile[wdata.mix_wavefile_ix+1];
  352. sample = wdata.mix_wavefile[wdata.mix_wavefile_ix] + (c * 256);
  353. wdata.mix_wavefile_ix += 2;
  354. }
  355. else
  356. {
  357. // a 8 bit sample, scaled
  358. sample = (signed char)wdata.mix_wavefile[wdata.mix_wavefile_ix++] * wdata.mix_wave_scale;
  359. }
  360. z2 = sample * wdata.amplitude_v / 1024;
  361. z2 = (z2 * wdata.mix_wave_amp)/40;
  362. temp += z2;
  363. }
  364. }
  365. // if fadeout is set, fade to zero over 64 samples, to avoid clicks at end of synthesis
  366. if(kt_globals.fadeout > 0)
  367. {
  368. kt_globals.fadeout--;
  369. temp = (temp * kt_globals.fadeout) / 64;
  370. }
  371. value = (int)temp + ((echo_buf[echo_tail++]*echo_amp) >> 8);
  372. if(echo_tail >= N_ECHO_BUF)
  373. echo_tail=0;
  374. if (value < -32768)
  375. {
  376. value = -32768;
  377. }
  378. if (value > 32767)
  379. {
  380. value = 32767;
  381. }
  382. *out_ptr++ = value;
  383. *out_ptr++ = value >> 8;
  384. echo_buf[echo_head++] = value;
  385. if(echo_head >= N_ECHO_BUF)
  386. echo_head = 0;
  387. sample_count++;
  388. if(out_ptr >= out_end)
  389. {
  390. return(1);
  391. }
  392. }
  393. return(0);
  394. } // end of parwave
  395. void KlattReset(int control)
  396. {
  397. int r_ix;
  398. if(control == 2)
  399. {
  400. //Full reset
  401. kt_globals.FLPhz = (950 * kt_globals.samrate) / 10000;
  402. kt_globals.BLPhz = (630 * kt_globals.samrate) / 10000;
  403. kt_globals.minus_pi_t = -PI / kt_globals.samrate;
  404. kt_globals.two_pi_t = -2.0 * kt_globals.minus_pi_t;
  405. setabc(kt_globals.FLPhz,kt_globals.BLPhz,&(kt_globals.rsn[RLP]));
  406. }
  407. if(control > 0)
  408. {
  409. kt_globals.nper = 0;
  410. kt_globals.T0 = 0;
  411. kt_globals.nopen = 0;
  412. kt_globals.nmod = 0;
  413. for(r_ix=RGL; r_ix < N_RSN; r_ix++)
  414. {
  415. kt_globals.rsn[r_ix].p1 = 0;
  416. kt_globals.rsn[r_ix].p2 = 0;
  417. }
  418. }
  419. for(r_ix=0; r_ix <= R6p; r_ix++)
  420. {
  421. kt_globals.rsn[r_ix].p1 = 0;
  422. kt_globals.rsn[r_ix].p2 = 0;
  423. }
  424. }
  425. /*
  426. function FRAME_INIT
  427. Use parameters from the input frame to set up resonator coefficients.
  428. */
  429. static void frame_init(klatt_frame_ptr frame)
  430. {
  431. double amp_par[7];
  432. static double amp_par_factor[7] = {0.6, 0.4, 0.15, 0.06, 0.04, 0.022, 0.03};
  433. long Gain0_tmp;
  434. int ix;
  435. kt_globals.original_f0 = frame->F0hz10 / 10;
  436. frame->AVdb_tmp = frame->AVdb - 7;
  437. if (frame->AVdb_tmp < 0)
  438. {
  439. frame->AVdb_tmp = 0;
  440. }
  441. kt_globals.amp_aspir = DBtoLIN(frame->ASP) * 0.05;
  442. kt_globals.amp_frica = DBtoLIN(frame->AF) * 0.25;
  443. kt_globals.par_amp_voice = DBtoLIN(frame->AVpdb);
  444. kt_globals.amp_bypas = DBtoLIN(frame->AB) * 0.05;
  445. for(ix=0; ix <= 6; ix++)
  446. {
  447. // parallel amplitudes F1 to F6, and parallel nasal pole
  448. amp_par[ix] = DBtoLIN(frame->Ap[ix]) * amp_par_factor[ix];
  449. }
  450. Gain0_tmp = frame->Gain0 - 3;
  451. if (Gain0_tmp <= 0)
  452. {
  453. Gain0_tmp = 57;
  454. }
  455. kt_globals.amp_gain0 = DBtoLIN(Gain0_tmp) / kt_globals.scale_wav;
  456. /* Set coefficients of variable cascade resonators */
  457. for(ix=1; ix<=9; ix++)
  458. {
  459. // formants 1 to 8, plus nasal pole
  460. setabc(frame->Fhz[ix],frame->Bhz[ix],&(kt_globals.rsn[ix]));
  461. if(ix <= 5)
  462. {
  463. setabc(frame->Fhz_next[ix],frame->Bhz_next[ix],&(kt_globals.rsn_next[ix]));
  464. kt_globals.rsn[ix].a_inc = (kt_globals.rsn_next[ix].a - kt_globals.rsn[ix].a) / 64.0;
  465. kt_globals.rsn[ix].b_inc = (kt_globals.rsn_next[ix].b - kt_globals.rsn[ix].b) / 64.0;
  466. kt_globals.rsn[ix].c_inc = (kt_globals.rsn_next[ix].c - kt_globals.rsn[ix].c) / 64.0;
  467. }
  468. }
  469. // nasal zero anti-resonator
  470. setzeroabc(frame->Fhz[F_NZ],frame->Bhz[F_NZ],&(kt_globals.rsn[Rnz]));
  471. setzeroabc(frame->Fhz_next[F_NZ],frame->Bhz_next[F_NZ],&(kt_globals.rsn_next[Rnz]));
  472. kt_globals.rsn[F_NZ].a_inc = (kt_globals.rsn_next[F_NZ].a - kt_globals.rsn[F_NZ].a) / 64.0;
  473. kt_globals.rsn[F_NZ].b_inc = (kt_globals.rsn_next[F_NZ].b - kt_globals.rsn[F_NZ].b) / 64.0;
  474. kt_globals.rsn[F_NZ].c_inc = (kt_globals.rsn_next[F_NZ].c - kt_globals.rsn[F_NZ].c) / 64.0;
  475. /* Set coefficients of parallel resonators, and amplitude of outputs */
  476. for(ix=0; ix<=6; ix++)
  477. {
  478. setabc(frame->Fhz[ix],frame->Bphz[ix],&(kt_globals.rsn[Rparallel+ix]));
  479. kt_globals.rsn[Rparallel+ix].a *= amp_par[ix];
  480. }
  481. /* output low-pass filter */
  482. setabc((long)0.0,(long)(kt_globals.samrate/2),&(kt_globals.rsn[Rout]));
  483. }
  484. /*
  485. function IMPULSIVE_SOURCE
  486. Generate a low pass filtered train of impulses as an approximation of
  487. a natural excitation waveform. Low-pass filter the differentiated impulse
  488. with a critically-damped second-order filter, time constant proportional
  489. to Kopen.
  490. */
  491. static double impulsive_source()
  492. {
  493. static double doublet[] = {0.0,13000000.0,-13000000.0};
  494. static double vwave;
  495. if (kt_globals.nper < 3)
  496. {
  497. vwave = doublet[kt_globals.nper];
  498. }
  499. else
  500. {
  501. vwave = 0.0;
  502. }
  503. return(resonator(&(kt_globals.rsn[RGL]),vwave));
  504. }
  505. /*
  506. function NATURAL_SOURCE
  507. Vwave is the differentiated glottal flow waveform, there is a weak
  508. spectral zero around 800 Hz, magic constants a,b reset pitch synchronously.
  509. */
  510. static double natural_source()
  511. {
  512. double lgtemp;
  513. static double vwave;
  514. if (kt_globals.nper < kt_globals.nopen)
  515. {
  516. kt_globals.pulse_shape_a -= kt_globals.pulse_shape_b;
  517. vwave += kt_globals.pulse_shape_a;
  518. lgtemp=vwave * 0.028;
  519. return(lgtemp);
  520. }
  521. else
  522. {
  523. vwave = 0.0;
  524. return(0.0);
  525. }
  526. }
  527. /*
  528. function PITCH_SYNC_PAR_RESET
  529. Reset selected parameters pitch-synchronously.
  530. Constant B0 controls shape of glottal pulse as a function
  531. of desired duration of open phase N0
  532. (Note that N0 is specified in terms of 40,000 samples/sec of speech)
  533. Assume voicing waveform V(t) has form: k1 t**2 - k2 t**3
  534. If the radiation characterivative, a temporal derivative
  535. is folded in, and we go from continuous time to discrete
  536. integers n: dV/dt = vwave[n]
  537. = sum over i=1,2,...,n of { a - (i * b) }
  538. = a n - b/2 n**2
  539. where the constants a and b control the detailed shape
  540. and amplitude of the voicing waveform over the open
  541. potion of the voicing cycle "nopen".
  542. Let integral of dV/dt have no net dc flow --> a = (b * nopen) / 3
  543. Let maximum of dUg(n)/dn be constant --> b = gain / (nopen * nopen)
  544. meaning as nopen gets bigger, V has bigger peak proportional to n
  545. Thus, to generate the table below for 40 <= nopen <= 263:
  546. B0[nopen - 40] = 1920000 / (nopen * nopen)
  547. */
  548. static void pitch_synch_par_reset(klatt_frame_ptr frame)
  549. {
  550. long temp;
  551. double temp1;
  552. static long skew;
  553. static short B0[224] =
  554. {
  555. 1200,1142,1088,1038, 991, 948, 907, 869, 833, 799, 768, 738, 710, 683, 658,
  556. 634, 612, 590, 570, 551, 533, 515, 499, 483, 468, 454, 440, 427, 415, 403,
  557. 391, 380, 370, 360, 350, 341, 332, 323, 315, 307, 300, 292, 285, 278, 272,
  558. 265, 259, 253, 247, 242, 237, 231, 226, 221, 217, 212, 208, 204, 199, 195,
  559. 192, 188, 184, 180, 177, 174, 170, 167, 164, 161, 158, 155, 153, 150, 147,
  560. 145, 142, 140, 137, 135, 133, 131, 128, 126, 124, 122, 120, 119, 117, 115,
  561. 113,111, 110, 108, 106, 105, 103, 102, 100, 99, 97, 96, 95, 93, 92, 91, 90,
  562. 88, 87, 86, 85, 84, 83, 82, 80, 79, 78, 77, 76, 75, 75, 74, 73, 72, 71,
  563. 70, 69, 68, 68, 67, 66, 65, 64, 64, 63, 62, 61, 61, 60, 59, 59, 58, 57,
  564. 57, 56, 56, 55, 55, 54, 54, 53, 53, 52, 52, 51, 51, 50, 50, 49, 49, 48, 48,
  565. 47, 47, 46, 46, 45, 45, 44, 44, 43, 43, 42, 42, 41, 41, 41, 41, 40, 40,
  566. 39, 39, 38, 38, 38, 38, 37, 37, 36, 36, 36, 36, 35, 35, 35, 35, 34, 34,33,
  567. 33, 33, 33, 32, 32, 32, 32, 31, 31, 31, 31, 30, 30, 30, 30, 29, 29, 29, 29,
  568. 28, 28, 28, 28, 27, 27
  569. };
  570. if (frame->F0hz10 > 0)
  571. {
  572. /* T0 is 4* the number of samples in one pitch period */
  573. kt_globals.T0 = (40 * kt_globals.samrate) / frame->F0hz10;
  574. kt_globals.amp_voice = DBtoLIN(frame->AVdb_tmp);
  575. /* Duration of period before amplitude modulation */
  576. kt_globals.nmod = kt_globals.T0;
  577. if (frame->AVdb_tmp > 0)
  578. {
  579. kt_globals.nmod >>= 1;
  580. }
  581. /* Breathiness of voicing waveform */
  582. kt_globals.amp_breth = DBtoLIN(frame->Aturb) * 0.1;
  583. /* Set open phase of glottal period where 40 <= open phase <= 263 */
  584. kt_globals.nopen = 4 * frame->Kopen;
  585. if ((kt_globals.glsource == IMPULSIVE) && (kt_globals.nopen > 263))
  586. {
  587. kt_globals.nopen = 263;
  588. }
  589. if (kt_globals.nopen >= (kt_globals.T0-1))
  590. {
  591. // printf("Warning: glottal open period cannot exceed T0, truncated\n");
  592. kt_globals.nopen = kt_globals.T0 - 2;
  593. }
  594. if (kt_globals.nopen < 40)
  595. {
  596. /* F0 max = 1000 Hz */
  597. // printf("Warning: minimum glottal open period is 10 samples.\n");
  598. // printf("truncated, nopen = %d\n",kt_globals.nopen);
  599. kt_globals.nopen = 40;
  600. }
  601. /* Reset a & b, which determine shape of "natural" glottal waveform */
  602. kt_globals.pulse_shape_b = B0[kt_globals.nopen-40];
  603. kt_globals.pulse_shape_a = (kt_globals.pulse_shape_b * kt_globals.nopen) * 0.333;
  604. /* Reset width of "impulsive" glottal pulse */
  605. temp = kt_globals.samrate / kt_globals.nopen;
  606. setabc((long)0,temp,&(kt_globals.rsn[RGL]));
  607. /* Make gain at F1 about constant */
  608. temp1 = kt_globals.nopen *.00833;
  609. kt_globals.rsn[RGL].a *= temp1 * temp1;
  610. /*
  611. Truncate skewness so as not to exceed duration of closed phase
  612. of glottal period.
  613. */
  614. temp = kt_globals.T0 - kt_globals.nopen;
  615. if (frame->Kskew > temp)
  616. {
  617. // printf("Kskew duration=%d > glottal closed period=%d, truncate\n", frame->Kskew, kt_globals.T0 - kt_globals.nopen);
  618. frame->Kskew = temp;
  619. }
  620. if (skew >= 0)
  621. {
  622. skew = frame->Kskew;
  623. }
  624. else
  625. {
  626. skew = - frame->Kskew;
  627. }
  628. /* Add skewness to closed portion of voicing period */
  629. kt_globals.T0 = kt_globals.T0 + skew;
  630. skew = - skew;
  631. }
  632. else
  633. {
  634. kt_globals.T0 = 4; /* Default for f0 undefined */
  635. kt_globals.amp_voice = 0.0;
  636. kt_globals.nmod = kt_globals.T0;
  637. kt_globals.amp_breth = 0.0;
  638. kt_globals.pulse_shape_a = 0.0;
  639. kt_globals.pulse_shape_b = 0.0;
  640. }
  641. /* Reset these pars pitch synchronously or at update rate if f0=0 */
  642. if ((kt_globals.T0 != 4) || (kt_globals.ns == 0))
  643. {
  644. /* Set one-pole low-pass filter that tilts glottal source */
  645. kt_globals.decay = (0.033 * frame->TLTdb);
  646. if (kt_globals.decay > 0.0)
  647. {
  648. kt_globals.onemd = 1.0 - kt_globals.decay;
  649. }
  650. else
  651. {
  652. kt_globals.onemd = 1.0;
  653. }
  654. }
  655. }
  656. /*
  657. function SETABC
  658. Convert formant freqencies and bandwidth into resonator difference
  659. equation constants.
  660. */
  661. static void setabc(long int f, long int bw, resonator_ptr rp)
  662. {
  663. double r;
  664. double arg;
  665. /* Let r = exp(-pi bw t) */
  666. arg = kt_globals.minus_pi_t * bw;
  667. r = exp(arg);
  668. /* Let c = -r**2 */
  669. rp->c = -(r * r);
  670. /* Let b = r * 2*cos(2 pi f t) */
  671. arg = kt_globals.two_pi_t * f;
  672. rp->b = r * cos(arg) * 2.0;
  673. /* Let a = 1.0 - b - c */
  674. rp->a = 1.0 - rp->b - rp->c;
  675. }
  676. /*
  677. function SETZEROABC
  678. Convert formant freqencies and bandwidth into anti-resonator difference
  679. equation constants.
  680. */
  681. static void setzeroabc(long int f, long int bw, resonator_ptr rp)
  682. {
  683. double r;
  684. double arg;
  685. f = -f;
  686. //NOTE, changes made 30.09.2011 for Reece Dunn <[email protected]>
  687. // fix a sound spike when f=0
  688. /* First compute ordinary resonator coefficients */
  689. /* Let r = exp(-pi bw t) */
  690. arg = kt_globals.minus_pi_t * bw;
  691. r = exp(arg);
  692. /* Let c = -r**2 */
  693. rp->c = -(r * r);
  694. /* Let b = r * 2*cos(2 pi f t) */
  695. arg = kt_globals.two_pi_t * f;
  696. rp->b = r * cos(arg) * 2.;
  697. /* Let a = 1.0 - b - c */
  698. rp->a = 1.0 - rp->b - rp->c;
  699. /* Now convert to antiresonator coefficients (a'=1/a, b'=b/a, c'=c/a) */
  700. /* If f == 0 then rp->a gets set to 0 which makes a'=1/a set a', b' and c' to
  701. * INF, causing an audible sound spike when triggered (e.g. apiration with the
  702. * nasal register set to f=0, bw=0).
  703. */
  704. if (rp->a != 0)
  705. {
  706. /* Now convert to antiresonator coefficients (a'=1/a, b'=b/a, c'=c/a) */
  707. rp->a = 1.0 / rp->a;
  708. rp->c *= -rp->a;
  709. rp->b *= -rp->a;
  710. }
  711. }
  712. /*
  713. function GEN_NOISE
  714. Random number generator (return a number between -8191 and +8191)
  715. Noise spectrum is tilted down by soft low-pass filter having a pole near
  716. the origin in the z-plane, i.e. output = input + (0.75 * lastoutput)
  717. */
  718. static double gen_noise(double noise)
  719. {
  720. long temp;
  721. static double nlast;
  722. temp = (long) getrandom(-8191,8191);
  723. kt_globals.nrand = (long) temp;
  724. noise = kt_globals.nrand + (0.75 * nlast);
  725. nlast = noise;
  726. return(noise);
  727. }
  728. /*
  729. function DBTOLIN
  730. Convert from decibels to a linear scale factor
  731. Conversion table, db to linear, 87 dB --> 32767
  732. 86 dB --> 29491 (1 dB down = 0.5**1/6)
  733. ...
  734. 81 dB --> 16384 (6 dB down = 0.5)
  735. ...
  736. 0 dB --> 0
  737. The just noticeable difference for a change in intensity of a vowel
  738. is approximately 1 dB. Thus all amplitudes are quantized to 1 dB
  739. steps.
  740. */
  741. static double DBtoLIN(long dB)
  742. {
  743. static short amptable[88] =
  744. {
  745. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7,
  746. 8, 9, 10, 11, 13, 14, 16, 18, 20, 22, 25, 28, 32,
  747. 35, 40, 45, 51, 57, 64, 71, 80, 90, 101, 114, 128,
  748. 142, 159, 179, 202, 227, 256, 284, 318, 359, 405,
  749. 455, 512, 568, 638, 719, 881, 911, 1024, 1137, 1276,
  750. 1438, 1622, 1823, 2048, 2273, 2552, 2875, 3244, 3645,
  751. 4096, 4547, 5104, 5751, 6488, 7291, 8192, 9093, 10207,
  752. 11502, 12976, 14582, 16384, 18350, 20644, 23429,
  753. 26214, 29491, 32767 };
  754. if ((dB < 0) || (dB > 87))
  755. {
  756. return(0);
  757. }
  758. return((double)(amptable[dB]) * 0.001);
  759. }
  760. extern voice_t *wvoice;
  761. static klatt_peaks_t peaks[N_PEAKS];
  762. static int end_wave;
  763. static int klattp[N_KLATTP];
  764. static double klattp1[N_KLATTP];
  765. static double klattp_inc[N_KLATTP];
  766. static int scale_wav_tab[] = {45,38,45,45}; // scale output from different voicing sources
  767. int Wavegen_Klatt(int resume)
  768. {//==========================
  769. int pk;
  770. int x;
  771. int ix;
  772. int fade;
  773. if(resume==0)
  774. {
  775. sample_count = 0;
  776. }
  777. while(sample_count < nsamples)
  778. {
  779. kt_frame.F0hz10 = (wdata.pitch * 10) / 4096;
  780. // formants F6,F7,F8 are fixed values for cascade resonators, set in KlattInit()
  781. // but F6 is used for parallel resonator
  782. // F0 is used for the nasal zero
  783. for(ix=0; ix < 6; ix++)
  784. {
  785. kt_frame.Fhz[ix] = peaks[ix].freq;
  786. if(ix < 4)
  787. {
  788. kt_frame.Bhz[ix] = peaks[ix].bw;
  789. }
  790. }
  791. for(ix=1; ix < 7; ix++)
  792. {
  793. kt_frame.Ap[ix] = 0;
  794. }
  795. kt_frame.AVdb = klattp[KLATT_AV];
  796. kt_frame.AVpdb = klattp[KLATT_AVp];
  797. kt_frame.AF = klattp[KLATT_Fric];
  798. kt_frame.AB = klattp[KLATT_FricBP];
  799. kt_frame.ASP = klattp[KLATT_Aspr];
  800. kt_frame.Aturb = klattp[KLATT_Turb];
  801. kt_frame.Kskew = klattp[KLATT_Skew];
  802. kt_frame.TLTdb = klattp[KLATT_Tilt];
  803. kt_frame.Kopen = klattp[KLATT_Kopen];
  804. // advance formants
  805. for(pk=0; pk<N_PEAKS; pk++)
  806. {
  807. peaks[pk].freq1 += peaks[pk].freq_inc;
  808. peaks[pk].freq = (int)peaks[pk].freq1;
  809. peaks[pk].bw1 += peaks[pk].bw_inc;
  810. peaks[pk].bw = (int)peaks[pk].bw1;
  811. peaks[pk].bp1 += peaks[pk].bp_inc;
  812. peaks[pk].bp = (int)peaks[pk].bp1;
  813. peaks[pk].ap1 += peaks[pk].ap_inc;
  814. peaks[pk].ap = (int)peaks[pk].ap1;
  815. }
  816. // advance other parameters
  817. for(ix=0; ix < N_KLATTP; ix++)
  818. {
  819. klattp1[ix] += klattp_inc[ix];
  820. klattp[ix] = (int)klattp1[ix];
  821. }
  822. for(ix=0; ix<=6; ix++)
  823. {
  824. kt_frame.Fhz_next[ix] = peaks[ix].freq;
  825. if(ix < 4)
  826. {
  827. kt_frame.Bhz_next[ix] = peaks[ix].bw;
  828. }
  829. }
  830. // advance the pitch
  831. wdata.pitch_ix += wdata.pitch_inc;
  832. if((ix = wdata.pitch_ix>>8) > 127) ix = 127;
  833. x = wdata.pitch_env[ix] * wdata.pitch_range;
  834. wdata.pitch = (x>>8) + wdata.pitch_base;
  835. kt_globals.nspfr = (nsamples - sample_count);
  836. if(kt_globals.nspfr > STEPSIZE)
  837. kt_globals.nspfr = STEPSIZE;
  838. frame_init(&kt_frame); /* get parameters for next frame of speech */
  839. if(parwave(&kt_frame) == 1)
  840. {
  841. return(1); // output buffer is full
  842. }
  843. }
  844. if(end_wave > 0)
  845. {
  846. #ifdef deleted
  847. if(end_wave == 2)
  848. {
  849. fade = (kt_globals.T0 - kt_globals.nper)/4; // samples until end of current cycle
  850. if(fade < 64)
  851. fade = 64;
  852. }
  853. else
  854. #endif
  855. {
  856. fade = 64; // not followd by formant synthesis
  857. }
  858. // fade out to avoid a click
  859. kt_globals.fadeout = fade;
  860. end_wave = 0;
  861. sample_count -= fade;
  862. kt_globals.nspfr = fade;
  863. if(parwave(&kt_frame) == 1)
  864. {
  865. return(1); // output buffer is full
  866. }
  867. }
  868. return(0);
  869. }
  870. void SetSynth_Klatt(int length, int modn, frame_t *fr1, frame_t *fr2, voice_t *v, int control)
  871. {//===========================================================================================
  872. int ix;
  873. DOUBLEX next;
  874. int qix;
  875. int cmd;
  876. frame_t *fr3;
  877. static frame_t prev_fr;
  878. if(wvoice != NULL)
  879. {
  880. if((wvoice->klattv[0] > 0) && (wvoice->klattv[0] <=3 ))
  881. {
  882. kt_globals.glsource = wvoice->klattv[0];
  883. kt_globals.scale_wav = scale_wav_tab[kt_globals.glsource];
  884. }
  885. kt_globals.f0_flutter = wvoice->flutter/32;
  886. }
  887. end_wave = 0;
  888. if(control & 2)
  889. {
  890. end_wave = 1; // fadeout at the end
  891. }
  892. if(control & 1)
  893. {
  894. end_wave = 1;
  895. for(qix=wcmdq_head+1;;qix++)
  896. {
  897. if(qix >= N_WCMDQ) qix = 0;
  898. if(qix == wcmdq_tail) break;
  899. cmd = wcmdq[qix][0];
  900. if(cmd==WCMD_KLATT)
  901. {
  902. end_wave = 0; // next wave generation is from another spectrum
  903. fr3 = (frame_t *)wcmdq[qix][2];
  904. for(ix=1; ix<6; ix++)
  905. {
  906. if(fr3->ffreq[ix] != fr2->ffreq[ix])
  907. {
  908. // there is a discontinuity in formants
  909. end_wave = 2;
  910. break;
  911. }
  912. }
  913. break;
  914. }
  915. if((cmd==WCMD_WAVE) || (cmd==WCMD_PAUSE))
  916. break; // next is not from spectrum, so continue until end of wave cycle
  917. }
  918. }
  919. #ifdef LOG_FRAMES
  920. if(option_log_frames)
  921. {
  922. FILE *f_log;
  923. f_log=fopen("log-espeakedit","a");
  924. if(f_log != NULL)
  925. {
  926. fprintf(f_log,"K %3dmS %3d %3d %4d %4d %4d %4d (%2d) to %3d %3d %4d %4d %4d %4d (%2d)\n",length*1000/samplerate,
  927. fr1->klattp[KLATT_FNZ]*2,fr1->ffreq[1],fr1->ffreq[2],fr1->ffreq[3],fr1->ffreq[4],fr1->ffreq[5], fr1->klattp[KLATT_AV],
  928. fr2->klattp[KLATT_FNZ]*2,fr2->ffreq[1],fr2->ffreq[2],fr2->ffreq[3],fr1->ffreq[4],fr1->ffreq[5], fr2->klattp[KLATT_AV] );
  929. fclose(f_log);
  930. }
  931. f_log=fopen("log-klatt","a");
  932. if(f_log != NULL)
  933. {
  934. fprintf(f_log,"K %3dmS %3d %3d %4d %4d (%2d) to %3d %3d %4d %4d (%2d)\n",length*1000/samplerate,
  935. fr1->klattp[KLATT_FNZ]*2,fr1->ffreq[1],fr1->ffreq[2],fr1->ffreq[3], fr1->klattp[KLATT_AV],
  936. fr2->klattp[KLATT_FNZ]*2,fr2->ffreq[1],fr2->ffreq[2],fr2->ffreq[3], fr2->klattp[KLATT_AV] );
  937. fclose(f_log);
  938. }
  939. }
  940. #endif
  941. if(control & 1)
  942. {
  943. for(ix=1; ix<6; ix++)
  944. {
  945. if(prev_fr.ffreq[ix] != fr1->ffreq[ix])
  946. {
  947. // Discontinuity in formants.
  948. // end_wave was set in SetSynth_Klatt() to fade out the previous frame
  949. KlattReset(0);
  950. break;
  951. }
  952. }
  953. memcpy(&prev_fr,fr2,sizeof(prev_fr));
  954. }
  955. for(ix=0; ix<N_KLATTP; ix++)
  956. {
  957. if((ix >= 5) && ((fr1->frflags & FRFLAG_KLATT) == 0))
  958. {
  959. klattp1[ix] = klattp[ix] = 0;
  960. klattp_inc[ix] = 0;
  961. }
  962. else
  963. {
  964. klattp1[ix] = klattp[ix] = fr1->klattp[ix];
  965. klattp_inc[ix] = (double)((fr2->klattp[ix] - klattp[ix]) * STEPSIZE)/length;
  966. }
  967. // get klatt parameter adjustments for the voice
  968. // if((ix>0) && (ix < KLATT_AVp))
  969. // klattp1[ix] = klattp[ix] = (klattp[ix] + wvoice->klattv[ix]);
  970. }
  971. nsamples = length;
  972. for(ix=1; ix < 6; ix++)
  973. {
  974. peaks[ix].freq1 = (fr1->ffreq[ix] * v->freq[ix] / 256.0) + v->freqadd[ix];
  975. peaks[ix].freq = (int)peaks[ix].freq1;
  976. next = (fr2->ffreq[ix] * v->freq[ix] / 256.0) + v->freqadd[ix];
  977. peaks[ix].freq_inc = ((next - peaks[ix].freq1) * STEPSIZE) / length;
  978. if(ix < 4)
  979. {
  980. // klatt bandwidth for f1, f2, f3 (others are fixed)
  981. peaks[ix].bw1 = fr1->bw[ix] * 2;
  982. peaks[ix].bw = (int)peaks[ix].bw1;
  983. next = fr2->bw[ix] * 2;
  984. peaks[ix].bw_inc = ((next - peaks[ix].bw1) * STEPSIZE) / length;
  985. }
  986. }
  987. // nasal zero frequency
  988. peaks[0].freq1 = fr1->klattp[KLATT_FNZ] * 2;
  989. if(peaks[0].freq1 == 0)
  990. peaks[0].freq1 = kt_frame.Fhz[F_NP]; // if no nasal zero, set it to same freq as nasal pole
  991. peaks[0].freq = (int)peaks[0].freq1;
  992. next = fr2->klattp[KLATT_FNZ] * 2;
  993. if(next == 0)
  994. next = kt_frame.Fhz[F_NP];
  995. peaks[0].freq_inc = ((next - peaks[0].freq1) * STEPSIZE) / length;
  996. peaks[0].bw1 = 89;
  997. peaks[0].bw = 89;
  998. peaks[0].bw_inc = 0;
  999. if(fr1->frflags & FRFLAG_KLATT)
  1000. {
  1001. // the frame contains additional parameters for parallel resonators
  1002. for(ix=1; ix < 7; ix++)
  1003. {
  1004. peaks[ix].bp1 = fr1->klatt_bp[ix] * 4; // parallel bandwidth
  1005. peaks[ix].bp = (int)peaks[ix].bp1;
  1006. next = fr2->klatt_bp[ix] * 2;
  1007. peaks[ix].bp_inc = ((next - peaks[ix].bp1) * STEPSIZE) / length;
  1008. peaks[ix].ap1 = fr1->klatt_ap[ix]; // parallal amplitude
  1009. peaks[ix].ap = (int)peaks[ix].ap1;
  1010. next = fr2->klatt_ap[ix] * 2;
  1011. peaks[ix].ap_inc = ((next - peaks[ix].ap1) * STEPSIZE) / length;
  1012. }
  1013. }
  1014. } // end of SetSynth_Klatt
  1015. int Wavegen_Klatt2(int length, int modulation, int resume, frame_t *fr1, frame_t *fr2)
  1016. {//===================================================================================
  1017. if(resume==0)
  1018. SetSynth_Klatt(length, modulation, fr1, fr2, wvoice, 1);
  1019. return(Wavegen_Klatt(resume));
  1020. }
  1021. void KlattInit()
  1022. {
  1023. #define NUMBER_OF_SAMPLES 100
  1024. static short natural_samples[NUMBER_OF_SAMPLES]=
  1025. {
  1026. -310,-400,530,356,224,89,23,-10,-58,-16,461,599,536,701,770,
  1027. 605,497,461,560,404,110,224,131,104,-97,155,278,-154,-1165,
  1028. -598,737,125,-592,41,11,-247,-10,65,92,80,-304,71,167,-1,122,
  1029. 233,161,-43,278,479,485,407,266,650,134,80,236,68,260,269,179,
  1030. 53,140,275,293,296,104,257,152,311,182,263,245,125,314,140,44,
  1031. 203,230,-235,-286,23,107,92,-91,38,464,443,176,98,-784,-2449,
  1032. -1891,-1045,-1600,-1462,-1384,-1261,-949,-730
  1033. };
  1034. static short formant_hz[10] = {280,688,1064,2806,3260,3700,6500,7000,8000,280};
  1035. static short bandwidth[10] = {89,160,70,160,200,200,500,500,500,89};
  1036. static short parallel_amp[10] = { 0,59,59,59,59,59,59,0,0,0};
  1037. static short parallel_bw[10] = {59,59,89,149,200,200,500,0,0,0};
  1038. int ix;
  1039. sample_count=0;
  1040. kt_globals.synthesis_model = CASCADE_PARALLEL;
  1041. kt_globals.samrate = 22050;
  1042. kt_globals.glsource = IMPULSIVE; // IMPULSIVE, NATURAL, SAMPLED
  1043. kt_globals.scale_wav = scale_wav_tab[kt_globals.glsource];
  1044. kt_globals.natural_samples = natural_samples;
  1045. kt_globals.num_samples = NUMBER_OF_SAMPLES;
  1046. kt_globals.sample_factor = 3.0;
  1047. kt_globals.nspfr = (kt_globals.samrate * 10) / 1000;
  1048. kt_globals.outsl = 0;
  1049. kt_globals.f0_flutter = 20;
  1050. KlattReset(2);
  1051. // set default values for frame parameters
  1052. for(ix=0; ix<=9; ix++)
  1053. {
  1054. kt_frame.Fhz[ix] = formant_hz[ix];
  1055. kt_frame.Bhz[ix] = bandwidth[ix];
  1056. kt_frame.Ap[ix] = parallel_amp[ix];
  1057. kt_frame.Bphz[ix] = parallel_bw[ix];
  1058. }
  1059. kt_frame.Bhz_next[F_NZ] = bandwidth[F_NZ];
  1060. kt_frame.F0hz10 = 1000;
  1061. kt_frame.AVdb = 59; // 59
  1062. kt_frame.ASP = 0;
  1063. kt_frame.Kopen = 40; // 40
  1064. kt_frame.Aturb = 0;
  1065. kt_frame.TLTdb = 0;
  1066. kt_frame.AF =50;
  1067. kt_frame.Kskew = 0;
  1068. kt_frame.AB = 0;
  1069. kt_frame.AVpdb = 0;
  1070. kt_frame.Gain0 = 62; // 60
  1071. } // end of KlattInit
  1072. #endif // INCLUDE_KLATT