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.c 34KB

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