klatt.cpp 31 KB

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