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

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