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

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